use {
crate::types::{LiquidationCache, LiquidationEntry},
carbon_core::{
account::AccountMetadata,
postgres::{metadata::AccountRowMetadata, primitives::Pubkey},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct LiquidationRecordRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub key: Pubkey,
pub marginfi_account: Pubkey,
pub record_payer: Pubkey,
pub liquidation_receiver: Pubkey,
pub entries: sqlx::types::Json<Vec<LiquidationEntry>>,
pub cache: sqlx::types::Json<LiquidationCache>,
pub reserved0: Vec<u8>,
pub reserved2: Vec<u8>,
pub reserved3: Vec<u8>,
}
impl LiquidationRecordRow {
pub fn from_parts(
source: crate::accounts::liquidation_record::LiquidationRecord,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
key: source.key.into(),
marginfi_account: source.marginfi_account.into(),
record_payer: source.record_payer.into(),
liquidation_receiver: source.liquidation_receiver.into(),
entries: sqlx::types::Json(source.entries.to_vec()),
cache: sqlx::types::Json(source.cache),
reserved0: source.reserved0.to_vec(),
reserved2: source.reserved2.to_vec(),
reserved3: source.reserved3.to_vec(),
}
}
}
impl TryFrom<LiquidationRecordRow> for crate::accounts::liquidation_record::LiquidationRecord {
type Error = carbon_core::error::Error;
fn try_from(source: LiquidationRecordRow) -> Result<Self, Self::Error> {
Ok(Self {
key: *source.key,
marginfi_account: *source.marginfi_account,
record_payer: *source.record_payer,
liquidation_receiver: *source.liquidation_receiver,
entries: source
.entries
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
cache: source.cache.0,
reserved0: source.reserved0.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 64 bytes"
.to_string(),
)
})?,
reserved2: source.reserved2.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 16 bytes"
.to_string(),
)
})?,
reserved3: source.reserved3.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 8 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::liquidation_record::LiquidationRecord
{
fn table() -> &'static str {
"liquidation_record_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"key",
"marginfi_account",
"record_payer",
"liquidation_receiver",
"entries",
"cache",
"reserved0",
"reserved2",
"reserved3",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for LiquidationRecordRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO liquidation_record_account (
"key",
"marginfi_account",
"record_payer",
"liquidation_receiver",
"entries",
"cache",
"reserved0",
"reserved2",
"reserved3",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
)"#,
)
.bind(self.key)
.bind(self.marginfi_account)
.bind(self.record_payer)
.bind(self.liquidation_receiver)
.bind(&self.entries)
.bind(&self.cache)
.bind(&self.reserved0)
.bind(&self.reserved2)
.bind(&self.reserved3)
.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 LiquidationRecordRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO liquidation_record_account (
"key",
"marginfi_account",
"record_payer",
"liquidation_receiver",
"entries",
"cache",
"reserved0",
"reserved2",
"reserved3",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"key" = EXCLUDED."key",
"marginfi_account" = EXCLUDED."marginfi_account",
"record_payer" = EXCLUDED."record_payer",
"liquidation_receiver" = EXCLUDED."liquidation_receiver",
"entries" = EXCLUDED."entries",
"cache" = EXCLUDED."cache",
"reserved0" = EXCLUDED."reserved0",
"reserved2" = EXCLUDED."reserved2",
"reserved3" = EXCLUDED."reserved3",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.key)
.bind(self.marginfi_account)
.bind(self.record_payer)
.bind(self.liquidation_receiver)
.bind(&self.entries)
.bind(&self.cache)
.bind(&self.reserved0)
.bind(&self.reserved2)
.bind(&self.reserved3)
.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 LiquidationRecordRow {
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 liquidation_record_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 LiquidationRecordRow {
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 liquidation_record_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 LiquidationRecordMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for LiquidationRecordMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS liquidation_record_account (
-- Account data
"key" BYTEA NOT NULL,
"marginfi_account" BYTEA NOT NULL,
"record_payer" BYTEA NOT NULL,
"liquidation_receiver" BYTEA NOT NULL,
"entries" JSONB NOT NULL,
"cache" JSONB NOT NULL,
"reserved0" BYTEA NOT NULL,
"reserved2" BYTEA NOT NULL,
"reserved3" 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 liquidation_record_account"#)
.execute(connection)
.await?;
Ok(())
}
}