use {
crate::types::WrappedI80F48,
carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{Pubkey, U32},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct InitGlobalFeeStateRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub admin: Pubkey,
pub fee_wallet: Pubkey,
pub bank_init_flat_sol_fee: U32,
pub liquidation_flat_sol_fee: U32,
pub order_init_flat_sol_fee: U32,
pub program_fee_fixed: sqlx::types::Json<WrappedI80F48>,
pub program_fee_rate: sqlx::types::Json<WrappedI80F48>,
pub liquidation_max_fee: sqlx::types::Json<WrappedI80F48>,
pub order_execution_max_fee: sqlx::types::Json<WrappedI80F48>,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl InitGlobalFeeStateRow {
pub fn from_parts(
source: crate::instructions::init_global_fee_state::InitGlobalFeeState,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
admin: source.admin.into(),
fee_wallet: source.fee_wallet.into(),
bank_init_flat_sol_fee: source.bank_init_flat_sol_fee.into(),
liquidation_flat_sol_fee: source.liquidation_flat_sol_fee.into(),
order_init_flat_sol_fee: source.order_init_flat_sol_fee.into(),
program_fee_fixed: sqlx::types::Json(source.program_fee_fixed),
program_fee_rate: sqlx::types::Json(source.program_fee_rate),
liquidation_max_fee: sqlx::types::Json(source.liquidation_max_fee),
order_execution_max_fee: sqlx::types::Json(source.order_execution_max_fee),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<InitGlobalFeeStateRow>
for crate::instructions::init_global_fee_state::InitGlobalFeeState
{
type Error = carbon_core::error::Error;
fn try_from(source: InitGlobalFeeStateRow) -> Result<Self, Self::Error> {
Ok(Self {
admin: *source.admin,
fee_wallet: *source.fee_wallet,
bank_init_flat_sol_fee: source.bank_init_flat_sol_fee.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
liquidation_flat_sol_fee: source.liquidation_flat_sol_fee.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
order_init_flat_sol_fee: source.order_init_flat_sol_fee.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
program_fee_fixed: source.program_fee_fixed.0,
program_fee_rate: source.program_fee_rate.0,
liquidation_max_fee: source.liquidation_max_fee.0,
order_execution_max_fee: source.order_execution_max_fee.0,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::instructions::init_global_fee_state::InitGlobalFeeState
{
fn table() -> &'static str {
"init_global_fee_state_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"admin",
"fee_wallet",
"bank_init_flat_sol_fee",
"liquidation_flat_sol_fee",
"order_init_flat_sol_fee",
"program_fee_fixed",
"program_fee_rate",
"liquidation_max_fee",
"order_execution_max_fee",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for InitGlobalFeeStateRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO init_global_fee_state_instruction (
"admin",
"fee_wallet",
"bank_init_flat_sol_fee",
"liquidation_flat_sol_fee",
"order_init_flat_sol_fee",
"program_fee_fixed",
"program_fee_rate",
"liquidation_max_fee",
"order_execution_max_fee",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)"#,
)
.bind(self.admin)
.bind(self.fee_wallet)
.bind(self.bank_init_flat_sol_fee)
.bind(self.liquidation_flat_sol_fee)
.bind(self.order_init_flat_sol_fee)
.bind(&self.program_fee_fixed)
.bind(&self.program_fee_rate)
.bind(&self.liquidation_max_fee)
.bind(&self.order_execution_max_fee)
.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 InitGlobalFeeStateRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO init_global_fee_state_instruction (
"admin",
"fee_wallet",
"bank_init_flat_sol_fee",
"liquidation_flat_sol_fee",
"order_init_flat_sol_fee",
"program_fee_fixed",
"program_fee_rate",
"liquidation_max_fee",
"order_execution_max_fee",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"admin" = EXCLUDED."admin",
"fee_wallet" = EXCLUDED."fee_wallet",
"bank_init_flat_sol_fee" = EXCLUDED."bank_init_flat_sol_fee",
"liquidation_flat_sol_fee" = EXCLUDED."liquidation_flat_sol_fee",
"order_init_flat_sol_fee" = EXCLUDED."order_init_flat_sol_fee",
"program_fee_fixed" = EXCLUDED."program_fee_fixed",
"program_fee_rate" = EXCLUDED."program_fee_rate",
"liquidation_max_fee" = EXCLUDED."liquidation_max_fee",
"order_execution_max_fee" = EXCLUDED."order_execution_max_fee",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.bind(self.admin)
.bind(self.fee_wallet)
.bind(self.bank_init_flat_sol_fee)
.bind(self.liquidation_flat_sol_fee)
.bind(self.order_init_flat_sol_fee)
.bind(&self.program_fee_fixed)
.bind(&self.program_fee_rate)
.bind(&self.liquidation_max_fee)
.bind(&self.order_execution_max_fee)
.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 InitGlobalFeeStateRow {
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 init_global_fee_state_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 InitGlobalFeeStateRow {
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 init_global_fee_state_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 InitGlobalFeeStateMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for InitGlobalFeeStateMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS init_global_fee_state_instruction (
-- Instruction data
"admin" BYTEA NOT NULL,
"fee_wallet" BYTEA NOT NULL,
"bank_init_flat_sol_fee" INT8 NOT NULL,
"liquidation_flat_sol_fee" INT8 NOT NULL,
"order_init_flat_sol_fee" INT8 NOT NULL,
"program_fee_fixed" JSONB NOT NULL,
"program_fee_rate" JSONB NOT NULL,
"liquidation_max_fee" JSONB NOT NULL,
"order_execution_max_fee" JSONB 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 init_global_fee_state_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}