carbon-boop-decoder 2.0.0

Boop program decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{
    account::AccountMetadata,
    postgres::{
        metadata::AccountRowMetadata,
        primitives::{Pubkey, U128, U64},
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct LockedCpLiquidityStateRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub locked_lp_amount: U64,
    pub claimed_lp_amount: U64,
    pub unclaimed_lp_amount: U64,
    pub last_lp: U64,
    pub last_k: U128,
    pub recent_epoch: U64,
    pub pool_id: Pubkey,
    pub fee_nft_mint: Pubkey,
    pub locked_owner: Pubkey,
    pub locked_lp_mint: Pubkey,
    pub padding: Vec<U64>,
}

impl LockedCpLiquidityStateRow {
    pub fn from_parts(
        source: crate::accounts::locked_cp_liquidity_state::LockedCpLiquidityState,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            locked_lp_amount: source.locked_lp_amount.into(),
            claimed_lp_amount: source.claimed_lp_amount.into(),
            unclaimed_lp_amount: source.unclaimed_lp_amount.into(),
            last_lp: source.last_lp.into(),
            last_k: source.last_k.into(),
            recent_epoch: source.recent_epoch.into(),
            pool_id: source.pool_id.into(),
            fee_nft_mint: source.fee_nft_mint.into(),
            locked_owner: source.locked_owner.into(),
            locked_lp_mint: source.locked_lp_mint.into(),
            padding: source
                .padding
                .into_iter()
                .map(|element| element.into())
                .collect(),
        }
    }
}

impl TryFrom<LockedCpLiquidityStateRow>
    for crate::accounts::locked_cp_liquidity_state::LockedCpLiquidityState
{
    type Error = carbon_core::error::Error;
    fn try_from(source: LockedCpLiquidityStateRow) -> Result<Self, Self::Error> {
        Ok(Self {
            locked_lp_amount: *source.locked_lp_amount,
            claimed_lp_amount: *source.claimed_lp_amount,
            unclaimed_lp_amount: *source.unclaimed_lp_amount,
            last_lp: *source.last_lp,
            last_k: *source.last_k,
            recent_epoch: *source.recent_epoch,
            pool_id: *source.pool_id,
            fee_nft_mint: *source.fee_nft_mint,
            locked_owner: *source.locked_owner,
            locked_lp_mint: *source.locked_lp_mint,
            padding: source
                .padding
                .into_iter()
                .map(|element| Ok(*element))
                .collect::<Result<Vec<_>, carbon_core::error::Error>>()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to collect array elements".to_string(),
                    )
                })?
                .try_into()
                .map_err(|_| {
                    carbon_core::error::Error::Custom(
                        "Failed to convert array element to primitive".to_string(),
                    )
                })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::locked_cp_liquidity_state::LockedCpLiquidityState
{
    fn table() -> &'static str {
        "locked_cp_liquidity_state_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "locked_lp_amount",
            "claimed_lp_amount",
            "unclaimed_lp_amount",
            "last_lp",
            "last_k",
            "recent_epoch",
            "pool_id",
            "fee_nft_mint",
            "locked_owner",
            "locked_lp_mint",
            "padding",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for LockedCpLiquidityStateRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO locked_cp_liquidity_state_account (
                "locked_lp_amount",
                "claimed_lp_amount",
                "unclaimed_lp_amount",
                "last_lp",
                "last_k",
                "recent_epoch",
                "pool_id",
                "fee_nft_mint",
                "locked_owner",
                "locked_lp_mint",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
            )"#,
        )
        .bind(&self.locked_lp_amount)
        .bind(&self.claimed_lp_amount)
        .bind(&self.unclaimed_lp_amount)
        .bind(&self.last_lp)
        .bind(&self.last_k)
        .bind(&self.recent_epoch)
        .bind(self.pool_id)
        .bind(self.fee_nft_mint)
        .bind(self.locked_owner)
        .bind(self.locked_lp_mint)
        .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 LockedCpLiquidityStateRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO locked_cp_liquidity_state_account (
                "locked_lp_amount",
                "claimed_lp_amount",
                "unclaimed_lp_amount",
                "last_lp",
                "last_k",
                "recent_epoch",
                "pool_id",
                "fee_nft_mint",
                "locked_owner",
                "locked_lp_mint",
                "padding",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "locked_lp_amount" = EXCLUDED."locked_lp_amount",
                "claimed_lp_amount" = EXCLUDED."claimed_lp_amount",
                "unclaimed_lp_amount" = EXCLUDED."unclaimed_lp_amount",
                "last_lp" = EXCLUDED."last_lp",
                "last_k" = EXCLUDED."last_k",
                "recent_epoch" = EXCLUDED."recent_epoch",
                "pool_id" = EXCLUDED."pool_id",
                "fee_nft_mint" = EXCLUDED."fee_nft_mint",
                "locked_owner" = EXCLUDED."locked_owner",
                "locked_lp_mint" = EXCLUDED."locked_lp_mint",
                "padding" = EXCLUDED."padding",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(&self.locked_lp_amount)
        .bind(&self.claimed_lp_amount)
        .bind(&self.unclaimed_lp_amount)
        .bind(&self.last_lp)
        .bind(&self.last_k)
        .bind(&self.recent_epoch)
        .bind(self.pool_id)
        .bind(self.fee_nft_mint)
        .bind(self.locked_owner)
        .bind(self.locked_lp_mint)
        .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 LockedCpLiquidityStateRow {
    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 locked_cp_liquidity_state_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 LockedCpLiquidityStateRow {
    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 locked_cp_liquidity_state_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 LockedCpLiquidityStateMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for LockedCpLiquidityStateMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS locked_cp_liquidity_state_account (
                -- Account data
                "locked_lp_amount" NUMERIC(20) NOT NULL,
                "claimed_lp_amount" NUMERIC(20) NOT NULL,
                "unclaimed_lp_amount" NUMERIC(20) NOT NULL,
                "last_lp" NUMERIC(20) NOT NULL,
                "last_k" NUMERIC(39) NOT NULL,
                "recent_epoch" NUMERIC(20) NOT NULL,
                "pool_id" BYTEA NOT NULL,
                "fee_nft_mint" BYTEA NOT NULL,
                "locked_owner" BYTEA NOT NULL,
                "locked_lp_mint" BYTEA NOT NULL,
                "padding" NUMERIC(20)[] 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 locked_cp_liquidity_state_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}