carbon-meteora-dbc-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ConfigRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub pool_fees: sqlx::types::Json<PoolFees>,
    pub activation_duration: U64,
    pub vault_config_key: Pubkey,
    pub pool_creator_authority: Pubkey,
    pub activation_type: U8,
    pub partner_fee_numerator: U64,
    pub padding: Vec<u8>,
}

impl ConfigRow {
    pub fn from_parts(source: crate::accounts::config::Config, metadata: AccountMetadata) -> Self {
        Self {
            account_metadata: metadata.into(),
            pool_fees: sqlx::types::Json(source.pool_fees),
            activation_duration: source.activation_duration.into(),
            vault_config_key: source.vault_config_key.into(),
            pool_creator_authority: source.pool_creator_authority.into(),
            activation_type: source.activation_type.into(),
            partner_fee_numerator: source.partner_fee_numerator.into(),
            padding: source.padding.to_vec(),
        }
    }
}

impl TryFrom<ConfigRow> for crate::accounts::config::Config {
    type Error = carbon_core::error::Error;
    fn try_from(source: ConfigRow) -> Result<Self, Self::Error> {
        Ok(Self {
            pool_fees: source.pool_fees.0,
            activation_duration: *source.activation_duration,
            vault_config_key: *source.vault_config_key,
            pool_creator_authority: *source.pool_creator_authority,
            activation_type: source.activation_type.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            partner_fee_numerator: *source.partner_fee_numerator,
            padding: source.padding.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 219 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::config::Config {
    fn table() -> &'static str {
        "config_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "pool_fees",
            "activation_duration",
            "vault_config_key",
            "pool_creator_authority",
            "activation_type",
            "partner_fee_numerator",
            "padding",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ConfigRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO config_account (
                "pool_fees",
                "activation_duration",
                "vault_config_key",
                "pool_creator_authority",
                "activation_type",
                "partner_fee_numerator",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9
            )"#,
        )
        .bind(&self.pool_fees)
        .bind(&self.activation_duration)
        .bind(self.vault_config_key)
        .bind(self.pool_creator_authority)
        .bind(self.activation_type)
        .bind(&self.partner_fee_numerator)
        .bind(&self.padding)
        .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 ConfigRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO config_account (
                "pool_fees",
                "activation_duration",
                "vault_config_key",
                "pool_creator_authority",
                "activation_type",
                "partner_fee_numerator",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "pool_fees" = EXCLUDED."pool_fees",
                "activation_duration" = EXCLUDED."activation_duration",
                "vault_config_key" = EXCLUDED."vault_config_key",
                "pool_creator_authority" = EXCLUDED."pool_creator_authority",
                "activation_type" = EXCLUDED."activation_type",
                "partner_fee_numerator" = EXCLUDED."partner_fee_numerator",
                "padding" = EXCLUDED."padding",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(&self.pool_fees)
        .bind(&self.activation_duration)
        .bind(self.vault_config_key)
        .bind(self.pool_creator_authority)
        .bind(self.activation_type)
        .bind(&self.partner_fee_numerator)
        .bind(&self.padding)
        .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 ConfigRow {
    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 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 ConfigRow {
    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 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 ConfigMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ConfigMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS config_account (
                -- Account data
                "pool_fees" JSONB NOT NULL,
                "activation_duration" NUMERIC(20) NOT NULL,
                "vault_config_key" BYTEA NOT NULL,
                "pool_creator_authority" BYTEA NOT NULL,
                "activation_type" INT2 NOT NULL,
                "partner_fee_numerator" NUMERIC(20) NOT NULL,
                "padding" 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 config_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}