carbon-pump-swap-decoder 1.0.0

PumpSwap Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::OptionBool,
    carbon_core::{
        instruction::InstructionMetadata,
        postgres::{
            metadata::InstructionRowMetadata,
            primitives::{Pubkey, U16, U64},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CreatePoolRow {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub index: U16,
    pub base_amount_in: U64,
    pub quote_amount_in: U64,
    pub coin_creator: Pubkey,
    pub is_mayhem_mode: bool,
    pub is_cashback_coin: sqlx::types::Json<OptionBool>,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl CreatePoolRow {
    pub fn from_parts(
        source: crate::instructions::create_pool::CreatePool,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            index: source.index.into(),
            base_amount_in: source.base_amount_in.into(),
            quote_amount_in: source.quote_amount_in.into(),
            coin_creator: source.coin_creator.into(),
            is_mayhem_mode: source.is_mayhem_mode,
            is_cashback_coin: sqlx::types::Json(source.is_cashback_coin),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<CreatePoolRow> for crate::instructions::create_pool::CreatePool {
    type Error = carbon_core::error::Error;
    fn try_from(source: CreatePoolRow) -> Result<Self, Self::Error> {
        Ok(Self {
            index: source.index.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            base_amount_in: *source.base_amount_in,
            quote_amount_in: *source.quote_amount_in,
            coin_creator: *source.coin_creator,
            is_mayhem_mode: source.is_mayhem_mode,
            is_cashback_coin: source.is_cashback_coin.0,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::instructions::create_pool::CreatePool {
    fn table() -> &'static str {
        "create_pool_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "index",
            "base_amount_in",
            "quote_amount_in",
            "coin_creator",
            "is_mayhem_mode",
            "is_cashback_coin",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CreatePoolRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO create_pool_instruction (
                "index",
                "base_amount_in",
                "quote_amount_in",
                "coin_creator",
                "is_mayhem_mode",
                "is_cashback_coin",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
            )"#,
        )
        .bind(self.index)
        .bind(&self.base_amount_in)
        .bind(&self.quote_amount_in)
        .bind(self.coin_creator)
        .bind(self.is_mayhem_mode)
        .bind(self.is_cashback_coin)
        .bind(&self.instruction_metadata.signature)
        .bind(self.instruction_metadata.instruction_index)
        .bind(self.instruction_metadata.stack_height)
        .bind(&self.instruction_metadata.slot)
        .bind(&self.accounts)
        .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 CreatePoolRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO create_pool_instruction (
                "index",
                "base_amount_in",
                "quote_amount_in",
                "coin_creator",
                "is_mayhem_mode",
                "is_cashback_coin",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
            ) ON CONFLICT (
                __signature, __instruction_index, __stack_height
            ) DO UPDATE SET
                "index" = EXCLUDED."index",
                "base_amount_in" = EXCLUDED."base_amount_in",
                "quote_amount_in" = EXCLUDED."quote_amount_in",
                "coin_creator" = EXCLUDED."coin_creator",
                "is_mayhem_mode" = EXCLUDED."is_mayhem_mode",
                "is_cashback_coin" = EXCLUDED."is_cashback_coin",
                __instruction_index = EXCLUDED.__instruction_index,
                __stack_height = EXCLUDED.__stack_height,
                __slot = EXCLUDED.__slot,
                __accounts = EXCLUDED.__accounts
            "#,
        )
        .bind(self.index)
        .bind(&self.base_amount_in)
        .bind(&self.quote_amount_in)
        .bind(self.coin_creator)
        .bind(self.is_mayhem_mode)
        .bind(self.is_cashback_coin)
        .bind(&self.instruction_metadata.signature)
        .bind(self.instruction_metadata.instruction_index)
        .bind(self.instruction_metadata.stack_height)
        .bind(&self.instruction_metadata.slot)
        .bind(&self.accounts)
        .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 CreatePoolRow {
    type Key = (
        String,
        carbon_core::postgres::primitives::U32,
        carbon_core::postgres::primitives::U32,
    );

    async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"DELETE FROM create_pool_instruction WHERE
                __signature = $1 AND __instruction_index = $2 AND __stack_height = $3
            "#,
        )
        .bind(key.0)
        .bind(key.1)
        .bind(key.2)
        .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 CreatePoolRow {
    type Key = (
        String,
        carbon_core::postgres::primitives::U32,
        carbon_core::postgres::primitives::U32,
    );

    async fn lookup(
        key: Self::Key,
        pool: &sqlx::PgPool,
    ) -> carbon_core::error::CarbonResult<Option<Self>> {
        let row = sqlx::query_as(
            r#"SELECT * FROM create_pool_instruction WHERE
                __signature = $1 AND __instruction_index = $2 AND __stack_height = $3
            "#,
        )
        .bind(key.0)
        .bind(key.1)
        .bind(key.2)
        .fetch_optional(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(row)
    }
}

pub struct CreatePoolMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CreatePoolMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS create_pool_instruction (
                -- Instruction data
                "index" INT4 NOT NULL,
                "base_amount_in" NUMERIC(20) NOT NULL,
                "quote_amount_in" NUMERIC(20) NOT NULL,
                "coin_creator" BYTEA NOT NULL,
                "is_mayhem_mode" BOOLEAN NOT NULL,
                "is_cashback_coin" JSONB NOT NULL,
                -- Instruction metadata
                __signature TEXT NOT NULL,
                __instruction_index BIGINT NOT NULL,
                __stack_height BIGINT NOT NULL,
                __slot NUMERIC(20),
                __accounts JSONB NOT NULL,
                PRIMARY KEY (__signature, __instruction_index, __stack_height)
            )"#,
        )
        .execute(connection)
        .await?;
        Ok(())
    }

    async fn down(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(r#"DROP TABLE IF EXISTS create_pool_instruction"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}