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, U8},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct MultisigRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub m: U8,
    pub n: U8,
    pub is_initialized: bool,
    pub signers: Vec<Pubkey>,
}

impl MultisigRow {
    pub fn from_parts(
        source: crate::accounts::multisig::Multisig,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            m: source.m.into(),
            n: source.n.into(),
            is_initialized: source.is_initialized,
            signers: source
                .signers
                .into_iter()
                .map(|element| element.into())
                .collect(),
        }
    }
}

impl TryFrom<MultisigRow> for crate::accounts::multisig::Multisig {
    type Error = carbon_core::error::Error;
    fn try_from(source: MultisigRow) -> Result<Self, Self::Error> {
        Ok(Self {
            m: source.m.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            n: source.n.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            is_initialized: source.is_initialized,
            signers: source
                .signers
                .into_iter()
                .map(|element| Ok(*element))
                .collect::<Result<Vec<_>, carbon_core::error::Error>>()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to collect array elements".to_string(),
                    )
                })?
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert array element to primitive".to_string(),
                    )
                })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::multisig::Multisig {
    fn table() -> &'static str {
        "multisig_account"
    }

    fn columns() -> Vec<&'static str> {
        vec!["__pubkey", "__slot", "m", "n", "is_initialized", "signers"]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for MultisigRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO multisig_account (
                "m",
                "n",
                "is_initialized",
                "signers",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            )"#,
        )
        .bind(self.m)
        .bind(self.n)
        .bind(self.is_initialized)
        .bind(&self.signers)
        .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 MultisigRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO multisig_account (
                "m",
                "n",
                "is_initialized",
                "signers",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "m" = EXCLUDED."m",
                "n" = EXCLUDED."n",
                "is_initialized" = EXCLUDED."is_initialized",
                "signers" = EXCLUDED."signers",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.m)
        .bind(self.n)
        .bind(self.is_initialized)
        .bind(&self.signers)
        .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 MultisigRow {
    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 multisig_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 MultisigRow {
    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 multisig_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 MultisigMigrationOperation;

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