carbon-pump-swap-decoder 1.0.0

PumpSwap Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::{FeeTier, Fees},
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U8},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct FeeConfigRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub bump: U8,
    pub admin: Pubkey,
    pub flat_fees: sqlx::types::Json<Fees>,
    pub fee_tiers: sqlx::types::Json<Vec<FeeTier>>,
}

impl FeeConfigRow {
    pub fn from_parts(
        source: crate::accounts::fee_config::FeeConfig,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            bump: source.bump.into(),
            admin: source.admin.into(),
            flat_fees: sqlx::types::Json(source.flat_fees),
            fee_tiers: sqlx::types::Json(source.fee_tiers.to_vec()),
        }
    }
}

impl TryFrom<FeeConfigRow> for crate::accounts::fee_config::FeeConfig {
    type Error = carbon_core::error::Error;
    fn try_from(source: FeeConfigRow) -> 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(),
                )
            })?,
            admin: *source.admin,
            flat_fees: source.flat_fees.0,
            fee_tiers: source.fee_tiers.0,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::fee_config::FeeConfig {
    fn table() -> &'static str {
        "fee_config_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "bump",
            "admin",
            "flat_fees",
            "fee_tiers",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for FeeConfigRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO fee_config_account (
                "bump",
                "admin",
                "flat_fees",
                "fee_tiers",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            )"#,
        )
        .bind(self.bump)
        .bind(self.admin)
        .bind(&self.flat_fees)
        .bind(&self.fee_tiers)
        .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 FeeConfigRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO fee_config_account (
                "bump",
                "admin",
                "flat_fees",
                "fee_tiers",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "bump" = EXCLUDED."bump",
                "admin" = EXCLUDED."admin",
                "flat_fees" = EXCLUDED."flat_fees",
                "fee_tiers" = EXCLUDED."fee_tiers",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.bump)
        .bind(self.admin)
        .bind(&self.flat_fees)
        .bind(&self.fee_tiers)
        .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 FeeConfigRow {
    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_config_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 FeeConfigRow {
    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_config_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 FeeConfigMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for FeeConfigMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS fee_config_account (
                -- Account data
                "bump" INT2 NOT NULL,
                "admin" BYTEA NOT NULL,
                "flat_fees" JSONB NOT NULL,
                "fee_tiers" JSONB 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_config_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}