use {
crate::types::ConstituentSpotBalance,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, I128, U16, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ConstituentRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub pubkey: Pubkey,
pub mint: Pubkey,
pub lp_pool: Pubkey,
pub vault: Pubkey,
pub total_swap_fees: I128,
pub spot_balance: sqlx::types::Json<ConstituentSpotBalance>,
pub last_spot_balance_token_amount: i64,
pub cumulative_spot_interest_accrued_token_amount: i64,
pub max_weight_deviation: i64,
pub swap_fee_min: i64,
pub swap_fee_max: i64,
pub max_borrow_token_amount: U64,
pub vault_token_balance: U64,
pub last_oracle_price: i64,
pub last_oracle_slot: U64,
pub oracle_staleness_threshold: U64,
pub flash_loan_initial_token_amount: U64,
pub next_swap_id: U64,
pub derivative_weight: U64,
pub volatility: U64,
pub constituent_derivative_depeg_threshold: U64,
pub constituent_derivative_index: i16,
pub spot_market_index: U16,
pub constituent_index: U16,
pub decimals: U8,
pub bump: U8,
pub vault_bump: U8,
pub gamma_inventory: U8,
pub gamma_execution: U8,
pub xi: U8,
pub status: U8,
pub paused_operations: U8,
pub padding: Vec<u8>,
}
impl ConstituentRow {
pub fn from_parts(
source: crate::accounts::constituent::Constituent,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
pubkey: source.pubkey.into(),
mint: source.mint.into(),
lp_pool: source.lp_pool.into(),
vault: source.vault.into(),
total_swap_fees: source.total_swap_fees.into(),
spot_balance: sqlx::types::Json(source.spot_balance),
last_spot_balance_token_amount: source.last_spot_balance_token_amount,
cumulative_spot_interest_accrued_token_amount: source
.cumulative_spot_interest_accrued_token_amount,
max_weight_deviation: source.max_weight_deviation,
swap_fee_min: source.swap_fee_min,
swap_fee_max: source.swap_fee_max,
max_borrow_token_amount: source.max_borrow_token_amount.into(),
vault_token_balance: source.vault_token_balance.into(),
last_oracle_price: source.last_oracle_price,
last_oracle_slot: source.last_oracle_slot.into(),
oracle_staleness_threshold: source.oracle_staleness_threshold.into(),
flash_loan_initial_token_amount: source.flash_loan_initial_token_amount.into(),
next_swap_id: source.next_swap_id.into(),
derivative_weight: source.derivative_weight.into(),
volatility: source.volatility.into(),
constituent_derivative_depeg_threshold: source
.constituent_derivative_depeg_threshold
.into(),
constituent_derivative_index: source.constituent_derivative_index,
spot_market_index: source.spot_market_index.into(),
constituent_index: source.constituent_index.into(),
decimals: source.decimals.into(),
bump: source.bump.into(),
vault_bump: source.vault_bump.into(),
gamma_inventory: source.gamma_inventory.into(),
gamma_execution: source.gamma_execution.into(),
xi: source.xi.into(),
status: source.status.into(),
paused_operations: source.paused_operations.into(),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<ConstituentRow> for crate::accounts::constituent::Constituent {
type Error = carbon_core::error::Error;
fn try_from(source: ConstituentRow) -> Result<Self, Self::Error> {
Ok(Self {
pubkey: *source.pubkey,
mint: *source.mint,
lp_pool: *source.lp_pool,
vault: *source.vault,
total_swap_fees: *source.total_swap_fees,
spot_balance: source.spot_balance.0,
last_spot_balance_token_amount: source.last_spot_balance_token_amount,
cumulative_spot_interest_accrued_token_amount: source
.cumulative_spot_interest_accrued_token_amount,
max_weight_deviation: source.max_weight_deviation,
swap_fee_min: source.swap_fee_min,
swap_fee_max: source.swap_fee_max,
max_borrow_token_amount: *source.max_borrow_token_amount,
vault_token_balance: *source.vault_token_balance,
last_oracle_price: source.last_oracle_price,
last_oracle_slot: *source.last_oracle_slot,
oracle_staleness_threshold: *source.oracle_staleness_threshold,
flash_loan_initial_token_amount: *source.flash_loan_initial_token_amount,
next_swap_id: *source.next_swap_id,
derivative_weight: *source.derivative_weight,
volatility: *source.volatility,
constituent_derivative_depeg_threshold: *source.constituent_derivative_depeg_threshold,
constituent_derivative_index: source.constituent_derivative_index,
spot_market_index: source.spot_market_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
constituent_index: source.constituent_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
decimals: source.decimals.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
vault_bump: source.vault_bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
gamma_inventory: source.gamma_inventory.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
gamma_execution: source.gamma_execution.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
xi: source.xi.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
status: source.status.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
paused_operations: source.paused_operations.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 162 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::constituent::Constituent {
fn table() -> &'static str {
"constituent_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"pubkey",
"mint",
"lp_pool",
"vault",
"total_swap_fees",
"spot_balance",
"last_spot_balance_token_amount",
"cumulative_spot_interest_accrued_token_amount",
"max_weight_deviation",
"swap_fee_min",
"swap_fee_max",
"max_borrow_token_amount",
"vault_token_balance",
"last_oracle_price",
"last_oracle_slot",
"oracle_staleness_threshold",
"flash_loan_initial_token_amount",
"next_swap_id",
"derivative_weight",
"volatility",
"constituent_derivative_depeg_threshold",
"constituent_derivative_index",
"spot_market_index",
"constituent_index",
"decimals",
"bump",
"vault_bump",
"gamma_inventory",
"gamma_execution",
"xi",
"status",
"paused_operations",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ConstituentRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO constituent_account (
"pubkey",
"mint",
"lp_pool",
"vault",
"total_swap_fees",
"spot_balance",
"last_spot_balance_token_amount",
"cumulative_spot_interest_accrued_token_amount",
"max_weight_deviation",
"swap_fee_min",
"swap_fee_max",
"max_borrow_token_amount",
"vault_token_balance",
"last_oracle_price",
"last_oracle_slot",
"oracle_staleness_threshold",
"flash_loan_initial_token_amount",
"next_swap_id",
"derivative_weight",
"volatility",
"constituent_derivative_depeg_threshold",
"constituent_derivative_index",
"spot_market_index",
"constituent_index",
"decimals",
"bump",
"vault_bump",
"gamma_inventory",
"gamma_execution",
"xi",
"status",
"paused_operations",
"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
)"#)
.bind(self.pubkey)
.bind(self.mint)
.bind(self.lp_pool)
.bind(self.vault)
.bind(&self.total_swap_fees)
.bind(&self.spot_balance)
.bind(self.last_spot_balance_token_amount)
.bind(self.cumulative_spot_interest_accrued_token_amount)
.bind(self.max_weight_deviation)
.bind(self.swap_fee_min)
.bind(self.swap_fee_max)
.bind(&self.max_borrow_token_amount)
.bind(&self.vault_token_balance)
.bind(self.last_oracle_price)
.bind(&self.last_oracle_slot)
.bind(&self.oracle_staleness_threshold)
.bind(&self.flash_loan_initial_token_amount)
.bind(&self.next_swap_id)
.bind(&self.derivative_weight)
.bind(&self.volatility)
.bind(&self.constituent_derivative_depeg_threshold)
.bind(self.constituent_derivative_index)
.bind(self.spot_market_index)
.bind(self.constituent_index)
.bind(self.decimals)
.bind(self.bump)
.bind(self.vault_bump)
.bind(self.gamma_inventory)
.bind(self.gamma_execution)
.bind(self.xi)
.bind(self.status)
.bind(self.paused_operations)
.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 ConstituentRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO constituent_account (
"pubkey",
"mint",
"lp_pool",
"vault",
"total_swap_fees",
"spot_balance",
"last_spot_balance_token_amount",
"cumulative_spot_interest_accrued_token_amount",
"max_weight_deviation",
"swap_fee_min",
"swap_fee_max",
"max_borrow_token_amount",
"vault_token_balance",
"last_oracle_price",
"last_oracle_slot",
"oracle_staleness_threshold",
"flash_loan_initial_token_amount",
"next_swap_id",
"derivative_weight",
"volatility",
"constituent_derivative_depeg_threshold",
"constituent_derivative_index",
"spot_market_index",
"constituent_index",
"decimals",
"bump",
"vault_bump",
"gamma_inventory",
"gamma_execution",
"xi",
"status",
"paused_operations",
"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
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"pubkey" = EXCLUDED."pubkey",
"mint" = EXCLUDED."mint",
"lp_pool" = EXCLUDED."lp_pool",
"vault" = EXCLUDED."vault",
"total_swap_fees" = EXCLUDED."total_swap_fees",
"spot_balance" = EXCLUDED."spot_balance",
"last_spot_balance_token_amount" = EXCLUDED."last_spot_balance_token_amount",
"cumulative_spot_interest_accrued_token_amount" = EXCLUDED."cumulative_spot_interest_accrued_token_amount",
"max_weight_deviation" = EXCLUDED."max_weight_deviation",
"swap_fee_min" = EXCLUDED."swap_fee_min",
"swap_fee_max" = EXCLUDED."swap_fee_max",
"max_borrow_token_amount" = EXCLUDED."max_borrow_token_amount",
"vault_token_balance" = EXCLUDED."vault_token_balance",
"last_oracle_price" = EXCLUDED."last_oracle_price",
"last_oracle_slot" = EXCLUDED."last_oracle_slot",
"oracle_staleness_threshold" = EXCLUDED."oracle_staleness_threshold",
"flash_loan_initial_token_amount" = EXCLUDED."flash_loan_initial_token_amount",
"next_swap_id" = EXCLUDED."next_swap_id",
"derivative_weight" = EXCLUDED."derivative_weight",
"volatility" = EXCLUDED."volatility",
"constituent_derivative_depeg_threshold" = EXCLUDED."constituent_derivative_depeg_threshold",
"constituent_derivative_index" = EXCLUDED."constituent_derivative_index",
"spot_market_index" = EXCLUDED."spot_market_index",
"constituent_index" = EXCLUDED."constituent_index",
"decimals" = EXCLUDED."decimals",
"bump" = EXCLUDED."bump",
"vault_bump" = EXCLUDED."vault_bump",
"gamma_inventory" = EXCLUDED."gamma_inventory",
"gamma_execution" = EXCLUDED."gamma_execution",
"xi" = EXCLUDED."xi",
"status" = EXCLUDED."status",
"paused_operations" = EXCLUDED."paused_operations",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#)
.bind(self.pubkey)
.bind(self.mint)
.bind(self.lp_pool)
.bind(self.vault)
.bind(&self.total_swap_fees)
.bind(&self.spot_balance)
.bind(self.last_spot_balance_token_amount)
.bind(self.cumulative_spot_interest_accrued_token_amount)
.bind(self.max_weight_deviation)
.bind(self.swap_fee_min)
.bind(self.swap_fee_max)
.bind(&self.max_borrow_token_amount)
.bind(&self.vault_token_balance)
.bind(self.last_oracle_price)
.bind(&self.last_oracle_slot)
.bind(&self.oracle_staleness_threshold)
.bind(&self.flash_loan_initial_token_amount)
.bind(&self.next_swap_id)
.bind(&self.derivative_weight)
.bind(&self.volatility)
.bind(&self.constituent_derivative_depeg_threshold)
.bind(self.constituent_derivative_index)
.bind(self.spot_market_index)
.bind(self.constituent_index)
.bind(self.decimals)
.bind(self.bump)
.bind(self.vault_bump)
.bind(self.gamma_inventory)
.bind(self.gamma_execution)
.bind(self.xi)
.bind(self.status)
.bind(self.paused_operations)
.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 ConstituentRow {
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 constituent_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 ConstituentRow {
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 constituent_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 ConstituentMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ConstituentMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS constituent_account (
-- Account data
"pubkey" BYTEA NOT NULL,
"mint" BYTEA NOT NULL,
"lp_pool" BYTEA NOT NULL,
"vault" BYTEA NOT NULL,
"total_swap_fees" NUMERIC(38) NOT NULL,
"spot_balance" JSONB NOT NULL,
"last_spot_balance_token_amount" INT8 NOT NULL,
"cumulative_spot_interest_accrued_token_amount" INT8 NOT NULL,
"max_weight_deviation" INT8 NOT NULL,
"swap_fee_min" INT8 NOT NULL,
"swap_fee_max" INT8 NOT NULL,
"max_borrow_token_amount" NUMERIC(20) NOT NULL,
"vault_token_balance" NUMERIC(20) NOT NULL,
"last_oracle_price" INT8 NOT NULL,
"last_oracle_slot" NUMERIC(20) NOT NULL,
"oracle_staleness_threshold" NUMERIC(20) NOT NULL,
"flash_loan_initial_token_amount" NUMERIC(20) NOT NULL,
"next_swap_id" NUMERIC(20) NOT NULL,
"derivative_weight" NUMERIC(20) NOT NULL,
"volatility" NUMERIC(20) NOT NULL,
"constituent_derivative_depeg_threshold" NUMERIC(20) NOT NULL,
"constituent_derivative_index" INT2 NOT NULL,
"spot_market_index" INT4 NOT NULL,
"constituent_index" INT4 NOT NULL,
"decimals" INT2 NOT NULL,
"bump" INT2 NOT NULL,
"vault_bump" INT2 NOT NULL,
"gamma_inventory" INT2 NOT NULL,
"gamma_execution" INT2 NOT NULL,
"xi" INT2 NOT NULL,
"status" INT2 NOT NULL,
"paused_operations" 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 constituent_account"#)
.execute(connection)
.await?;
Ok(())
}
}