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},
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ProtocolAdminStateRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub current_protocol_admin: Pubkey,
}

impl ProtocolAdminStateRow {
    pub fn from_parts(
        source: crate::accounts::protocol_admin_state::ProtocolAdminState,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            current_protocol_admin: source.current_protocol_admin.into(),
        }
    }
}

impl TryFrom<ProtocolAdminStateRow> for crate::accounts::protocol_admin_state::ProtocolAdminState {
    type Error = carbon_core::error::Error;
    fn try_from(source: ProtocolAdminStateRow) -> Result<Self, Self::Error> {
        Ok(Self {
            current_protocol_admin: *source.current_protocol_admin,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::protocol_admin_state::ProtocolAdminState
{
    fn table() -> &'static str {
        "protocol_admin_state_account"
    }

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

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

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