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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct WhirlpoolsConfigExtensionRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub whirlpools_config: Pubkey,
    pub config_extension_authority: Pubkey,
    pub token_badge_authority: Pubkey,
}

impl WhirlpoolsConfigExtensionRow {
    pub fn from_parts(
        source: crate::accounts::whirlpools_config_extension::WhirlpoolsConfigExtension,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            whirlpools_config: source.whirlpools_config.into(),
            config_extension_authority: source.config_extension_authority.into(),
            token_badge_authority: source.token_badge_authority.into(),
        }
    }
}

impl TryFrom<WhirlpoolsConfigExtensionRow>
    for crate::accounts::whirlpools_config_extension::WhirlpoolsConfigExtension
{
    type Error = carbon_core::error::Error;
    fn try_from(source: WhirlpoolsConfigExtensionRow) -> Result<Self, Self::Error> {
        Ok(Self {
            whirlpools_config: *source.whirlpools_config,
            config_extension_authority: *source.config_extension_authority,
            token_badge_authority: *source.token_badge_authority,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::whirlpools_config_extension::WhirlpoolsConfigExtension
{
    fn table() -> &'static str {
        "whirlpools_config_extension_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "whirlpools_config",
            "config_extension_authority",
            "token_badge_authority",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for WhirlpoolsConfigExtensionRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO whirlpools_config_extension_account (
                "whirlpools_config",
                "config_extension_authority",
                "token_badge_authority",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            )"#,
        )
        .bind(self.whirlpools_config)
        .bind(self.config_extension_authority)
        .bind(self.token_badge_authority)
        .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 WhirlpoolsConfigExtensionRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO whirlpools_config_extension_account (
                "whirlpools_config",
                "config_extension_authority",
                "token_badge_authority",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "whirlpools_config" = EXCLUDED."whirlpools_config",
                "config_extension_authority" = EXCLUDED."config_extension_authority",
                "token_badge_authority" = EXCLUDED."token_badge_authority",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.whirlpools_config)
        .bind(self.config_extension_authority)
        .bind(self.token_badge_authority)
        .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 WhirlpoolsConfigExtensionRow {
    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_extension_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 WhirlpoolsConfigExtensionRow {
    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_extension_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 WhirlpoolsConfigExtensionMigrationOperation;

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