use {
crate::types::{LiquidityPoolMarketCapBasedFees, LiquidityPoolSlotOffsetBasedFees},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{U16, U32, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ProtocolConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub create_pool_fee: U64,
pub initial_token_b_amount: f64,
pub initial_token_a_amount: U64,
pub unstaked_wsol_reserve: U64,
pub total_sol_spent: U64,
pub total_msol_received: U64,
pub total_realized_profit: U64,
pub pool_count: U64,
pub max_supply_per_wallet: U64,
pub creator_trading_fee_trading_volume_threshold: f64,
pub market_cap_based_fees: sqlx::types::Json<LiquidityPoolMarketCapBasedFees>,
pub buffer_bps: U16,
pub auto_staking_threshold_bps: U16,
pub version: U16,
pub protocol_config_state_bump: U8,
pub allow_create_pool: U8,
pub supported_pool_type: U8,
pub default_leader_slot_window: U8,
pub auto_staking_enabled: U8,
pub leader_slot_window: U8,
pub sandwich_resistence_enabled: U8,
pub token_a_decimals: U8,
pub migration_market_cap_threshold: U16,
pub pad: Vec<u8>,
pub max_creator_trading_fee: U32,
pub slot_offset_based_fees: sqlx::types::Json<LiquidityPoolSlotOffsetBasedFees>,
}
impl ProtocolConfigRow {
pub fn from_parts(
source: crate::accounts::protocol_config::ProtocolConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
create_pool_fee: source.create_pool_fee.into(),
initial_token_b_amount: source.initial_token_b_amount,
initial_token_a_amount: source.initial_token_a_amount.into(),
unstaked_wsol_reserve: source.unstaked_wsol_reserve.into(),
total_sol_spent: source.total_sol_spent.into(),
total_msol_received: source.total_msol_received.into(),
total_realized_profit: source.total_realized_profit.into(),
pool_count: source.pool_count.into(),
max_supply_per_wallet: source.max_supply_per_wallet.into(),
creator_trading_fee_trading_volume_threshold: source
.creator_trading_fee_trading_volume_threshold,
market_cap_based_fees: sqlx::types::Json(source.market_cap_based_fees),
buffer_bps: source.buffer_bps.into(),
auto_staking_threshold_bps: source.auto_staking_threshold_bps.into(),
version: source.version.into(),
protocol_config_state_bump: source.protocol_config_state_bump.into(),
allow_create_pool: source.allow_create_pool.into(),
supported_pool_type: source.supported_pool_type.into(),
default_leader_slot_window: source.default_leader_slot_window.into(),
auto_staking_enabled: source.auto_staking_enabled.into(),
leader_slot_window: source.leader_slot_window.into(),
sandwich_resistence_enabled: source.sandwich_resistence_enabled.into(),
token_a_decimals: source.token_a_decimals.into(),
migration_market_cap_threshold: source.migration_market_cap_threshold.into(),
pad: source.pad.to_vec(),
max_creator_trading_fee: source.max_creator_trading_fee.into(),
slot_offset_based_fees: sqlx::types::Json(source.slot_offset_based_fees),
}
}
}
impl TryFrom<ProtocolConfigRow> for crate::accounts::protocol_config::ProtocolConfig {
type Error = carbon_core::error::Error;
fn try_from(source: ProtocolConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
create_pool_fee: *source.create_pool_fee,
initial_token_b_amount: source.initial_token_b_amount,
initial_token_a_amount: *source.initial_token_a_amount,
unstaked_wsol_reserve: *source.unstaked_wsol_reserve,
total_sol_spent: *source.total_sol_spent,
total_msol_received: *source.total_msol_received,
total_realized_profit: *source.total_realized_profit,
pool_count: *source.pool_count,
max_supply_per_wallet: *source.max_supply_per_wallet,
creator_trading_fee_trading_volume_threshold: source
.creator_trading_fee_trading_volume_threshold,
market_cap_based_fees: source.market_cap_based_fees.0,
buffer_bps: source.buffer_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
auto_staking_threshold_bps: source.auto_staking_threshold_bps.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
version: source.version.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
protocol_config_state_bump: source.protocol_config_state_bump.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
allow_create_pool: source.allow_create_pool.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
supported_pool_type: source.supported_pool_type.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
default_leader_slot_window: source.default_leader_slot_window.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
auto_staking_enabled: source.auto_staking_enabled.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
leader_slot_window: source.leader_slot_window.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
sandwich_resistence_enabled: source.sandwich_resistence_enabled.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
token_a_decimals: source.token_a_decimals.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
migration_market_cap_threshold: source
.migration_market_cap_threshold
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
pad: source.pad.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 8 bytes"
.to_string(),
)
})?,
max_creator_trading_fee: source.max_creator_trading_fee.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
slot_offset_based_fees: source.slot_offset_based_fees.0,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::protocol_config::ProtocolConfig {
fn table() -> &'static str {
"protocol_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"create_pool_fee",
"initial_token_b_amount",
"initial_token_a_amount",
"unstaked_wsol_reserve",
"total_sol_spent",
"total_msol_received",
"total_realized_profit",
"pool_count",
"max_supply_per_wallet",
"creator_trading_fee_trading_volume_threshold",
"market_cap_based_fees",
"buffer_bps",
"auto_staking_threshold_bps",
"version",
"protocol_config_state_bump",
"allow_create_pool",
"supported_pool_type",
"default_leader_slot_window",
"auto_staking_enabled",
"leader_slot_window",
"sandwich_resistence_enabled",
"token_a_decimals",
"migration_market_cap_threshold",
"pad",
"max_creator_trading_fee",
"slot_offset_based_fees",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ProtocolConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO protocol_config_account (
"create_pool_fee",
"initial_token_b_amount",
"initial_token_a_amount",
"unstaked_wsol_reserve",
"total_sol_spent",
"total_msol_received",
"total_realized_profit",
"pool_count",
"max_supply_per_wallet",
"creator_trading_fee_trading_volume_threshold",
"market_cap_based_fees",
"buffer_bps",
"auto_staking_threshold_bps",
"version",
"protocol_config_state_bump",
"allow_create_pool",
"supported_pool_type",
"default_leader_slot_window",
"auto_staking_enabled",
"leader_slot_window",
"sandwich_resistence_enabled",
"token_a_decimals",
"migration_market_cap_threshold",
"pad",
"max_creator_trading_fee",
"slot_offset_based_fees",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28
)"#)
.bind(&self.create_pool_fee)
.bind(self.initial_token_b_amount)
.bind(&self.initial_token_a_amount)
.bind(&self.unstaked_wsol_reserve)
.bind(&self.total_sol_spent)
.bind(&self.total_msol_received)
.bind(&self.total_realized_profit)
.bind(&self.pool_count)
.bind(&self.max_supply_per_wallet)
.bind(self.creator_trading_fee_trading_volume_threshold)
.bind(&self.market_cap_based_fees)
.bind(self.buffer_bps)
.bind(self.auto_staking_threshold_bps)
.bind(self.version)
.bind(self.protocol_config_state_bump)
.bind(self.allow_create_pool)
.bind(self.supported_pool_type)
.bind(self.default_leader_slot_window)
.bind(self.auto_staking_enabled)
.bind(self.leader_slot_window)
.bind(self.sandwich_resistence_enabled)
.bind(self.token_a_decimals)
.bind(self.migration_market_cap_threshold)
.bind(&self.pad)
.bind(self.max_creator_trading_fee)
.bind(&self.slot_offset_based_fees)
.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 ProtocolConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO protocol_config_account (
"create_pool_fee",
"initial_token_b_amount",
"initial_token_a_amount",
"unstaked_wsol_reserve",
"total_sol_spent",
"total_msol_received",
"total_realized_profit",
"pool_count",
"max_supply_per_wallet",
"creator_trading_fee_trading_volume_threshold",
"market_cap_based_fees",
"buffer_bps",
"auto_staking_threshold_bps",
"version",
"protocol_config_state_bump",
"allow_create_pool",
"supported_pool_type",
"default_leader_slot_window",
"auto_staking_enabled",
"leader_slot_window",
"sandwich_resistence_enabled",
"token_a_decimals",
"migration_market_cap_threshold",
"pad",
"max_creator_trading_fee",
"slot_offset_based_fees",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"create_pool_fee" = EXCLUDED."create_pool_fee",
"initial_token_b_amount" = EXCLUDED."initial_token_b_amount",
"initial_token_a_amount" = EXCLUDED."initial_token_a_amount",
"unstaked_wsol_reserve" = EXCLUDED."unstaked_wsol_reserve",
"total_sol_spent" = EXCLUDED."total_sol_spent",
"total_msol_received" = EXCLUDED."total_msol_received",
"total_realized_profit" = EXCLUDED."total_realized_profit",
"pool_count" = EXCLUDED."pool_count",
"max_supply_per_wallet" = EXCLUDED."max_supply_per_wallet",
"creator_trading_fee_trading_volume_threshold" = EXCLUDED."creator_trading_fee_trading_volume_threshold",
"market_cap_based_fees" = EXCLUDED."market_cap_based_fees",
"buffer_bps" = EXCLUDED."buffer_bps",
"auto_staking_threshold_bps" = EXCLUDED."auto_staking_threshold_bps",
"version" = EXCLUDED."version",
"protocol_config_state_bump" = EXCLUDED."protocol_config_state_bump",
"allow_create_pool" = EXCLUDED."allow_create_pool",
"supported_pool_type" = EXCLUDED."supported_pool_type",
"default_leader_slot_window" = EXCLUDED."default_leader_slot_window",
"auto_staking_enabled" = EXCLUDED."auto_staking_enabled",
"leader_slot_window" = EXCLUDED."leader_slot_window",
"sandwich_resistence_enabled" = EXCLUDED."sandwich_resistence_enabled",
"token_a_decimals" = EXCLUDED."token_a_decimals",
"migration_market_cap_threshold" = EXCLUDED."migration_market_cap_threshold",
"pad" = EXCLUDED."pad",
"max_creator_trading_fee" = EXCLUDED."max_creator_trading_fee",
"slot_offset_based_fees" = EXCLUDED."slot_offset_based_fees",
__slot = EXCLUDED.__slot
"#)
.bind(&self.create_pool_fee)
.bind(self.initial_token_b_amount)
.bind(&self.initial_token_a_amount)
.bind(&self.unstaked_wsol_reserve)
.bind(&self.total_sol_spent)
.bind(&self.total_msol_received)
.bind(&self.total_realized_profit)
.bind(&self.pool_count)
.bind(&self.max_supply_per_wallet)
.bind(self.creator_trading_fee_trading_volume_threshold)
.bind(&self.market_cap_based_fees)
.bind(self.buffer_bps)
.bind(self.auto_staking_threshold_bps)
.bind(self.version)
.bind(self.protocol_config_state_bump)
.bind(self.allow_create_pool)
.bind(self.supported_pool_type)
.bind(self.default_leader_slot_window)
.bind(self.auto_staking_enabled)
.bind(self.leader_slot_window)
.bind(self.sandwich_resistence_enabled)
.bind(self.token_a_decimals)
.bind(self.migration_market_cap_threshold)
.bind(&self.pad)
.bind(self.max_creator_trading_fee)
.bind(&self.slot_offset_based_fees)
.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 ProtocolConfigRow {
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 protocol_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 ProtocolConfigRow {
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 protocol_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 ProtocolConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ProtocolConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS protocol_config_account (
-- Account data
"create_pool_fee" NUMERIC(20) NOT NULL,
"initial_token_b_amount" DOUBLE PRECISION NOT NULL,
"initial_token_a_amount" NUMERIC(20) NOT NULL,
"unstaked_wsol_reserve" NUMERIC(20) NOT NULL,
"total_sol_spent" NUMERIC(20) NOT NULL,
"total_msol_received" NUMERIC(20) NOT NULL,
"total_realized_profit" NUMERIC(20) NOT NULL,
"pool_count" NUMERIC(20) NOT NULL,
"max_supply_per_wallet" NUMERIC(20) NOT NULL,
"creator_trading_fee_trading_volume_threshold" DOUBLE PRECISION NOT NULL,
"market_cap_based_fees" JSONB NOT NULL,
"buffer_bps" INT4 NOT NULL,
"auto_staking_threshold_bps" INT4 NOT NULL,
"version" INT4 NOT NULL,
"protocol_config_state_bump" INT2 NOT NULL,
"allow_create_pool" INT2 NOT NULL,
"supported_pool_type" INT2 NOT NULL,
"default_leader_slot_window" INT2 NOT NULL,
"auto_staking_enabled" INT2 NOT NULL,
"leader_slot_window" INT2 NOT NULL,
"sandwich_resistence_enabled" INT2 NOT NULL,
"token_a_decimals" INT2 NOT NULL,
"migration_market_cap_threshold" INT4 NOT NULL,
"pad" BYTEA NOT NULL,
"max_creator_trading_fee" INT8 NOT NULL,
"slot_offset_based_fees" 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 protocol_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}