carbon-token-program-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct MintRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub mint_authority: Option<Pubkey>,
    pub supply: U64,
    pub decimals: U8,
    pub is_initialized: bool,
    pub freeze_authority: Option<Pubkey>,
}

impl MintRow {
    pub fn from_parts(source: crate::accounts::mint::Mint, metadata: AccountMetadata) -> Self {
        Self {
            account_metadata: metadata.into(),
            mint_authority: source.mint_authority.map(|value| value.into()),
            supply: source.supply.into(),
            decimals: source.decimals.into(),
            is_initialized: source.is_initialized,
            freeze_authority: source.freeze_authority.map(|value| value.into()),
        }
    }
}

impl TryFrom<MintRow> for crate::accounts::mint::Mint {
    type Error = carbon_core::error::Error;
    fn try_from(source: MintRow) -> Result<Self, Self::Error> {
        Ok(Self {
            mint_authority: source.mint_authority.map(|value| *value),
            supply: *source.supply,
            decimals: source.decimals.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            is_initialized: source.is_initialized,
            freeze_authority: source.freeze_authority.map(|value| *value),
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::mint::Mint {
    fn table() -> &'static str {
        "mint_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "mint_authority",
            "supply",
            "decimals",
            "is_initialized",
            "freeze_authority",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for MintRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO mint_account (
                "mint_authority",
                "supply",
                "decimals",
                "is_initialized",
                "freeze_authority",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7
            )"#,
        )
        .bind(self.mint_authority)
        .bind(&self.supply)
        .bind(self.decimals)
        .bind(self.is_initialized)
        .bind(self.freeze_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 MintRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO mint_account (
                "mint_authority",
                "supply",
                "decimals",
                "is_initialized",
                "freeze_authority",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "mint_authority" = EXCLUDED."mint_authority",
                "supply" = EXCLUDED."supply",
                "decimals" = EXCLUDED."decimals",
                "is_initialized" = EXCLUDED."is_initialized",
                "freeze_authority" = EXCLUDED."freeze_authority",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.mint_authority)
        .bind(&self.supply)
        .bind(self.decimals)
        .bind(self.is_initialized)
        .bind(self.freeze_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 MintRow {
    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 mint_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 MintRow {
    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 mint_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 MintMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for MintMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS mint_account (
                -- Account data
                "mint_authority" BYTEA,
                "supply" NUMERIC(20) NOT NULL,
                "decimals" INT2 NOT NULL,
                "is_initialized" BOOLEAN NOT NULL,
                "freeze_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 mint_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}