use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U32},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct AdaptiveFeeTierRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub whirlpools_config: Pubkey,
pub fee_tier_index: U16,
pub tick_spacing: U16,
pub initialize_pool_authority: Pubkey,
pub delegated_fee_authority: Pubkey,
pub default_base_fee_rate: U16,
pub filter_period: U16,
pub decay_period: U16,
pub reduction_factor: U16,
pub adaptive_fee_control_factor: U32,
pub max_volatility_accumulator: U32,
pub tick_group_size: U16,
pub major_swap_threshold_ticks: U16,
}
impl AdaptiveFeeTierRow {
pub fn from_parts(
source: crate::accounts::adaptive_fee_tier::AdaptiveFeeTier,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
whirlpools_config: source.whirlpools_config.into(),
fee_tier_index: source.fee_tier_index.into(),
tick_spacing: source.tick_spacing.into(),
initialize_pool_authority: source.initialize_pool_authority.into(),
delegated_fee_authority: source.delegated_fee_authority.into(),
default_base_fee_rate: source.default_base_fee_rate.into(),
filter_period: source.filter_period.into(),
decay_period: source.decay_period.into(),
reduction_factor: source.reduction_factor.into(),
adaptive_fee_control_factor: source.adaptive_fee_control_factor.into(),
max_volatility_accumulator: source.max_volatility_accumulator.into(),
tick_group_size: source.tick_group_size.into(),
major_swap_threshold_ticks: source.major_swap_threshold_ticks.into(),
}
}
}
impl TryFrom<AdaptiveFeeTierRow> for crate::accounts::adaptive_fee_tier::AdaptiveFeeTier {
type Error = carbon_core::error::Error;
fn try_from(source: AdaptiveFeeTierRow) -> Result<Self, Self::Error> {
Ok(Self {
whirlpools_config: *source.whirlpools_config,
fee_tier_index: source.fee_tier_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
tick_spacing: source.tick_spacing.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
initialize_pool_authority: *source.initialize_pool_authority,
delegated_fee_authority: *source.delegated_fee_authority,
default_base_fee_rate: source.default_base_fee_rate.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
filter_period: source.filter_period.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
decay_period: source.decay_period.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
reduction_factor: source.reduction_factor.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
adaptive_fee_control_factor: source.adaptive_fee_control_factor.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
max_volatility_accumulator: source.max_volatility_accumulator.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
tick_group_size: source.tick_group_size.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
major_swap_threshold_ticks: source.major_swap_threshold_ticks.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::adaptive_fee_tier::AdaptiveFeeTier
{
fn table() -> &'static str {
"adaptive_fee_tier_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"whirlpools_config",
"fee_tier_index",
"tick_spacing",
"initialize_pool_authority",
"delegated_fee_authority",
"default_base_fee_rate",
"filter_period",
"decay_period",
"reduction_factor",
"adaptive_fee_control_factor",
"max_volatility_accumulator",
"tick_group_size",
"major_swap_threshold_ticks",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for AdaptiveFeeTierRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO adaptive_fee_tier_account (
"whirlpools_config",
"fee_tier_index",
"tick_spacing",
"initialize_pool_authority",
"delegated_fee_authority",
"default_base_fee_rate",
"filter_period",
"decay_period",
"reduction_factor",
"adaptive_fee_control_factor",
"max_volatility_accumulator",
"tick_group_size",
"major_swap_threshold_ticks",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
)"#,
)
.bind(self.whirlpools_config)
.bind(self.fee_tier_index)
.bind(self.tick_spacing)
.bind(self.initialize_pool_authority)
.bind(self.delegated_fee_authority)
.bind(self.default_base_fee_rate)
.bind(self.filter_period)
.bind(self.decay_period)
.bind(self.reduction_factor)
.bind(self.adaptive_fee_control_factor)
.bind(self.max_volatility_accumulator)
.bind(self.tick_group_size)
.bind(self.major_swap_threshold_ticks)
.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 AdaptiveFeeTierRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO adaptive_fee_tier_account (
"whirlpools_config",
"fee_tier_index",
"tick_spacing",
"initialize_pool_authority",
"delegated_fee_authority",
"default_base_fee_rate",
"filter_period",
"decay_period",
"reduction_factor",
"adaptive_fee_control_factor",
"max_volatility_accumulator",
"tick_group_size",
"major_swap_threshold_ticks",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"whirlpools_config" = EXCLUDED."whirlpools_config",
"fee_tier_index" = EXCLUDED."fee_tier_index",
"tick_spacing" = EXCLUDED."tick_spacing",
"initialize_pool_authority" = EXCLUDED."initialize_pool_authority",
"delegated_fee_authority" = EXCLUDED."delegated_fee_authority",
"default_base_fee_rate" = EXCLUDED."default_base_fee_rate",
"filter_period" = EXCLUDED."filter_period",
"decay_period" = EXCLUDED."decay_period",
"reduction_factor" = EXCLUDED."reduction_factor",
"adaptive_fee_control_factor" = EXCLUDED."adaptive_fee_control_factor",
"max_volatility_accumulator" = EXCLUDED."max_volatility_accumulator",
"tick_group_size" = EXCLUDED."tick_group_size",
"major_swap_threshold_ticks" = EXCLUDED."major_swap_threshold_ticks",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.whirlpools_config)
.bind(self.fee_tier_index)
.bind(self.tick_spacing)
.bind(self.initialize_pool_authority)
.bind(self.delegated_fee_authority)
.bind(self.default_base_fee_rate)
.bind(self.filter_period)
.bind(self.decay_period)
.bind(self.reduction_factor)
.bind(self.adaptive_fee_control_factor)
.bind(self.max_volatility_accumulator)
.bind(self.tick_group_size)
.bind(self.major_swap_threshold_ticks)
.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 AdaptiveFeeTierRow {
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 adaptive_fee_tier_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 AdaptiveFeeTierRow {
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 adaptive_fee_tier_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 AdaptiveFeeTierMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for AdaptiveFeeTierMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS adaptive_fee_tier_account (
-- Account data
"whirlpools_config" BYTEA NOT NULL,
"fee_tier_index" INT4 NOT NULL,
"tick_spacing" INT4 NOT NULL,
"initialize_pool_authority" BYTEA NOT NULL,
"delegated_fee_authority" BYTEA NOT NULL,
"default_base_fee_rate" INT4 NOT NULL,
"filter_period" INT4 NOT NULL,
"decay_period" INT4 NOT NULL,
"reduction_factor" INT4 NOT NULL,
"adaptive_fee_control_factor" INT8 NOT NULL,
"max_volatility_accumulator" INT8 NOT NULL,
"tick_group_size" INT4 NOT NULL,
"major_swap_threshold_ticks" INT4 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 adaptive_fee_tier_account"#)
.execute(connection)
.await?;
Ok(())
}
}