use {
crate::types::{LockedProfitTracker, VaultBumps},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct VaultRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub enabled: U8,
pub bumps: sqlx::types::Json<VaultBumps>,
pub total_amount: U64,
pub token_vault: Pubkey,
pub fee_vault: Pubkey,
pub token_mint: Pubkey,
pub lp_mint: Pubkey,
pub strategies: Vec<Pubkey>,
pub base: Pubkey,
pub admin: Pubkey,
pub operator: Pubkey,
pub locked_profit_tracker: sqlx::types::Json<LockedProfitTracker>,
}
impl VaultRow {
pub fn from_parts(source: crate::accounts::vault::Vault, metadata: AccountMetadata) -> Self {
Self {
account_metadata: metadata.into(),
enabled: source.enabled.into(),
bumps: sqlx::types::Json(source.bumps),
total_amount: source.total_amount.into(),
token_vault: source.token_vault.into(),
fee_vault: source.fee_vault.into(),
token_mint: source.token_mint.into(),
lp_mint: source.lp_mint.into(),
strategies: source
.strategies
.into_iter()
.map(|element| element.into())
.collect(),
base: source.base.into(),
admin: source.admin.into(),
operator: source.operator.into(),
locked_profit_tracker: sqlx::types::Json(source.locked_profit_tracker),
}
}
}
impl TryFrom<VaultRow> for crate::accounts::vault::Vault {
type Error = carbon_core::error::Error;
fn try_from(source: VaultRow) -> Result<Self, Self::Error> {
Ok(Self {
enabled: source.enabled.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
bumps: source.bumps.0,
total_amount: *source.total_amount,
token_vault: *source.token_vault,
fee_vault: *source.fee_vault,
token_mint: *source.token_mint,
lp_mint: *source.lp_mint,
strategies: source
.strategies
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
base: *source.base,
admin: *source.admin,
operator: *source.operator,
locked_profit_tracker: source.locked_profit_tracker.0,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::vault::Vault {
fn table() -> &'static str {
"vault_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"enabled",
"bumps",
"total_amount",
"token_vault",
"fee_vault",
"token_mint",
"lp_mint",
"strategies",
"base",
"admin",
"operator",
"locked_profit_tracker",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for VaultRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO vault_account (
"enabled",
"bumps",
"total_amount",
"token_vault",
"fee_vault",
"token_mint",
"lp_mint",
"strategies",
"base",
"admin",
"operator",
"locked_profit_tracker",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)"#,
)
.bind(self.enabled)
.bind(&self.bumps)
.bind(&self.total_amount)
.bind(self.token_vault)
.bind(self.fee_vault)
.bind(self.token_mint)
.bind(self.lp_mint)
.bind(&self.strategies)
.bind(self.base)
.bind(self.admin)
.bind(self.operator)
.bind(&self.locked_profit_tracker)
.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 VaultRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO vault_account (
"enabled",
"bumps",
"total_amount",
"token_vault",
"fee_vault",
"token_mint",
"lp_mint",
"strategies",
"base",
"admin",
"operator",
"locked_profit_tracker",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"enabled" = EXCLUDED."enabled",
"bumps" = EXCLUDED."bumps",
"total_amount" = EXCLUDED."total_amount",
"token_vault" = EXCLUDED."token_vault",
"fee_vault" = EXCLUDED."fee_vault",
"token_mint" = EXCLUDED."token_mint",
"lp_mint" = EXCLUDED."lp_mint",
"strategies" = EXCLUDED."strategies",
"base" = EXCLUDED."base",
"admin" = EXCLUDED."admin",
"operator" = EXCLUDED."operator",
"locked_profit_tracker" = EXCLUDED."locked_profit_tracker",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.enabled)
.bind(&self.bumps)
.bind(&self.total_amount)
.bind(self.token_vault)
.bind(self.fee_vault)
.bind(self.token_mint)
.bind(self.lp_mint)
.bind(&self.strategies)
.bind(self.base)
.bind(self.admin)
.bind(self.operator)
.bind(&self.locked_profit_tracker)
.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 VaultRow {
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 vault_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 VaultRow {
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 vault_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 VaultMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for VaultMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS vault_account (
-- Account data
"enabled" INT2 NOT NULL,
"bumps" JSONB NOT NULL,
"total_amount" NUMERIC(20) NOT NULL,
"token_vault" BYTEA NOT NULL,
"fee_vault" BYTEA NOT NULL,
"token_mint" BYTEA NOT NULL,
"lp_mint" BYTEA NOT NULL,
"strategies" BYTEA[] NOT NULL,
"base" BYTEA NOT NULL,
"admin" BYTEA NOT NULL,
"operator" BYTEA NOT NULL,
"locked_profit_tracker" JSONB 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 vault_account"#)
.execute(connection)
.await?;
Ok(())
}
}