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::{U16, U32},
    },
};

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

impl SetAdaptiveFeeConstantsRow {
    pub fn from_parts(
        source: crate::instructions::set_adaptive_fee_constants::SetAdaptiveFeeConstants,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            filter_period: source.filter_period.map(|value| value.into()),
            decay_period: source.decay_period.map(|value| value.into()),
            reduction_factor: source.reduction_factor.map(|value| value.into()),
            adaptive_fee_control_factor: source
                .adaptive_fee_control_factor
                .map(|value| value.into()),
            max_volatility_accumulator: source.max_volatility_accumulator.map(|value| value.into()),
            tick_group_size: source.tick_group_size.map(|value| value.into()),
            major_swap_threshold_ticks: source.major_swap_threshold_ticks.map(|value| value.into()),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<SetAdaptiveFeeConstantsRow>
    for crate::instructions::set_adaptive_fee_constants::SetAdaptiveFeeConstants
{
    type Error = carbon_core::error::Error;
    fn try_from(source: SetAdaptiveFeeConstantsRow) -> Result<Self, Self::Error> {
        Ok(Self {
            filter_period: source
                .filter_period
                .map(|value| {
                    value.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .transpose()?,
            decay_period: source
                .decay_period
                .map(|value| {
                    value.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .transpose()?,
            reduction_factor: source
                .reduction_factor
                .map(|value| {
                    value.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .transpose()?,
            adaptive_fee_control_factor: source
                .adaptive_fee_control_factor
                .map(|value| {
                    value.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .transpose()?,
            max_volatility_accumulator: source
                .max_volatility_accumulator
                .map(|value| {
                    value.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .transpose()?,
            tick_group_size: source
                .tick_group_size
                .map(|value| {
                    value.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .transpose()?,
            major_swap_threshold_ticks: source
                .major_swap_threshold_ticks
                .map(|value| {
                    value.try_into().map_err(|_| {
                        carbon_core::error::Error::Custom(
                            "Failed to convert value from postgres primitive".to_string(),
                        )
                    })
                })
                .transpose()?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::instructions::set_adaptive_fee_constants::SetAdaptiveFeeConstants
{
    fn table() -> &'static str {
        "set_adaptive_fee_constants_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "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 SetAdaptiveFeeConstantsRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO set_adaptive_fee_constants_instruction (
                "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
            )"#,
        )
        .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 SetAdaptiveFeeConstantsRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO set_adaptive_fee_constants_instruction (
                "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
            ) ON CONFLICT (
                __signature, __instruction_index, __stack_height
            ) DO UPDATE SET
                "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.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 SetAdaptiveFeeConstantsRow {
    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 set_adaptive_fee_constants_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 SetAdaptiveFeeConstantsRow {
    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 set_adaptive_fee_constants_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 SetAdaptiveFeeConstantsMigrationOperation;

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