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