use {
crate::types::{PanicState, WrappedI80F48},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U32, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct FeeStateRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub key: Pubkey,
pub global_fee_admin: Pubkey,
pub global_fee_wallet: Pubkey,
pub placeholder0: U64,
pub bank_init_flat_sol_fee: U32,
pub bump_seed: U8,
pub padding0: Vec<u8>,
pub liquidation_max_fee: sqlx::types::Json<WrappedI80F48>,
pub program_fee_fixed: sqlx::types::Json<WrappedI80F48>,
pub program_fee_rate: sqlx::types::Json<WrappedI80F48>,
pub panic_state: sqlx::types::Json<PanicState>,
pub placeholder1: U64,
pub liquidation_flat_sol_fee: U32,
pub order_init_flat_sol_fee: U32,
pub order_execution_max_fee: sqlx::types::Json<WrappedI80F48>,
pub reserved1: Vec<u8>,
}
impl FeeStateRow {
pub fn from_parts(
source: crate::accounts::fee_state::FeeState,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
key: source.key.into(),
global_fee_admin: source.global_fee_admin.into(),
global_fee_wallet: source.global_fee_wallet.into(),
placeholder0: source.placeholder0.into(),
bank_init_flat_sol_fee: source.bank_init_flat_sol_fee.into(),
bump_seed: source.bump_seed.into(),
padding0: source.padding0.to_vec(),
liquidation_max_fee: sqlx::types::Json(source.liquidation_max_fee),
program_fee_fixed: sqlx::types::Json(source.program_fee_fixed),
program_fee_rate: sqlx::types::Json(source.program_fee_rate),
panic_state: sqlx::types::Json(source.panic_state),
placeholder1: source.placeholder1.into(),
liquidation_flat_sol_fee: source.liquidation_flat_sol_fee.into(),
order_init_flat_sol_fee: source.order_init_flat_sol_fee.into(),
order_execution_max_fee: sqlx::types::Json(source.order_execution_max_fee),
reserved1: source.reserved1.to_vec(),
}
}
}
impl TryFrom<FeeStateRow> for crate::accounts::fee_state::FeeState {
type Error = carbon_core::error::Error;
fn try_from(source: FeeStateRow) -> Result<Self, Self::Error> {
Ok(Self {
key: *source.key,
global_fee_admin: *source.global_fee_admin,
global_fee_wallet: *source.global_fee_wallet,
placeholder0: *source.placeholder0,
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(),
)
})?,
bump_seed: source.bump_seed.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding0: source.padding0.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 3 bytes"
.to_string(),
)
})?,
liquidation_max_fee: source.liquidation_max_fee.0,
program_fee_fixed: source.program_fee_fixed.0,
program_fee_rate: source.program_fee_rate.0,
panic_state: source.panic_state.0,
placeholder1: *source.placeholder1,
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(),
)
})?,
order_execution_max_fee: source.order_execution_max_fee.0,
reserved1: source.reserved1.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 32 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::fee_state::FeeState {
fn table() -> &'static str {
"fee_state_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"key",
"global_fee_admin",
"global_fee_wallet",
"placeholder0",
"bank_init_flat_sol_fee",
"bump_seed",
"padding0",
"liquidation_max_fee",
"program_fee_fixed",
"program_fee_rate",
"panic_state",
"placeholder1",
"liquidation_flat_sol_fee",
"order_init_flat_sol_fee",
"order_execution_max_fee",
"reserved1",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for FeeStateRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO fee_state_account (
"key",
"global_fee_admin",
"global_fee_wallet",
"placeholder0",
"bank_init_flat_sol_fee",
"bump_seed",
"padding0",
"liquidation_max_fee",
"program_fee_fixed",
"program_fee_rate",
"panic_state",
"placeholder1",
"liquidation_flat_sol_fee",
"order_init_flat_sol_fee",
"order_execution_max_fee",
"reserved1",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
)"#,
)
.bind(self.key)
.bind(self.global_fee_admin)
.bind(self.global_fee_wallet)
.bind(&self.placeholder0)
.bind(self.bank_init_flat_sol_fee)
.bind(self.bump_seed)
.bind(&self.padding0)
.bind(&self.liquidation_max_fee)
.bind(&self.program_fee_fixed)
.bind(&self.program_fee_rate)
.bind(&self.panic_state)
.bind(&self.placeholder1)
.bind(self.liquidation_flat_sol_fee)
.bind(self.order_init_flat_sol_fee)
.bind(&self.order_execution_max_fee)
.bind(&self.reserved1)
.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 FeeStateRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO fee_state_account (
"key",
"global_fee_admin",
"global_fee_wallet",
"placeholder0",
"bank_init_flat_sol_fee",
"bump_seed",
"padding0",
"liquidation_max_fee",
"program_fee_fixed",
"program_fee_rate",
"panic_state",
"placeholder1",
"liquidation_flat_sol_fee",
"order_init_flat_sol_fee",
"order_execution_max_fee",
"reserved1",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"key" = EXCLUDED."key",
"global_fee_admin" = EXCLUDED."global_fee_admin",
"global_fee_wallet" = EXCLUDED."global_fee_wallet",
"placeholder0" = EXCLUDED."placeholder0",
"bank_init_flat_sol_fee" = EXCLUDED."bank_init_flat_sol_fee",
"bump_seed" = EXCLUDED."bump_seed",
"padding0" = EXCLUDED."padding0",
"liquidation_max_fee" = EXCLUDED."liquidation_max_fee",
"program_fee_fixed" = EXCLUDED."program_fee_fixed",
"program_fee_rate" = EXCLUDED."program_fee_rate",
"panic_state" = EXCLUDED."panic_state",
"placeholder1" = EXCLUDED."placeholder1",
"liquidation_flat_sol_fee" = EXCLUDED."liquidation_flat_sol_fee",
"order_init_flat_sol_fee" = EXCLUDED."order_init_flat_sol_fee",
"order_execution_max_fee" = EXCLUDED."order_execution_max_fee",
"reserved1" = EXCLUDED."reserved1",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.key)
.bind(self.global_fee_admin)
.bind(self.global_fee_wallet)
.bind(&self.placeholder0)
.bind(self.bank_init_flat_sol_fee)
.bind(self.bump_seed)
.bind(&self.padding0)
.bind(&self.liquidation_max_fee)
.bind(&self.program_fee_fixed)
.bind(&self.program_fee_rate)
.bind(&self.panic_state)
.bind(&self.placeholder1)
.bind(self.liquidation_flat_sol_fee)
.bind(self.order_init_flat_sol_fee)
.bind(&self.order_execution_max_fee)
.bind(&self.reserved1)
.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 FeeStateRow {
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 fee_state_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 FeeStateRow {
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 fee_state_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 FeeStateMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for FeeStateMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS fee_state_account (
-- Account data
"key" BYTEA NOT NULL,
"global_fee_admin" BYTEA NOT NULL,
"global_fee_wallet" BYTEA NOT NULL,
"placeholder0" NUMERIC(20) NOT NULL,
"bank_init_flat_sol_fee" INT8 NOT NULL,
"bump_seed" INT2 NOT NULL,
"padding0" BYTEA NOT NULL,
"liquidation_max_fee" JSONB NOT NULL,
"program_fee_fixed" JSONB NOT NULL,
"program_fee_rate" JSONB NOT NULL,
"panic_state" JSONB NOT NULL,
"placeholder1" NUMERIC(20) NOT NULL,
"liquidation_flat_sol_fee" INT8 NOT NULL,
"order_init_flat_sol_fee" INT8 NOT NULL,
"order_execution_max_fee" JSONB NOT NULL,
"reserved1" BYTEA 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 fee_state_account"#)
.execute(connection)
.await?;
Ok(())
}
}