carbon-sharky-decoder 2.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenLendingPoolRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub orderbook: Pubkey,
    pub lender: Pubkey,
    pub usdc_vault: Pubkey,
    pub collateral_vault: Pubkey,
    pub nonce: U64,
    pub total_usdc_liquidity: U64,
    pub available_usdc_liquidity: U64,
    pub collateral_required_amount: U64,
    pub total_usdc_interest: U64,
    pub total_loan_count: U64,
    pub active_loan_count: U64,
    pub total_defaulted_loan_count: U64,
    pub total_defaulted_usdc_amount: U64,
    pub total_defaulted_collateral_amount: U64,
    pub version: U8,
    pub bump: U8,
    pub padding: Vec<u8>,
}

impl TokenLendingPoolRow {
    pub fn from_parts(
        source: crate::accounts::token_lending_pool::TokenLendingPool,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            orderbook: source.orderbook.into(),
            lender: source.lender.into(),
            usdc_vault: source.usdc_vault.into(),
            collateral_vault: source.collateral_vault.into(),
            nonce: source.nonce.into(),
            total_usdc_liquidity: source.total_usdc_liquidity.into(),
            available_usdc_liquidity: source.available_usdc_liquidity.into(),
            collateral_required_amount: source.collateral_required_amount.into(),
            total_usdc_interest: source.total_usdc_interest.into(),
            total_loan_count: source.total_loan_count.into(),
            active_loan_count: source.active_loan_count.into(),
            total_defaulted_loan_count: source.total_defaulted_loan_count.into(),
            total_defaulted_usdc_amount: source.total_defaulted_usdc_amount.into(),
            total_defaulted_collateral_amount: source.total_defaulted_collateral_amount.into(),
            version: source.version.into(),
            bump: source.bump.into(),
            padding: source.padding.to_vec(),
        }
    }
}

