use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U32, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct UserBorrowPositionRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub protocol: Pubkey,
pub mint: Pubkey,
pub with_interest: U8,
pub amount: U64,
pub debt_ceiling: U64,
pub last_update: U64,
pub expand_pct: U16,
pub expand_duration: U32,
pub base_debt_ceiling: U64,
pub max_debt_ceiling: U64,
pub status: U8,
}
impl UserBorrowPositionRow {
pub fn from_parts(
source: crate::accounts::user_borrow_position::UserBorrowPosition,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
protocol: source.protocol.into(),
mint: source.mint.into(),
with_interest: source.with_interest.into(),
amount: source.amount.into(),
debt_ceiling: source.debt_ceiling.into(),
last_update: source.last_update.into(),
expand_pct: source.expand_pct.into(),
expand_duration: source.expand_duration.into(),
base_debt_ceiling: source.base_debt_ceiling.into(),
max_debt_ceiling: source.max_debt_ceiling.into(),
status: source.status.into(),
}
}
}
impl TryFrom<UserBorrowPositionRow> for crate::accounts::user_borrow_position::UserBorrowPosition {
type Error = carbon_core::error::Error;
fn try_from(source: UserBorrowPositionRow) -> Result<Self, Self::Error> {
Ok(Self {
protocol: *source.protocol,
mint: *source.mint,
with_interest: source.with_interest.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
amount: *source.amount,
debt_ceiling: *source.debt_ceiling,
last_update: *source.last_update,
expand_pct: source.expand_pct.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
expand_duration: source.expand_duration.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
base_debt_ceiling: *source.base_debt_ceiling,
max_debt_ceiling: *source.max_debt_ceiling,
status: source.status.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::user_borrow_position::UserBorrowPosition
{
fn table() -> &'static str {
"user_borrow_position_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"protocol",
"mint",
"with_interest",
"amount",
"debt_ceiling",
"last_update",
"expand_pct",
"expand_duration",
"base_debt_ceiling",
"max_debt_ceiling",
"status",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for UserBorrowPositionRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO user_borrow_position_account (
"protocol",
"mint",
"with_interest",
"amount",
"debt_ceiling",
"last_update",
"expand_pct",
"expand_duration",
"base_debt_ceiling",
"max_debt_ceiling",
"status",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
)"#,
)
.bind(self.protocol)
.bind(self.mint)
.bind(self.with_interest)
.bind(&self.amount)
.bind(&self.debt_ceiling)
.bind(&self.last_update)
.bind(self.expand_pct)
.bind(self.expand_duration)
.bind(&self.base_debt_ceiling)
.bind(&self.max_debt_ceiling)
.bind(self.status)
.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 UserBorrowPositionRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO user_borrow_position_account (
"protocol",
"mint",
"with_interest",
"amount",
"debt_ceiling",
"last_update",
"expand_pct",
"expand_duration",
"base_debt_ceiling",
"max_debt_ceiling",
"status",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"protocol" = EXCLUDED."protocol",
"mint" = EXCLUDED."mint",
"with_interest" = EXCLUDED."with_interest",
"amount" = EXCLUDED."amount",
"debt_ceiling" = EXCLUDED."debt_ceiling",
"last_update" = EXCLUDED."last_update",
"expand_pct" = EXCLUDED."expand_pct",
"expand_duration" = EXCLUDED."expand_duration",
"base_debt_ceiling" = EXCLUDED."base_debt_ceiling",
"max_debt_ceiling" = EXCLUDED."max_debt_ceiling",
"status" = EXCLUDED."status",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.protocol)
.bind(self.mint)
.bind(self.with_interest)
.bind(&self.amount)
.bind(&self.debt_ceiling)
.bind(&self.last_update)
.bind(self.expand_pct)
.bind(self.expand_duration)
.bind(&self.base_debt_ceiling)
.bind(&self.max_debt_ceiling)
.bind(self.status)
.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 UserBorrowPositionRow {
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 user_borrow_position_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 UserBorrowPositionRow {
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 user_borrow_position_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 UserBorrowPositionMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for UserBorrowPositionMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS user_borrow_position_account (
-- Account data
"protocol" BYTEA NOT NULL,
"mint" BYTEA NOT NULL,
"with_interest" INT2 NOT NULL,
"amount" NUMERIC(20) NOT NULL,
"debt_ceiling" NUMERIC(20) NOT NULL,
"last_update" NUMERIC(20) NOT NULL,
"expand_pct" INT4 NOT NULL,
"expand_duration" INT8 NOT NULL,
"base_debt_ceiling" NUMERIC(20) NOT NULL,
"max_debt_ceiling" NUMERIC(20) NOT NULL,
"status" INT2 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 user_borrow_position_account"#)
.execute(connection)
.await?;
Ok(())
}
}