carbon-orca-whirlpool-decoder 1.0.0

Orca Whirlpool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{
    account::AccountMetadata,
    postgres::{metadata::AccountRowMetadata, primitives::Pubkey},
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenBadgeRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub whirlpools_config: Pubkey,
    pub token_mint: Pubkey,
    pub attribute_require_non_transferable_position: bool,
}

impl TokenBadgeRow {
    pub fn from_parts(
        source: crate::accounts::token_badge::TokenBadge,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            whirlpools_config: source.whirlpools_config.into(),
            token_mint: source.token_mint.into(),
            attribute_require_non_transferable_position: source
                .attribute_require_non_transferable_position,
        }
    }
}

impl TryFrom<TokenBadgeRow> for crate::accounts::token_badge::TokenBadge {
    type Error = carbon_core::error::Error;
    fn try_from(source: TokenBadgeRow) -> Result<Self, Self::Error> {
        Ok(Self {
            whirlpools_config: *source.whirlpools_config,
            token_mint: *source.token_mint,
            attribute_require_non_transferable_position: source
                .attribute_require_non_transferable_position,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::token_badge::TokenBadge {
    fn table() -> &'static str {
        "token_badge_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "whirlpools_config",
            "token_mint",
            "attribute_require_non_transferable_position",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TokenBadgeRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO token_badge_account (
                "whirlpools_config",
                "token_mint",
                "attribute_require_non_transferable_position",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            )"#,
        )
        .bind(self.whirlpools_config)
        .bind(self.token_mint)
        .bind(self.attribute_require_non_transferable_position)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Upsert for TokenBadgeRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(r#"INSERT INTO token_badge_account (
                "whirlpools_config",
                "token_mint",
                "attribute_require_non_transferable_position",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "whirlpools_config" = EXCLUDED."whirlpools_config",
                "token_mint" = EXCLUDED."token_mint",
                "attribute_require_non_transferable_position" = EXCLUDED."attribute_require_non_transferable_position",
                __slot = EXCLUDED.__slot
            "#)
        .bind(self.whirlpools_config)
        .bind(self.token_mint)
        .bind(self.attribute_require_non_transferable_position)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .execute(pool).await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Delete for TokenBadgeRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"DELETE FROM token_badge_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .execute(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Lookup for TokenBadgeRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn lookup(
        key: Self::Key,
        pool: &sqlx::PgPool,
    ) -> carbon_core::error::CarbonResult<Option<Self>> {
        let row = sqlx::query_as(
            r#"SELECT * FROM token_badge_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .fetch_optional(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(row)
    }
}

pub struct TokenBadgeMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TokenBadgeMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS token_badge_account (
                -- Account data
                "whirlpools_config" BYTEA NOT NULL,
                "token_mint" BYTEA NOT NULL,
                "attribute_require_non_transferable_position" BOOLEAN NOT NULL,
                -- Account metadata
                __pubkey BYTEA NOT NULL,
                __slot NUMERIC(20),
                PRIMARY KEY (__pubkey)
            )"#,
        )
        .execute(connection)
        .await?;
        Ok(())
    }

    async fn down(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(r#"DROP TABLE IF EXISTS token_badge_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}