use {
crate::types::{AnchorDecimal, MarginAccountType, ProductLedger},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CrossMarginAccountRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub authority: Pubkey,
pub delegated_pubkey: Pubkey,
pub balance: U64,
pub subaccount_index: U8,
pub nonce: U8,
pub force_cancel_flag: bool,
pub account_type: sqlx::types::Json<MarginAccountType>,
pub open_orders_nonces: Vec<u8>,
pub open_orders_nonces_padding: Vec<u8>,
pub rebalance_amount: i64,
pub last_funding_deltas: sqlx::types::Json<Vec<AnchorDecimal>>,
pub last_funding_deltas_padding: sqlx::types::Json<Vec<AnchorDecimal>>,
pub product_ledgers: sqlx::types::Json<Vec<ProductLedger>>,
pub product_ledgers_padding: sqlx::types::Json<Vec<ProductLedger>>,
pub padding: Vec<u8>,
}
impl CrossMarginAccountRow {
pub fn from_parts(
source: crate::accounts::cross_margin_account::CrossMarginAccount,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
authority: source.authority.into(),
delegated_pubkey: source.delegated_pubkey.into(),
balance: source.balance.into(),
subaccount_index: source.subaccount_index.into(),
nonce: source.nonce.into(),
force_cancel_flag: source.force_cancel_flag,
account_type: sqlx::types::Json(source.account_type),
open_orders_nonces: source.open_orders_nonces.to_vec(),
open_orders_nonces_padding: source.open_orders_nonces_padding.to_vec(),
rebalance_amount: source.rebalance_amount,
last_funding_deltas: sqlx::types::Json(source.last_funding_deltas.to_vec()),
last_funding_deltas_padding: sqlx::types::Json(
source.last_funding_deltas_padding.to_vec(),
),
product_ledgers: sqlx::types::Json(source.product_ledgers.to_vec()),
product_ledgers_padding: sqlx::types::Json(source.product_ledgers_padding.to_vec()),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<CrossMarginAccountRow> for crate::accounts::cross_margin_account::CrossMarginAccount {
type Error = carbon_core::error::Error;
fn try_from(source: CrossMarginAccountRow) -> Result<Self, Self::Error> {
Ok(Self {
authority: *source.authority,
delegated_pubkey: *source.delegated_pubkey,
balance: *source.balance,
subaccount_index: source.subaccount_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
nonce: source.nonce.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
force_cancel_flag: source.force_cancel_flag,
account_type: source.account_type.0,
open_orders_nonces: source
.open_orders_nonces
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 5 bytes"
.to_string(),
)
})?,
open_orders_nonces_padding: source
.open_orders_nonces_padding
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 20 bytes"
.to_string(),
)
})?,
rebalance_amount: source.rebalance_amount,
last_funding_deltas: source
.last_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(),
)
})?,
last_funding_deltas_padding: source
.last_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(),
)
})?,
product_ledgers: source
.product_ledgers
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
product_ledgers_padding: source
.product_ledgers_padding
.0
.into_iter()
.collect::<Vec<_>>()
.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 2000 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::cross_margin_account::CrossMarginAccount
{
fn table() -> &'static str {
"cross_margin_account_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"authority",
"delegated_pubkey",
"balance",
"subaccount_index",
"nonce",
"force_cancel_flag",
"account_type",
"open_orders_nonces",
"open_orders_nonces_padding",
"rebalance_amount",
"last_funding_deltas",
"last_funding_deltas_padding",
"product_ledgers",
"product_ledgers_padding",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CrossMarginAccountRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO cross_margin_account_account (
"authority",
"delegated_pubkey",
"balance",
"subaccount_index",
"nonce",
"force_cancel_flag",
"account_type",
"open_orders_nonces",
"open_orders_nonces_padding",
"rebalance_amount",
"last_funding_deltas",
"last_funding_deltas_padding",
"product_ledgers",
"product_ledgers_padding",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
)"#,
)
.bind(self.authority)
.bind(self.delegated_pubkey)
.bind(&self.balance)
.bind(self.subaccount_index)
.bind(self.nonce)
.bind(self.force_cancel_flag)
.bind(&self.account_type)
.bind(&self.open_orders_nonces)
.bind(&self.open_orders_nonces_padding)
.bind(self.rebalance_amount)
.bind(&self.last_funding_deltas)
.bind(&self.last_funding_deltas_padding)
.bind(&self.product_ledgers)
.bind(&self.product_ledgers_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 CrossMarginAccountRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO cross_margin_account_account (
"authority",
"delegated_pubkey",
"balance",
"subaccount_index",
"nonce",
"force_cancel_flag",
"account_type",
"open_orders_nonces",
"open_orders_nonces_padding",
"rebalance_amount",
"last_funding_deltas",
"last_funding_deltas_padding",
"product_ledgers",
"product_ledgers_padding",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"authority" = EXCLUDED."authority",
"delegated_pubkey" = EXCLUDED."delegated_pubkey",
"balance" = EXCLUDED."balance",
"subaccount_index" = EXCLUDED."subaccount_index",
"nonce" = EXCLUDED."nonce",
"force_cancel_flag" = EXCLUDED."force_cancel_flag",
"account_type" = EXCLUDED."account_type",
"open_orders_nonces" = EXCLUDED."open_orders_nonces",
"open_orders_nonces_padding" = EXCLUDED."open_orders_nonces_padding",
"rebalance_amount" = EXCLUDED."rebalance_amount",
"last_funding_deltas" = EXCLUDED."last_funding_deltas",
"last_funding_deltas_padding" = EXCLUDED."last_funding_deltas_padding",
"product_ledgers" = EXCLUDED."product_ledgers",
"product_ledgers_padding" = EXCLUDED."product_ledgers_padding",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.authority)
.bind(self.delegated_pubkey)
.bind(&self.balance)
.bind(self.subaccount_index)
.bind(self.nonce)
.bind(self.force_cancel_flag)
.bind(&self.account_type)
.bind(&self.open_orders_nonces)
.bind(&self.open_orders_nonces_padding)
.bind(self.rebalance_amount)
.bind(&self.last_funding_deltas)
.bind(&self.last_funding_deltas_padding)
.bind(&self.product_ledgers)
.bind(&self.product_ledgers_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 CrossMarginAccountRow {
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 cross_margin_account_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 CrossMarginAccountRow {
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 cross_margin_account_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 CrossMarginAccountMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CrossMarginAccountMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS cross_margin_account_account (
-- Account data
"authority" BYTEA NOT NULL,
"delegated_pubkey" BYTEA NOT NULL,
"balance" NUMERIC(20) NOT NULL,
"subaccount_index" INT2 NOT NULL,
"nonce" INT2 NOT NULL,
"force_cancel_flag" BOOLEAN NOT NULL,
"account_type" JSONB NOT NULL,
"open_orders_nonces" BYTEA NOT NULL,
"open_orders_nonces_padding" BYTEA NOT NULL,
"rebalance_amount" INT8 NOT NULL,
"last_funding_deltas" JSONB NOT NULL,
"last_funding_deltas_padding" JSONB NOT NULL,
"product_ledgers" JSONB NOT NULL,
"product_ledgers_padding" JSONB 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 cross_margin_account_account"#)
.execute(connection)
.await?;
Ok(())
}
}