carbon-bubblegum-decoder 2.0.0

Bubblegum Program Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::LeafSchema,
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U32},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct VoucherRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub leaf_schema: sqlx::types::Json<LeafSchema>,
    pub index: U32,
    pub merkle_tree: Pubkey,
}

impl VoucherRow {
    pub fn from_parts(
        source: crate::accounts::voucher::Voucher,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            leaf_schema: sqlx::types::Json(source.leaf_schema),
            index: source.index.into(),
            merkle_tree: source.merkle_tree.into(),
        }
    }
}

impl TryFrom<VoucherRow> for crate::accounts::voucher::Voucher {
    type Error = carbon_core::error::Error;
    fn try_from(source: VoucherRow) -> Result<Self, Self::Error> {
        Ok(Self {
            leaf_schema: source.leaf_schema.0,
            index: source.index.try_into().map_err(|_| {
                carbon_core::error::Error::Custom(
                    "Failed to convert value from postgres primitive".to_string(),
                )
            })?,
            merkle_tree: *source.merkle_tree,
        })
    }
}

impl carbon_core::postgres::operations::Table for crate::accounts::voucher::Voucher {
    fn table() -> &'static str {
        "voucher_account"
    }

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

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

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