carbon-mpl-token-metadata-decoder 1.0.0

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

#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ReservationListV2Row {
    #[sqlx(flatten)]
    pub account_metadata: AccountRowMetadata,
    pub key: sqlx::types::Json<Key>,
    pub master_edition: Pubkey,
    pub supply_snapshot: Option<U64>,
    pub reservations: sqlx::types::Json<Vec<Reservation>>,
    pub total_reservation_spots: U64,
    pub current_reservation_spots: U64,
}

impl ReservationListV2Row {
    pub fn from_parts(
        source: crate::accounts::reservation_list_v2::ReservationListV2,
        metadata: AccountMetadata,
    ) -> Self {
        Self {
            account_metadata: metadata.into(),
            key: sqlx::types::Json(source.key),
            master_edition: source.master_edition.into(),
            supply_snapshot: source.supply_snapshot.map(|value| value.into()),
            reservations: sqlx::types::Json(source.reservations.to_vec()),
            total_reservation_spots: source.total_reservation_spots.into(),
            current_reservation_spots: source.current_reservation_spots.into(),
        }
    }
}

impl TryFrom<ReservationListV2Row> for crate::accounts::reservation_list_v2::ReservationListV2 {
    type Error = carbon_core::error::Error;
    fn try_from(source: ReservationListV2Row) -> Result<Self, Self::Error> {
        Ok(Self {
            key: source.key.0,
            master_edition: *source.master_edition,
            supply_snapshot: source.supply_snapshot.map(|value| *value),
            reservations: source.reservations.0,
            total_reservation_spots: *source.total_reservation_spots,
            current_reservation_spots: *source.current_reservation_spots,
        })
    }
}

impl carbon_core::postgres::operations::Table
    for crate::accounts::reservation_list_v2::ReservationListV2
{
    fn table() -> &'static str {
        "reservation_list_v2_account"
    }

    fn columns() -> Vec<&'static str> {
        vec![
            "__pubkey",
            "__slot",
            "key",
            "master_edition",
            "supply_snapshot",
            "reservations",
            "total_reservation_spots",
            "current_reservation_spots",
        ]
    }
}

#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ReservationListV2Row {
    async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"
            INSERT INTO reservation_list_v2_account (
                "key",
                "master_edition",
                "supply_snapshot",
                "reservations",
                "total_reservation_spots",
                "current_reservation_spots",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            )"#,
        )
        .bind(&self.key)
        .bind(self.master_edition)
        .bind(&self.supply_snapshot)
        .bind(&self.reservations)
        .bind(&self.total_reservation_spots)
        .bind(&self.current_reservation_spots)
        .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 ReservationListV2Row {
    async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
        sqlx::query(
            r#"INSERT INTO reservation_list_v2_account (
                "key",
                "master_edition",
                "supply_snapshot",
                "reservations",
                "total_reservation_spots",
                "current_reservation_spots",
                __pubkey, __slot
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8
            ) ON CONFLICT (
                __pubkey
            ) DO UPDATE SET
                "key" = EXCLUDED."key",
                "master_edition" = EXCLUDED."master_edition",
                "supply_snapshot" = EXCLUDED."supply_snapshot",
                "reservations" = EXCLUDED."reservations",
                "total_reservation_spots" = EXCLUDED."total_reservation_spots",
                "current_reservation_spots" = EXCLUDED."current_reservation_spots",
                __slot = EXCLUDED.__slot
            "#,
        )
        .bind(&self.key)
        .bind(self.master_edition)
        .bind(&self.supply_snapshot)
        .bind(&self.reservations)
        .bind(&self.total_reservation_spots)
        .bind(&self.current_reservation_spots)
        .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 ReservationListV2Row {
    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 reservation_list_v2_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 ReservationListV2Row {
    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 reservation_list_v2_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 ReservationListV2MigrationOperation;

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