use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128, U16, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct UserSupplyPositionRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub protocol: Pubkey,
pub mint: Pubkey,
pub with_interest: U8,
pub amount: U64,
pub withdrawal_limit: U128,
pub last_update: U64,
pub expand_pct: U16,
pub expand_duration: U64,
pub base_withdrawal_limit: U64,
pub status: U8,
}
impl UserSupplyPositionRow {
pub fn from_parts(
source: crate::accounts::user_supply_position::UserSupplyPosition,
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(),
withdrawal_limit: source.withdrawal_limit.into(),
last_update: source.last_update.into(),
expand_pct: source.expand_pct.into(),
expand_duration: source.expand_duration.into(),
base_withdrawal_limit: source.base_withdrawal_limit.into(),
status: source.status.into(),
}
}
}
impl TryFrom<UserSupplyPositionRow> for crate::accounts::user_supply_position::UserSupplyPosition {
type Error = carbon_core::error::Error;
fn try_from(source: UserSupplyPositionRow) -> 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,
withdrawal_limit: *source.withdrawal_limit,
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,
base_withdrawal_limit: *source.base_withdrawal_limit,
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_supply_position::UserSupplyPosition
{
fn table() -> &'static str {
"user_supply_position_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"protocol",
"mint",
"with_interest",
"amount",
"withdrawal_limit",
"last_update",
"expand_pct",
"expand_duration",
"base_withdrawal_limit",
"status",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for UserSupplyPositionRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO user_supply_position_account (
"protocol",
"mint",
"with_interest",
"amount",
"withdrawal_limit",
"last_update",
"expand_pct",
"expand_duration",
"base_withdrawal_limit",
"status",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
)"#,
)
.bind(self.protocol)
.bind(self.mint)
.bind(self.with_interest)
.bind(&self.amount)
.bind(&self.withdrawal_limit)
.bind(&self.last_update)
.bind(self.expand_pct)
.bind(&self.expand_duration)
.bind(&self.base_withdrawal_limit)
.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 UserSupplyPositionRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO user_supply_position_account (
"protocol",
"mint",
"with_interest",
"amount",
"withdrawal_limit",
"last_update",
"expand_pct",
"expand_duration",
"base_withdrawal_limit",
"status",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"protocol" = EXCLUDED."protocol",
"mint" = EXCLUDED."mint",
"with_interest" = EXCLUDED."with_interest",
"amount" = EXCLUDED."amount",
"withdrawal_limit" = EXCLUDED."withdrawal_limit",
"last_update" = EXCLUDED."last_update",
"expand_pct" = EXCLUDED."expand_pct",
"expand_duration" = EXCLUDED."expand_duration",
"base_withdrawal_limit" = EXCLUDED."base_withdrawal_limit",
"status" = EXCLUDED."status",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.protocol)
.bind(self.mint)
.bind(self.with_interest)
.bind(&self.amount)
.bind(&self.withdrawal_limit)
.bind(&self.last_update)
.bind(self.expand_pct)
.bind(&self.expand_duration)
.bind(&self.base_withdrawal_limit)
.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 UserSupplyPositionRow {
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_supply_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 UserSupplyPositionRow {
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_supply_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 UserSupplyPositionMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for UserSupplyPositionMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS user_supply_position_account (
-- Account data
"protocol" BYTEA NOT NULL,
"mint" BYTEA NOT NULL,
"with_interest" INT2 NOT NULL,
"amount" NUMERIC(20) NOT NULL,
"withdrawal_limit" NUMERIC(39) NOT NULL,
"last_update" NUMERIC(20) NOT NULL,
"expand_pct" INT4 NOT NULL,
"expand_duration" NUMERIC(20) NOT NULL,
"base_withdrawal_limit" 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_supply_position_account"#)
.execute(connection)
.await?;
Ok(())
}
}