carbon-marginfi-v2-decoder 1.0.0

MarginfiV2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::WrappedI80F48,
    carbon_core::{
        instruction::InstructionMetadata,
        postgres::{metadata::InstructionRowMetadata, primitives::Pubkey},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct MarginfiGroupConfigureRow {
    #[sqlx(flatten)]
    pub instruction_metadata: InstructionRowMetadata,
    pub new_admin: Option<Pubkey>,
    pub new_emode_admin: Option<Pubkey>,
    pub new_curve_admin: Option<Pubkey>,
    pub new_limit_admin: Option<Pubkey>,
    pub new_flow_admin: Option<Pubkey>,
    pub new_emissions_admin: Option<Pubkey>,
    pub new_metadata_admin: Option<Pubkey>,
    pub new_risk_admin: Option<Pubkey>,
    pub emode_max_init_leverage: Option<sqlx::types::Json<WrappedI80F48>>,
    pub emode_max_maint_leverage: Option<sqlx::types::Json<WrappedI80F48>>,
    #[sqlx(rename = "__accounts")]
    pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}

impl MarginfiGroupConfigureRow {
    pub fn from_parts(
        source: crate::instructions::marginfi_group_configure::MarginfiGroupConfigure,
        metadata: InstructionMetadata,
        accounts: Vec<solana_instruction::AccountMeta>,
    ) -> Self {
        Self {
            instruction_metadata: metadata.into(),
            new_admin: source.new_admin.map(|value| value.into()),
            new_emode_admin: source.new_emode_admin.map(|value| value.into()),
            new_curve_admin: source.new_curve_admin.map(|value| value.into()),
            new_limit_admin: source.new_limit_admin.map(|value| value.into()),
            new_flow_admin: source.new_flow_admin.map(|value| value.into()),
            new_emissions_admin: source.new_emissions_admin.map(|value| value.into()),
            new_metadata_admin: source.new_metadata_admin.map(|value| value.into()),
            new_risk_admin: source.new_risk_admin.map(|value| value.into()),
            emode_max_init_leverage: source.emode_max_init_leverage.map(sqlx::types::Json),
            emode_max_maint_leverage: source.emode_max_maint_leverage.map(sqlx::types::Json),
            accounts: sqlx::types::Json(accounts),
        }
    }
}

impl TryFrom<MarginfiGroupConfigureRow>
    for crate::instructions::marginfi_group_configure::MarginfiGroupConfigure
{
    type Error = carbon_core::error::Error;
    fn try_from(source: MarginfiGroupConfigureRow) -> Result<Self, Self::Error> {
        Ok(Self {
            new_admin: source.new_admin.map(|value| *value),
            new_emode_admin: source.new_emode_admin.map(|value| *value),
            new_curve_admin: source.new_curve_admin.map(|value| *value),
            new_limit_admin: source.new_limit_admin.map(|value| *value),
            new_flow_admin: source.new_flow_admin.map(|value| *value),
            new_emissions_admin: source.new_emissions_admin.map(|value| *value),
            new_metadata_admin: source.new_metadata_admin.map(|value| *value),
            new_risk_admin: source.new_risk_admin.map(|value| *value),
            emode_max_init_leverage: source.emode_max_init_leverage.map(|value| value.0),
            emode_max_maint_leverage: source.emode_max_maint_leverage.map(|value| value.0),
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::instructions::marginfi_group_configure::MarginfiGroupConfigure
{
    fn table() -> &'static str {
        "marginfi_group_configure_instruction"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__signature",
            "__instruction_index",
            "__stack_height",
            "__slot",
            "new_admin",
            "new_emode_admin",
            "new_curve_admin",
            "new_limit_admin",
            "new_flow_admin",
            "new_emissions_admin",
            "new_metadata_admin",
            "new_risk_admin",
            "emode_max_init_leverage",
            "emode_max_maint_leverage",
            "__accounts",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for MarginfiGroupConfigureRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO marginfi_group_configure_instruction (
                "new_admin",
                "new_emode_admin",
                "new_curve_admin",
                "new_limit_admin",
                "new_flow_admin",
                "new_emissions_admin",
                "new_metadata_admin",
                "new_risk_admin",
                "emode_max_init_leverage",
                "emode_max_maint_leverage",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
            )"#,
        )
        .bind(self.new_admin)
        .bind(self.new_emode_admin)
        .bind(self.new_curve_admin)
        .bind(self.new_limit_admin)
        .bind(self.new_flow_admin)
        .bind(self.new_emissions_admin)
        .bind(self.new_metadata_admin)
        .bind(self.new_risk_admin)
        .bind(&self.emode_max_init_leverage)
        .bind(&self.emode_max_maint_leverage)
        .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 MarginfiGroupConfigureRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO marginfi_group_configure_instruction (
                "new_admin",
                "new_emode_admin",
                "new_curve_admin",
                "new_limit_admin",
                "new_flow_admin",
                "new_emissions_admin",
                "new_metadata_admin",
                "new_risk_admin",
                "emode_max_init_leverage",
                "emode_max_maint_leverage",
                __signature, __instruction_index, __stack_height, __slot, __accounts
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
            ) ON CONFLICT (
                __signature, __instruction_index, __stack_height
            ) DO UPDATE SET
                "new_admin" = EXCLUDED."new_admin",
                "new_emode_admin" = EXCLUDED."new_emode_admin",
                "new_curve_admin" = EXCLUDED."new_curve_admin",
                "new_limit_admin" = EXCLUDED."new_limit_admin",
                "new_flow_admin" = EXCLUDED."new_flow_admin",
                "new_emissions_admin" = EXCLUDED."new_emissions_admin",
                "new_metadata_admin" = EXCLUDED."new_metadata_admin",
                "new_risk_admin" = EXCLUDED."new_risk_admin",
                "emode_max_init_leverage" = EXCLUDED."emode_max_init_leverage",
                "emode_max_maint_leverage" = EXCLUDED."emode_max_maint_leverage",
                __instruction_index = EXCLUDED.__instruction_index,
                __stack_height = EXCLUDED.__stack_height,
                __slot = EXCLUDED.__slot,
                __accounts = EXCLUDED.__accounts
            "#,
        )
        .bind(self.new_admin)
        .bind(self.new_emode_admin)
        .bind(self.new_curve_admin)
        .bind(self.new_limit_admin)
        .bind(self.new_flow_admin)
        .bind(self.new_emissions_admin)
        .bind(self.new_metadata_admin)
        .bind(self.new_risk_admin)
        .bind(&self.emode_max_init_leverage)
        .bind(&self.emode_max_maint_leverage)
        .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 MarginfiGroupConfigureRow {
    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 marginfi_group_configure_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 MarginfiGroupConfigureRow {
    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 marginfi_group_configure_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 MarginfiGroupConfigureMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for MarginfiGroupConfigureMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS marginfi_group_configure_instruction (
                -- Instruction data
                "new_admin" BYTEA,
                "new_emode_admin" BYTEA,
                "new_curve_admin" BYTEA,
                "new_limit_admin" BYTEA,
                "new_flow_admin" BYTEA,
                "new_emissions_admin" BYTEA,
                "new_metadata_admin" BYTEA,
                "new_risk_admin" BYTEA,
                "emode_max_init_leverage" JSONB,
                "emode_max_maint_leverage" 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 marginfi_group_configure_instruction"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}