impl TryFrom<TokenLendingPoolRow> for crate::accounts::token_lending_pool::TokenLendingPool {
    type Error = carbon_core::error::Error;
    fn try_from(source: TokenLendingPoolRow) -> Result<Self, Self::Error> {
        Ok(Self {
            orderbook: *source.orderbook,
            lender: *source.lender,
            usdc_vault: *source.usdc_vault,
            collateral_vault: *source.collateral_vault,
            nonce: *source.nonce,
            total_usdc_liquidity: *source.total_usdc_liquidity,
            available_usdc_liquidity: *source.available_usdc_liquidity,
            collateral_required_amount: *source.collateral_required_amount,
            total_usdc_interest: *source.total_usdc_interest,
            total_loan_count: *source.total_loan_count,
            active_loan_count: *source.active_loan_count,
            total_defaulted_loan_count: *source.total_defaulted_loan_count,
            total_defaulted_usdc_amount: *source.total_defaulted_usdc_amount,
            total_defaulted_collateral_amount: *source.total_defaulted_collateral_amount,
            version: source.version.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            bump: source.bump.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            padding: source.padding.as_slice().try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert padding from postgres primitive: expected 6 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::token_lending_pool::TokenLendingPool
{
    fn table() -> &'static str {
        "token_lending_pool_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "orderbook",
            "lender",
            "usdc_vault",
            "collateral_vault",
            "nonce",
            "total_usdc_liquidity",
            "available_usdc_liquidity",
            "collateral_required_amount",
            "total_usdc_interest",
            "total_loan_count",
            "active_loan_count",
            "total_defaulted_loan_count",
            "total_defaulted_usdc_amount",
            "total_defaulted_collateral_amount",
            "version",
            "bump",
            "padding",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TokenLendingPoolRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO token_lending_pool_account (
                "orderbook",
                "lender",
                "usdc_vault",
                "collateral_vault",
                "nonce",
                "total_usdc_liquidity",
                "available_usdc_liquidity",
                "collateral_required_amount",
                "total_usdc_interest",
                "total_loan_count",
                "active_loan_count",
                "total_defaulted_loan_count",
                "total_defaulted_usdc_amount",
                "total_defaulted_collateral_amount",
                "version",
                "bump",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19
            )"#,
        )
        .bind(self.orderbook)
        .bind(self.lender)
        .bind(self.usdc_vault)
        .bind(self.collateral_vault)
        .bind(&self.nonce)
        .bind(&self.total_usdc_liquidity)
        .bind(&self.available_usdc_liquidity)
        .bind(&self.collateral_required_amount)
        .bind(&self.total_usdc_interest)
        .bind(&self.total_loan_count)
        .bind(&self.active_loan_count)
        .bind(&self.total_defaulted_loan_count)
        .bind(&self.total_defaulted_usdc_amount)
        .bind(&self.total_defaulted_collateral_amount)
        .bind(self.version)
        .bind(self.bump)
        .bind(&self.padding)
        .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 TokenLendingPoolRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO token_lending_pool_account (
                "orderbook",
                "lender",
                "usdc_vault",
                "collateral_vault",
                "nonce",
                "total_usdc_liquidity",
                "available_usdc_liquidity",
                "collateral_required_amount",
                "total_usdc_interest",
                "total_loan_count",
                "active_loan_count",
                "total_defaulted_loan_count",
                "total_defaulted_usdc_amount",
                "total_defaulted_collateral_amount",
                "version",
                "bump",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "orderbook" = EXCLUDED."orderbook",
                "lender" = EXCLUDED."lender",
                "usdc_vault" = EXCLUDED."usdc_vault",
                "collateral_vault" = EXCLUDED."collateral_vault",
                "nonce" = EXCLUDED."nonce",
                "total_usdc_liquidity" = EXCLUDED."total_usdc_liquidity",
                "available_usdc_liquidity" = EXCLUDED."available_usdc_liquidity",
                "collateral_required_amount" = EXCLUDED."collateral_required_amount",
                "total_usdc_interest" = EXCLUDED."total_usdc_interest",
                "total_loan_count" = EXCLUDED."total_loan_count",
                "active_loan_count" = EXCLUDED."active_loan_count",
                "total_defaulted_loan_count" = EXCLUDED."total_defaulted_loan_count",
                "total_defaulted_usdc_amount" = EXCLUDED."total_defaulted_usdc_amount",
                "total_defaulted_collateral_amount" = EXCLUDED."total_defaulted_collateral_amount",
                "version" = EXCLUDED."version",
                "bump" = EXCLUDED."bump",
                "padding" = EXCLUDED."padding",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.orderbook)
        .bind(self.lender)
        .bind(self.usdc_vault)
        .bind(self.collateral_vault)
        .bind(&self.nonce)
        .bind(&self.total_usdc_liquidity)
        .bind(&self.available_usdc_liquidity)
        .bind(&self.collateral_required_amount)
        .bind(&self.total_usdc_interest)
        .bind(&self.total_loan_count)
        .bind(&self.active_loan_count)
        .bind(&self.total_defaulted_loan_count)
        .bind(&self.total_defaulted_usdc_amount)
        .bind(&self.total_defaulted_collateral_amount)
        .bind(self.version)
        .bind(self.bump)
        .bind(&self.padding)
        .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 TokenLendingPoolRow {
    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 token_lending_pool_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 TokenLendingPoolRow {
    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 token_lending_pool_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 TokenLendingPoolMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TokenLendingPoolMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS token_lending_pool_account (
                -- Account data
                "orderbook" BYTEA NOT NULL,
                "lender" BYTEA NOT NULL,
                "usdc_vault" BYTEA NOT NULL,
                "collateral_vault" BYTEA NOT NULL,
                "nonce" NUMERIC(20) NOT NULL,
                "total_usdc_liquidity" NUMERIC(20) NOT NULL,
                "available_usdc_liquidity" NUMERIC(20) NOT NULL,
                "collateral_required_amount" NUMERIC(20) NOT NULL,
                "total_usdc_interest" NUMERIC(20) NOT NULL,
                "total_loan_count" NUMERIC(20) NOT NULL,
                "active_loan_count" NUMERIC(20) NOT NULL,
                "total_defaulted_loan_count" NUMERIC(20) NOT NULL,
                "total_defaulted_usdc_amount" NUMERIC(20) NOT NULL,
                "total_defaulted_collateral_amount" NUMERIC(20) NOT NULL,
                "version" INT2 NOT NULL,
                "bump" INT2 NOT NULL,
                "padding" 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 token_lending_pool_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}