use {
crate::types::{AnchorDecimal, MarginParameters, PerpParameters, Product},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PricingRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub nonce: U8,
pub mark_prices: Vec<U64>,
pub mark_prices_padding: Vec<U64>,
pub update_timestamps: Vec<U64>,
pub update_timestamps_padding: Vec<U64>,
pub funding_deltas: sqlx::types::Json<Vec<AnchorDecimal>>,
pub funding_deltas_padding: sqlx::types::Json<Vec<AnchorDecimal>>,
pub latest_funding_rates: sqlx::types::Json<Vec<AnchorDecimal>>,
pub latest_funding_rates_padding: sqlx::types::Json<Vec<AnchorDecimal>>,
pub latest_midpoints: Vec<U64>,
pub latest_midpoints_padding: Vec<U64>,
pub oracles: Vec<Pubkey>,
pub oracles_padding: Vec<Pubkey>,
pub oracle_backup_feeds: Vec<Pubkey>,
pub oracle_backup_feeds_padding: Vec<Pubkey>,
pub markets: Vec<Pubkey>,
pub markets_padding: Vec<Pubkey>,
pub perp_sync_queues: Vec<Pubkey>,
pub perp_sync_queues_padding: Vec<Pubkey>,
pub perp_parameters: sqlx::types::Json<Vec<PerpParameters>>,
pub perp_parameters_padding: sqlx::types::Json<Vec<PerpParameters>>,
pub margin_parameters: sqlx::types::Json<Vec<MarginParameters>>,
pub margin_parameters_padding: sqlx::types::Json<Vec<MarginParameters>>,
pub products: sqlx::types::Json<Vec<Product>>,
pub products_padding: sqlx::types::Json<Vec<Product>>,
pub zeta_group_keys: Vec<Pubkey>,
pub zeta_group_keys_padding: Vec<Pubkey>,
pub total_insurance_vault_deposits: U64,
pub last_withdraw_timestamp: U64,
pub net_outflow_sum: i64,
pub halt_force_pricing: Vec<bool>,
pub halt_force_pricing_padding: Vec<bool>,
pub padding: Vec<u8>,
}
impl PricingRow {
pub fn from_parts(
source: crate::accounts::pricing::Pricing,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
nonce: source.nonce.into(),
mark_prices: source
.mark_prices
.into_iter()
.map(|element| element.into())
.collect(),
mark_prices_padding: source
.mark_prices_padding
.into_iter()
.map(|element| element.into())
.collect(),
update_timestamps: source
.update_timestamps
.into_iter()
.map(|element| element.into())
.collect(),
update_timestamps_padding: source
.update_timestamps_padding
.into_iter()
.map(|element| element.into())
.collect(),
funding_deltas: sqlx::types::Json(source.funding_deltas.to_vec()),
funding_deltas_padding: sqlx::types::Json(source.funding_deltas_padding.to_vec()),
latest_funding_rates: sqlx::types::Json(source.latest_funding_rates.to_vec()),
latest_funding_rates_padding: sqlx::types::Json(
source.latest_funding_rates_padding.to_vec(),
),
latest_midpoints: source
.latest_midpoints
.into_iter()
.map(|element| element.into())
.collect(),
latest_midpoints_padding: source
.latest_midpoints_padding
.into_iter()
.map(|element| element.into())
.collect(),
oracles: source
.oracles
.into_iter()
.map(|element| element.into())
.collect(),
oracles_padding: source
.oracles_padding
.into_iter()
.map(|element| element.into())
.collect(),
oracle_backup_feeds: source
.oracle_backup_feeds
.into_iter()
.map(|element| element.into())
.collect(),
oracle_backup_feeds_padding: source
.oracle_backup_feeds_padding
.into_iter()
.map(|element| element.into())
.collect(),
markets: source
.markets
.into_iter()
.map(|element| element.into())
.collect(),
markets_padding: source
.markets_padding
.into_iter()
.map(|element| element.into())
.collect(),
perp_sync_queues: source
.perp_sync_queues
.into_iter()
.map(|element| element.into())
.collect(),
perp_sync_queues_padding: source
.perp_sync_queues_padding
.into_iter()
.map(|element| element.into())
.collect(),
perp_parameters: sqlx::types::Json(source.perp_parameters.to_vec()),
perp_parameters_padding: sqlx::types::Json(source.perp_parameters_padding.to_vec()),
margin_parameters: sqlx::types::Json(source.margin_parameters.to_vec()),
margin_parameters_padding: sqlx::types::Json(source.margin_parameters_padding.to_vec()),
products: sqlx::types::Json(source.products.to_vec()),
products_padding: sqlx::types::Json(source.products_padding.to_vec()),
zeta_group_keys: source
.zeta_group_keys
.into_iter()
.map(|element| element.into())
.collect(),
zeta_group_keys_padding: source
.zeta_group_keys_padding
.into_iter()
.map(|element| element.into())
.collect(),
total_insurance_vault_deposits: source.total_insurance_vault_deposits.into(),
last_withdraw_timestamp: source.last_withdraw_timestamp.into(),
net_outflow_sum: source.net_outflow_sum,
halt_force_pricing: source.halt_force_pricing.to_vec(),
halt_force_pricing_padding: source.halt_force_pricing_padding.to_vec(),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<PricingRow> for crate::accounts::pricing::Pricing {
type Error = carbon_core::error::Error;
fn try_from(source: PricingRow) -> Result<Self, Self::Error> {
Ok(Self {
nonce: source.nonce.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
mark_prices: source
.mark_prices
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
mark_prices_padding: source
.mark_prices_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
update_timestamps: source
.update_timestamps
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
update_timestamps_padding: source
.update_timestamps_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
funding_deltas: source
.funding_deltas
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
funding_deltas_padding: source
.funding_deltas_padding
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
latest_funding_rates: source
.latest_funding_rates
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
latest_funding_rates_padding: source
.latest_funding_rates_padding
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
latest_midpoints: source
.latest_midpoints
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
latest_midpoints_padding: source
.latest_midpoints_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
oracles: source
.oracles
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
oracles_padding: source
.oracles_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
oracle_backup_feeds: source
.oracle_backup_feeds
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
oracle_backup_feeds_padding: source
.oracle_backup_feeds_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
markets: source
.markets
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
markets_padding: source
.markets_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
perp_sync_queues: source
.perp_sync_queues
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
perp_sync_queues_padding: source
.perp_sync_queues_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
perp_parameters: source
.perp_parameters
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
perp_parameters_padding: source
.perp_parameters_padding
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
margin_parameters: source
.margin_parameters
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
margin_parameters_padding: source
.margin_parameters_padding
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
products: source
.products
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
products_padding: source
.products_padding
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
zeta_group_keys: source
.zeta_group_keys
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
zeta_group_keys_padding: source
.zeta_group_keys_padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
total_insurance_vault_deposits: *source.total_insurance_vault_deposits,
last_withdraw_timestamp: *source.last_withdraw_timestamp,
net_outflow_sum: source.net_outflow_sum,
halt_force_pricing: source
.halt_force_pricing
.into_iter()
.map(Ok)
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
halt_force_pricing_padding: source
.halt_force_pricing_padding
.into_iter()
.map(Ok)
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to 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 2707 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::pricing::Pricing {
fn table() -> &'static str {
"pricing_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"nonce",
"mark_prices",
"mark_prices_padding",
"update_timestamps",
"update_timestamps_padding",
"funding_deltas",
"funding_deltas_padding",
"latest_funding_rates",
"latest_funding_rates_padding",
"latest_midpoints",
"latest_midpoints_padding",
"oracles",
"oracles_padding",
"oracle_backup_feeds",
"oracle_backup_feeds_padding",
"markets",
"markets_padding",
"perp_sync_queues",
"perp_sync_queues_padding",
"perp_parameters",
"perp_parameters_padding",
"margin_parameters",
"margin_parameters_padding",
"products",
"products_padding",
"zeta_group_keys",
"zeta_group_keys_padding",
"total_insurance_vault_deposits",
"last_withdraw_timestamp",
"net_outflow_sum",
"halt_force_pricing",
"halt_force_pricing_padding",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PricingRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO pricing_account (
"nonce",
"mark_prices",
"mark_prices_padding",
"update_timestamps",
"update_timestamps_padding",
"funding_deltas",
"funding_deltas_padding",
"latest_funding_rates",
"latest_funding_rates_padding",
"latest_midpoints",
"latest_midpoints_padding",
"oracles",
"oracles_padding",
"oracle_backup_feeds",
"oracle_backup_feeds_padding",
"markets",
"markets_padding",
"perp_sync_queues",
"perp_sync_queues_padding",
"perp_parameters",
"perp_parameters_padding",
"margin_parameters",
"margin_parameters_padding",
"products",
"products_padding",
"zeta_group_keys",
"zeta_group_keys_padding",
"total_insurance_vault_deposits",
"last_withdraw_timestamp",
"net_outflow_sum",
"halt_force_pricing",
"halt_force_pricing_padding",
"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.nonce)
.bind(&self.mark_prices)
.bind(&self.mark_prices_padding)
.bind(&self.update_timestamps)
.bind(&self.update_timestamps_padding)
.bind(&self.funding_deltas)
.bind(&self.funding_deltas_padding)
.bind(&self.latest_funding_rates)
.bind(&self.latest_funding_rates_padding)
.bind(&self.latest_midpoints)
.bind(&self.latest_midpoints_padding)
.bind(&self.oracles)
.bind(&self.oracles_padding)
.bind(&self.oracle_backup_feeds)
.bind(&self.oracle_backup_feeds_padding)
.bind(&self.markets)
.bind(&self.markets_padding)
.bind(&self.perp_sync_queues)
.bind(&self.perp_sync_queues_padding)
.bind(&self.perp_parameters)
.bind(&self.perp_parameters_padding)
.bind(&self.margin_parameters)
.bind(&self.margin_parameters_padding)
.bind(&self.products)
.bind(&self.products_padding)
.bind(&self.zeta_group_keys)
.bind(&self.zeta_group_keys_padding)
.bind(&self.total_insurance_vault_deposits)
.bind(&self.last_withdraw_timestamp)
.bind(self.net_outflow_sum)
.bind(&self.halt_force_pricing)
.bind(&self.halt_force_pricing_padding)
.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 PricingRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO pricing_account (
"nonce",
"mark_prices",
"mark_prices_padding",
"update_timestamps",
"update_timestamps_padding",
"funding_deltas",
"funding_deltas_padding",
"latest_funding_rates",
"latest_funding_rates_padding",
"latest_midpoints",
"latest_midpoints_padding",
"oracles",
"oracles_padding",
"oracle_backup_feeds",
"oracle_backup_feeds_padding",
"markets",
"markets_padding",
"perp_sync_queues",
"perp_sync_queues_padding",
"perp_parameters",
"perp_parameters_padding",
"margin_parameters",
"margin_parameters_padding",
"products",
"products_padding",
"zeta_group_keys",
"zeta_group_keys_padding",
"total_insurance_vault_deposits",
"last_withdraw_timestamp",
"net_outflow_sum",
"halt_force_pricing",
"halt_force_pricing_padding",
"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
"nonce" = EXCLUDED."nonce",
"mark_prices" = EXCLUDED."mark_prices",
"mark_prices_padding" = EXCLUDED."mark_prices_padding",
"update_timestamps" = EXCLUDED."update_timestamps",
"update_timestamps_padding" = EXCLUDED."update_timestamps_padding",
"funding_deltas" = EXCLUDED."funding_deltas",
"funding_deltas_padding" = EXCLUDED."funding_deltas_padding",
"latest_funding_rates" = EXCLUDED."latest_funding_rates",
"latest_funding_rates_padding" = EXCLUDED."latest_funding_rates_padding",
"latest_midpoints" = EXCLUDED."latest_midpoints",
"latest_midpoints_padding" = EXCLUDED."latest_midpoints_padding",
"oracles" = EXCLUDED."oracles",
"oracles_padding" = EXCLUDED."oracles_padding",
"oracle_backup_feeds" = EXCLUDED."oracle_backup_feeds",
"oracle_backup_feeds_padding" = EXCLUDED."oracle_backup_feeds_padding",
"markets" = EXCLUDED."markets",
"markets_padding" = EXCLUDED."markets_padding",
"perp_sync_queues" = EXCLUDED."perp_sync_queues",
"perp_sync_queues_padding" = EXCLUDED."perp_sync_queues_padding",
"perp_parameters" = EXCLUDED."perp_parameters",
"perp_parameters_padding" = EXCLUDED."perp_parameters_padding",
"margin_parameters" = EXCLUDED."margin_parameters",
"margin_parameters_padding" = EXCLUDED."margin_parameters_padding",
"products" = EXCLUDED."products",
"products_padding" = EXCLUDED."products_padding",
"zeta_group_keys" = EXCLUDED."zeta_group_keys",
"zeta_group_keys_padding" = EXCLUDED."zeta_group_keys_padding",
"total_insurance_vault_deposits" = EXCLUDED."total_insurance_vault_deposits",
"last_withdraw_timestamp" = EXCLUDED."last_withdraw_timestamp",
"net_outflow_sum" = EXCLUDED."net_outflow_sum",
"halt_force_pricing" = EXCLUDED."halt_force_pricing",
"halt_force_pricing_padding" = EXCLUDED."halt_force_pricing_padding",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#)
.bind(self.nonce)
.bind(&self.mark_prices)
.bind(&self.mark_prices_padding)
.bind(&self.update_timestamps)
.bind(&self.update_timestamps_padding)
.bind(&self.funding_deltas)
.bind(&self.funding_deltas_padding)
.bind(&self.latest_funding_rates)
.bind(&self.latest_funding_rates_padding)
.bind(&self.latest_midpoints)
.bind(&self.latest_midpoints_padding)
.bind(&self.oracles)
.bind(&self.oracles_padding)
.bind(&self.oracle_backup_feeds)
.bind(&self.oracle_backup_feeds_padding)
.bind(&self.markets)
.bind(&self.markets_padding)
.bind(&self.perp_sync_queues)
.bind(&self.perp_sync_queues_padding)
.bind(&self.perp_parameters)
.bind(&self.perp_parameters_padding)
.bind(&self.margin_parameters)
.bind(&self.margin_parameters_padding)
.bind(&self.products)
.bind(&self.products_padding)
.bind(&self.zeta_group_keys)
.bind(&self.zeta_group_keys_padding)
.bind(&self.total_insurance_vault_deposits)
.bind(&self.last_withdraw_timestamp)
.bind(self.net_outflow_sum)
.bind(&self.halt_force_pricing)
.bind(&self.halt_force_pricing_padding)
.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 PricingRow {
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 pricing_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 PricingRow {
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 pricing_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 PricingMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PricingMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS pricing_account (
-- Account data
"nonce" INT2 NOT NULL,
"mark_prices" NUMERIC(20)[] NOT NULL,
"mark_prices_padding" NUMERIC(20)[] NOT NULL,
"update_timestamps" NUMERIC(20)[] NOT NULL,
"update_timestamps_padding" NUMERIC(20)[] NOT NULL,
"funding_deltas" JSONB NOT NULL,
"funding_deltas_padding" JSONB NOT NULL,
"latest_funding_rates" JSONB NOT NULL,
"latest_funding_rates_padding" JSONB NOT NULL,
"latest_midpoints" NUMERIC(20)[] NOT NULL,
"latest_midpoints_padding" NUMERIC(20)[] NOT NULL,
"oracles" BYTEA[] NOT NULL,
"oracles_padding" BYTEA[] NOT NULL,
"oracle_backup_feeds" BYTEA[] NOT NULL,
"oracle_backup_feeds_padding" BYTEA[] NOT NULL,
"markets" BYTEA[] NOT NULL,
"markets_padding" BYTEA[] NOT NULL,
"perp_sync_queues" BYTEA[] NOT NULL,
"perp_sync_queues_padding" BYTEA[] NOT NULL,
"perp_parameters" JSONB NOT NULL,
"perp_parameters_padding" JSONB NOT NULL,
"margin_parameters" JSONB NOT NULL,
"margin_parameters_padding" JSONB NOT NULL,
"products" JSONB NOT NULL,
"products_padding" JSONB NOT NULL,
"zeta_group_keys" BYTEA[] NOT NULL,
"zeta_group_keys_padding" BYTEA[] NOT NULL,
"total_insurance_vault_deposits" NUMERIC(20) NOT NULL,
"last_withdraw_timestamp" NUMERIC(20) NOT NULL,
"net_outflow_sum" INT8 NOT NULL,
"halt_force_pricing" BOOLEAN[] NOT NULL,
"halt_force_pricing_padding" BOOLEAN[] 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 pricing_account"#)
.execute(connection)
.await?;
Ok(())
}
}