carbon-heaven-decoder 2.0.0

Heaven program 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 UserLpPositionRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub pool: Pubkey,
    pub user: Pubkey,
    pub lp_token_balance: U64,
    pub reward_debt: U64,
    pub pending_fees: U64,
    pub bump: U8,
}

impl UserLpPositionRow {
    pub fn from_parts(
        source: crate::accounts::user_lp_position::UserLpPosition,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            pool: source.pool.into(),
            user: source.user.into(),
            lp_token_balance: source.lp_token_balance.into(),
            reward_debt: source.reward_debt.into(),
            pending_fees: source.pending_fees.into(),
            bump: source.bump.into(),
        }
    }
}

impl TryFrom<UserLpPositionRow> for crate::accounts::user_lp_position::UserLpPosition {
    type Error = carbon_core::error::Error;
    fn try_from(source: UserLpPositionRow) -> Result<Self, Self::Error> {
        Ok(Self {
            pool: *source.pool,
            user: *source.user,
            lp_token_balance: *source.lp_token_balance,
            reward_debt: *source.reward_debt,
            pending_fees: *source.pending_fees,
            bump: source.bump.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::user_lp_position::UserLpPosition
{
    fn table() -> &'static str {
        "user_lp_position_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "pool",
            "user",
            "lp_token_balance",
            "reward_debt",
            "pending_fees",
            "bump",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for UserLpPositionRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO user_lp_position_account (
                "pool",
                "user",
                "lp_token_balance",
                "reward_debt",
                "pending_fees",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            )"#,
        )
        .bind(self.pool)
        .bind(self.user)
        .bind(&self.lp_token_balance)
        .bind(&self.reward_debt)
        .bind(&self.pending_fees)
        .bind(self.bump)
        .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 UserLpPositionRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO user_lp_position_account (
                "pool",
                "user",
                "lp_token_balance",
                "reward_debt",
                "pending_fees",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "pool" = EXCLUDED."pool",
                "user" = EXCLUDED."user",
                "lp_token_balance" = EXCLUDED."lp_token_balance",
                "reward_debt" = EXCLUDED."reward_debt",
                "pending_fees" = EXCLUDED."pending_fees",
                "bump" = EXCLUDED."bump",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.pool)
        .bind(self.user)
        .bind(&self.lp_token_balance)
        .bind(&self.reward_debt)
        .bind(&self.pending_fees)
        .bind(self.bump)
        .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 UserLpPositionRow {
    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 user_lp_position_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 UserLpPositionRow {
    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 user_lp_position_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 UserLpPositionMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for UserLpPositionMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS user_lp_position_account (
                -- Account data
                "pool" BYTEA NOT NULL,
                "user" BYTEA NOT NULL,
                "lp_token_balance" NUMERIC(20) NOT NULL,
                "reward_debt" NUMERIC(20) NOT NULL,
                "pending_fees" NUMERIC(20) NOT NULL,
                "bump" INT2 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 user_lp_position_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}