use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SolendMinimalReserveRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub last_update_slot: U64,
pub last_update_stale: U8,
pub lending_market: Pubkey,
pub liquidity_mint_pubkey: Pubkey,
pub liquidity_mint_decimals: U8,
pub liquidity_supply_pubkey: Pubkey,
pub liquidity_pyth_oracle_pubkey: Pubkey,
pub liquidity_switchboard_oracle_pubkey: Pubkey,
pub liquidity_available_amount: U64,
pub liquidity_borrowed_amount_wads: Vec<u8>,
pub liquidity_cumulative_borrow_rate_wads: Vec<u8>,
pub liquidity_market_price: Vec<u8>,
pub collateral_mint_pubkey: Pubkey,
pub collateral_mint_total_supply: U64,
pub collateral_supply_pubkey: Pubkey,
pub config_optimal_utilization_rate: U8,
pub config_loan_to_value_ratio: U8,
pub config_liquidation_bonus: U8,
pub config_liquidation_threshold: U8,
pub padding_to_fees64: Vec<u8>,
pub padding_to_fees6: Vec<u8>,
pub liquidity_accumulated_protocol_fees_wads: Vec<u8>,
pub padding_final128: Vec<u8>,
pub padding_final64: Vec<u8>,
pub padding_final32: Vec<u8>,
pub padding_final6: Vec<u8>,
}
impl SolendMinimalReserveRow {
pub fn from_parts(
source: crate::accounts::solend_minimal_reserve::SolendMinimalReserve,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
last_update_slot: source.last_update_slot.into(),
last_update_stale: source.last_update_stale.into(),
lending_market: source.lending_market.into(),
liquidity_mint_pubkey: source.liquidity_mint_pubkey.into(),
liquidity_mint_decimals: source.liquidity_mint_decimals.into(),
liquidity_supply_pubkey: source.liquidity_supply_pubkey.into(),
liquidity_pyth_oracle_pubkey: source.liquidity_pyth_oracle_pubkey.into(),
liquidity_switchboard_oracle_pubkey: source.liquidity_switchboard_oracle_pubkey.into(),
liquidity_available_amount: source.liquidity_available_amount.into(),
liquidity_borrowed_amount_wads: source.liquidity_borrowed_amount_wads.to_vec(),
liquidity_cumulative_borrow_rate_wads: source
.liquidity_cumulative_borrow_rate_wads
.to_vec(),
liquidity_market_price: source.liquidity_market_price.to_vec(),
collateral_mint_pubkey: source.collateral_mint_pubkey.into(),
collateral_mint_total_supply: source.collateral_mint_total_supply.into(),
collateral_supply_pubkey: source.collateral_supply_pubkey.into(),
config_optimal_utilization_rate: source.config_optimal_utilization_rate.into(),
config_loan_to_value_ratio: source.config_loan_to_value_ratio.into(),
config_liquidation_bonus: source.config_liquidation_bonus.into(),
config_liquidation_threshold: source.config_liquidation_threshold.into(),
padding_to_fees64: source.padding_to_fees64.to_vec(),
padding_to_fees6: source.padding_to_fees6.to_vec(),
liquidity_accumulated_protocol_fees_wads: source
.liquidity_accumulated_protocol_fees_wads
.to_vec(),
padding_final128: source.padding_final128.to_vec(),
padding_final64: source.padding_final64.to_vec(),
padding_final32: source.padding_final32.to_vec(),
padding_final6: source.padding_final6.to_vec(),
}
}
}
impl TryFrom<SolendMinimalReserveRow>
for crate::accounts::solend_minimal_reserve::SolendMinimalReserve
{
type Error = carbon_core::error::Error;
fn try_from(source: SolendMinimalReserveRow) -> Result<Self, Self::Error> {
Ok(Self {
last_update_slot: *source.last_update_slot,
last_update_stale: source.last_update_stale.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
lending_market: *source.lending_market,
liquidity_mint_pubkey: *source.liquidity_mint_pubkey,
liquidity_mint_decimals: source.liquidity_mint_decimals.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
liquidity_supply_pubkey: *source.liquidity_supply_pubkey,
liquidity_pyth_oracle_pubkey: *source.liquidity_pyth_oracle_pubkey,
liquidity_switchboard_oracle_pubkey: *source.liquidity_switchboard_oracle_pubkey,
liquidity_available_amount: *source.liquidity_available_amount,
liquidity_borrowed_amount_wads: source
.liquidity_borrowed_amount_wads
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 16 bytes"
.to_string(),
)
})?,
liquidity_cumulative_borrow_rate_wads: source
.liquidity_cumulative_borrow_rate_wads
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 16 bytes"
.to_string(),
)
})?,
liquidity_market_price: source
.liquidity_market_price
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 16 bytes"
.to_string(),
)
})?,
collateral_mint_pubkey: *source.collateral_mint_pubkey,
collateral_mint_total_supply: *source.collateral_mint_total_supply,
collateral_supply_pubkey: *source.collateral_supply_pubkey,
config_optimal_utilization_rate: source
.config_optimal_utilization_rate
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
config_loan_to_value_ratio: source.config_loan_to_value_ratio.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
config_liquidation_bonus: source.config_liquidation_bonus.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
config_liquidation_threshold: source.config_liquidation_threshold.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
padding_to_fees64: source
.padding_to_fees64
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 64 bytes"
.to_string(),
)
})?,
padding_to_fees6: source.padding_to_fees6.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 6 bytes"
.to_string(),
)
})?,
liquidity_accumulated_protocol_fees_wads: source
.liquidity_accumulated_protocol_fees_wads
.as_slice()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 16 bytes"
.to_string(),
)
})?,
padding_final128: source.padding_final128.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 128 bytes"
.to_string(),
)
})?,
padding_final64: source.padding_final64.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 64 bytes"
.to_string(),
)
})?,
padding_final32: source.padding_final32.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 32 bytes"
.to_string(),
)
})?,
padding_final6: source.padding_final6.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 6 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::solend_minimal_reserve::SolendMinimalReserve
{
fn table() -> &'static str {
"solend_minimal_reserve_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"last_update_slot",
"last_update_stale",
"lending_market",
"liquidity_mint_pubkey",
"liquidity_mint_decimals",
"liquidity_supply_pubkey",
"liquidity_pyth_oracle_pubkey",
"liquidity_switchboard_oracle_pubkey",
"liquidity_available_amount",
"liquidity_borrowed_amount_wads",
"liquidity_cumulative_borrow_rate_wads",
"liquidity_market_price",
"collateral_mint_pubkey",
"collateral_mint_total_supply",
"collateral_supply_pubkey",
"config_optimal_utilization_rate",
"config_loan_to_value_ratio",
"config_liquidation_bonus",
"config_liquidation_threshold",
"padding_to_fees64",
"padding_to_fees6",
"liquidity_accumulated_protocol_fees_wads",
"padding_final128",
"padding_final64",
"padding_final32",
"padding_final6",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SolendMinimalReserveRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO solend_minimal_reserve_account (
"last_update_slot",
"last_update_stale",
"lending_market",
"liquidity_mint_pubkey",
"liquidity_mint_decimals",
"liquidity_supply_pubkey",
"liquidity_pyth_oracle_pubkey",
"liquidity_switchboard_oracle_pubkey",
"liquidity_available_amount",
"liquidity_borrowed_amount_wads",
"liquidity_cumulative_borrow_rate_wads",
"liquidity_market_price",
"collateral_mint_pubkey",
"collateral_mint_total_supply",
"collateral_supply_pubkey",
"config_optimal_utilization_rate",
"config_loan_to_value_ratio",
"config_liquidation_bonus",
"config_liquidation_threshold",
"padding_to_fees64",
"padding_to_fees6",
"liquidity_accumulated_protocol_fees_wads",
"padding_final128",
"padding_final64",
"padding_final32",
"padding_final6",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28
)"#)
.bind(&self.last_update_slot)
.bind(self.last_update_stale)
.bind(self.lending_market)
.bind(self.liquidity_mint_pubkey)
.bind(self.liquidity_mint_decimals)
.bind(self.liquidity_supply_pubkey)
.bind(self.liquidity_pyth_oracle_pubkey)
.bind(self.liquidity_switchboard_oracle_pubkey)
.bind(&self.liquidity_available_amount)
.bind(&self.liquidity_borrowed_amount_wads)
.bind(&self.liquidity_cumulative_borrow_rate_wads)
.bind(&self.liquidity_market_price)
.bind(self.collateral_mint_pubkey)
.bind(&self.collateral_mint_total_supply)
.bind(self.collateral_supply_pubkey)
.bind(self.config_optimal_utilization_rate)
.bind(self.config_loan_to_value_ratio)
.bind(self.config_liquidation_bonus)
.bind(self.config_liquidation_threshold)
.bind(&self.padding_to_fees64)
.bind(&self.padding_to_fees6)
.bind(&self.liquidity_accumulated_protocol_fees_wads)
.bind(&self.padding_final128)
.bind(&self.padding_final64)
.bind(&self.padding_final32)
.bind(&self.padding_final6)
.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 SolendMinimalReserveRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO solend_minimal_reserve_account (
"last_update_slot",
"last_update_stale",
"lending_market",
"liquidity_mint_pubkey",
"liquidity_mint_decimals",
"liquidity_supply_pubkey",
"liquidity_pyth_oracle_pubkey",
"liquidity_switchboard_oracle_pubkey",
"liquidity_available_amount",
"liquidity_borrowed_amount_wads",
"liquidity_cumulative_borrow_rate_wads",
"liquidity_market_price",
"collateral_mint_pubkey",
"collateral_mint_total_supply",
"collateral_supply_pubkey",
"config_optimal_utilization_rate",
"config_loan_to_value_ratio",
"config_liquidation_bonus",
"config_liquidation_threshold",
"padding_to_fees64",
"padding_to_fees6",
"liquidity_accumulated_protocol_fees_wads",
"padding_final128",
"padding_final64",
"padding_final32",
"padding_final6",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"last_update_slot" = EXCLUDED."last_update_slot",
"last_update_stale" = EXCLUDED."last_update_stale",
"lending_market" = EXCLUDED."lending_market",
"liquidity_mint_pubkey" = EXCLUDED."liquidity_mint_pubkey",
"liquidity_mint_decimals" = EXCLUDED."liquidity_mint_decimals",
"liquidity_supply_pubkey" = EXCLUDED."liquidity_supply_pubkey",
"liquidity_pyth_oracle_pubkey" = EXCLUDED."liquidity_pyth_oracle_pubkey",
"liquidity_switchboard_oracle_pubkey" = EXCLUDED."liquidity_switchboard_oracle_pubkey",
"liquidity_available_amount" = EXCLUDED."liquidity_available_amount",
"liquidity_borrowed_amount_wads" = EXCLUDED."liquidity_borrowed_amount_wads",
"liquidity_cumulative_borrow_rate_wads" = EXCLUDED."liquidity_cumulative_borrow_rate_wads",
"liquidity_market_price" = EXCLUDED."liquidity_market_price",
"collateral_mint_pubkey" = EXCLUDED."collateral_mint_pubkey",
"collateral_mint_total_supply" = EXCLUDED."collateral_mint_total_supply",
"collateral_supply_pubkey" = EXCLUDED."collateral_supply_pubkey",
"config_optimal_utilization_rate" = EXCLUDED."config_optimal_utilization_rate",
"config_loan_to_value_ratio" = EXCLUDED."config_loan_to_value_ratio",
"config_liquidation_bonus" = EXCLUDED."config_liquidation_bonus",
"config_liquidation_threshold" = EXCLUDED."config_liquidation_threshold",
"padding_to_fees64" = EXCLUDED."padding_to_fees64",
"padding_to_fees6" = EXCLUDED."padding_to_fees6",
"liquidity_accumulated_protocol_fees_wads" = EXCLUDED."liquidity_accumulated_protocol_fees_wads",
"padding_final128" = EXCLUDED."padding_final128",
"padding_final64" = EXCLUDED."padding_final64",
"padding_final32" = EXCLUDED."padding_final32",
"padding_final6" = EXCLUDED."padding_final6",
__slot = EXCLUDED.__slot
"#)
.bind(&self.last_update_slot)
.bind(self.last_update_stale)
.bind(self.lending_market)
.bind(self.liquidity_mint_pubkey)
.bind(self.liquidity_mint_decimals)
.bind(self.liquidity_supply_pubkey)
.bind(self.liquidity_pyth_oracle_pubkey)
.bind(self.liquidity_switchboard_oracle_pubkey)
.bind(&self.liquidity_available_amount)
.bind(&self.liquidity_borrowed_amount_wads)
.bind(&self.liquidity_cumulative_borrow_rate_wads)
.bind(&self.liquidity_market_price)
.bind(self.collateral_mint_pubkey)
.bind(&self.collateral_mint_total_supply)
.bind(self.collateral_supply_pubkey)
.bind(self.config_optimal_utilization_rate)
.bind(self.config_loan_to_value_ratio)
.bind(self.config_liquidation_bonus)
.bind(self.config_liquidation_threshold)
.bind(&self.padding_to_fees64)
.bind(&self.padding_to_fees6)
.bind(&self.liquidity_accumulated_protocol_fees_wads)
.bind(&self.padding_final128)
.bind(&self.padding_final64)
.bind(&self.padding_final32)
.bind(&self.padding_final6)
.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 SolendMinimalReserveRow {
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 solend_minimal_reserve_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 SolendMinimalReserveRow {
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 solend_minimal_reserve_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 SolendMinimalReserveMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SolendMinimalReserveMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS solend_minimal_reserve_account (
-- Account data
"last_update_slot" NUMERIC(20) NOT NULL,
"last_update_stale" INT2 NOT NULL,
"lending_market" BYTEA NOT NULL,
"liquidity_mint_pubkey" BYTEA NOT NULL,
"liquidity_mint_decimals" INT2 NOT NULL,
"liquidity_supply_pubkey" BYTEA NOT NULL,
"liquidity_pyth_oracle_pubkey" BYTEA NOT NULL,
"liquidity_switchboard_oracle_pubkey" BYTEA NOT NULL,
"liquidity_available_amount" NUMERIC(20) NOT NULL,
"liquidity_borrowed_amount_wads" BYTEA NOT NULL,
"liquidity_cumulative_borrow_rate_wads" BYTEA NOT NULL,
"liquidity_market_price" BYTEA NOT NULL,
"collateral_mint_pubkey" BYTEA NOT NULL,
"collateral_mint_total_supply" NUMERIC(20) NOT NULL,
"collateral_supply_pubkey" BYTEA NOT NULL,
"config_optimal_utilization_rate" INT2 NOT NULL,
"config_loan_to_value_ratio" INT2 NOT NULL,
"config_liquidation_bonus" INT2 NOT NULL,
"config_liquidation_threshold" INT2 NOT NULL,
"padding_to_fees64" BYTEA NOT NULL,
"padding_to_fees6" BYTEA NOT NULL,
"liquidity_accumulated_protocol_fees_wads" BYTEA NOT NULL,
"padding_final128" BYTEA NOT NULL,
"padding_final64" BYTEA NOT NULL,
"padding_final32" BYTEA NOT NULL,
"padding_final6" 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 solend_minimal_reserve_account"#)
.execute(connection)
.await?;
Ok(())
}
}