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, U16, U32, U64, U8},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenLendingOrderBookRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub fee_authority: Pubkey,
    pub loan_mint: Pubkey,
    pub collateral_mint: Pubkey,
    pub collateral_token_program: Pubkey,
    pub duration: U64,
    pub min_liquidity: U64,
    pub max_liquidity: U64,
    pub apr: U32,
    pub fee_permillicentage: U16,
    pub collateral_decimals: U8,
    pub version: U8,
    pub bump: U8,
    pub padding: Vec<u8>,
}

impl TokenLendingOrderBookRow {
    pub fn from_parts(
        source: crate::accounts::token_lending_order_book::TokenLendingOrderBook,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            fee_authority: source.fee_authority.into(),
            loan_mint: source.loan_mint.into(),
            collateral_mint: source.collateral_mint.into(),
            collateral_token_program: source.collateral_token_program.into(),
            duration: source.duration.into(),
            min_liquidity: source.min_liquidity.into(),
            max_liquidity: source.max_liquidity.into(),
            apr: source.apr.into(),
            fee_permillicentage: source.fee_permillicentage.into(),
            collateral_decimals: source.collateral_decimals.into(),
            version: source.version.into(),
            bump: source.bump.into(),
            padding: source.padding.to_vec(),
        }
    }
}

impl TryFrom<TokenLendingOrderBookRow>
    for crate::accounts::token_lending_order_book::TokenLendingOrderBook
{
    type Error = carbon_core::error::Error;
    fn try_from(source: TokenLendingOrderBookRow) -> Result<Self, Self::Error> {
        Ok(Self {
            fee_authority: *source.fee_authority,
            loan_mint: *source.loan_mint,
            collateral_mint: *source.collateral_mint,
            collateral_token_program: *source.collateral_token_program,
            duration: *source.duration,
            min_liquidity: *source.min_liquidity,
            max_liquidity: *source.max_liquidity,
            apr: source.apr.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            fee_permillicentage: source.fee_permillicentage.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            collateral_decimals: source.collateral_decimals.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            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 7 bytes"
                        .to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::token_lending_order_book::TokenLendingOrderBook
{
    fn table() -> &'static str {
        "token_lending_order_book_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "fee_authority",
            "loan_mint",
            "collateral_mint",
            "collateral_token_program",
            "duration",
            "min_liquidity",
            "max_liquidity",
            "apr",
            "fee_permillicentage",
            "collateral_decimals",
            "version",
            "bump",
            "padding",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TokenLendingOrderBookRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO token_lending_order_book_account (
                "fee_authority",
                "loan_mint",
                "collateral_mint",
                "collateral_token_program",
                "duration",
                "min_liquidity",
                "max_liquidity",
                "apr",
                "fee_permillicentage",
                "collateral_decimals",
                "version",
                "bump",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
            )"#,
        )
        .bind(self.fee_authority)
        .bind(self.loan_mint)
        .bind(self.collateral_mint)
        .bind(self.collateral_token_program)
        .bind(&self.duration)
        .bind(&self.min_liquidity)
        .bind(&self.max_liquidity)
        .bind(self.apr)
        .bind(self.fee_permillicentage)
        .bind(self.collateral_decimals)
        .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 TokenLendingOrderBookRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO token_lending_order_book_account (
                "fee_authority",
                "loan_mint",
                "collateral_mint",
                "collateral_token_program",
                "duration",
                "min_liquidity",
                "max_liquidity",
                "apr",
                "fee_permillicentage",
                "collateral_decimals",
                "version",
                "bump",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "fee_authority" = EXCLUDED."fee_authority",
                "loan_mint" = EXCLUDED."loan_mint",
                "collateral_mint" = EXCLUDED."collateral_mint",
                "collateral_token_program" = EXCLUDED."collateral_token_program",
                "duration" = EXCLUDED."duration",
                "min_liquidity" = EXCLUDED."min_liquidity",
                "max_liquidity" = EXCLUDED."max_liquidity",
                "apr" = EXCLUDED."apr",
                "fee_permillicentage" = EXCLUDED."fee_permillicentage",
                "collateral_decimals" = EXCLUDED."collateral_decimals",
                "version" = EXCLUDED."version",
                "bump" = EXCLUDED."bump",
                "padding" = EXCLUDED."padding",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.fee_authority)
        .bind(self.loan_mint)
        .bind(self.collateral_mint)
        .bind(self.collateral_token_program)
        .bind(&self.duration)
        .bind(&self.min_liquidity)
        .bind(&self.max_liquidity)
        .bind(self.apr)
        .bind(self.fee_permillicentage)
        .bind(self.collateral_decimals)
        .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 TokenLendingOrderBookRow {
    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_order_book_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 TokenLendingOrderBookRow {
    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_order_book_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 TokenLendingOrderBookMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TokenLendingOrderBookMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS token_lending_order_book_account (
                -- Account data
                "fee_authority" BYTEA NOT NULL,
                "loan_mint" BYTEA NOT NULL,
                "collateral_mint" BYTEA NOT NULL,
                "collateral_token_program" BYTEA NOT NULL,
                "duration" NUMERIC(20) NOT NULL,
                "min_liquidity" NUMERIC(20) NOT NULL,
                "max_liquidity" NUMERIC(20) NOT NULL,
                "apr" INT8 NOT NULL,
                "fee_permillicentage" INT4 NOT NULL,
                "collateral_decimals" INT2 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_order_book_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}