carbon-token-program-decoder 1.0.0

Token Program Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::AccountState,
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U64},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub mint: Pubkey,
    pub owner: Pubkey,
    pub amount: U64,
    pub delegate: Option<Pubkey>,
    pub state: sqlx::types::Json<AccountState>,
    pub is_native: Option<U64>,
    pub delegated_amount: U64,
    pub close_authority: Option<Pubkey>,
}

impl TokenRow {
    pub fn from_parts(source: crate::accounts::token::Token, metadata: AccountMetadata) -> Self {
        Self {
            account_metadata: metadata.into(),
            mint: source.mint.into(),
            owner: source.owner.into(),
            amount: source.amount.into(),
            delegate: source.delegate.map(|value| value.into()),
            state: sqlx::types::Json(source.state),
            is_native: source.is_native.map(|value| value.into()),
            delegated_amount: source.delegated_amount.into(),
            close_authority: source.close_authority.map(|value| value.into()),
        }
    }
}

impl TryFrom<TokenRow> for crate::accounts::token::Token {
    type Error = carbon_core::error::Error;
    fn try_from(source: TokenRow) -> Result<Self, Self::Error> {
        Ok(Self {
            mint: *source.mint,
            owner: *source.owner,
            amount: *source.amount,
            delegate: source.delegate.map(|value| *value),
            state: source.state.0,
            is_native: source.is_native.map(|value| *value),
            delegated_amount: *source.delegated_amount,
            close_authority: source.close_authority.map(|value| *value),
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::token::Token {
    fn table() -> &'static str {
        "token_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "mint",
            "owner",
            "amount",
            "delegate",
            "state",
            "is_native",
            "delegated_amount",
            "close_authority",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TokenRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO token_account (
                "mint",
                "owner",
                "amount",
                "delegate",
                "state",
                "is_native",
                "delegated_amount",
                "close_authority",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10
            )"#,
        )
        .bind(self.mint)
        .bind(self.owner)
        .bind(&self.amount)
        .bind(self.delegate)
        .bind(&self.state)
        .bind(&self.is_native)
        .bind(&self.delegated_amount)
        .bind(self.close_authority)
        .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 TokenRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO token_account (
                "mint",
                "owner",
                "amount",
                "delegate",
                "state",
                "is_native",
                "delegated_amount",
                "close_authority",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "mint" = EXCLUDED."mint",
                "owner" = EXCLUDED."owner",
                "amount" = EXCLUDED."amount",
                "delegate" = EXCLUDED."delegate",
                "state" = EXCLUDED."state",
                "is_native" = EXCLUDED."is_native",
                "delegated_amount" = EXCLUDED."delegated_amount",
                "close_authority" = EXCLUDED."close_authority",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.mint)
        .bind(self.owner)
        .bind(&self.amount)
        .bind(self.delegate)
        .bind(&self.state)
        .bind(&self.is_native)
        .bind(&self.delegated_amount)
        .bind(self.close_authority)
        .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 TokenRow {
    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_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 TokenRow {
    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_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 TokenMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TokenMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS token_account (
                -- Account data
                "mint" BYTEA NOT NULL,
                "owner" BYTEA NOT NULL,
                "amount" NUMERIC(20) NOT NULL,
                "delegate" BYTEA,
                "state" JSONB NOT NULL,
                "is_native" NUMERIC(20),
                "delegated_amount" NUMERIC(20) NOT NULL,
                "close_authority" BYTEA,
                -- 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_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}