carbon-orca-whirlpool-decoder 1.0.0

Orca Whirlpool Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{
    instruction::InstructionMetadata,
    postgres::{
        metadata::InstructionRowMetadata,
        primitives::{Pubkey, U16, U32},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct InitializeAdaptiveFeeTierRow {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub fee_tier_index: U16,
    pub tick_spacing: U16,
    pub initialize_pool_authority: Pubkey,
    pub delegated_fee_authority: Pubkey,
    pub default_base_fee_rate: U16,
    pub filter_period: U16,
    pub decay_period: U16,
    pub reduction_factor: U16,
    pub adaptive_fee_control_factor: U32,
    pub max_volatility_accumulator: U32,
    pub tick_group_size: U16,
    pub major_swap_threshold_ticks: U16,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl InitializeAdaptiveFeeTierRow {
    pub fn from_parts(
        source: crate::instructions::initialize_adaptive_fee_tier::InitializeAdaptiveFeeTier,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            fee_tier_index: source.fee_tier_index.into(),
            tick_spacing: source.tick_spacing.into(),
            initialize_pool_authority: source.initialize_pool_authority.into(),
            delegated_fee_authority: source.delegated_fee_authority.into(),
            default_base_fee_rate: source.default_base_fee_rate.into(),
            filter_period: source.filter_period.into(),
            decay_period: source.decay_period.into(),
            reduction_factor: source.reduction_factor.into(),
            adaptive_fee_control_factor: source.adaptive_fee_control_factor.into(),
            max_volatility_accumulator: source.max_volatility_accumulator.into(),
            tick_group_size: source.tick_group_size.into(),
            major_swap_threshold_ticks: source.major_swap_threshold_ticks.into(),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<InitializeAdaptiveFeeTierRow>
    for crate::instructions::initialize_adaptive_fee_tier::InitializeAdaptiveFeeTier
{
    type Error = carbon_core::error::Error;
    fn try_from(source: InitializeAdaptiveFeeTierRow) -> Result<Self, Self::Error> {
        Ok(Self {
            fee_tier_index: source.fee_tier_index.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            tick_spacing: source.tick_spacing.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            initialize_pool_authority: *source.initialize_pool_authority,
            delegated_fee_authority: *source.delegated_fee_authority,
            default_base_fee_rate: source.default_base_fee_rate.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            filter_period: source.filter_period.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            decay_period: source.decay_period.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            reduction_factor: source.reduction_factor.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            adaptive_fee_control_factor: source.adaptive_fee_control_factor.try_into().map_err(
                |_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                },
            )?,
            max_volatility_accumulator: source.max_volatility_accumulator.try_into().map_err(
                |_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                },
            )?,
            tick_group_size: source.tick_group_size.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            major_swap_threshold_ticks: source.major_swap_threshold_ticks.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::instructions::initialize_adaptive_fee_tier::InitializeAdaptiveFeeTier
{
    fn table() -> &'static str {
        "initialize_adaptive_fee_tier_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "fee_tier_index",
            "tick_spacing",
            "initialize_pool_authority",
            "delegated_fee_authority",
            "default_base_fee_rate",
            "filter_period",
            "decay_period",
            "reduction_factor",
            "adaptive_fee_control_factor",
            "max_volatility_accumulator",
            "tick_group_size",
            "major_swap_threshold_ticks",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for InitializeAdaptiveFeeTierRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO initialize_adaptive_fee_tier_instruction (
                "fee_tier_index",
                "tick_spacing",
                "initialize_pool_authority",
                "delegated_fee_authority",
                "default_base_fee_rate",
                "filter_period",
                "decay_period",
                "reduction_factor",
                "adaptive_fee_control_factor",
                "max_volatility_accumulator",
                "tick_group_size",
                "major_swap_threshold_ticks",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
            )"#,
        )
        .bind(self.fee_tier_index)
        .bind(self.tick_spacing)
        .bind(self.initialize_pool_authority)
        .bind(self.delegated_fee_authority)
        .bind(self.default_base_fee_rate)
        .bind(self.filter_period)
        .bind(self.decay_period)
        .bind(self.reduction_factor)
        .bind(self.adaptive_fee_control_factor)
        .bind(self.max_volatility_accumulator)
        .bind(self.tick_group_size)
        .bind(self.major_swap_threshold_ticks)
        .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 InitializeAdaptiveFeeTierRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO initialize_adaptive_fee_tier_instruction (
                "fee_tier_index",
                "tick_spacing",
                "initialize_pool_authority",
                "delegated_fee_authority",
                "default_base_fee_rate",
                "filter_period",
                "decay_period",
                "reduction_factor",
                "adaptive_fee_control_factor",
                "max_volatility_accumulator",
                "tick_group_size",
                "major_swap_threshold_ticks",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
            ) ON CONFLICT (
                __signature, __instruction_index, __stack_height
            ) DO UPDATE SET
                "fee_tier_index" = EXCLUDED."fee_tier_index",
                "tick_spacing" = EXCLUDED."tick_spacing",
                "initialize_pool_authority" = EXCLUDED."initialize_pool_authority",
                "delegated_fee_authority" = EXCLUDED."delegated_fee_authority",
                "default_base_fee_rate" = EXCLUDED."default_base_fee_rate",
                "filter_period" = EXCLUDED."filter_period",
                "decay_period" = EXCLUDED."decay_period",
                "reduction_factor" = EXCLUDED."reduction_factor",
                "adaptive_fee_control_factor" = EXCLUDED."adaptive_fee_control_factor",
                "max_volatility_accumulator" = EXCLUDED."max_volatility_accumulator",
                "tick_group_size" = EXCLUDED."tick_group_size",
                "major_swap_threshold_ticks" = EXCLUDED."major_swap_threshold_ticks",
                __instruction_index = EXCLUDED.__instruction_index,
                __stack_height = EXCLUDED.__stack_height,
                __slot = EXCLUDED.__slot,
                __accounts = EXCLUDED.__accounts
            "#,
        )
        .bind(self.fee_tier_index)
        .bind(self.tick_spacing)
        .bind(self.initialize_pool_authority)
        .bind(self.delegated_fee_authority)
        .bind(self.default_base_fee_rate)
        .bind(self.filter_period)
        .bind(self.decay_period)
        .bind(self.reduction_factor)
        .bind(self.adaptive_fee_control_factor)
        .bind(self.max_volatility_accumulator)
        .bind(self.tick_group_size)
        .bind(self.major_swap_threshold_ticks)
        .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 InitializeAdaptiveFeeTierRow {
    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 initialize_adaptive_fee_tier_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 InitializeAdaptiveFeeTierRow {
    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 initialize_adaptive_fee_tier_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 InitializeAdaptiveFeeTierMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for InitializeAdaptiveFeeTierMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS initialize_adaptive_fee_tier_instruction (
                -- Instruction data
                "fee_tier_index" INT4 NOT NULL,
                "tick_spacing" INT4 NOT NULL,
                "initialize_pool_authority" BYTEA NOT NULL,
                "delegated_fee_authority" BYTEA NOT NULL,
                "default_base_fee_rate" INT4 NOT NULL,
                "filter_period" INT4 NOT NULL,
                "decay_period" INT4 NOT NULL,
                "reduction_factor" INT4 NOT NULL,
                "adaptive_fee_control_factor" INT8 NOT NULL,
                "max_volatility_accumulator" INT8 NOT NULL,
                "tick_group_size" INT4 NOT NULL,
                "major_swap_threshold_ticks" INT4 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 initialize_adaptive_fee_tier_instruction"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}