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