carbon-pump-swap-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PoolRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub pool_bump: U8,
    pub index: U16,
    pub creator: Pubkey,
    pub base_mint: Pubkey,
    pub quote_mint: Pubkey,
    pub lp_mint: Pubkey,
    pub pool_base_token_account: Pubkey,
    pub pool_quote_token_account: Pubkey,
    pub lp_supply: U64,
    pub coin_creator: Pubkey,
    pub is_mayhem_mode: bool,
    pub is_cashback_coin: bool,
}

impl PoolRow {
    pub fn from_parts(source: crate::accounts::pool::Pool, metadata: AccountMetadata) -> Self {
        Self {
            account_metadata: metadata.into(),
            pool_bump: source.pool_bump.into(),
            index: source.index.into(),
            creator: source.creator.into(),
            base_mint: source.base_mint.into(),
            quote_mint: source.quote_mint.into(),
            lp_mint: source.lp_mint.into(),
            pool_base_token_account: source.pool_base_token_account.into(),
            pool_quote_token_account: source.pool_quote_token_account.into(),
            lp_supply: source.lp_supply.into(),
            coin_creator: source.coin_creator.into(),
            is_mayhem_mode: source.is_mayhem_mode,
            is_cashback_coin: source.is_cashback_coin,
        }
    }
}

impl TryFrom<PoolRow> for crate::accounts::pool::Pool {
    type Error = carbon_core::error::Error;
    fn try_from(source: PoolRow) -> Result<Self, Self::Error> {
        Ok(Self {
            pool_bump: source.pool_bump.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            index: source.index.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            creator: *source.creator,
            base_mint: *source.base_mint,
            quote_mint: *source.quote_mint,
            lp_mint: *source.lp_mint,
            pool_base_token_account: *source.pool_base_token_account,
            pool_quote_token_account: *source.pool_quote_token_account,
            lp_supply: *source.lp_supply,
            coin_creator: *source.coin_creator,
            is_mayhem_mode: source.is_mayhem_mode,
            is_cashback_coin: source.is_cashback_coin,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::pool::Pool {
    fn table() -> &'static str {
        "pool_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "pool_bump",
            "index",
            "creator",
            "base_mint",
            "quote_mint",
            "lp_mint",
            "pool_base_token_account",
            "pool_quote_token_account",
            "lp_supply",
            "coin_creator",
            "is_mayhem_mode",
            "is_cashback_coin",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PoolRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO pool_account (
                "pool_bump",
                "index",
                "creator",
                "base_mint",
                "quote_mint",
                "lp_mint",
                "pool_base_token_account",
                "pool_quote_token_account",
                "lp_supply",
                "coin_creator",
                "is_mayhem_mode",
                "is_cashback_coin",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
            )"#,
        )
        .bind(self.pool_bump)
        .bind(self.index)
        .bind(self.creator)
        .bind(self.base_mint)
        .bind(self.quote_mint)
        .bind(self.lp_mint)
        .bind(self.pool_base_token_account)
        .bind(self.pool_quote_token_account)
        .bind(&self.lp_supply)
        .bind(self.coin_creator)
        .bind(self.is_mayhem_mode)
        .bind(self.is_cashback_coin)
        .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 PoolRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO pool_account (
                "pool_bump",
                "index",
                "creator",
                "base_mint",
                "quote_mint",
                "lp_mint",
                "pool_base_token_account",
                "pool_quote_token_account",
                "lp_supply",
                "coin_creator",
                "is_mayhem_mode",
                "is_cashback_coin",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "pool_bump" = EXCLUDED."pool_bump",
                "index" = EXCLUDED."index",
                "creator" = EXCLUDED."creator",
                "base_mint" = EXCLUDED."base_mint",
                "quote_mint" = EXCLUDED."quote_mint",
                "lp_mint" = EXCLUDED."lp_mint",
                "pool_base_token_account" = EXCLUDED."pool_base_token_account",
                "pool_quote_token_account" = EXCLUDED."pool_quote_token_account",
                "lp_supply" = EXCLUDED."lp_supply",
                "coin_creator" = EXCLUDED."coin_creator",
                "is_mayhem_mode" = EXCLUDED."is_mayhem_mode",
                "is_cashback_coin" = EXCLUDED."is_cashback_coin",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.pool_bump)
        .bind(self.index)
        .bind(self.creator)
        .bind(self.base_mint)
        .bind(self.quote_mint)
        .bind(self.lp_mint)
        .bind(self.pool_base_token_account)
        .bind(self.pool_quote_token_account)
        .bind(&self.lp_supply)
        .bind(self.coin_creator)
        .bind(self.is_mayhem_mode)
        .bind(self.is_cashback_coin)
        .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 PoolRow {
    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 pool_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 PoolRow {
    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 pool_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 PoolMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PoolMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS pool_account (
                -- Account data
                "pool_bump" INT2 NOT NULL,
                "index" INT4 NOT NULL,
                "creator" BYTEA NOT NULL,
                "base_mint" BYTEA NOT NULL,
                "quote_mint" BYTEA NOT NULL,
                "lp_mint" BYTEA NOT NULL,
                "pool_base_token_account" BYTEA NOT NULL,
                "pool_quote_token_account" BYTEA NOT NULL,
                "lp_supply" NUMERIC(20) NOT NULL,
                "coin_creator" BYTEA NOT NULL,
                "is_mayhem_mode" BOOLEAN NOT NULL,
                "is_cashback_coin" BOOLEAN 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 pool_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}