use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenReserveRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub mint: Pubkey,
pub vault: Pubkey,
pub borrow_rate: U16,
pub fee_on_interest: U16,
pub last_utilization: U16,
pub last_update_timestamp: U64,
pub supply_exchange_price: U64,
pub borrow_exchange_price: U64,
pub max_utilization: U16,
pub total_supply_with_interest: U64,
pub total_supply_interest_free: U64,
pub total_borrow_with_interest: U64,
pub total_borrow_interest_free: U64,
pub total_claim_amount: U64,
pub interacting_protocol: Pubkey,
pub interacting_timestamp: U64,
pub interacting_balance: U64,
}
impl TokenReserveRow {
pub fn from_parts(
source: crate::accounts::token_reserve::TokenReserve,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
mint: source.mint.into(),
vault: source.vault.into(),
borrow_rate: source.borrow_rate.into(),
fee_on_interest: source.fee_on_interest.into(),
last_utilization: source.last_utilization.into(),
last_update_timestamp: source.last_update_timestamp.into(),
supply_exchange_price: source.supply_exchange_price.into(),
borrow_exchange_price: source.borrow_exchange_price.into(),
max_utilization: source.max_utilization.into(),
total_supply_with_interest: source.total_supply_with_interest.into(),
total_supply_interest_free: source.total_supply_interest_free.into(),
total_borrow_with_interest: source.total_borrow_with_interest.into(),
total_borrow_interest_free: source.total_borrow_interest_free.into(),
total_claim_amount: source.total_claim_amount.into(),
interacting_protocol: source.interacting_protocol.into(),
interacting_timestamp: source.interacting_timestamp.into(),
interacting_balance: source.interacting_balance.into(),
}
}
}
impl TryFrom<TokenReserveRow> for crate::accounts::token_reserve::TokenReserve {
type Error = carbon_core::error::Error;
fn try_from(source: TokenReserveRow) -> Result<Self, Self::Error> {
Ok(Self {
mint: *source.mint,
vault: *source.vault,
borrow_rate: source.borrow_rate.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
fee_on_interest: source.fee_on_interest.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
last_utilization: source.last_utilization.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
last_update_timestamp: *source.last_update_timestamp,
supply_exchange_price: *source.supply_exchange_price,
borrow_exchange_price: *source.borrow_exchange_price,
max_utilization: source.max_utilization.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
total_supply_with_interest: *source.total_supply_with_interest,
total_supply_interest_free: *source.total_supply_interest_free,
total_borrow_with_interest: *source.total_borrow_with_interest,
total_borrow_interest_free: *source.total_borrow_interest_free,
total_claim_amount: *source.total_claim_amount,
interacting_protocol: *source.interacting_protocol,
interacting_timestamp: *source.interacting_timestamp,
interacting_balance: *source.interacting_balance,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::token_reserve::TokenReserve {
fn table() -> &'static str {
"token_reserve_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"mint",
"vault",
"borrow_rate",
"fee_on_interest",
"last_utilization",
"last_update_timestamp",
"supply_exchange_price",
"borrow_exchange_price",
"max_utilization",
"total_supply_with_interest",
"total_supply_interest_free",
"total_borrow_with_interest",
"total_borrow_interest_free",
"total_claim_amount",
"interacting_protocol",
"interacting_timestamp",
"interacting_balance",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TokenReserveRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO token_reserve_account (
"mint",
"vault",
"borrow_rate",
"fee_on_interest",
"last_utilization",
"last_update_timestamp",
"supply_exchange_price",
"borrow_exchange_price",
"max_utilization",
"total_supply_with_interest",
"total_supply_interest_free",
"total_borrow_with_interest",
"total_borrow_interest_free",
"total_claim_amount",
"interacting_protocol",
"interacting_timestamp",
"interacting_balance",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19
)"#,
)
.bind(self.mint)
.bind(self.vault)
.bind(self.borrow_rate)
.bind(self.fee_on_interest)
.bind(self.last_utilization)
.bind(&self.last_update_timestamp)
.bind(&self.supply_exchange_price)
.bind(&self.borrow_exchange_price)
.bind(self.max_utilization)
.bind(&self.total_supply_with_interest)
.bind(&self.total_supply_interest_free)
.bind(&self.total_borrow_with_interest)
.bind(&self.total_borrow_interest_free)
.bind(&self.total_claim_amount)
.bind(self.interacting_protocol)
.bind(&self.interacting_timestamp)
.bind(&self.interacting_balance)
.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 TokenReserveRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO token_reserve_account (
"mint",
"vault",
"borrow_rate",
"fee_on_interest",
"last_utilization",
"last_update_timestamp",
"supply_exchange_price",
"borrow_exchange_price",
"max_utilization",
"total_supply_with_interest",
"total_supply_interest_free",
"total_borrow_with_interest",
"total_borrow_interest_free",
"total_claim_amount",
"interacting_protocol",
"interacting_timestamp",
"interacting_balance",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"mint" = EXCLUDED."mint",
"vault" = EXCLUDED."vault",
"borrow_rate" = EXCLUDED."borrow_rate",
"fee_on_interest" = EXCLUDED."fee_on_interest",
"last_utilization" = EXCLUDED."last_utilization",
"last_update_timestamp" = EXCLUDED."last_update_timestamp",
"supply_exchange_price" = EXCLUDED."supply_exchange_price",
"borrow_exchange_price" = EXCLUDED."borrow_exchange_price",
"max_utilization" = EXCLUDED."max_utilization",
"total_supply_with_interest" = EXCLUDED."total_supply_with_interest",
"total_supply_interest_free" = EXCLUDED."total_supply_interest_free",
"total_borrow_with_interest" = EXCLUDED."total_borrow_with_interest",
"total_borrow_interest_free" = EXCLUDED."total_borrow_interest_free",
"total_claim_amount" = EXCLUDED."total_claim_amount",
"interacting_protocol" = EXCLUDED."interacting_protocol",
"interacting_timestamp" = EXCLUDED."interacting_timestamp",
"interacting_balance" = EXCLUDED."interacting_balance",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.mint)
.bind(self.vault)
.bind(self.borrow_rate)
.bind(self.fee_on_interest)
.bind(self.last_utilization)
.bind(&self.last_update_timestamp)
.bind(&self.supply_exchange_price)
.bind(&self.borrow_exchange_price)
.bind(self.max_utilization)
.bind(&self.total_supply_with_interest)
.bind(&self.total_supply_interest_free)
.bind(&self.total_borrow_with_interest)
.bind(&self.total_borrow_interest_free)
.bind(&self.total_claim_amount)
.bind(self.interacting_protocol)
.bind(&self.interacting_timestamp)
.bind(&self.interacting_balance)
.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 TokenReserveRow {
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 token_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 TokenReserveRow {
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 token_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 TokenReserveMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TokenReserveMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS token_reserve_account (
-- Account data
"mint" BYTEA NOT NULL,
"vault" BYTEA NOT NULL,
"borrow_rate" INT4 NOT NULL,
"fee_on_interest" INT4 NOT NULL,
"last_utilization" INT4 NOT NULL,
"last_update_timestamp" NUMERIC(20) NOT NULL,
"supply_exchange_price" NUMERIC(20) NOT NULL,
"borrow_exchange_price" NUMERIC(20) NOT NULL,
"max_utilization" INT4 NOT NULL,
"total_supply_with_interest" NUMERIC(20) NOT NULL,
"total_supply_interest_free" NUMERIC(20) NOT NULL,
"total_borrow_with_interest" NUMERIC(20) NOT NULL,
"total_borrow_interest_free" NUMERIC(20) NOT NULL,
"total_claim_amount" NUMERIC(20) NOT NULL,
"interacting_protocol" BYTEA NOT NULL,
"interacting_timestamp" NUMERIC(20) NOT NULL,
"interacting_balance" NUMERIC(20) 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 token_reserve_account"#)
.execute(connection)
.await?;
Ok(())
}
}