use carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{U16, U32},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SetPresetAdaptiveFeeConstantsRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
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,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl SetPresetAdaptiveFeeConstantsRow {
pub fn from_parts(
source: crate::instructions::set_preset_adaptive_fee_constants::SetPresetAdaptiveFeeConstants,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.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(),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<SetPresetAdaptiveFeeConstantsRow>
for crate::instructions::set_preset_adaptive_fee_constants::SetPresetAdaptiveFeeConstants
{
type Error = carbon_core::error::Error;
fn try_from(source: SetPresetAdaptiveFeeConstantsRow) -> Result<Self, Self::Error> {
Ok(Self {
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::instructions::set_preset_adaptive_fee_constants::SetPresetAdaptiveFeeConstants
{
fn table() -> &'static str {
"set_preset_adaptive_fee_constants_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"filter_period",
"decay_period",
"reduction_factor",
"adaptive_fee_control_factor",
"max_volatility_accumulator",
"tick_group_size",
"major_swap_threshold_ticks",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SetPresetAdaptiveFeeConstantsRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO set_preset_adaptive_fee_constants_instruction (
"filter_period",
"decay_period",
"reduction_factor",
"adaptive_fee_control_factor",
"max_volatility_accumulator",
"tick_group_size",
"major_swap_threshold_ticks",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
)"#,
)
.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.instruction_metadata.signature)
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(&self.instruction_metadata.slot)
.bind(&self.accounts)
.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 SetPresetAdaptiveFeeConstantsRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO set_preset_adaptive_fee_constants_instruction (
"filter_period",
"decay_period",
"reduction_factor",
"adaptive_fee_control_factor",
"max_volatility_accumulator",
"tick_group_size",
"major_swap_threshold_ticks",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"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",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.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.instruction_metadata.signature)
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(&self.instruction_metadata.slot)
.bind(&self.accounts)
.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 SetPresetAdaptiveFeeConstantsRow {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM set_preset_adaptive_fee_constants_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.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 SetPresetAdaptiveFeeConstantsRow {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
async fn lookup(
key: Self::Key,
pool: &sqlx::PgPool,
) -> carbon_core::error::CarbonResult<Option<Self>> {
let row = sqlx::query_as(
r#"SELECT * FROM set_preset_adaptive_fee_constants_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct SetPresetAdaptiveFeeConstantsMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SetPresetAdaptiveFeeConstantsMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS set_preset_adaptive_fee_constants_instruction (
-- Instruction data
"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,
-- Instruction metadata
__signature TEXT NOT NULL,
__instruction_index BIGINT NOT NULL,
__stack_height BIGINT NOT NULL,
__slot NUMERIC(20),
__accounts JSONB NOT NULL,
PRIMARY KEY (__signature, __instruction_index, __stack_height)
)"#,
)
.execute(connection)
.await?;
Ok(())
}
async fn down(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(r#"DROP TABLE IF EXISTS set_preset_adaptive_fee_constants_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}