carbon-orca-whirlpool-decoder 1.0.0

Orca Whirlpool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::RemainingAccountsInfo,
    carbon_core::{
        instruction::InstructionMetadata,
        postgres::{
            metadata::InstructionRowMetadata,
            primitives::{U128, U64},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SwapV2Row {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub amount: U64,
    pub other_amount_threshold: U64,
    pub sqrt_price_limit: U128,
    pub amount_specified_is_input: bool,
    pub a_to_b: bool,
    pub remaining_accounts_info: Option<sqlx::types::Json<RemainingAccountsInfo>>,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl SwapV2Row {
    pub fn from_parts(
        source: crate::instructions::swap_v2::SwapV2,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            amount: source.amount.into(),
            other_amount_threshold: source.other_amount_threshold.into(),
            sqrt_price_limit: source.sqrt_price_limit.into(),
            amount_specified_is_input: source.amount_specified_is_input,
            a_to_b: source.a_to_b,
            remaining_accounts_info: source.remaining_accounts_info.map(sqlx::types::Json),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<SwapV2Row> for crate::instructions::swap_v2::SwapV2 {
    type Error = carbon_core::error::Error;
    fn try_from(source: SwapV2Row) -> Result<Self, Self::Error> {
        Ok(Self {
            amount: *source.amount,
            other_amount_threshold: *source.other_amount_threshold,
            sqrt_price_limit: *source.sqrt_price_limit,
            amount_specified_is_input: source.amount_specified_is_input,
            a_to_b: source.a_to_b,
            remaining_accounts_info: source.remaining_accounts_info.map(|value| value.0),
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::instructions::swap_v2::SwapV2 {
    fn table() -> &'static str {
        "swap_v2_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "amount",
            "other_amount_threshold",
            "sqrt_price_limit",
            "amount_specified_is_input",
            "a_to_b",
            "remaining_accounts_info",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SwapV2Row {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO swap_v2_instruction (
                "amount",
                "other_amount_threshold",
                "sqrt_price_limit",
                "amount_specified_is_input",
                "a_to_b",
                "remaining_accounts_info",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
            )"#,
        )
        .bind(&self.amount)
        .bind(&self.other_amount_threshold)
        .bind(&self.sqrt_price_limit)
        .bind(self.amount_specified_is_input)
        .bind(self.a_to_b)
        .bind(&self.remaining_accounts_info)
        .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 SwapV2Row {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO swap_v2_instruction (
                "amount",
                "other_amount_threshold",
                "sqrt_price_limit",
                "amount_specified_is_input",
                "a_to_b",
                "remaining_accounts_info",
                __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
                "amount" = EXCLUDED."amount",
                "other_amount_threshold" = EXCLUDED."other_amount_threshold",
                "sqrt_price_limit" = EXCLUDED."sqrt_price_limit",
                "amount_specified_is_input" = EXCLUDED."amount_specified_is_input",
                "a_to_b" = EXCLUDED."a_to_b",
                "remaining_accounts_info" = EXCLUDED."remaining_accounts_info",
                __instruction_index = EXCLUDED.__instruction_index,
                __stack_height = EXCLUDED.__stack_height,
                __slot = EXCLUDED.__slot,
                __accounts = EXCLUDED.__accounts
            "#,
        )
        .bind(&self.amount)
        .bind(&self.other_amount_threshold)
        .bind(&self.sqrt_price_limit)
        .bind(self.amount_specified_is_input)
        .bind(self.a_to_b)
        .bind(&self.remaining_accounts_info)
        .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 SwapV2Row {
    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 swap_v2_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 SwapV2Row {
    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 swap_v2_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 SwapV2MigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SwapV2MigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS swap_v2_instruction (
                -- Instruction data
                "amount" NUMERIC(20) NOT NULL,
                "other_amount_threshold" NUMERIC(20) NOT NULL,
                "sqrt_price_limit" NUMERIC(39) NOT NULL,
                "amount_specified_is_input" BOOLEAN NOT NULL,
                "a_to_b" BOOLEAN NOT NULL,
                "remaining_accounts_info" JSONB,
                -- 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 swap_v2_instruction"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}