use {
crate::types::{BookLoanTerms, OrderBookType, APY},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct OrderBookRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub version: U8,
pub order_book_type: sqlx::types::Json<OrderBookType>,
pub apy: sqlx::types::Json<APY>,
pub loan_terms: sqlx::types::Json<BookLoanTerms>,
pub fee_permillicentage: U16,
pub fee_authority: Pubkey,
}
impl OrderBookRow {
pub fn from_parts(
source: crate::accounts::order_book::OrderBook,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
version: source.version.into(),
order_book_type: sqlx::types::Json(source.order_book_type),
apy: sqlx::types::Json(source.apy),
loan_terms: sqlx::types::Json(source.loan_terms),
fee_permillicentage: source.fee_permillicentage.into(),
fee_authority: source.fee_authority.into(),
}
}
}
impl TryFrom<OrderBookRow> for crate::accounts::order_book::OrderBook {
type Error = carbon_core::error::Error;
fn try_from(source: OrderBookRow) -> Result<Self, Self::Error> {
Ok(Self {
version: source.version.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
order_book_type: source.order_book_type.0,
apy: source.apy.0,
loan_terms: source.loan_terms.0,
fee_permillicentage: source.fee_permillicentage.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
fee_authority: *source.fee_authority,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::order_book::OrderBook {
fn table() -> &'static str {
"order_book_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"version",
"order_book_type",
"apy",
"loan_terms",
"fee_permillicentage",
"fee_authority",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for OrderBookRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO order_book_account (
"version",
"order_book_type",
"apy",
"loan_terms",
"fee_permillicentage",
"fee_authority",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)"#,
)
.bind(self.version)
.bind(&self.order_book_type)
.bind(&self.apy)
.bind(&self.loan_terms)
.bind(self.fee_permillicentage)
.bind(self.fee_authority)
.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 OrderBookRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO order_book_account (
"version",
"order_book_type",
"apy",
"loan_terms",
"fee_permillicentage",
"fee_authority",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"version" = EXCLUDED."version",
"order_book_type" = EXCLUDED."order_book_type",
"apy" = EXCLUDED."apy",
"loan_terms" = EXCLUDED."loan_terms",
"fee_permillicentage" = EXCLUDED."fee_permillicentage",
"fee_authority" = EXCLUDED."fee_authority",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.version)
.bind(&self.order_book_type)
.bind(&self.apy)
.bind(&self.loan_terms)
.bind(self.fee_permillicentage)
.bind(self.fee_authority)
.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 OrderBookRow {
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 order_book_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 OrderBookRow {
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 order_book_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 OrderBookMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for OrderBookMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS order_book_account (
-- Account data
"version" INT2 NOT NULL,
"order_book_type" JSONB NOT NULL,
"apy" JSONB NOT NULL,
"loan_terms" JSONB NOT NULL,
"fee_permillicentage" INT4 NOT NULL,
"fee_authority" 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 order_book_account"#)
.execute(connection)
.await?;
Ok(())
}
}