use {
crate::types::{AssetTier, OracleSource},
carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{U32, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct InitializeSpotMarketRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub optimal_utilization: U32,
pub optimal_borrow_rate: U32,
pub max_borrow_rate: U32,
pub oracle_source: sqlx::types::Json<OracleSource>,
pub initial_asset_weight: U32,
pub maintenance_asset_weight: U32,
pub initial_liability_weight: U32,
pub maintenance_liability_weight: U32,
pub imf_factor: U32,
pub liquidator_fee: U32,
pub if_liquidation_fee: U32,
pub active_status: bool,
pub asset_tier: sqlx::types::Json<AssetTier>,
pub scale_initial_asset_weight_start: U64,
pub withdraw_guard_threshold: U64,
pub order_tick_size: U64,
pub order_step_size: U64,
pub if_total_factor: U32,
pub name: Vec<u8>,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl InitializeSpotMarketRow {
pub fn from_parts(
source: crate::instructions::initialize_spot_market::InitializeSpotMarket,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
optimal_utilization: source.optimal_utilization.into(),
optimal_borrow_rate: source.optimal_borrow_rate.into(),
max_borrow_rate: source.max_borrow_rate.into(),
oracle_source: sqlx::types::Json(source.oracle_source),
initial_asset_weight: source.initial_asset_weight.into(),
maintenance_asset_weight: source.maintenance_asset_weight.into(),
initial_liability_weight: source.initial_liability_weight.into(),
maintenance_liability_weight: source.maintenance_liability_weight.into(),
imf_factor: source.imf_factor.into(),
liquidator_fee: source.liquidator_fee.into(),
if_liquidation_fee: source.if_liquidation_fee.into(),
active_status: source.active_status,
asset_tier: sqlx::types::Json(source.asset_tier),
scale_initial_asset_weight_start: source.scale_initial_asset_weight_start.into(),
withdraw_guard_threshold: source.withdraw_guard_threshold.into(),
order_tick_size: source.order_tick_size.into(),
order_step_size: source.order_step_size.into(),
if_total_factor: source.if_total_factor.into(),
name: source.name.to_vec(),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<InitializeSpotMarketRow>
for crate::instructions::initialize_spot_market::InitializeSpotMarket
{
type Error = carbon_core::error::Error;
fn try_from(source: InitializeSpotMarketRow) -> Result<Self, Self::Error> {
Ok(Self {
optimal_utilization: source.optimal_utilization.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
optimal_borrow_rate: source.optimal_borrow_rate.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
max_borrow_rate: source.max_borrow_rate.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
oracle_source: source.oracle_source.0,
initial_asset_weight: source.initial_asset_weight.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
maintenance_asset_weight: source.maintenance_asset_weight.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
initial_liability_weight: source.initial_liability_weight.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
maintenance_liability_weight: source.maintenance_liability_weight.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
imf_factor: source.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(),
)
})?,
active_status: source.active_status,
asset_tier: source.asset_tier.0,
scale_initial_asset_weight_start: *source.scale_initial_asset_weight_start,
withdraw_guard_threshold: *source.withdraw_guard_threshold,
order_tick_size: *source.order_tick_size,
order_step_size: *source.order_step_size,
if_total_factor: source.if_total_factor.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
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(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::instructions::initialize_spot_market::InitializeSpotMarket
{
fn table() -> &'static str {
"initialize_spot_market_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"optimal_utilization",
"optimal_borrow_rate",
"max_borrow_rate",
"oracle_source",
"initial_asset_weight",
"maintenance_asset_weight",
"initial_liability_weight",
"maintenance_liability_weight",
"imf_factor",
"liquidator_fee",
"if_liquidation_fee",
"active_status",
"asset_tier",
"scale_initial_asset_weight_start",
"withdraw_guard_threshold",
"order_tick_size",
"order_step_size",
"if_total_factor",
"name",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for InitializeSpotMarketRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO initialize_spot_market_instruction (
"optimal_utilization",
"optimal_borrow_rate",
"max_borrow_rate",
"oracle_source",
"initial_asset_weight",
"maintenance_asset_weight",
"initial_liability_weight",
"maintenance_liability_weight",
"imf_factor",
"liquidator_fee",
"if_liquidation_fee",
"active_status",
"asset_tier",
"scale_initial_asset_weight_start",
"withdraw_guard_threshold",
"order_tick_size",
"order_step_size",
"if_total_factor",
"name",
__signature, __instruction_index, __stack_height, __slot, __accounts
) 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
)"#)
.bind(self.optimal_utilization)
.bind(self.optimal_borrow_rate)
.bind(self.max_borrow_rate)
.bind(&self.oracle_source)
.bind(self.initial_asset_weight)
.bind(self.maintenance_asset_weight)
.bind(self.initial_liability_weight)
.bind(self.maintenance_liability_weight)
.bind(self.imf_factor)
.bind(self.liquidator_fee)
.bind(self.if_liquidation_fee)
.bind(self.active_status)
.bind(&self.asset_tier)
.bind(&self.scale_initial_asset_weight_start)
.bind(&self.withdraw_guard_threshold)
.bind(&self.order_tick_size)
.bind(&self.order_step_size)
.bind(self.if_total_factor)
.bind(&self.name)
.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 InitializeSpotMarketRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO initialize_spot_market_instruction (
"optimal_utilization",
"optimal_borrow_rate",
"max_borrow_rate",
"oracle_source",
"initial_asset_weight",
"maintenance_asset_weight",
"initial_liability_weight",
"maintenance_liability_weight",
"imf_factor",
"liquidator_fee",
"if_liquidation_fee",
"active_status",
"asset_tier",
"scale_initial_asset_weight_start",
"withdraw_guard_threshold",
"order_tick_size",
"order_step_size",
"if_total_factor",
"name",
__signature, __instruction_index, __stack_height, __slot, __accounts
) 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
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"optimal_utilization" = EXCLUDED."optimal_utilization",
"optimal_borrow_rate" = EXCLUDED."optimal_borrow_rate",
"max_borrow_rate" = EXCLUDED."max_borrow_rate",
"oracle_source" = EXCLUDED."oracle_source",
"initial_asset_weight" = EXCLUDED."initial_asset_weight",
"maintenance_asset_weight" = EXCLUDED."maintenance_asset_weight",
"initial_liability_weight" = EXCLUDED."initial_liability_weight",
"maintenance_liability_weight" = EXCLUDED."maintenance_liability_weight",
"imf_factor" = EXCLUDED."imf_factor",
"liquidator_fee" = EXCLUDED."liquidator_fee",
"if_liquidation_fee" = EXCLUDED."if_liquidation_fee",
"active_status" = EXCLUDED."active_status",
"asset_tier" = EXCLUDED."asset_tier",
"scale_initial_asset_weight_start" = EXCLUDED."scale_initial_asset_weight_start",
"withdraw_guard_threshold" = EXCLUDED."withdraw_guard_threshold",
"order_tick_size" = EXCLUDED."order_tick_size",
"order_step_size" = EXCLUDED."order_step_size",
"if_total_factor" = EXCLUDED."if_total_factor",
"name" = EXCLUDED."name",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#)
.bind(self.optimal_utilization)
.bind(self.optimal_borrow_rate)
.bind(self.max_borrow_rate)
.bind(&self.oracle_source)
.bind(self.initial_asset_weight)
.bind(self.maintenance_asset_weight)
.bind(self.initial_liability_weight)
.bind(self.maintenance_liability_weight)
.bind(self.imf_factor)
.bind(self.liquidator_fee)
.bind(self.if_liquidation_fee)
.bind(self.active_status)
.bind(&self.asset_tier)
.bind(&self.scale_initial_asset_weight_start)
.bind(&self.withdraw_guard_threshold)
.bind(&self.order_tick_size)
.bind(&self.order_step_size)
.bind(self.if_total_factor)
.bind(&self.name)
.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 InitializeSpotMarketRow {
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 initialize_spot_market_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 InitializeSpotMarketRow {
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 initialize_spot_market_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 InitializeSpotMarketMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for InitializeSpotMarketMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS initialize_spot_market_instruction (
-- Instruction data
"optimal_utilization" INT8 NOT NULL,
"optimal_borrow_rate" INT8 NOT NULL,
"max_borrow_rate" INT8 NOT NULL,
"oracle_source" JSONB NOT NULL,
"initial_asset_weight" INT8 NOT NULL,
"maintenance_asset_weight" INT8 NOT NULL,
"initial_liability_weight" INT8 NOT NULL,
"maintenance_liability_weight" INT8 NOT NULL,
"imf_factor" INT8 NOT NULL,
"liquidator_fee" INT8 NOT NULL,
"if_liquidation_fee" INT8 NOT NULL,
"active_status" BOOLEAN NOT NULL,
"asset_tier" JSONB NOT NULL,
"scale_initial_asset_weight_start" NUMERIC(20) NOT NULL,
"withdraw_guard_threshold" NUMERIC(20) NOT NULL,
"order_tick_size" NUMERIC(20) NOT NULL,
"order_step_size" NUMERIC(20) NOT NULL,
"if_total_factor" INT8 NOT NULL,
"name" BYTEA 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 initialize_spot_market_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}