carbon-marginfi-v2-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct MinimalObligationRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub tag: U64,
    pub last_update_slot: U64,
    pub last_update_stale: U8,
    pub last_update_price_status: U8,
    pub last_update_placeholder: Vec<u8>,
    pub lending_market: Pubkey,
    pub owner: Pubkey,
    pub deposits: sqlx::types::Json<Vec<MinimalObligationCollateral>>,
    pub lowest_reserve_deposit_liquidation_ltv: U64,
    pub deposited_value_sf: Vec<u8>,
    pub padding_part1: Vec<u8>,
    pub padding_part2: Vec<u8>,
    pub padding_part3: Vec<u8>,
    pub padding_part4: Vec<u8>,
    pub padding_part5a: Vec<u8>,
    pub padding_part5c: Vec<u8>,
}

impl MinimalObligationRow {
    pub fn from_parts(
        source: crate::accounts::minimal_obligation::MinimalObligation,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            tag: source.tag.into(),
            last_update_slot: source.last_update_slot.into(),
            last_update_stale: source.last_update_stale.into(),
            last_update_price_status: source.last_update_price_status.into(),
            last_update_placeholder: source.last_update_placeholder.to_vec(),
            lending_market: source.lending_market.into(),
            owner: source.owner.into(),
            deposits: sqlx::types::Json(source.deposits.to_vec()),
            lowest_reserve_deposit_liquidation_ltv: source
                .lowest_reserve_deposit_liquidation_ltv
                .into(),
            deposited_value_sf: source.deposited_value_sf.to_vec(),
            padding_part1: source.padding_part1.to_vec(),
            padding_part2: source.padding_part2.to_vec(),
            padding_part3: source.padding_part3.to_vec(),
            padding_part4: source.padding_part4.to_vec(),
            padding_part5a: source.padding_part5a.to_vec(),
            padding_part5c: source.padding_part5c.to_vec(),
        }
    }
}

