use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct GlobalConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub admin: Pubkey,
pub lp_fee_basis_points: U64,
pub protocol_fee_basis_points: U64,
pub disable_flags: U8,
pub protocol_fee_recipients: Vec<Pubkey>,
pub coin_creator_fee_basis_points: U64,
pub admin_set_coin_creator_authority: Pubkey,
pub whitelist_pda: Pubkey,
pub reserved_fee_recipient: Pubkey,
pub mayhem_mode_enabled: bool,
pub reserved_fee_recipients: Vec<Pubkey>,
pub is_cashback_enabled: bool,
}
impl GlobalConfigRow {
pub fn from_parts(
source: crate::accounts::global_config::GlobalConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
admin: source.admin.into(),
lp_fee_basis_points: source.lp_fee_basis_points.into(),
protocol_fee_basis_points: source.protocol_fee_basis_points.into(),
disable_flags: source.disable_flags.into(),
protocol_fee_recipients: source
.protocol_fee_recipients
.into_iter()
.map(|element| element.into())
.collect(),
coin_creator_fee_basis_points: source.coin_creator_fee_basis_points.into(),
admin_set_coin_creator_authority: source.admin_set_coin_creator_authority.into(),
whitelist_pda: source.whitelist_pda.into(),
reserved_fee_recipient: source.reserved_fee_recipient.into(),
mayhem_mode_enabled: source.mayhem_mode_enabled,
reserved_fee_recipients: source
.reserved_fee_recipients
.into_iter()
.map(|element| element.into())
.collect(),
is_cashback_enabled: source.is_cashback_enabled,
}
}
}
impl TryFrom<GlobalConfigRow> for crate::accounts::global_config::GlobalConfig {
type Error = carbon_core::error::Error;
fn try_from(source: GlobalConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
admin: *source.admin,
lp_fee_basis_points: *source.lp_fee_basis_points,
protocol_fee_basis_points: *source.protocol_fee_basis_points,
disable_flags: source.disable_flags.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
protocol_fee_recipients: source
.protocol_fee_recipients
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
coin_creator_fee_basis_points: *source.coin_creator_fee_basis_points,
admin_set_coin_creator_authority: *source.admin_set_coin_creator_authority,
whitelist_pda: *source.whitelist_pda,
reserved_fee_recipient: *source.reserved_fee_recipient,
mayhem_mode_enabled: source.mayhem_mode_enabled,
reserved_fee_recipients: source
.reserved_fee_recipients
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
is_cashback_enabled: source.is_cashback_enabled,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::global_config::GlobalConfig {
fn table() -> &'static str {
"global_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"admin",
"lp_fee_basis_points",
"protocol_fee_basis_points",
"disable_flags",
"protocol_fee_recipients",
"coin_creator_fee_basis_points",
"admin_set_coin_creator_authority",
"whitelist_pda",
"reserved_fee_recipient",
"mayhem_mode_enabled",
"reserved_fee_recipients",
"is_cashback_enabled",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for GlobalConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO global_config_account (
"admin",
"lp_fee_basis_points",
"protocol_fee_basis_points",
"disable_flags",
"protocol_fee_recipients",
"coin_creator_fee_basis_points",
"admin_set_coin_creator_authority",
"whitelist_pda",
"reserved_fee_recipient",
"mayhem_mode_enabled",
"reserved_fee_recipients",
"is_cashback_enabled",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)"#,
)
.bind(self.admin)
.bind(&self.lp_fee_basis_points)
.bind(&self.protocol_fee_basis_points)
.bind(self.disable_flags)
.bind(&self.protocol_fee_recipients)
.bind(&self.coin_creator_fee_basis_points)
.bind(self.admin_set_coin_creator_authority)
.bind(self.whitelist_pda)
.bind(self.reserved_fee_recipient)
.bind(self.mayhem_mode_enabled)
.bind(&self.reserved_fee_recipients)
.bind(self.is_cashback_enabled)
.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 GlobalConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO global_config_account (
"admin",
"lp_fee_basis_points",
"protocol_fee_basis_points",
"disable_flags",
"protocol_fee_recipients",
"coin_creator_fee_basis_points",
"admin_set_coin_creator_authority",
"whitelist_pda",
"reserved_fee_recipient",
"mayhem_mode_enabled",
"reserved_fee_recipients",
"is_cashback_enabled",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"admin" = EXCLUDED."admin",
"lp_fee_basis_points" = EXCLUDED."lp_fee_basis_points",
"protocol_fee_basis_points" = EXCLUDED."protocol_fee_basis_points",
"disable_flags" = EXCLUDED."disable_flags",
"protocol_fee_recipients" = EXCLUDED."protocol_fee_recipients",
"coin_creator_fee_basis_points" = EXCLUDED."coin_creator_fee_basis_points",
"admin_set_coin_creator_authority" = EXCLUDED."admin_set_coin_creator_authority",
"whitelist_pda" = EXCLUDED."whitelist_pda",
"reserved_fee_recipient" = EXCLUDED."reserved_fee_recipient",
"mayhem_mode_enabled" = EXCLUDED."mayhem_mode_enabled",
"reserved_fee_recipients" = EXCLUDED."reserved_fee_recipients",
"is_cashback_enabled" = EXCLUDED."is_cashback_enabled",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.admin)
.bind(&self.lp_fee_basis_points)
.bind(&self.protocol_fee_basis_points)
.bind(self.disable_flags)
.bind(&self.protocol_fee_recipients)
.bind(&self.coin_creator_fee_basis_points)
.bind(self.admin_set_coin_creator_authority)
.bind(self.whitelist_pda)
.bind(self.reserved_fee_recipient)
.bind(self.mayhem_mode_enabled)
.bind(&self.reserved_fee_recipients)
.bind(self.is_cashback_enabled)
.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 GlobalConfigRow {
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 global_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 GlobalConfigRow {
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 global_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 GlobalConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for GlobalConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS global_config_account (
-- Account data
"admin" BYTEA NOT NULL,
"lp_fee_basis_points" NUMERIC(20) NOT NULL,
"protocol_fee_basis_points" NUMERIC(20) NOT NULL,
"disable_flags" INT2 NOT NULL,
"protocol_fee_recipients" BYTEA[] NOT NULL,
"coin_creator_fee_basis_points" NUMERIC(20) NOT NULL,
"admin_set_coin_creator_authority" BYTEA NOT NULL,
"whitelist_pda" BYTEA NOT NULL,
"reserved_fee_recipient" BYTEA NOT NULL,
"mayhem_mode_enabled" BOOLEAN NOT NULL,
"reserved_fee_recipients" BYTEA[] NOT NULL,
"is_cashback_enabled" BOOLEAN 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 global_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}