carbon-openbook-v2-decoder 2.0.0

Openbook V2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::OracleConfigParams,
    carbon_core::{instruction::InstructionMetadata, postgres::metadata::InstructionRowMetadata},
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CreateMarketRow {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub name: String,
    pub oracle_config: sqlx::types::Json<OracleConfigParams>,
    pub quote_lot_size: i64,
    pub base_lot_size: i64,
    pub maker_fee: i64,
    pub taker_fee: i64,
    pub time_expiry: i64,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl CreateMarketRow {
    pub fn from_parts(
        source: crate::instructions::create_market::CreateMarket,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            name: source.name,
            oracle_config: sqlx::types::Json(source.oracle_config),
            quote_lot_size: source.quote_lot_size,
            base_lot_size: source.base_lot_size,
            maker_fee: source.maker_fee,
            taker_fee: source.taker_fee,
            time_expiry: source.time_expiry,
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<CreateMarketRow> for crate::instructions::create_market::CreateMarket {
    type Error = carbon_core::error::Error;
    fn try_from(source: CreateMarketRow) -> Result<Self, Self::Error> {
        Ok(Self {
            name: source.name,
            oracle_config: source.oracle_config.0,
            quote_lot_size: source.quote_lot_size,
            base_lot_size: source.base_lot_size,
            maker_fee: source.maker_fee,
            taker_fee: source.taker_fee,
            time_expiry: source.time_expiry,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::instructions::create_market::CreateMarket {
    fn table() -> &'static str {
        "create_market_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "name",
            "oracle_config",
            "quote_lot_size",
            "base_lot_size",
            "maker_fee",
            "taker_fee",
            "time_expiry",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CreateMarketRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO create_market_instruction (
                "name",
                "oracle_config",
                "quote_lot_size",
                "base_lot_size",
                "maker_fee",
                "taker_fee",
                "time_expiry",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
            )"#,
        )
        .bind(&self.name)
        .bind(&self.oracle_config)
        .bind(self.quote_lot_size)
        .bind(self.base_lot_size)
        .bind(self.maker_fee)
        .bind(self.taker_fee)
        .bind(self.time_expiry)
        .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 CreateMarketRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO create_market_instruction (
                "name",
                "oracle_config",
                "quote_lot_size",
                "base_lot_size",
                "maker_fee",
                "taker_fee",
                "time_expiry",
                __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
                "name" = EXCLUDED."name",
                "oracle_config" = EXCLUDED."oracle_config",
                "quote_lot_size" = EXCLUDED."quote_lot_size",
                "base_lot_size" = EXCLUDED."base_lot_size",
                "maker_fee" = EXCLUDED."maker_fee",
                "taker_fee" = EXCLUDED."taker_fee",
                "time_expiry" = EXCLUDED."time_expiry",
                __instruction_index = EXCLUDED.__instruction_index,
                __stack_height = EXCLUDED.__stack_height,
                __slot = EXCLUDED.__slot,
                __accounts = EXCLUDED.__accounts
            "#,
        )
        .bind(&self.name)
        .bind(&self.oracle_config)
        .bind(self.quote_lot_size)
        .bind(self.base_lot_size)
        .bind(self.maker_fee)
        .bind(self.taker_fee)
        .bind(self.time_expiry)
        .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 CreateMarketRow {
    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_market_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 CreateMarketRow {
    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_market_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 CreateMarketMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CreateMarketMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS create_market_instruction (
                -- Instruction data
                "name" TEXT NOT NULL,
                "oracle_config" JSONB NOT NULL,
                "quote_lot_size" INT8 NOT NULL,
                "base_lot_size" INT8 NOT NULL,
                "maker_fee" INT8 NOT NULL,
                "taker_fee" INT8 NOT NULL,
                "time_expiry" 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 create_market_instruction"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}