use {
crate::types::DecompressibleState,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TreeConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub tree_creator: Pubkey,
pub tree_delegate: Pubkey,
pub total_mint_capacity: U64,
pub num_minted: U64,
pub is_public: bool,
pub is_decompressible: sqlx::types::Json<DecompressibleState>,
}
impl TreeConfigRow {
pub fn from_parts(
source: crate::accounts::tree_config::TreeConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
tree_creator: source.tree_creator.into(),
tree_delegate: source.tree_delegate.into(),
total_mint_capacity: source.total_mint_capacity.into(),
num_minted: source.num_minted.into(),
is_public: source.is_public,
is_decompressible: sqlx::types::Json(source.is_decompressible),
}
}
}
impl TryFrom<TreeConfigRow> for crate::accounts::tree_config::TreeConfig {
type Error = carbon_core::error::Error;
fn try_from(source: TreeConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
tree_creator: *source.tree_creator,
tree_delegate: *source.tree_delegate,
total_mint_capacity: *source.total_mint_capacity,
num_minted: *source.num_minted,
is_public: source.is_public,
is_decompressible: source.is_decompressible.0,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::tree_config::TreeConfig {
fn table() -> &'static str {
"tree_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"tree_creator",
"tree_delegate",
"total_mint_capacity",
"num_minted",
"is_public",
"is_decompressible",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TreeConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO tree_config_account (
"tree_creator",
"tree_delegate",
"total_mint_capacity",
"num_minted",
"is_public",
"is_decompressible",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)"#,
)
.bind(self.tree_creator)
.bind(self.tree_delegate)
.bind(&self.total_mint_capacity)
.bind(&self.num_minted)
.bind(self.is_public)
.bind(&self.is_decompressible)
.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 TreeConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO tree_config_account (
"tree_creator",
"tree_delegate",
"total_mint_capacity",
"num_minted",
"is_public",
"is_decompressible",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"tree_creator" = EXCLUDED."tree_creator",
"tree_delegate" = EXCLUDED."tree_delegate",
"total_mint_capacity" = EXCLUDED."total_mint_capacity",
"num_minted" = EXCLUDED."num_minted",
"is_public" = EXCLUDED."is_public",
"is_decompressible" = EXCLUDED."is_decompressible",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.tree_creator)
.bind(self.tree_delegate)
.bind(&self.total_mint_capacity)
.bind(&self.num_minted)
.bind(self.is_public)
.bind(&self.is_decompressible)
.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 TreeConfigRow {
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 tree_config_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 TreeConfigRow {
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 tree_config_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 TreeConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TreeConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS tree_config_account (
-- Account data
"tree_creator" BYTEA NOT NULL,
"tree_delegate" BYTEA NOT NULL,
"total_mint_capacity" NUMERIC(20) NOT NULL,
"num_minted" NUMERIC(20) NOT NULL,
"is_public" BOOLEAN NOT NULL,
"is_decompressible" JSONB 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 tree_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}