use carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{Pubkey, U16, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct UpdateConfigRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub new_protocol_fee_recipient: Pubkey,
pub new_virtual_sol_reserves: U64,
pub new_virtual_token_reserves: U64,
pub new_graduation_target: U64,
pub new_graduation_fee: U64,
pub new_damping_term: U8,
pub new_swap_fee_basis_points: U8,
pub new_token_for_stakers_basis_points: U16,
pub new_token_amount_for_raydium_liquidity: U64,
pub new_max_graduation_price_deviation_basis_points: U16,
pub new_max_swap_amount_for_pool_price_correction_basis_points: U16,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl UpdateConfigRow {
pub fn from_parts(
source: crate::instructions::update_config::UpdateConfig,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
new_protocol_fee_recipient: source.new_protocol_fee_recipient.into(),
new_virtual_sol_reserves: source.new_virtual_sol_reserves.into(),
new_virtual_token_reserves: source.new_virtual_token_reserves.into(),
new_graduation_target: source.new_graduation_target.into(),
new_graduation_fee: source.new_graduation_fee.into(),
new_damping_term: source.new_damping_term.into(),
new_swap_fee_basis_points: source.new_swap_fee_basis_points.into(),
new_token_for_stakers_basis_points: source.new_token_for_stakers_basis_points.into(),
new_token_amount_for_raydium_liquidity: source
.new_token_amount_for_raydium_liquidity
.into(),
new_max_graduation_price_deviation_basis_points: source
.new_max_graduation_price_deviation_basis_points
.into(),
new_max_swap_amount_for_pool_price_correction_basis_points: source
.new_max_swap_amount_for_pool_price_correction_basis_points
.into(),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<UpdateConfigRow> for crate::instructions::update_config::UpdateConfig {
type Error = carbon_core::error::Error;
fn try_from(source: UpdateConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
new_protocol_fee_recipient: *source.new_protocol_fee_recipient,
new_virtual_sol_reserves: *source.new_virtual_sol_reserves,
new_virtual_token_reserves: *source.new_virtual_token_reserves,
new_graduation_target: *source.new_graduation_target,
new_graduation_fee: *source.new_graduation_fee,
new_damping_term: source.new_damping_term.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
new_swap_fee_basis_points: source.new_swap_fee_basis_points.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
new_token_for_stakers_basis_points: source
.new_token_for_stakers_basis_points
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
new_token_amount_for_raydium_liquidity: *source.new_token_amount_for_raydium_liquidity,
new_max_graduation_price_deviation_basis_points: source
.new_max_graduation_price_deviation_basis_points
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
new_max_swap_amount_for_pool_price_correction_basis_points: source
.new_max_swap_amount_for_pool_price_correction_basis_points
.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::update_config::UpdateConfig {
fn table() -> &'static str {
"update_config_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"new_protocol_fee_recipient",
"new_virtual_sol_reserves",
"new_virtual_token_reserves",
"new_graduation_target",
"new_graduation_fee",
"new_damping_term",
"new_swap_fee_basis_points",
"new_token_for_stakers_basis_points",
"new_token_amount_for_raydium_liquidity",
"new_max_graduation_price_deviation_basis_points",
"new_max_swap_amount_for_pool_price_correction_basis_points",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for UpdateConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO update_config_instruction (
"new_protocol_fee_recipient",
"new_virtual_sol_reserves",
"new_virtual_token_reserves",
"new_graduation_target",
"new_graduation_fee",
"new_damping_term",
"new_swap_fee_basis_points",
"new_token_for_stakers_basis_points",
"new_token_amount_for_raydium_liquidity",
"new_max_graduation_price_deviation_basis_points",
"new_max_swap_amount_for_pool_price_correction_basis_points",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16
)"#,
)
.bind(self.new_protocol_fee_recipient)
.bind(&self.new_virtual_sol_reserves)
.bind(&self.new_virtual_token_reserves)
.bind(&self.new_graduation_target)
.bind(&self.new_graduation_fee)
.bind(self.new_damping_term)
.bind(self.new_swap_fee_basis_points)
.bind(self.new_token_for_stakers_basis_points)
.bind(&self.new_token_amount_for_raydium_liquidity)
.bind(self.new_max_graduation_price_deviation_basis_points)
.bind(self.new_max_swap_amount_for_pool_price_correction_basis_points)
.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 UpdateConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO update_config_instruction (
"new_protocol_fee_recipient",
"new_virtual_sol_reserves",
"new_virtual_token_reserves",
"new_graduation_target",
"new_graduation_fee",
"new_damping_term",
"new_swap_fee_basis_points",
"new_token_for_stakers_basis_points",
"new_token_amount_for_raydium_liquidity",
"new_max_graduation_price_deviation_basis_points",
"new_max_swap_amount_for_pool_price_correction_basis_points",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"new_protocol_fee_recipient" = EXCLUDED."new_protocol_fee_recipient",
"new_virtual_sol_reserves" = EXCLUDED."new_virtual_sol_reserves",
"new_virtual_token_reserves" = EXCLUDED."new_virtual_token_reserves",
"new_graduation_target" = EXCLUDED."new_graduation_target",
"new_graduation_fee" = EXCLUDED."new_graduation_fee",
"new_damping_term" = EXCLUDED."new_damping_term",
"new_swap_fee_basis_points" = EXCLUDED."new_swap_fee_basis_points",
"new_token_for_stakers_basis_points" = EXCLUDED."new_token_for_stakers_basis_points",
"new_token_amount_for_raydium_liquidity" = EXCLUDED."new_token_amount_for_raydium_liquidity",
"new_max_graduation_price_deviation_basis_points" = EXCLUDED."new_max_graduation_price_deviation_basis_points",
"new_max_swap_amount_for_pool_price_correction_basis_points" = EXCLUDED."new_max_swap_amount_for_pool_price_correction_basis_points",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#)
.bind(self.new_protocol_fee_recipient)
.bind(&self.new_virtual_sol_reserves)
.bind(&self.new_virtual_token_reserves)
.bind(&self.new_graduation_target)
.bind(&self.new_graduation_fee)
.bind(self.new_damping_term)
.bind(self.new_swap_fee_basis_points)
.bind(self.new_token_for_stakers_basis_points)
.bind(&self.new_token_amount_for_raydium_liquidity)
.bind(self.new_max_graduation_price_deviation_basis_points)
.bind(self.new_max_swap_amount_for_pool_price_correction_basis_points)
.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 UpdateConfigRow {
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 update_config_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 UpdateConfigRow {
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 update_config_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 UpdateConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for UpdateConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS update_config_instruction (
-- Instruction data
"new_protocol_fee_recipient" BYTEA NOT NULL,
"new_virtual_sol_reserves" NUMERIC(20) NOT NULL,
"new_virtual_token_reserves" NUMERIC(20) NOT NULL,
"new_graduation_target" NUMERIC(20) NOT NULL,
"new_graduation_fee" NUMERIC(20) NOT NULL,
"new_damping_term" INT2 NOT NULL,
"new_swap_fee_basis_points" INT2 NOT NULL,
"new_token_for_stakers_basis_points" INT4 NOT NULL,
"new_token_amount_for_raydium_liquidity" NUMERIC(20) NOT NULL,
"new_max_graduation_price_deviation_basis_points" INT4 NOT NULL,
"new_max_swap_amount_for_pool_price_correction_basis_points" 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 update_config_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}