carbon-pump-fees-decoder 2.0.0

PumpFees 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 FeeProgramGlobalRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub bump: U8,
    pub authority: Pubkey,
    pub disable_flags: U8,
    pub social_claim_authority: Pubkey,
    pub claim_rate_limit: U64,
    pub reserved: Vec<U8>,
}

impl FeeProgramGlobalRow {
    pub fn from_parts(
        source: crate::accounts::fee_program_global::FeeProgramGlobal,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            bump: source.bump.into(),
            authority: source.authority.into(),
            disable_flags: source.disable_flags.into(),
            social_claim_authority: source.social_claim_authority.into(),
            claim_rate_limit: source.claim_rate_limit.into(),
            reserved: source
                .reserved
                .into_iter()
                .map(|element| element.into())
                .collect(),
        }
    }
}

impl TryFrom<FeeProgramGlobalRow> for crate::accounts::fee_program_global::FeeProgramGlobal {
    type Error = carbon_core::error::Error;
    fn try_from(source: FeeProgramGlobalRow) -> Result<Self, Self::Error> {
        Ok(Self {
            bump: source.bump.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            authority: *source.authority,
            disable_flags: source.disable_flags.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            social_claim_authority: *source.social_claim_authority,
            claim_rate_limit: *source.claim_rate_limit,
            reserved: source
                .reserved
                .into_iter()
                .map(|element| {
                    element.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .collect::<Result<Vec<_>, carbon_core::error::Error>>()?
                .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::fee_program_global::FeeProgramGlobal
{
    fn table() -> &'static str {
        "fee_program_global_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "bump",
            "authority",
            "disable_flags",
            "social_claim_authority",
            "claim_rate_limit",
            "reserved",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for FeeProgramGlobalRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO fee_program_global_account (
                "bump",
                "authority",
                "disable_flags",
                "social_claim_authority",
                "claim_rate_limit",
                "reserved",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            )"#,
        )
        .bind(self.bump)
        .bind(self.authority)
        .bind(self.disable_flags)
        .bind(self.social_claim_authority)
        .bind(&self.claim_rate_limit)
        .bind(&self.reserved)
        .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 FeeProgramGlobalRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO fee_program_global_account (
                "bump",
                "authority",
                "disable_flags",
                "social_claim_authority",
                "claim_rate_limit",
                "reserved",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "bump" = EXCLUDED."bump",
                "authority" = EXCLUDED."authority",
                "disable_flags" = EXCLUDED."disable_flags",
                "social_claim_authority" = EXCLUDED."social_claim_authority",
                "claim_rate_limit" = EXCLUDED."claim_rate_limit",
                "reserved" = EXCLUDED."reserved",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.bump)
        .bind(self.authority)
        .bind(self.disable_flags)
        .bind(self.social_claim_authority)
        .bind(&self.claim_rate_limit)
        .bind(&self.reserved)
        .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 FeeProgramGlobalRow {
    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 fee_program_global_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 FeeProgramGlobalRow {
    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 fee_program_global_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 FeeProgramGlobalMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for FeeProgramGlobalMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS fee_program_global_account (
                -- Account data
                "bump" INT2 NOT NULL,
                "authority" BYTEA NOT NULL,
                "disable_flags" INT2 NOT NULL,
                "social_claim_authority" BYTEA NOT NULL,
                "claim_rate_limit" NUMERIC(20) NOT NULL,
                "reserved" INT2[] 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 fee_program_global_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}