impl TryFrom<MinimalObligationRow> for crate::accounts::minimal_obligation::MinimalObligation {
    type Error = carbon_core::error::Error;
    fn try_from(source: MinimalObligationRow) -> Result<Self, Self::Error> {
        Ok(Self {
            tag: *source.tag,
            last_update_slot: *source.last_update_slot,
            last_update_stale: source.last_update_stale.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            last_update_price_status: source.last_update_price_status.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            last_update_placeholder: source
                .last_update_placeholder
                .as_slice()
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert padding from postgres primitive: expected 6 bytes"
                            .to_string(),
                    )
                })?,
            lending_market: *source.lending_market,
            owner: *source.owner,
            deposits: source
                .deposits
                .0
                .into_iter()
                .collect::<Vec<_>>()
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert value from postgres primitive".to_string(),
                    )
                })?,
            lowest_reserve_deposit_liquidation_ltv: *source.lowest_reserve_deposit_liquidation_ltv,
            deposited_value_sf: source
                .deposited_value_sf
                .as_slice()
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert padding from postgres primitive: expected 16 bytes"
                            .to_string(),
                    )
                })?,
            padding_part1: source.padding_part1.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 512 bytes"
                        .to_string(),
                )
            })?,
            padding_part2: source.padding_part2.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 512 bytes"
                        .to_string(),
                )
            })?,
            padding_part3: source.padding_part3.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 512 bytes"
                        .to_string(),
                )
            })?,
            padding_part4: source.padding_part4.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 512 bytes"
                        .to_string(),
                )
            })?,
            padding_part5a: source.padding_part5a.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 64 bytes"
                        .to_string(),
                )
            })?,
            padding_part5c: source.padding_part5c.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 24 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::minimal_obligation::MinimalObligation
{
    fn table() -> &'static str {
        "minimal_obligation_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "tag",
            "last_update_slot",
            "last_update_stale",
            "last_update_price_status",
            "last_update_placeholder",
            "lending_market",
            "owner",
            "deposits",
            "lowest_reserve_deposit_liquidation_ltv",
            "deposited_value_sf",
            "padding_part1",
            "padding_part2",
            "padding_part3",
            "padding_part4",
            "padding_part5a",
            "padding_part5c",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for MinimalObligationRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO minimal_obligation_account (
                "tag",
                "last_update_slot",
                "last_update_stale",
                "last_update_price_status",
                "last_update_placeholder",
                "lending_market",
                "owner",
                "deposits",
                "lowest_reserve_deposit_liquidation_ltv",
                "deposited_value_sf",
                "padding_part1",
                "padding_part2",
                "padding_part3",
                "padding_part4",
                "padding_part5a",
                "padding_part5c",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
            )"#,
        )
        .bind(&self.tag)
        .bind(&self.last_update_slot)
        .bind(self.last_update_stale)
        .bind(self.last_update_price_status)
        .bind(&self.last_update_placeholder)
        .bind(self.lending_market)
        .bind(self.owner)
        .bind(&self.deposits)
        .bind(&self.lowest_reserve_deposit_liquidation_ltv)
        .bind(&self.deposited_value_sf)
        .bind(&self.padding_part1)
        .bind(&self.padding_part2)
        .bind(&self.padding_part3)
        .bind(&self.padding_part4)
        .bind(&self.padding_part5a)
        .bind(&self.padding_part5c)
        .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 MinimalObligationRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(r#"INSERT INTO minimal_obligation_account (
                "tag",
                "last_update_slot",
                "last_update_stale",
                "last_update_price_status",
                "last_update_placeholder",
                "lending_market",
                "owner",
                "deposits",
                "lowest_reserve_deposit_liquidation_ltv",
                "deposited_value_sf",
                "padding_part1",
                "padding_part2",
                "padding_part3",
                "padding_part4",
                "padding_part5a",
                "padding_part5c",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "tag" = EXCLUDED."tag",
                "last_update_slot" = EXCLUDED."last_update_slot",
                "last_update_stale" = EXCLUDED."last_update_stale",
                "last_update_price_status" = EXCLUDED."last_update_price_status",
                "last_update_placeholder" = EXCLUDED."last_update_placeholder",
                "lending_market" = EXCLUDED."lending_market",
                "owner" = EXCLUDED."owner",
                "deposits" = EXCLUDED."deposits",
                "lowest_reserve_deposit_liquidation_ltv" = EXCLUDED."lowest_reserve_deposit_liquidation_ltv",
                "deposited_value_sf" = EXCLUDED."deposited_value_sf",
                "padding_part1" = EXCLUDED."padding_part1",
                "padding_part2" = EXCLUDED."padding_part2",
                "padding_part3" = EXCLUDED."padding_part3",
                "padding_part4" = EXCLUDED."padding_part4",
                "padding_part5a" = EXCLUDED."padding_part5a",
                "padding_part5c" = EXCLUDED."padding_part5c",
                __slot = EXCLUDED.__slot
            "#)
        .bind(&self.tag)
        .bind(&self.last_update_slot)
        .bind(self.last_update_stale)
        .bind(self.last_update_price_status)
        .bind(&self.last_update_placeholder)
        .bind(self.lending_market)
        .bind(self.owner)
        .bind(&self.deposits)
        .bind(&self.lowest_reserve_deposit_liquidation_ltv)
        .bind(&self.deposited_value_sf)
        .bind(&self.padding_part1)
        .bind(&self.padding_part2)
        .bind(&self.padding_part3)
        .bind(&self.padding_part4)
        .bind(&self.padding_part5a)
        .bind(&self.padding_part5c)
        .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 MinimalObligationRow {
    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 minimal_obligation_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 MinimalObligationRow {
    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 minimal_obligation_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 MinimalObligationMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for MinimalObligationMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS minimal_obligation_account (
                -- Account data
                "tag" NUMERIC(20) NOT NULL,
                "last_update_slot" NUMERIC(20) NOT NULL,
                "last_update_stale" INT2 NOT NULL,
                "last_update_price_status" INT2 NOT NULL,
                "last_update_placeholder" BYTEA NOT NULL,
                "lending_market" BYTEA NOT NULL,
                "owner" BYTEA NOT NULL,
                "deposits" JSONB NOT NULL,
                "lowest_reserve_deposit_liquidation_ltv" NUMERIC(20) NOT NULL,
                "deposited_value_sf" BYTEA NOT NULL,
                "padding_part1" BYTEA NOT NULL,
                "padding_part2" BYTEA NOT NULL,
                "padding_part3" BYTEA NOT NULL,
                "padding_part4" BYTEA NOT NULL,
                "padding_part5a" BYTEA NOT NULL,
                "padding_part5c" 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 minimal_obligation_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}