carbon-orca-whirlpool-decoder 1.0.0

Orca Whirlpool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{
    account::AccountMetadata,
    postgres::{
        metadata::AccountRowMetadata,
        primitives::{Pubkey, U16},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct WhirlpoolsConfigRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub fee_authority: Pubkey,
    pub collect_protocol_fees_authority: Pubkey,
    pub reward_emissions_super_authority: Pubkey,
    pub default_protocol_fee_rate: U16,
    pub feature_flags: U16,
}

impl WhirlpoolsConfigRow {
    pub fn from_parts(
        source: crate::accounts::whirlpools_config::WhirlpoolsConfig,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            fee_authority: source.fee_authority.into(),
            collect_protocol_fees_authority: source.collect_protocol_fees_authority.into(),
            reward_emissions_super_authority: source.reward_emissions_super_authority.into(),
            default_protocol_fee_rate: source.default_protocol_fee_rate.into(),
            feature_flags: source.feature_flags.into(),
        }
    }
}

impl TryFrom<WhirlpoolsConfigRow> for crate::accounts::whirlpools_config::WhirlpoolsConfig {
    type Error = carbon_core::error::Error;
    fn try_from(source: WhirlpoolsConfigRow) -> Result<Self, Self::Error> {
        Ok(Self {
            fee_authority: *source.fee_authority,
            collect_protocol_fees_authority: *source.collect_protocol_fees_authority,
            reward_emissions_super_authority: *source.reward_emissions_super_authority,
            default_protocol_fee_rate: source.default_protocol_fee_rate.try_into().map_err(
                |_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                },
            )?,
            feature_flags: source.feature_flags.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::whirlpools_config::WhirlpoolsConfig
{
    fn table() -> &'static str {
        "whirlpools_config_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "fee_authority",
            "collect_protocol_fees_authority",
            "reward_emissions_super_authority",
            "default_protocol_fee_rate",
            "feature_flags",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for WhirlpoolsConfigRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO whirlpools_config_account (
                "fee_authority",
                "collect_protocol_fees_authority",
                "reward_emissions_super_authority",
                "default_protocol_fee_rate",
                "feature_flags",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7
            )"#,
        )
        .bind(self.fee_authority)
        .bind(self.collect_protocol_fees_authority)
        .bind(self.reward_emissions_super_authority)
        .bind(self.default_protocol_fee_rate)
        .bind(self.feature_flags)
        .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 WhirlpoolsConfigRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO whirlpools_config_account (
                "fee_authority",
                "collect_protocol_fees_authority",
                "reward_emissions_super_authority",
                "default_protocol_fee_rate",
                "feature_flags",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "fee_authority" = EXCLUDED."fee_authority",
                "collect_protocol_fees_authority" = EXCLUDED."collect_protocol_fees_authority",
                "reward_emissions_super_authority" = EXCLUDED."reward_emissions_super_authority",
                "default_protocol_fee_rate" = EXCLUDED."default_protocol_fee_rate",
                "feature_flags" = EXCLUDED."feature_flags",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.fee_authority)
        .bind(self.collect_protocol_fees_authority)
        .bind(self.reward_emissions_super_authority)
        .bind(self.default_protocol_fee_rate)
        .bind(self.feature_flags)
        .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 WhirlpoolsConfigRow {
    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 whirlpools_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 WhirlpoolsConfigRow {
    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 whirlpools_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 WhirlpoolsConfigMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for WhirlpoolsConfigMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS whirlpools_config_account (
                -- Account data
                "fee_authority" BYTEA NOT NULL,
                "collect_protocol_fees_authority" BYTEA NOT NULL,
                "reward_emissions_super_authority" BYTEA NOT NULL,
                "default_protocol_fee_rate" INT4 NOT NULL,
                "feature_flags" INT4 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 whirlpools_config_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}