use {
crate::types::Key,
carbon_core::{
account::AccountMetadata,
postgres::{metadata::AccountRowMetadata, primitives::U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct MasterEditionV2Row {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub key: sqlx::types::Json<Key>,
pub supply: U64,
pub max_supply: Option<U64>,
}
impl MasterEditionV2Row {
pub fn from_parts(
source: crate::accounts::master_edition_v2::MasterEditionV2,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
key: sqlx::types::Json(source.key),
supply: source.supply.into(),
max_supply: source.max_supply.map(|value| value.into()),
}
}
}
impl TryFrom<MasterEditionV2Row> for crate::accounts::master_edition_v2::MasterEditionV2 {
type Error = carbon_core::error::Error;
fn try_from(source: MasterEditionV2Row) -> Result<Self, Self::Error> {
Ok(Self {
key: source.key.0,
supply: *source.supply,
max_supply: source.max_supply.map(|value| *value),
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::master_edition_v2::MasterEditionV2
{
fn table() -> &'static str {
"master_edition_v2_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "key", "supply", "max_supply"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for MasterEditionV2Row {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO master_edition_v2_account (
"key",
"supply",
"max_supply",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
)"#,
)
.bind(&self.key)
.bind(&self.supply)
.bind(&self.max_supply)
.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 MasterEditionV2Row {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO master_edition_v2_account (
"key",
"supply",
"max_supply",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"key" = EXCLUDED."key",
"supply" = EXCLUDED."supply",
"max_supply" = EXCLUDED."max_supply",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.key)
.bind(&self.supply)
.bind(&self.max_supply)
.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 MasterEditionV2Row {
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 master_edition_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 MasterEditionV2Row {
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 master_edition_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 MasterEditionV2MigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for MasterEditionV2MigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS master_edition_v2_account (
-- Account data
"key" JSONB NOT NULL,
"supply" NUMERIC(20) NOT NULL,
"max_supply" NUMERIC(20),
-- 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 master_edition_v2_account"#)
.execute(connection)
.await?;
Ok(())
}
}