use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128, U16, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct InsuranceFundStakeRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub authority: Pubkey,
pub if_shares: U128,
pub last_withdraw_request_shares: U128,
pub if_base: U128,
pub last_valid_ts: i64,
pub last_withdraw_request_value: U64,
pub last_withdraw_request_ts: i64,
pub cost_basis: i64,
pub market_index: U16,
pub padding: Vec<u8>,
}
impl InsuranceFundStakeRow {
pub fn from_parts(
source: crate::accounts::insurance_fund_stake::InsuranceFundStake,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
authority: source.authority.into(),
if_shares: source.if_shares.into(),
last_withdraw_request_shares: source.last_withdraw_request_shares.into(),
if_base: source.if_base.into(),
last_valid_ts: source.last_valid_ts,
last_withdraw_request_value: source.last_withdraw_request_value.into(),
last_withdraw_request_ts: source.last_withdraw_request_ts,
cost_basis: source.cost_basis,
market_index: source.market_index.into(),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<InsuranceFundStakeRow> for crate::accounts::insurance_fund_stake::InsuranceFundStake {
type Error = carbon_core::error::Error;
fn try_from(source: InsuranceFundStakeRow) -> Result<Self, Self::Error> {
Ok(Self {
authority: *source.authority,
if_shares: *source.if_shares,
last_withdraw_request_shares: *source.last_withdraw_request_shares,
if_base: *source.if_base,
last_valid_ts: source.last_valid_ts,
last_withdraw_request_value: *source.last_withdraw_request_value,
last_withdraw_request_ts: source.last_withdraw_request_ts,
cost_basis: source.cost_basis,
market_index: source.market_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding: source.padding.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 14 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::insurance_fund_stake::InsuranceFundStake
{
fn table() -> &'static str {
"insurance_fund_stake_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"authority",
"if_shares",
"last_withdraw_request_shares",
"if_base",
"last_valid_ts",
"last_withdraw_request_value",
"last_withdraw_request_ts",
"cost_basis",
"market_index",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for InsuranceFundStakeRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO insurance_fund_stake_account (
"authority",
"if_shares",
"last_withdraw_request_shares",
"if_base",
"last_valid_ts",
"last_withdraw_request_value",
"last_withdraw_request_ts",
"cost_basis",
"market_index",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
)"#,
)
.bind(self.authority)
.bind(&self.if_shares)
.bind(&self.last_withdraw_request_shares)
.bind(&self.if_base)
.bind(self.last_valid_ts)
.bind(&self.last_withdraw_request_value)
.bind(self.last_withdraw_request_ts)
.bind(self.cost_basis)
.bind(self.market_index)
.bind(&self.padding)
.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 InsuranceFundStakeRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO insurance_fund_stake_account (
"authority",
"if_shares",
"last_withdraw_request_shares",
"if_base",
"last_valid_ts",
"last_withdraw_request_value",
"last_withdraw_request_ts",
"cost_basis",
"market_index",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"authority" = EXCLUDED."authority",
"if_shares" = EXCLUDED."if_shares",
"last_withdraw_request_shares" = EXCLUDED."last_withdraw_request_shares",
"if_base" = EXCLUDED."if_base",
"last_valid_ts" = EXCLUDED."last_valid_ts",
"last_withdraw_request_value" = EXCLUDED."last_withdraw_request_value",
"last_withdraw_request_ts" = EXCLUDED."last_withdraw_request_ts",
"cost_basis" = EXCLUDED."cost_basis",
"market_index" = EXCLUDED."market_index",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.authority)
.bind(&self.if_shares)
.bind(&self.last_withdraw_request_shares)
.bind(&self.if_base)
.bind(self.last_valid_ts)
.bind(&self.last_withdraw_request_value)
.bind(self.last_withdraw_request_ts)
.bind(self.cost_basis)
.bind(self.market_index)
.bind(&self.padding)
.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 InsuranceFundStakeRow {
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 insurance_fund_stake_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 InsuranceFundStakeRow {
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 insurance_fund_stake_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 InsuranceFundStakeMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for InsuranceFundStakeMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS insurance_fund_stake_account (
-- Account data
"authority" BYTEA NOT NULL,
"if_shares" NUMERIC(39) NOT NULL,
"last_withdraw_request_shares" NUMERIC(39) NOT NULL,
"if_base" NUMERIC(39) NOT NULL,
"last_valid_ts" INT8 NOT NULL,
"last_withdraw_request_value" NUMERIC(20) NOT NULL,
"last_withdraw_request_ts" INT8 NOT NULL,
"cost_basis" INT8 NOT NULL,
"market_index" INT4 NOT NULL,
"padding" 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 insurance_fund_stake_account"#)
.execute(connection)
.await?;
Ok(())
}
}