use {
crate::types::{
Collection, CollectionDetails, Data, Key, ProgrammableConfig, TokenStandard, Uses,
},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct MetadataRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub key: sqlx::types::Json<Key>,
pub update_authority: Pubkey,
pub mint: Pubkey,
pub data: sqlx::types::Json<Data>,
pub primary_sale_happened: bool,
pub is_mutable: bool,
pub edition_nonce: Option<U8>,
pub token_standard: Option<sqlx::types::Json<TokenStandard>>,
pub collection: Option<sqlx::types::Json<Collection>>,
pub uses: Option<sqlx::types::Json<Uses>>,
pub collection_details: Option<sqlx::types::Json<CollectionDetails>>,
pub programmable_config: Option<sqlx::types::Json<ProgrammableConfig>>,
}
impl MetadataRow {
pub fn from_parts(
source: crate::accounts::metadata::Metadata,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
key: sqlx::types::Json(source.key),
update_authority: source.update_authority.into(),
mint: source.mint.into(),
data: sqlx::types::Json(source.data),
primary_sale_happened: source.primary_sale_happened,
is_mutable: source.is_mutable,
edition_nonce: source.edition_nonce.map(|value| value.into()),
token_standard: source.token_standard.map(sqlx::types::Json),
collection: source.collection.map(sqlx::types::Json),
uses: source.uses.map(sqlx::types::Json),
collection_details: source.collection_details.map(sqlx::types::Json),
programmable_config: source.programmable_config.map(sqlx::types::Json),
}
}
}
impl TryFrom<MetadataRow> for crate::accounts::metadata::Metadata {
type Error = carbon_core::error::Error;
fn try_from(source: MetadataRow) -> Result<Self, Self::Error> {
Ok(Self {
key: source.key.0,
update_authority: *source.update_authority,
mint: *source.mint,
data: source.data.0,
primary_sale_happened: source.primary_sale_happened,
is_mutable: source.is_mutable,
edition_nonce: source
.edition_nonce
.map(|value| {
value.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.transpose()?,
token_standard: source.token_standard.map(|value| value.0),
collection: source.collection.map(|value| value.0),
uses: source.uses.map(|value| value.0),
collection_details: source.collection_details.map(|value| value.0),
programmable_config: source.programmable_config.map(|value| value.0),
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::metadata::Metadata {
fn table() -> &'static str {
"metadata_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"key",
"update_authority",
"mint",
"data",
"primary_sale_happened",
"is_mutable",
"edition_nonce",
"token_standard",
"collection",
"uses",
"collection_details",
"programmable_config",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for MetadataRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO metadata_account (
"key",
"update_authority",
"mint",
"data",
"primary_sale_happened",
"is_mutable",
"edition_nonce",
"token_standard",
"collection",
"uses",
"collection_details",
"programmable_config",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)"#,
)
.bind(&self.key)
.bind(self.update_authority)
.bind(self.mint)
.bind(&self.data)
.bind(self.primary_sale_happened)
.bind(self.is_mutable)
.bind(self.edition_nonce)
.bind(&self.token_standard)
.bind(&self.collection)
.bind(&self.uses)
.bind(&self.collection_details)
.bind(&self.programmable_config)
.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 MetadataRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO metadata_account (
"key",
"update_authority",
"mint",
"data",
"primary_sale_happened",
"is_mutable",
"edition_nonce",
"token_standard",
"collection",
"uses",
"collection_details",
"programmable_config",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"key" = EXCLUDED."key",
"update_authority" = EXCLUDED."update_authority",
"mint" = EXCLUDED."mint",
"data" = EXCLUDED."data",
"primary_sale_happened" = EXCLUDED."primary_sale_happened",
"is_mutable" = EXCLUDED."is_mutable",
"edition_nonce" = EXCLUDED."edition_nonce",
"token_standard" = EXCLUDED."token_standard",
"collection" = EXCLUDED."collection",
"uses" = EXCLUDED."uses",
"collection_details" = EXCLUDED."collection_details",
"programmable_config" = EXCLUDED."programmable_config",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.key)
.bind(self.update_authority)
.bind(self.mint)
.bind(&self.data)
.bind(self.primary_sale_happened)
.bind(self.is_mutable)
.bind(self.edition_nonce)
.bind(&self.token_standard)
.bind(&self.collection)
.bind(&self.uses)
.bind(&self.collection_details)
.bind(&self.programmable_config)
.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 MetadataRow {
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 metadata_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 MetadataRow {
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 metadata_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 MetadataMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for MetadataMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS metadata_account (
-- Account data
"key" JSONB NOT NULL,
"update_authority" BYTEA NOT NULL,
"mint" BYTEA NOT NULL,
"data" JSONB NOT NULL,
"primary_sale_happened" BOOLEAN NOT NULL,
"is_mutable" BOOLEAN NOT NULL,
"edition_nonce" INT2,
"token_standard" JSONB,
"collection" JSONB,
"uses" JSONB,
"collection_details" JSONB,
"programmable_config" JSONB,
-- 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 metadata_account"#)
.execute(connection)
.await?;
Ok(())
}
}