use {
crate::types::{ContractTier, ContractType, InsuranceClaim, MarketStatus, PoolBalance, AMM},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U32, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PerpMarketRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub pubkey: Pubkey,
pub amm: sqlx::types::Json<AMM>,
pub pnl_pool: sqlx::types::Json<PoolBalance>,
pub name: Vec<u8>,
pub insurance_claim: sqlx::types::Json<InsuranceClaim>,
pub unrealized_pnl_max_imbalance: U64,
pub expiry_ts: i64,
pub expiry_price: i64,
pub next_fill_record_id: U64,
pub next_funding_rate_record_id: U64,
pub next_curve_record_id: U64,
pub imf_factor: U32,
pub unrealized_pnl_imf_factor: U32,
pub liquidator_fee: U32,
pub if_liquidation_fee: U32,
pub margin_ratio_initial: U32,
pub margin_ratio_maintenance: U32,
pub unrealized_pnl_initial_asset_weight: U32,
pub unrealized_pnl_maintenance_asset_weight: U32,
pub number_of_users_with_base: U32,
pub number_of_users: U32,
pub market_index: U16,
pub status: sqlx::types::Json<MarketStatus>,
pub contract_type: sqlx::types::Json<ContractType>,
pub contract_tier: sqlx::types::Json<ContractTier>,
pub paused_operations: U8,
pub quote_spot_market_index: U16,
pub fee_adjustment: i16,
pub fuel_boost_position: U8,
pub fuel_boost_taker: U8,
pub fuel_boost_maker: U8,
pub pool_id: U8,
pub high_leverage_margin_ratio_initial: U16,
pub high_leverage_margin_ratio_maintenance: U16,
pub protected_maker_limit_price_divisor: U8,
pub protected_maker_dynamic_divisor: U8,
pub lp_fee_transfer_scalar: U8,
pub lp_status: U8,
pub lp_paused_operations: U8,
pub lp_exchange_fee_excluscion_scalar: U8,
pub last_fill_price: U64,
pub lp_pool_id: U8,
pub market_config: U8,
pub padding: Vec<u8>,
}
impl PerpMarketRow {
pub fn from_parts(
source: crate::accounts::perp_market::PerpMarket,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
pubkey: source.pubkey.into(),
amm: sqlx::types::Json(source.amm),
pnl_pool: sqlx::types::Json(source.pnl_pool),
name: source.name.to_vec(),
insurance_claim: sqlx::types::Json(source.insurance_claim),
unrealized_pnl_max_imbalance: source.unrealized_pnl_max_imbalance.into(),
expiry_ts: source.expiry_ts,
expiry_price: source.expiry_price,
next_fill_record_id: source.next_fill_record_id.into(),
next_funding_rate_record_id: source.next_funding_rate_record_id.into(),
next_curve_record_id: source.next_curve_record_id.into(),
imf_factor: source.imf_factor.into(),
unrealized_pnl_imf_factor: source.unrealized_pnl_imf_factor.into(),
liquidator_fee: source.liquidator_fee.into(),
if_liquidation_fee: source.if_liquidation_fee.into(),
margin_ratio_initial: source.margin_ratio_initial.into(),
margin_ratio_maintenance: source.margin_ratio_maintenance.into(),
unrealized_pnl_initial_asset_weight: source.unrealized_pnl_initial_asset_weight.into(),
unrealized_pnl_maintenance_asset_weight: source
.unrealized_pnl_maintenance_asset_weight
.into(),
number_of_users_with_base: source.number_of_users_with_base.into(),
number_of_users: source.number_of_users.into(),
market_index: source.market_index.into(),
status: sqlx::types::Json(source.status),
contract_type: sqlx::types::Json(source.contract_type),
contract_tier: sqlx::types::Json(source.contract_tier),
paused_operations: source.paused_operations.into(),
quote_spot_market_index: source.quote_spot_market_index.into(),
fee_adjustment: source.fee_adjustment,
fuel_boost_position: source.fuel_boost_position.into(),
fuel_boost_taker: source.fuel_boost_taker.into(),
fuel_boost_maker: source.fuel_boost_maker.into(),
pool_id: source.pool_id.into(),
high_leverage_margin_ratio_initial: source.high_leverage_margin_ratio_initial.into(),
high_leverage_margin_ratio_maintenance: source
.high_leverage_margin_ratio_maintenance
.into(),
protected_maker_limit_price_divisor: source.protected_maker_limit_price_divisor.into(),
protected_maker_dynamic_divisor: source.protected_maker_dynamic_divisor.into(),
lp_fee_transfer_scalar: source.lp_fee_transfer_scalar.into(),
lp_status: source.lp_status.into(),
lp_paused_operations: source.lp_paused_operations.into(),
lp_exchange_fee_excluscion_scalar: source.lp_exchange_fee_excluscion_scalar.into(),
last_fill_price: source.last_fill_price.into(),
lp_pool_id: source.lp_pool_id.into(),
market_config: source.market_config.into(),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<PerpMarketRow> for crate::accounts::perp_market::PerpMarket {
type Error = carbon_core::error::Error;
fn try_from(source: PerpMarketRow) -> Result<Self, Self::Error> {
Ok(Self {
pubkey: *source.pubkey,
amm: source.amm.0,
pnl_pool: source.pnl_pool.0,
name: source.name.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 32 bytes"
.to_string(),
)
})?,
insurance_claim: source.insurance_claim.0,
unrealized_pnl_max_imbalance: *source.unrealized_pnl_max_imbalance,
expiry_ts: source.expiry_ts,
expiry_price: source.expiry_price,
next_fill_record_id: *source.next_fill_record_id,
next_funding_rate_record_id: *source.next_funding_rate_record_id,
next_curve_record_id: *source.next_curve_record_id,
imf_factor: source.imf_factor.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
unrealized_pnl_imf_factor: source.unrealized_pnl_imf_factor.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
liquidator_fee: source.liquidator_fee.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
if_liquidation_fee: source.if_liquidation_fee.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
margin_ratio_initial: source.margin_ratio_initial.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
margin_ratio_maintenance: source.margin_ratio_maintenance.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
unrealized_pnl_initial_asset_weight: source
.unrealized_pnl_initial_asset_weight
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
unrealized_pnl_maintenance_asset_weight: source
.unrealized_pnl_maintenance_asset_weight
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
number_of_users_with_base: source.number_of_users_with_base.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
number_of_users: source.number_of_users.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
market_index: source.market_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
status: source.status.0,
contract_type: source.contract_type.0,
contract_tier: source.contract_tier.0,
paused_operations: source.paused_operations.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
quote_spot_market_index: source.quote_spot_market_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
fee_adjustment: source.fee_adjustment,
fuel_boost_position: source.fuel_boost_position.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
fuel_boost_taker: source.fuel_boost_taker.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
fuel_boost_maker: source.fuel_boost_maker.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
pool_id: source.pool_id.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
high_leverage_margin_ratio_initial: source
.high_leverage_margin_ratio_initial
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
high_leverage_margin_ratio_maintenance: source
.high_leverage_margin_ratio_maintenance
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
protected_maker_limit_price_divisor: source
.protected_maker_limit_price_divisor
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
protected_maker_dynamic_divisor: source
.protected_maker_dynamic_divisor
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
lp_fee_transfer_scalar: source.lp_fee_transfer_scalar.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
lp_status: source.lp_status.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
lp_paused_operations: source.lp_paused_operations.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
lp_exchange_fee_excluscion_scalar: source
.lp_exchange_fee_excluscion_scalar
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
last_fill_price: *source.last_fill_price,
lp_pool_id: source.lp_pool_id.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
market_config: source.market_config.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding: source.padding.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 22 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::perp_market::PerpMarket {
fn table() -> &'static str {
"perp_market_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"pubkey",
"amm",
"pnl_pool",
"name",
"insurance_claim",
"unrealized_pnl_max_imbalance",
"expiry_ts",
"expiry_price",
"next_fill_record_id",
"next_funding_rate_record_id",
"next_curve_record_id",
"imf_factor",
"unrealized_pnl_imf_factor",
"liquidator_fee",
"if_liquidation_fee",
"margin_ratio_initial",
"margin_ratio_maintenance",
"unrealized_pnl_initial_asset_weight",
"unrealized_pnl_maintenance_asset_weight",
"number_of_users_with_base",
"number_of_users",
"market_index",
"status",
"contract_type",
"contract_tier",
"paused_operations",
"quote_spot_market_index",
"fee_adjustment",
"fuel_boost_position",
"fuel_boost_taker",
"fuel_boost_maker",
"pool_id",
"high_leverage_margin_ratio_initial",
"high_leverage_margin_ratio_maintenance",
"protected_maker_limit_price_divisor",
"protected_maker_dynamic_divisor",
"lp_fee_transfer_scalar",
"lp_status",
"lp_paused_operations",
"lp_exchange_fee_excluscion_scalar",
"last_fill_price",
"lp_pool_id",
"market_config",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PerpMarketRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO perp_market_account (
"pubkey",
"amm",
"pnl_pool",
"name",
"insurance_claim",
"unrealized_pnl_max_imbalance",
"expiry_ts",
"expiry_price",
"next_fill_record_id",
"next_funding_rate_record_id",
"next_curve_record_id",
"imf_factor",
"unrealized_pnl_imf_factor",
"liquidator_fee",
"if_liquidation_fee",
"margin_ratio_initial",
"margin_ratio_maintenance",
"unrealized_pnl_initial_asset_weight",
"unrealized_pnl_maintenance_asset_weight",
"number_of_users_with_base",
"number_of_users",
"market_index",
"status",
"contract_type",
"contract_tier",
"paused_operations",
"quote_spot_market_index",
"fee_adjustment",
"fuel_boost_position",
"fuel_boost_taker",
"fuel_boost_maker",
"pool_id",
"high_leverage_margin_ratio_initial",
"high_leverage_margin_ratio_maintenance",
"protected_maker_limit_price_divisor",
"protected_maker_dynamic_divisor",
"lp_fee_transfer_scalar",
"lp_status",
"lp_paused_operations",
"lp_exchange_fee_excluscion_scalar",
"last_fill_price",
"lp_pool_id",
"market_config",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46
)"#)
.bind(self.pubkey)
.bind(&self.amm)
.bind(&self.pnl_pool)
.bind(&self.name)
.bind(&self.insurance_claim)
.bind(&self.unrealized_pnl_max_imbalance)
.bind(self.expiry_ts)
.bind(self.expiry_price)
.bind(&self.next_fill_record_id)
.bind(&self.next_funding_rate_record_id)
.bind(&self.next_curve_record_id)
.bind(self.imf_factor)
.bind(self.unrealized_pnl_imf_factor)
.bind(self.liquidator_fee)
.bind(self.if_liquidation_fee)
.bind(self.margin_ratio_initial)
.bind(self.margin_ratio_maintenance)
.bind(self.unrealized_pnl_initial_asset_weight)
.bind(self.unrealized_pnl_maintenance_asset_weight)
.bind(self.number_of_users_with_base)
.bind(self.number_of_users)
.bind(self.market_index)
.bind(&self.status)
.bind(&self.contract_type)
.bind(&self.contract_tier)
.bind(self.paused_operations)
.bind(self.quote_spot_market_index)
.bind(self.fee_adjustment)
.bind(self.fuel_boost_position)
.bind(self.fuel_boost_taker)
.bind(self.fuel_boost_maker)
.bind(self.pool_id)
.bind(self.high_leverage_margin_ratio_initial)
.bind(self.high_leverage_margin_ratio_maintenance)
.bind(self.protected_maker_limit_price_divisor)
.bind(self.protected_maker_dynamic_divisor)
.bind(self.lp_fee_transfer_scalar)
.bind(self.lp_status)
.bind(self.lp_paused_operations)
.bind(self.lp_exchange_fee_excluscion_scalar)
.bind(&self.last_fill_price)
.bind(self.lp_pool_id)
.bind(self.market_config)
.bind(&self.padding)
.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 PerpMarketRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO perp_market_account (
"pubkey",
"amm",
"pnl_pool",
"name",
"insurance_claim",
"unrealized_pnl_max_imbalance",
"expiry_ts",
"expiry_price",
"next_fill_record_id",
"next_funding_rate_record_id",
"next_curve_record_id",
"imf_factor",
"unrealized_pnl_imf_factor",
"liquidator_fee",
"if_liquidation_fee",
"margin_ratio_initial",
"margin_ratio_maintenance",
"unrealized_pnl_initial_asset_weight",
"unrealized_pnl_maintenance_asset_weight",
"number_of_users_with_base",
"number_of_users",
"market_index",
"status",
"contract_type",
"contract_tier",
"paused_operations",
"quote_spot_market_index",
"fee_adjustment",
"fuel_boost_position",
"fuel_boost_taker",
"fuel_boost_maker",
"pool_id",
"high_leverage_margin_ratio_initial",
"high_leverage_margin_ratio_maintenance",
"protected_maker_limit_price_divisor",
"protected_maker_dynamic_divisor",
"lp_fee_transfer_scalar",
"lp_status",
"lp_paused_operations",
"lp_exchange_fee_excluscion_scalar",
"last_fill_price",
"lp_pool_id",
"market_config",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $40, $41, $42, $43, $44, $45, $46
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"pubkey" = EXCLUDED."pubkey",
"amm" = EXCLUDED."amm",
"pnl_pool" = EXCLUDED."pnl_pool",
"name" = EXCLUDED."name",
"insurance_claim" = EXCLUDED."insurance_claim",
"unrealized_pnl_max_imbalance" = EXCLUDED."unrealized_pnl_max_imbalance",
"expiry_ts" = EXCLUDED."expiry_ts",
"expiry_price" = EXCLUDED."expiry_price",
"next_fill_record_id" = EXCLUDED."next_fill_record_id",
"next_funding_rate_record_id" = EXCLUDED."next_funding_rate_record_id",
"next_curve_record_id" = EXCLUDED."next_curve_record_id",
"imf_factor" = EXCLUDED."imf_factor",
"unrealized_pnl_imf_factor" = EXCLUDED."unrealized_pnl_imf_factor",
"liquidator_fee" = EXCLUDED."liquidator_fee",
"if_liquidation_fee" = EXCLUDED."if_liquidation_fee",
"margin_ratio_initial" = EXCLUDED."margin_ratio_initial",
"margin_ratio_maintenance" = EXCLUDED."margin_ratio_maintenance",
"unrealized_pnl_initial_asset_weight" = EXCLUDED."unrealized_pnl_initial_asset_weight",
"unrealized_pnl_maintenance_asset_weight" = EXCLUDED."unrealized_pnl_maintenance_asset_weight",
"number_of_users_with_base" = EXCLUDED."number_of_users_with_base",
"number_of_users" = EXCLUDED."number_of_users",
"market_index" = EXCLUDED."market_index",
"status" = EXCLUDED."status",
"contract_type" = EXCLUDED."contract_type",
"contract_tier" = EXCLUDED."contract_tier",
"paused_operations" = EXCLUDED."paused_operations",
"quote_spot_market_index" = EXCLUDED."quote_spot_market_index",
"fee_adjustment" = EXCLUDED."fee_adjustment",
"fuel_boost_position" = EXCLUDED."fuel_boost_position",
"fuel_boost_taker" = EXCLUDED."fuel_boost_taker",
"fuel_boost_maker" = EXCLUDED."fuel_boost_maker",
"pool_id" = EXCLUDED."pool_id",
"high_leverage_margin_ratio_initial" = EXCLUDED."high_leverage_margin_ratio_initial",
"high_leverage_margin_ratio_maintenance" = EXCLUDED."high_leverage_margin_ratio_maintenance",
"protected_maker_limit_price_divisor" = EXCLUDED."protected_maker_limit_price_divisor",
"protected_maker_dynamic_divisor" = EXCLUDED."protected_maker_dynamic_divisor",
"lp_fee_transfer_scalar" = EXCLUDED."lp_fee_transfer_scalar",
"lp_status" = EXCLUDED."lp_status",
"lp_paused_operations" = EXCLUDED."lp_paused_operations",
"lp_exchange_fee_excluscion_scalar" = EXCLUDED."lp_exchange_fee_excluscion_scalar",
"last_fill_price" = EXCLUDED."last_fill_price",
"lp_pool_id" = EXCLUDED."lp_pool_id",
"market_config" = EXCLUDED."market_config",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#)
.bind(self.pubkey)
.bind(&self.amm)
.bind(&self.pnl_pool)
.bind(&self.name)
.bind(&self.insurance_claim)
.bind(&self.unrealized_pnl_max_imbalance)
.bind(self.expiry_ts)
.bind(self.expiry_price)
.bind(&self.next_fill_record_id)
.bind(&self.next_funding_rate_record_id)
.bind(&self.next_curve_record_id)
.bind(self.imf_factor)
.bind(self.unrealized_pnl_imf_factor)
.bind(self.liquidator_fee)
.bind(self.if_liquidation_fee)
.bind(self.margin_ratio_initial)
.bind(self.margin_ratio_maintenance)
.bind(self.unrealized_pnl_initial_asset_weight)
.bind(self.unrealized_pnl_maintenance_asset_weight)
.bind(self.number_of_users_with_base)
.bind(self.number_of_users)
.bind(self.market_index)
.bind(&self.status)
.bind(&self.contract_type)
.bind(&self.contract_tier)
.bind(self.paused_operations)
.bind(self.quote_spot_market_index)
.bind(self.fee_adjustment)
.bind(self.fuel_boost_position)
.bind(self.fuel_boost_taker)
.bind(self.fuel_boost_maker)
.bind(self.pool_id)
.bind(self.high_leverage_margin_ratio_initial)
.bind(self.high_leverage_margin_ratio_maintenance)
.bind(self.protected_maker_limit_price_divisor)
.bind(self.protected_maker_dynamic_divisor)
.bind(self.lp_fee_transfer_scalar)
.bind(self.lp_status)
.bind(self.lp_paused_operations)
.bind(self.lp_exchange_fee_excluscion_scalar)
.bind(&self.last_fill_price)
.bind(self.lp_pool_id)
.bind(self.market_config)
.bind(&self.padding)
.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 PerpMarketRow {
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 perp_market_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 PerpMarketRow {
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 perp_market_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 PerpMarketMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PerpMarketMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS perp_market_account (
-- Account data
"pubkey" BYTEA NOT NULL,
"amm" JSONB NOT NULL,
"pnl_pool" JSONB NOT NULL,
"name" BYTEA NOT NULL,
"insurance_claim" JSONB NOT NULL,
"unrealized_pnl_max_imbalance" NUMERIC(20) NOT NULL,
"expiry_ts" INT8 NOT NULL,
"expiry_price" INT8 NOT NULL,
"next_fill_record_id" NUMERIC(20) NOT NULL,
"next_funding_rate_record_id" NUMERIC(20) NOT NULL,
"next_curve_record_id" NUMERIC(20) NOT NULL,
"imf_factor" INT8 NOT NULL,
"unrealized_pnl_imf_factor" INT8 NOT NULL,
"liquidator_fee" INT8 NOT NULL,
"if_liquidation_fee" INT8 NOT NULL,
"margin_ratio_initial" INT8 NOT NULL,
"margin_ratio_maintenance" INT8 NOT NULL,
"unrealized_pnl_initial_asset_weight" INT8 NOT NULL,
"unrealized_pnl_maintenance_asset_weight" INT8 NOT NULL,
"number_of_users_with_base" INT8 NOT NULL,
"number_of_users" INT8 NOT NULL,
"market_index" INT4 NOT NULL,
"status" JSONB NOT NULL,
"contract_type" JSONB NOT NULL,
"contract_tier" JSONB NOT NULL,
"paused_operations" INT2 NOT NULL,
"quote_spot_market_index" INT4 NOT NULL,
"fee_adjustment" INT2 NOT NULL,
"fuel_boost_position" INT2 NOT NULL,
"fuel_boost_taker" INT2 NOT NULL,
"fuel_boost_maker" INT2 NOT NULL,
"pool_id" INT2 NOT NULL,
"high_leverage_margin_ratio_initial" INT4 NOT NULL,
"high_leverage_margin_ratio_maintenance" INT4 NOT NULL,
"protected_maker_limit_price_divisor" INT2 NOT NULL,
"protected_maker_dynamic_divisor" INT2 NOT NULL,
"lp_fee_transfer_scalar" INT2 NOT NULL,
"lp_status" INT2 NOT NULL,
"lp_paused_operations" INT2 NOT NULL,
"lp_exchange_fee_excluscion_scalar" INT2 NOT NULL,
"last_fill_price" NUMERIC(20) NOT NULL,
"lp_pool_id" INT2 NOT NULL,
"market_config" INT2 NOT NULL,
"padding" 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 perp_market_account"#)
.execute(connection)
.await?;
Ok(())
}
}