use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct GlobalConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub epoch: U64,
pub curve_type: U8,
pub index: U16,
pub migrate_fee: U64,
pub trade_fee_rate: U64,
pub max_share_fee_rate: U64,
pub min_base_supply: U64,
pub max_lock_rate: U64,
pub min_base_sell_rate: U64,
pub min_base_migrate_rate: U64,
pub min_quote_fund_raising: U64,
pub quote_mint: Pubkey,
pub protocol_fee_owner: Pubkey,
pub migrate_fee_owner: Pubkey,
pub migrate_to_amm_wallet: Pubkey,
pub migrate_to_cpswap_wallet: Pubkey,
pub requires_platform_auth: U8,
pub padding_alignment: Vec<u8>,
pub padding: Vec<U64>,
}
impl GlobalConfigRow {
pub fn from_parts(
source: crate::accounts::global_config::GlobalConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
epoch: source.epoch.into(),
curve_type: source.curve_type.into(),
index: source.index.into(),
migrate_fee: source.migrate_fee.into(),
trade_fee_rate: source.trade_fee_rate.into(),
max_share_fee_rate: source.max_share_fee_rate.into(),
min_base_supply: source.min_base_supply.into(),
max_lock_rate: source.max_lock_rate.into(),
min_base_sell_rate: source.min_base_sell_rate.into(),
min_base_migrate_rate: source.min_base_migrate_rate.into(),
min_quote_fund_raising: source.min_quote_fund_raising.into(),
quote_mint: source.quote_mint.into(),
protocol_fee_owner: source.protocol_fee_owner.into(),
migrate_fee_owner: source.migrate_fee_owner.into(),
migrate_to_amm_wallet: source.migrate_to_amm_wallet.into(),
migrate_to_cpswap_wallet: source.migrate_to_cpswap_wallet.into(),
requires_platform_auth: source.requires_platform_auth.into(),
padding_alignment: source.padding_alignment.to_vec(),
padding: source
.padding
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
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 {
epoch: *source.epoch,
curve_type: source.curve_type.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
index: source.index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
migrate_fee: *source.migrate_fee,
trade_fee_rate: *source.trade_fee_rate,
max_share_fee_rate: *source.max_share_fee_rate,
min_base_supply: *source.min_base_supply,
max_lock_rate: *source.max_lock_rate,
min_base_sell_rate: *source.min_base_sell_rate,
min_base_migrate_rate: *source.min_base_migrate_rate,
min_quote_fund_raising: *source.min_quote_fund_raising,
quote_mint: *source.quote_mint,
protocol_fee_owner: *source.protocol_fee_owner,
migrate_fee_owner: *source.migrate_fee_owner,
migrate_to_amm_wallet: *source.migrate_to_amm_wallet,
migrate_to_cpswap_wallet: *source.migrate_to_cpswap_wallet,
requires_platform_auth: source.requires_platform_auth.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding_alignment: source
.padding_alignment
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 7 bytes"
.to_string(),
)
})?,
padding: source
.padding
.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(),
)
})?,
})
}
}
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",
"epoch",
"curve_type",
"index",
"migrate_fee",
"trade_fee_rate",
"max_share_fee_rate",
"min_base_supply",
"max_lock_rate",
"min_base_sell_rate",
"min_base_migrate_rate",
"min_quote_fund_raising",
"quote_mint",
"protocol_fee_owner",
"migrate_fee_owner",
"migrate_to_amm_wallet",
"migrate_to_cpswap_wallet",
"requires_platform_auth",
"padding_alignment",
"padding",
]
}
}
#[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 (
"epoch",
"curve_type",
"index",
"migrate_fee",
"trade_fee_rate",
"max_share_fee_rate",
"min_base_supply",
"max_lock_rate",
"min_base_sell_rate",
"min_base_migrate_rate",
"min_quote_fund_raising",
"quote_mint",
"protocol_fee_owner",
"migrate_fee_owner",
"migrate_to_amm_wallet",
"migrate_to_cpswap_wallet",
"requires_platform_auth",
"padding_alignment",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21
)"#)
.bind(&self.epoch)
.bind(self.curve_type)
.bind(self.index)
.bind(&self.migrate_fee)
.bind(&self.trade_fee_rate)
.bind(&self.max_share_fee_rate)
.bind(&self.min_base_supply)
.bind(&self.max_lock_rate)
.bind(&self.min_base_sell_rate)
.bind(&self.min_base_migrate_rate)
.bind(&self.min_quote_fund_raising)
.bind(self.quote_mint)
.bind(self.protocol_fee_owner)
.bind(self.migrate_fee_owner)
.bind(self.migrate_to_amm_wallet)
.bind(self.migrate_to_cpswap_wallet)
.bind(self.requires_platform_auth)
.bind(&self.padding_alignment)
.bind(&self.padding)
.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 (
"epoch",
"curve_type",
"index",
"migrate_fee",
"trade_fee_rate",
"max_share_fee_rate",
"min_base_supply",
"max_lock_rate",
"min_base_sell_rate",
"min_base_migrate_rate",
"min_quote_fund_raising",
"quote_mint",
"protocol_fee_owner",
"migrate_fee_owner",
"migrate_to_amm_wallet",
"migrate_to_cpswap_wallet",
"requires_platform_auth",
"padding_alignment",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"epoch" = EXCLUDED."epoch",
"curve_type" = EXCLUDED."curve_type",
"index" = EXCLUDED."index",
"migrate_fee" = EXCLUDED."migrate_fee",
"trade_fee_rate" = EXCLUDED."trade_fee_rate",
"max_share_fee_rate" = EXCLUDED."max_share_fee_rate",
"min_base_supply" = EXCLUDED."min_base_supply",
"max_lock_rate" = EXCLUDED."max_lock_rate",
"min_base_sell_rate" = EXCLUDED."min_base_sell_rate",
"min_base_migrate_rate" = EXCLUDED."min_base_migrate_rate",
"min_quote_fund_raising" = EXCLUDED."min_quote_fund_raising",
"quote_mint" = EXCLUDED."quote_mint",
"protocol_fee_owner" = EXCLUDED."protocol_fee_owner",
"migrate_fee_owner" = EXCLUDED."migrate_fee_owner",
"migrate_to_amm_wallet" = EXCLUDED."migrate_to_amm_wallet",
"migrate_to_cpswap_wallet" = EXCLUDED."migrate_to_cpswap_wallet",
"requires_platform_auth" = EXCLUDED."requires_platform_auth",
"padding_alignment" = EXCLUDED."padding_alignment",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#)
.bind(&self.epoch)
.bind(self.curve_type)
.bind(self.index)
.bind(&self.migrate_fee)
.bind(&self.trade_fee_rate)
.bind(&self.max_share_fee_rate)
.bind(&self.min_base_supply)
.bind(&self.max_lock_rate)
.bind(&self.min_base_sell_rate)
.bind(&self.min_base_migrate_rate)
.bind(&self.min_quote_fund_raising)
.bind(self.quote_mint)
.bind(self.protocol_fee_owner)
.bind(self.migrate_fee_owner)
.bind(self.migrate_to_amm_wallet)
.bind(self.migrate_to_cpswap_wallet)
.bind(self.requires_platform_auth)
.bind(&self.padding_alignment)
.bind(&self.padding)
.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
"epoch" NUMERIC(20) NOT NULL,
"curve_type" INT2 NOT NULL,
"index" INT4 NOT NULL,
"migrate_fee" NUMERIC(20) NOT NULL,
"trade_fee_rate" NUMERIC(20) NOT NULL,
"max_share_fee_rate" NUMERIC(20) NOT NULL,
"min_base_supply" NUMERIC(20) NOT NULL,
"max_lock_rate" NUMERIC(20) NOT NULL,
"min_base_sell_rate" NUMERIC(20) NOT NULL,
"min_base_migrate_rate" NUMERIC(20) NOT NULL,
"min_quote_fund_raising" NUMERIC(20) NOT NULL,
"quote_mint" BYTEA NOT NULL,
"protocol_fee_owner" BYTEA NOT NULL,
"migrate_fee_owner" BYTEA NOT NULL,
"migrate_to_amm_wallet" BYTEA NOT NULL,
"migrate_to_cpswap_wallet" BYTEA NOT NULL,
"requires_platform_auth" INT2 NOT NULL,
"padding_alignment" BYTEA NOT NULL,
"padding" NUMERIC(20)[] 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(())
}
}