carbon-marginfi-v2-decoder 1.0.0

MarginfiV2 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::{RiskTier, WrappedI80F48},
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U16, U64},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct StakedSettingsRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub key: Pubkey,
    pub marginfi_group: Pubkey,
    pub oracle: Pubkey,
    pub asset_weight_init: sqlx::types::Json<WrappedI80F48>,
    pub asset_weight_maint: sqlx::types::Json<WrappedI80F48>,
    pub deposit_limit: U64,
    pub total_asset_value_init_limit: U64,
    pub oracle_max_age: U16,
    pub risk_tier: sqlx::types::Json<RiskTier>,
    pub pad0: Vec<u8>,
    pub reserved0: Vec<u8>,
    pub reserved1: Vec<u8>,
    pub reserved2: Vec<u8>,
}

impl StakedSettingsRow {
    pub fn from_parts(
        source: crate::accounts::staked_settings::StakedSettings,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            key: source.key.into(),
            marginfi_group: source.marginfi_group.into(),
            oracle: source.oracle.into(),
            asset_weight_init: sqlx::types::Json(source.asset_weight_init),
            asset_weight_maint: sqlx::types::Json(source.asset_weight_maint),
            deposit_limit: source.deposit_limit.into(),
            total_asset_value_init_limit: source.total_asset_value_init_limit.into(),
            oracle_max_age: source.oracle_max_age.into(),
            risk_tier: sqlx::types::Json(source.risk_tier),
            pad0: source.pad0.to_vec(),
            reserved0: source.reserved0.to_vec(),
            reserved1: source.reserved1.to_vec(),
            reserved2: source.reserved2.to_vec(),
        }
    }
}

impl TryFrom<StakedSettingsRow> for crate::accounts::staked_settings::StakedSettings {
    type Error = carbon_core::error::Error;
    fn try_from(source: StakedSettingsRow) -> Result<Self, Self::Error> {
        Ok(Self {
            key: *source.key,
            marginfi_group: *source.marginfi_group,
            oracle: *source.oracle,
            asset_weight_init: source.asset_weight_init.0,
            asset_weight_maint: source.asset_weight_maint.0,
            deposit_limit: *source.deposit_limit,
            total_asset_value_init_limit: *source.total_asset_value_init_limit,
            oracle_max_age: source.oracle_max_age.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            risk_tier: source.risk_tier.0,
            pad0: source.pad0.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 5 bytes"
                        .to_string(),
                )
            })?,
            reserved0: source.reserved0.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 8 bytes"
                        .to_string(),
                )
            })?,
            reserved1: source.reserved1.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 32 bytes"
                        .to_string(),
                )
            })?,
            reserved2: source.reserved2.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 64 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::staked_settings::StakedSettings {
    fn table() -> &'static str {
        "staked_settings_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "key",
            "marginfi_group",
            "oracle",
            "asset_weight_init",
            "asset_weight_maint",
            "deposit_limit",
            "total_asset_value_init_limit",
            "oracle_max_age",
            "risk_tier",
            "pad0",
            "reserved0",
            "reserved1",
            "reserved2",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for StakedSettingsRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO staked_settings_account (
                "key",
                "marginfi_group",
                "oracle",
                "asset_weight_init",
                "asset_weight_maint",
                "deposit_limit",
                "total_asset_value_init_limit",
                "oracle_max_age",
                "risk_tier",
                "pad0",
                "reserved0",
                "reserved1",
                "reserved2",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
            )"#,
        )
        .bind(self.key)
        .bind(self.marginfi_group)
        .bind(self.oracle)
        .bind(&self.asset_weight_init)
        .bind(&self.asset_weight_maint)
        .bind(&self.deposit_limit)
        .bind(&self.total_asset_value_init_limit)
        .bind(self.oracle_max_age)
        .bind(&self.risk_tier)
        .bind(&self.pad0)
        .bind(&self.reserved0)
        .bind(&self.reserved1)
        .bind(&self.reserved2)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .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 StakedSettingsRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO staked_settings_account (
                "key",
                "marginfi_group",
                "oracle",
                "asset_weight_init",
                "asset_weight_maint",
                "deposit_limit",
                "total_asset_value_init_limit",
                "oracle_max_age",
                "risk_tier",
                "pad0",
                "reserved0",
                "reserved1",
                "reserved2",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "key" = EXCLUDED."key",
                "marginfi_group" = EXCLUDED."marginfi_group",
                "oracle" = EXCLUDED."oracle",
                "asset_weight_init" = EXCLUDED."asset_weight_init",
                "asset_weight_maint" = EXCLUDED."asset_weight_maint",
                "deposit_limit" = EXCLUDED."deposit_limit",
                "total_asset_value_init_limit" = EXCLUDED."total_asset_value_init_limit",
                "oracle_max_age" = EXCLUDED."oracle_max_age",
                "risk_tier" = EXCLUDED."risk_tier",
                "pad0" = EXCLUDED."pad0",
                "reserved0" = EXCLUDED."reserved0",
                "reserved1" = EXCLUDED."reserved1",
                "reserved2" = EXCLUDED."reserved2",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.key)
        .bind(self.marginfi_group)
        .bind(self.oracle)
        .bind(&self.asset_weight_init)
        .bind(&self.asset_weight_maint)
        .bind(&self.deposit_limit)
        .bind(&self.total_asset_value_init_limit)
        .bind(self.oracle_max_age)
        .bind(&self.risk_tier)
        .bind(&self.pad0)
        .bind(&self.reserved0)
        .bind(&self.reserved1)
        .bind(&self.reserved2)
        .bind(self.account_metadata.pubkey)
        .bind(&self.account_metadata.slot)
        .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 StakedSettingsRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"DELETE FROM staked_settings_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .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 StakedSettingsRow {
    type Key = carbon_core::postgres::primitives::Pubkey;

    async fn lookup(
        key: Self::Key,
        pool: &sqlx::PgPool,
    ) -> carbon_core::error::CarbonResult<Option<Self>> {
        let row = sqlx::query_as(
            r#"SELECT * FROM staked_settings_account WHERE
                __pubkey = $1
            "#,
        )
        .bind(key)
        .fetch_optional(pool)
        .await
        .map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
        Ok(row)
    }
}

pub struct StakedSettingsMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for StakedSettingsMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS staked_settings_account (
                -- Account data
                "key" BYTEA NOT NULL,
                "marginfi_group" BYTEA NOT NULL,
                "oracle" BYTEA NOT NULL,
                "asset_weight_init" JSONB NOT NULL,
                "asset_weight_maint" JSONB NOT NULL,
                "deposit_limit" NUMERIC(20) NOT NULL,
                "total_asset_value_init_limit" NUMERIC(20) NOT NULL,
                "oracle_max_age" INT4 NOT NULL,
                "risk_tier" JSONB NOT NULL,
                "pad0" BYTEA NOT NULL,
                "reserved0" BYTEA NOT NULL,
                "reserved1" BYTEA NOT NULL,
                "reserved2" BYTEA NOT NULL,
                -- Account metadata
                __pubkey BYTEA NOT NULL,
                __slot NUMERIC(20),
                PRIMARY KEY (__pubkey)
            )"#,
        )
        .execute(connection)
        .await?;
        Ok(())
    }

    async fn down(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(r#"DROP TABLE IF EXISTS staked_settings_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}