use {
crate::types::PlatformCurveParam,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PlatformConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub epoch: U64,
pub platform_fee_wallet: Pubkey,
pub platform_nft_wallet: Pubkey,
pub platform_scale: U64,
pub creator_scale: U64,
pub burn_scale: U64,
pub fee_rate: U64,
pub name: Vec<u8>,
pub web: Vec<u8>,
pub img: Vec<u8>,
pub cpswap_config: Pubkey,
pub creator_fee_rate: U64,
pub transfer_fee_extension_auth: Pubkey,
pub platform_vesting_wallet: Pubkey,
pub platform_vesting_scale: U64,
pub platform_cp_creator: Pubkey,
pub padding: Vec<u8>,
pub curve_params: sqlx::types::Json<Vec<PlatformCurveParam>>,
}
impl PlatformConfigRow {
pub fn from_parts(
source: crate::accounts::platform_config::PlatformConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
epoch: source.epoch.into(),
platform_fee_wallet: source.platform_fee_wallet.into(),
platform_nft_wallet: source.platform_nft_wallet.into(),
platform_scale: source.platform_scale.into(),
creator_scale: source.creator_scale.into(),
burn_scale: source.burn_scale.into(),
fee_rate: source.fee_rate.into(),
name: source.name.to_vec(),
web: source.web.to_vec(),
img: source.img.to_vec(),
cpswap_config: source.cpswap_config.into(),
creator_fee_rate: source.creator_fee_rate.into(),
transfer_fee_extension_auth: source.transfer_fee_extension_auth.into(),
platform_vesting_wallet: source.platform_vesting_wallet.into(),
platform_vesting_scale: source.platform_vesting_scale.into(),
platform_cp_creator: source.platform_cp_creator.into(),
padding: source.padding.to_vec(),
curve_params: sqlx::types::Json(source.curve_params.to_vec()),
}
}
}
impl TryFrom<PlatformConfigRow> for crate::accounts::platform_config::PlatformConfig {
type Error = carbon_core::error::Error;
fn try_from(source: PlatformConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
epoch: *source.epoch,
platform_fee_wallet: *source.platform_fee_wallet,
platform_nft_wallet: *source.platform_nft_wallet,
platform_scale: *source.platform_scale,
creator_scale: *source.creator_scale,
burn_scale: *source.burn_scale,
fee_rate: *source.fee_rate,
name: source.name.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 64 bytes"
.to_string(),
)
})?,
web: source.web.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 256 bytes"
.to_string(),
)
})?,
img: source.img.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 256 bytes"
.to_string(),
)
})?,
cpswap_config: *source.cpswap_config,
creator_fee_rate: *source.creator_fee_rate,
transfer_fee_extension_auth: *source.transfer_fee_extension_auth,
platform_vesting_wallet: *source.platform_vesting_wallet,
platform_vesting_scale: *source.platform_vesting_scale,
platform_cp_creator: *source.platform_cp_creator,
padding: source.padding.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 108 bytes"
.to_string(),
)
})?,
curve_params: source.curve_params.0,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::platform_config::PlatformConfig {
fn table() -> &'static str {
"platform_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"epoch",
"platform_fee_wallet",
"platform_nft_wallet",
"platform_scale",
"creator_scale",
"burn_scale",
"fee_rate",
"name",
"web",
"img",
"cpswap_config",
"creator_fee_rate",
"transfer_fee_extension_auth",
"platform_vesting_wallet",
"platform_vesting_scale",
"platform_cp_creator",
"padding",
"curve_params",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PlatformConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO platform_config_account (
"epoch",
"platform_fee_wallet",
"platform_nft_wallet",
"platform_scale",
"creator_scale",
"burn_scale",
"fee_rate",
"name",
"web",
"img",
"cpswap_config",
"creator_fee_rate",
"transfer_fee_extension_auth",
"platform_vesting_wallet",
"platform_vesting_scale",
"platform_cp_creator",
"padding",
"curve_params",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
)"#)
.bind(&self.epoch)
.bind(self.platform_fee_wallet)
.bind(self.platform_nft_wallet)
.bind(&self.platform_scale)
.bind(&self.creator_scale)
.bind(&self.burn_scale)
.bind(&self.fee_rate)
.bind(&self.name)
.bind(&self.web)
.bind(&self.img)
.bind(self.cpswap_config)
.bind(&self.creator_fee_rate)
.bind(self.transfer_fee_extension_auth)
.bind(self.platform_vesting_wallet)
.bind(&self.platform_vesting_scale)
.bind(self.platform_cp_creator)
.bind(&self.padding)
.bind(&self.curve_params)
.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 PlatformConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO platform_config_account (
"epoch",
"platform_fee_wallet",
"platform_nft_wallet",
"platform_scale",
"creator_scale",
"burn_scale",
"fee_rate",
"name",
"web",
"img",
"cpswap_config",
"creator_fee_rate",
"transfer_fee_extension_auth",
"platform_vesting_wallet",
"platform_vesting_scale",
"platform_cp_creator",
"padding",
"curve_params",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"epoch" = EXCLUDED."epoch",
"platform_fee_wallet" = EXCLUDED."platform_fee_wallet",
"platform_nft_wallet" = EXCLUDED."platform_nft_wallet",
"platform_scale" = EXCLUDED."platform_scale",
"creator_scale" = EXCLUDED."creator_scale",
"burn_scale" = EXCLUDED."burn_scale",
"fee_rate" = EXCLUDED."fee_rate",
"name" = EXCLUDED."name",
"web" = EXCLUDED."web",
"img" = EXCLUDED."img",
"cpswap_config" = EXCLUDED."cpswap_config",
"creator_fee_rate" = EXCLUDED."creator_fee_rate",
"transfer_fee_extension_auth" = EXCLUDED."transfer_fee_extension_auth",
"platform_vesting_wallet" = EXCLUDED."platform_vesting_wallet",
"platform_vesting_scale" = EXCLUDED."platform_vesting_scale",
"platform_cp_creator" = EXCLUDED."platform_cp_creator",
"padding" = EXCLUDED."padding",
"curve_params" = EXCLUDED."curve_params",
__slot = EXCLUDED.__slot
"#)
.bind(&self.epoch)
.bind(self.platform_fee_wallet)
.bind(self.platform_nft_wallet)
.bind(&self.platform_scale)
.bind(&self.creator_scale)
.bind(&self.burn_scale)
.bind(&self.fee_rate)
.bind(&self.name)
.bind(&self.web)
.bind(&self.img)
.bind(self.cpswap_config)
.bind(&self.creator_fee_rate)
.bind(self.transfer_fee_extension_auth)
.bind(self.platform_vesting_wallet)
.bind(&self.platform_vesting_scale)
.bind(self.platform_cp_creator)
.bind(&self.padding)
.bind(&self.curve_params)
.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 PlatformConfigRow {
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 platform_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 PlatformConfigRow {
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 platform_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 PlatformConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PlatformConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS platform_config_account (
-- Account data
"epoch" NUMERIC(20) NOT NULL,
"platform_fee_wallet" BYTEA NOT NULL,
"platform_nft_wallet" BYTEA NOT NULL,
"platform_scale" NUMERIC(20) NOT NULL,
"creator_scale" NUMERIC(20) NOT NULL,
"burn_scale" NUMERIC(20) NOT NULL,
"fee_rate" NUMERIC(20) NOT NULL,
"name" BYTEA NOT NULL,
"web" BYTEA NOT NULL,
"img" BYTEA NOT NULL,
"cpswap_config" BYTEA NOT NULL,
"creator_fee_rate" NUMERIC(20) NOT NULL,
"transfer_fee_extension_auth" BYTEA NOT NULL,
"platform_vesting_wallet" BYTEA NOT NULL,
"platform_vesting_scale" NUMERIC(20) NOT NULL,
"platform_cp_creator" BYTEA NOT NULL,
"padding" BYTEA NOT NULL,
"curve_params" 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 platform_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}