use {
crate::types::StrategyType,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct StrategyRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub reserve: Pubkey,
pub collateral_vault: Pubkey,
pub strategy_type: sqlx::types::Json<StrategyType>,
pub current_liquidity: U64,
pub bumps: Vec<u8>,
pub vault: Pubkey,
pub is_disable: U8,
}
impl StrategyRow {
pub fn from_parts(
source: crate::accounts::strategy::Strategy,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
reserve: source.reserve.into(),
collateral_vault: source.collateral_vault.into(),
strategy_type: sqlx::types::Json(source.strategy_type),
current_liquidity: source.current_liquidity.into(),
bumps: source.bumps.to_vec(),
vault: source.vault.into(),
is_disable: source.is_disable.into(),
}
}
}
impl TryFrom<StrategyRow> for crate::accounts::strategy::Strategy {
type Error = carbon_core::error::Error;
fn try_from(source: StrategyRow) -> Result<Self, Self::Error> {
Ok(Self {
reserve: *source.reserve,
collateral_vault: *source.collateral_vault,
strategy_type: source.strategy_type.0,
current_liquidity: *source.current_liquidity,
bumps: source.bumps.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 10 bytes"
.to_string(),
)
})?,
vault: *source.vault,
is_disable: source.is_disable.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::strategy::Strategy {
fn table() -> &'static str {
"strategy_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"reserve",
"collateral_vault",
"strategy_type",
"current_liquidity",
"bumps",
"vault",
"is_disable",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for StrategyRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO strategy_account (
"reserve",
"collateral_vault",
"strategy_type",
"current_liquidity",
"bumps",
"vault",
"is_disable",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
)"#,
)
.bind(self.reserve)
.bind(self.collateral_vault)
.bind(&self.strategy_type)
.bind(&self.current_liquidity)
.bind(&self.bumps)
.bind(self.vault)
.bind(self.is_disable)
.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 StrategyRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO strategy_account (
"reserve",
"collateral_vault",
"strategy_type",
"current_liquidity",
"bumps",
"vault",
"is_disable",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"reserve" = EXCLUDED."reserve",
"collateral_vault" = EXCLUDED."collateral_vault",
"strategy_type" = EXCLUDED."strategy_type",
"current_liquidity" = EXCLUDED."current_liquidity",
"bumps" = EXCLUDED."bumps",
"vault" = EXCLUDED."vault",
"is_disable" = EXCLUDED."is_disable",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.reserve)
.bind(self.collateral_vault)
.bind(&self.strategy_type)
.bind(&self.current_liquidity)
.bind(&self.bumps)
.bind(self.vault)
.bind(self.is_disable)
.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 StrategyRow {
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 strategy_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 StrategyRow {
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 strategy_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 StrategyMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for StrategyMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS strategy_account (
-- Account data
"reserve" BYTEA NOT NULL,
"collateral_vault" BYTEA NOT NULL,
"strategy_type" JSONB NOT NULL,
"current_liquidity" NUMERIC(20) NOT NULL,
"bumps" BYTEA NOT NULL,
"vault" BYTEA NOT NULL,
"is_disable" 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 strategy_account"#)
.execute(connection)
.await?;
Ok(())
}
}