carbon-solayer-restaking-program-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct RestakingPoolRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub lst_mint: Pubkey,
    pub rst_mint: Pubkey,
    pub bump: U8,
}

impl RestakingPoolRow {
    pub fn from_parts(
        source: crate::accounts::restaking_pool::RestakingPool,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            lst_mint: source.lst_mint.into(),
            rst_mint: source.rst_mint.into(),
            bump: source.bump.into(),
        }
    }
}

impl TryFrom<RestakingPoolRow> for crate::accounts::restaking_pool::RestakingPool {
    type Error = carbon_core::error::Error;
    fn try_from(source: RestakingPoolRow) -> Result<Self, Self::Error> {
        Ok(Self {
            lst_mint: *source.lst_mint,
            rst_mint: *source.rst_mint,
            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::restaking_pool::RestakingPool {
    fn table() -> &'static str {
        "restaking_pool_account"
    }

    fn columns() -> Vec<&'static str> {
        vec!["__pubkey", "__slot", "lst_mint", "rst_mint", "bump"]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for RestakingPoolRow {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO restaking_pool_account (
                "lst_mint",
                "rst_mint",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            )"#,
        )
        .bind(self.lst_mint)
        .bind(self.rst_mint)
        .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 RestakingPoolRow {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO restaking_pool_account (
                "lst_mint",
                "rst_mint",
                "bump",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "lst_mint" = EXCLUDED."lst_mint",
                "rst_mint" = EXCLUDED."rst_mint",
                "bump" = EXCLUDED."bump",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(self.lst_mint)
        .bind(self.rst_mint)
        .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 RestakingPoolRow {
    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 restaking_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 RestakingPoolRow {
    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 restaking_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 RestakingPoolMigrationOperation;

#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for RestakingPoolMigrationOperation {
    async fn up(
        &self,
        connection: &mut sqlx::PgConnection,
    ) -> Result<(), sqlx_migrator::error::Error> {
        sqlx::query(
            r#"CREATE TABLE IF NOT EXISTS restaking_pool_account (
                -- Account data
                "lst_mint" BYTEA NOT NULL,
                "rst_mint" BYTEA 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 restaking_pool_account"#)
            .execute(connection)
            .await?;
        Ok(())
    }
}