carbon-mpl-token-metadata-decoder 1.0.0

MPL Token Metadata Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::{EscrowAuthority, Key},
    carbon_core::{
        account::AccountMetadata,
        postgres::{
            metadata::AccountRowMetadata,
            primitives::{Pubkey, U8},
        },
    },
};

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenOwnedEscrowRow {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub key: sqlx::types::Json<Key>,
    pub base_token: Pubkey,
    pub authority: sqlx::types::Json<EscrowAuthority>,
    pub bump: U8,
}

impl TokenOwnedEscrowRow {
    pub fn from_parts(
        source: crate::accounts::token_owned_escrow::TokenOwnedEscrow,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            key: sqlx::types::Json(source.key),
            base_token: source.base_token.into(),
            authority: sqlx::types::Json(source.authority),
            bump: source.bump.into(),
        }
    }
}

impl TryFrom<TokenOwnedEscrowRow> for crate::accounts::token_owned_escrow::TokenOwnedEscrow {
    type Error = carbon_core::error::Error;
    fn try_from(source: TokenOwnedEscrowRow) -> Result<Self, Self::Error> {
        Ok(Self {
            key: source.key.0,
            base_token: *source.base_token,
            authority: source.authority.0,
            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::token_owned_escrow::TokenOwnedEscrow
{
    fn table() -> &'static str {
        "token_owned_escrow_account"
    }

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

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

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