carbon-drift-v2-decoder 1.0.0

Drift v2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{
    instruction::InstructionMetadata,
    postgres::{
        metadata::InstructionRowMetadata,
        primitives::{U16, U64, U8},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct InitializeConstituentRow {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub spot_market_index: U16,
    pub decimals: U8,
    pub max_weight_deviation: i64,
    pub swap_fee_min: i64,
    pub swap_fee_max: i64,
    pub max_borrow_token_amount: U64,
    pub oracle_staleness_threshold: U64,
    pub cost_to_trade: i32,
    pub constituent_derivative_index: Option<i16>,
    pub constituent_derivative_depeg_threshold: U64,
    pub derivative_weight: U64,
    pub volatility: U64,
    pub gamma_execution: U8,
    pub gamma_inventory: U8,
    pub xi: U8,
    pub new_constituent_correlations: Vec<i64>,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl InitializeConstituentRow {
    pub fn from_parts(
        source: crate::instructions::initialize_constituent::InitializeConstituent,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            spot_market_index: source.spot_market_index.into(),
            decimals: source.decimals.into(),
            max_weight_deviation: source.max_weight_deviation,
            swap_fee_min: source.swap_fee_min,
            swap_fee_max: source.swap_fee_max,
            max_borrow_token_amount: source.max_borrow_token_amount.into(),
            oracle_staleness_threshold: source.oracle_staleness_threshold.into(),
            cost_to_trade: source.cost_to_trade,
            constituent_derivative_index: source.constituent_derivative_index,
            constituent_derivative_depeg_threshold: source
                .constituent_derivative_depeg_threshold
                .into(),
            derivative_weight: source.derivative_weight.into(),
            volatility: source.volatility.into(),
            gamma_execution: source.gamma_execution.into(),
            gamma_inventory: source.gamma_inventory.into(),
            xi: source.xi.into(),
            new_constituent_correlations: source.new_constituent_correlations.to_vec(),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<InitializeConstituentRow>
    for crate::instructions::initialize_constituent::InitializeConstituent
{
    type Error = carbon_core::error::Error;
    fn try_from(source: InitializeConstituentRow) -> Result<Self, Self::Error> {
        Ok(Self {
            spot_market_index: source.spot_market_index.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            decimals: source.decimals.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            max_weight_deviation: source.max_weight_deviation,
            swap_fee_min: source.swap_fee_min,
            swap_fee_max: source.swap_fee_max,
            max_borrow_token_amount: *source.max_borrow_token_amount,
            oracle_staleness_threshold: *source.oracle_staleness_threshold,
            cost_to_trade: source.cost_to_trade,
            constituent_derivative_index: source.constituent_derivative_index,
            constituent_derivative_depeg_threshold: *source.constituent_derivative_depeg_threshold,
            derivative_weight: *source.derivative_weight,
            volatility: *source.volatility,
            gamma_execution: source.gamma_execution.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            gamma_inventory: source.gamma_inventory.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            xi: source.xi.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            new_constituent_correlations: source.new_constituent_correlations.to_vec(),
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::instructions::initialize_constituent::InitializeConstituent
{
    fn table() -> &'static str {
        "initialize_constituent_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "spot_market_index",
            "decimals",
            "max_weight_deviation",
            "swap_fee_min",
            "swap_fee_max",
            "max_borrow_token_amount",
            "oracle_staleness_threshold",
            "cost_to_trade",
            "constituent_derivative_index",
            "constituent_derivative_depeg_threshold",
            "derivative_weight",
            "volatility",
            "gamma_execution",
            "gamma_inventory",
            "xi",
            "new_constituent_correlations",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for InitializeConstituentRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(r#"
            INSERT INTO initialize_constituent_instruction (
                "spot_market_index",
                "decimals",
                "max_weight_deviation",
                "swap_fee_min",
                "swap_fee_max",
                "max_borrow_token_amount",
                "oracle_staleness_threshold",
                "cost_to_trade",
                "constituent_derivative_index",
                "constituent_derivative_depeg_threshold",
                "derivative_weight",
                "volatility",
                "gamma_execution",
                "gamma_inventory",
                "xi",
                "new_constituent_correlations",
                __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, $18, $19, $20, $21
            )"#)
        .bind(self.spot_market_index)
        .bind(self.decimals)
        .bind(self.max_weight_deviation)
        .bind(self.swap_fee_min)
        .bind(self.swap_fee_max)
        .bind(&self.max_borrow_token_amount)
        .bind(&self.oracle_staleness_threshold)
        .bind(self.cost_to_trade)
        .bind(self.constituent_derivative_index)
        .bind(&self.constituent_derivative_depeg_threshold)
        .bind(&self.derivative_weight)
        .bind(&self.volatility)
        .bind(self.gamma_execution)
        .bind(self.gamma_inventory)
        .bind(self.xi)
        .bind(&self.new_constituent_correlations)
        .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 InitializeConstituentRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(r#"INSERT INTO initialize_constituent_instruction (
                "spot_market_index",
                "decimals",
                "max_weight_deviation",
                "swap_fee_min",
                "swap_fee_max",
                "max_borrow_token_amount",
                "oracle_staleness_threshold",
                "cost_to_trade",
                "constituent_derivative_index",
                "constituent_derivative_depeg_threshold",
                "derivative_weight",
                "volatility",
                "gamma_execution",
                "gamma_inventory",
                "xi",
                "new_constituent_correlations",
                __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, $18, $19, $20, $21
            ) ON CONFLICT (
                __signature, __instruction_index, __stack_height
            ) DO UPDATE SET
                "spot_market_index" = EXCLUDED."spot_market_index",
                "decimals" = EXCLUDED."decimals",
                "max_weight_deviation" = EXCLUDED."max_weight_deviation",
                "swap_fee_min" = EXCLUDED."swap_fee_min",
                "swap_fee_max" = EXCLUDED."swap_fee_max",
                "max_borrow_token_amount" = EXCLUDED."max_borrow_token_amount",
                "oracle_staleness_threshold" = EXCLUDED."oracle_staleness_threshold",
                "cost_to_trade" = EXCLUDED."cost_to_trade",
                "constituent_derivative_index" = EXCLUDED."constituent_derivative_index",
                "constituent_derivative_depeg_threshold" = EXCLUDED."constituent_derivative_depeg_threshold",
                "derivative_weight" = EXCLUDED."derivative_weight",
                "volatility" = EXCLUDED."volatility",
                "gamma_execution" = EXCLUDED."gamma_execution",
                "gamma_inventory" = EXCLUDED."gamma_inventory",
                "xi" = EXCLUDED."xi",
                "new_constituent_correlations" = EXCLUDED."new_constituent_correlations",
                __instruction_index = EXCLUDED.__instruction_index,
                __stack_height = EXCLUDED.__stack_height,
                __slot = EXCLUDED.__slot,
                __accounts = EXCLUDED.__accounts
            "#)
        .bind(self.spot_market_index)
        .bind(self.decimals)
        .bind(self.max_weight_deviation)
        .bind(self.swap_fee_min)
        .bind(self.swap_fee_max)
        .bind(&self.max_borrow_token_amount)
        .bind(&self.oracle_staleness_threshold)
        .bind(self.cost_to_trade)
        .bind(self.constituent_derivative_index)
        .bind(&self.constituent_derivative_depeg_threshold)
        .bind(&self.derivative_weight)
        .bind(&self.volatility)
        .bind(self.gamma_execution)
        .bind(self.gamma_inventory)
        .bind(self.xi)
        .bind(&self.new_constituent_correlations)
        .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 InitializeConstituentRow {
    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_constituent_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 InitializeConstituentRow {
    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_constituent_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 InitializeConstituentMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for InitializeConstituentMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS initialize_constituent_instruction (
                -- Instruction data
                "spot_market_index" INT4 NOT NULL,
                "decimals" INT2 NOT NULL,
                "max_weight_deviation" INT8 NOT NULL,
                "swap_fee_min" INT8 NOT NULL,
                "swap_fee_max" INT8 NOT NULL,
                "max_borrow_token_amount" NUMERIC(20) NOT NULL,
                "oracle_staleness_threshold" NUMERIC(20) NOT NULL,
                "cost_to_trade" INT4 NOT NULL,
                "constituent_derivative_index" INT2,
                "constituent_derivative_depeg_threshold" NUMERIC(20) NOT NULL,
                "derivative_weight" NUMERIC(20) NOT NULL,
                "volatility" NUMERIC(20) NOT NULL,
                "gamma_execution" INT2 NOT NULL,
                "gamma_inventory" INT2 NOT NULL,
                "xi" INT2 NOT NULL,
                "new_constituent_correlations" INT8[] 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_constituent_instruction"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}