use {
crate::types::{PoolMetrics, VolatilityTracker},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128, U16, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct VirtualPoolRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub volatility_tracker: sqlx::types::Json<VolatilityTracker>,
pub config: Pubkey,
pub creator: Pubkey,
pub base_mint: Pubkey,
pub base_vault: Pubkey,
pub quote_vault: Pubkey,
pub base_reserve: U64,
pub quote_reserve: U64,
pub protocol_base_fee: U64,
pub protocol_quote_fee: U64,
pub partner_base_fee: U64,
pub partner_quote_fee: U64,
pub sqrt_price: U128,
pub activation_point: U64,
pub pool_type: U8,
pub is_migrated: U8,
pub is_partner_withdraw_surplus: U8,
pub is_protocol_withdraw_surplus: U8,
pub migration_progress: U8,
pub is_withdraw_leftover: U8,
pub is_creator_withdraw_surplus: U8,
pub migration_fee_withdraw_status: U8,
pub metrics: sqlx::types::Json<PoolMetrics>,
pub finish_curve_timestamp: U64,
pub creator_base_fee: U64,
pub creator_quote_fee: U64,
pub legacy_creation_fee_bits: U8,
pub creation_fee_bits: U8,
pub has_swap: U8,
pub padding0: Vec<u8>,
pub protocol_liquidity_migration_fee_bps: U16,
pub padding1: Vec<u8>,
pub protocol_migration_base_fee_amount: U64,
pub protocol_migration_quote_fee_amount: U64,
pub padding2: Vec<U64>,
}
impl VirtualPoolRow {
pub fn from_parts(
source: crate::accounts::virtual_pool::VirtualPool,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
volatility_tracker: sqlx::types::Json(source.volatility_tracker),
config: source.config.into(),
creator: source.creator.into(),
base_mint: source.base_mint.into(),
base_vault: source.base_vault.into(),
quote_vault: source.quote_vault.into(),
base_reserve: source.base_reserve.into(),
quote_reserve: source.quote_reserve.into(),
protocol_base_fee: source.protocol_base_fee.into(),
protocol_quote_fee: source.protocol_quote_fee.into(),
partner_base_fee: source.partner_base_fee.into(),
partner_quote_fee: source.partner_quote_fee.into(),
sqrt_price: source.sqrt_price.into(),
activation_point: source.activation_point.into(),
pool_type: source.pool_type.into(),
is_migrated: source.is_migrated.into(),
is_partner_withdraw_surplus: source.is_partner_withdraw_surplus.into(),
is_protocol_withdraw_surplus: source.is_protocol_withdraw_surplus.into(),
migration_progress: source.migration_progress.into(),
is_withdraw_leftover: source.is_withdraw_leftover.into(),
is_creator_withdraw_surplus: source.is_creator_withdraw_surplus.into(),
migration_fee_withdraw_status: source.migration_fee_withdraw_status.into(),
metrics: sqlx::types::Json(source.metrics),
finish_curve_timestamp: source.finish_curve_timestamp.into(),
creator_base_fee: source.creator_base_fee.into(),
creator_quote_fee: source.creator_quote_fee.into(),
legacy_creation_fee_bits: source.legacy_creation_fee_bits.into(),
creation_fee_bits: source.creation_fee_bits.into(),
has_swap: source.has_swap.into(),
padding0: source.padding0.to_vec(),
protocol_liquidity_migration_fee_bps: source
.protocol_liquidity_migration_fee_bps
.into(),
padding1: source.padding1.to_vec(),
protocol_migration_base_fee_amount: source.protocol_migration_base_fee_amount.into(),
protocol_migration_quote_fee_amount: source.protocol_migration_quote_fee_amount.into(),
padding2: source
.padding2
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<VirtualPoolRow> for crate::accounts::virtual_pool::VirtualPool {
type Error = carbon_core::error::Error;
fn try_from(source: VirtualPoolRow) -> Result<Self, Self::Error> {
Ok(Self {
volatility_tracker: source.volatility_tracker.0,
config: *source.config,
creator: *source.creator,
base_mint: *source.base_mint,
base_vault: *source.base_vault,
quote_vault: *source.quote_vault,
base_reserve: *source.base_reserve,
quote_reserve: *source.quote_reserve,
protocol_base_fee: *source.protocol_base_fee,
protocol_quote_fee: *source.protocol_quote_fee,
partner_base_fee: *source.partner_base_fee,
partner_quote_fee: *source.partner_quote_fee,
sqrt_price: *source.sqrt_price,
activation_point: *source.activation_point,
pool_type: source.pool_type.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
is_migrated: source.is_migrated.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
is_partner_withdraw_surplus: source.is_partner_withdraw_surplus.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
is_protocol_withdraw_surplus: source.is_protocol_withdraw_surplus.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
migration_progress: source.migration_progress.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
is_withdraw_leftover: source.is_withdraw_leftover.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
is_creator_withdraw_surplus: source.is_creator_withdraw_surplus.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
migration_fee_withdraw_status: source
.migration_fee_withdraw_status
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
metrics: source.metrics.0,
finish_curve_timestamp: *source.finish_curve_timestamp,
creator_base_fee: *source.creator_base_fee,
creator_quote_fee: *source.creator_quote_fee,
legacy_creation_fee_bits: source.legacy_creation_fee_bits.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
creation_fee_bits: source.creation_fee_bits.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
has_swap: source.has_swap.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding0: source.padding0.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 5 bytes"
.to_string(),
)
})?,
protocol_liquidity_migration_fee_bps: source
.protocol_liquidity_migration_fee_bps
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding1: source.padding1.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 6 bytes"
.to_string(),
)
})?,
protocol_migration_base_fee_amount: *source.protocol_migration_base_fee_amount,
protocol_migration_quote_fee_amount: *source.protocol_migration_quote_fee_amount,
padding2: source
.padding2
.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(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::virtual_pool::VirtualPool {
fn table() -> &'static str {
"virtual_pool_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"volatility_tracker",
"config",
"creator",
"base_mint",
"base_vault",
"quote_vault",
"base_reserve",
"quote_reserve",
"protocol_base_fee",
"protocol_quote_fee",
"partner_base_fee",
"partner_quote_fee",
"sqrt_price",
"activation_point",
"pool_type",
"is_migrated",
"is_partner_withdraw_surplus",
"is_protocol_withdraw_surplus",
"migration_progress",
"is_withdraw_leftover",
"is_creator_withdraw_surplus",
"migration_fee_withdraw_status",
"metrics",
"finish_curve_timestamp",
"creator_base_fee",
"creator_quote_fee",
"legacy_creation_fee_bits",
"creation_fee_bits",
"has_swap",
"padding0",
"protocol_liquidity_migration_fee_bps",
"padding1",
"protocol_migration_base_fee_amount",
"protocol_migration_quote_fee_amount",
"padding2",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for VirtualPoolRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO virtual_pool_account (
"volatility_tracker",
"config",
"creator",
"base_mint",
"base_vault",
"quote_vault",
"base_reserve",
"quote_reserve",
"protocol_base_fee",
"protocol_quote_fee",
"partner_base_fee",
"partner_quote_fee",
"sqrt_price",
"activation_point",
"pool_type",
"is_migrated",
"is_partner_withdraw_surplus",
"is_protocol_withdraw_surplus",
"migration_progress",
"is_withdraw_leftover",
"is_creator_withdraw_surplus",
"migration_fee_withdraw_status",
"metrics",
"finish_curve_timestamp",
"creator_base_fee",
"creator_quote_fee",
"legacy_creation_fee_bits",
"creation_fee_bits",
"has_swap",
"padding0",
"protocol_liquidity_migration_fee_bps",
"padding1",
"protocol_migration_base_fee_amount",
"protocol_migration_quote_fee_amount",
"padding2",
__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, $29, $30, $31, $32, $33, $34, $35, $36, $37
)"#)
.bind(&self.volatility_tracker)
.bind(self.config)
.bind(self.creator)
.bind(self.base_mint)
.bind(self.base_vault)
.bind(self.quote_vault)
.bind(&self.base_reserve)
.bind(&self.quote_reserve)
.bind(&self.protocol_base_fee)
.bind(&self.protocol_quote_fee)
.bind(&self.partner_base_fee)
.bind(&self.partner_quote_fee)
.bind(&self.sqrt_price)
.bind(&self.activation_point)
.bind(self.pool_type)
.bind(self.is_migrated)
.bind(self.is_partner_withdraw_surplus)
.bind(self.is_protocol_withdraw_surplus)
.bind(self.migration_progress)
.bind(self.is_withdraw_leftover)
.bind(self.is_creator_withdraw_surplus)
.bind(self.migration_fee_withdraw_status)
.bind(&self.metrics)
.bind(&self.finish_curve_timestamp)
.bind(&self.creator_base_fee)
.bind(&self.creator_quote_fee)
.bind(self.legacy_creation_fee_bits)
.bind(self.creation_fee_bits)
.bind(self.has_swap)
.bind(&self.padding0)
.bind(self.protocol_liquidity_migration_fee_bps)
.bind(&self.padding1)
.bind(&self.protocol_migration_base_fee_amount)
.bind(&self.protocol_migration_quote_fee_amount)
.bind(&self.padding2)
.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 VirtualPoolRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO virtual_pool_account (
"volatility_tracker",
"config",
"creator",
"base_mint",
"base_vault",
"quote_vault",
"base_reserve",
"quote_reserve",
"protocol_base_fee",
"protocol_quote_fee",
"partner_base_fee",
"partner_quote_fee",
"sqrt_price",
"activation_point",
"pool_type",
"is_migrated",
"is_partner_withdraw_surplus",
"is_protocol_withdraw_surplus",
"migration_progress",
"is_withdraw_leftover",
"is_creator_withdraw_surplus",
"migration_fee_withdraw_status",
"metrics",
"finish_curve_timestamp",
"creator_base_fee",
"creator_quote_fee",
"legacy_creation_fee_bits",
"creation_fee_bits",
"has_swap",
"padding0",
"protocol_liquidity_migration_fee_bps",
"padding1",
"protocol_migration_base_fee_amount",
"protocol_migration_quote_fee_amount",
"padding2",
__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, $29, $30, $31, $32, $33, $34, $35, $36, $37
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"volatility_tracker" = EXCLUDED."volatility_tracker",
"config" = EXCLUDED."config",
"creator" = EXCLUDED."creator",
"base_mint" = EXCLUDED."base_mint",
"base_vault" = EXCLUDED."base_vault",
"quote_vault" = EXCLUDED."quote_vault",
"base_reserve" = EXCLUDED."base_reserve",
"quote_reserve" = EXCLUDED."quote_reserve",
"protocol_base_fee" = EXCLUDED."protocol_base_fee",
"protocol_quote_fee" = EXCLUDED."protocol_quote_fee",
"partner_base_fee" = EXCLUDED."partner_base_fee",
"partner_quote_fee" = EXCLUDED."partner_quote_fee",
"sqrt_price" = EXCLUDED."sqrt_price",
"activation_point" = EXCLUDED."activation_point",
"pool_type" = EXCLUDED."pool_type",
"is_migrated" = EXCLUDED."is_migrated",
"is_partner_withdraw_surplus" = EXCLUDED."is_partner_withdraw_surplus",
"is_protocol_withdraw_surplus" = EXCLUDED."is_protocol_withdraw_surplus",
"migration_progress" = EXCLUDED."migration_progress",
"is_withdraw_leftover" = EXCLUDED."is_withdraw_leftover",
"is_creator_withdraw_surplus" = EXCLUDED."is_creator_withdraw_surplus",
"migration_fee_withdraw_status" = EXCLUDED."migration_fee_withdraw_status",
"metrics" = EXCLUDED."metrics",
"finish_curve_timestamp" = EXCLUDED."finish_curve_timestamp",
"creator_base_fee" = EXCLUDED."creator_base_fee",
"creator_quote_fee" = EXCLUDED."creator_quote_fee",
"legacy_creation_fee_bits" = EXCLUDED."legacy_creation_fee_bits",
"creation_fee_bits" = EXCLUDED."creation_fee_bits",
"has_swap" = EXCLUDED."has_swap",
"padding0" = EXCLUDED."padding0",
"protocol_liquidity_migration_fee_bps" = EXCLUDED."protocol_liquidity_migration_fee_bps",
"padding1" = EXCLUDED."padding1",
"protocol_migration_base_fee_amount" = EXCLUDED."protocol_migration_base_fee_amount",
"protocol_migration_quote_fee_amount" = EXCLUDED."protocol_migration_quote_fee_amount",
"padding2" = EXCLUDED."padding2",
__slot = EXCLUDED.__slot
"#)
.bind(&self.volatility_tracker)
.bind(self.config)
.bind(self.creator)
.bind(self.base_mint)
.bind(self.base_vault)
.bind(self.quote_vault)
.bind(&self.base_reserve)
.bind(&self.quote_reserve)
.bind(&self.protocol_base_fee)
.bind(&self.protocol_quote_fee)
.bind(&self.partner_base_fee)
.bind(&self.partner_quote_fee)
.bind(&self.sqrt_price)
.bind(&self.activation_point)
.bind(self.pool_type)
.bind(self.is_migrated)
.bind(self.is_partner_withdraw_surplus)
.bind(self.is_protocol_withdraw_surplus)
.bind(self.migration_progress)
.bind(self.is_withdraw_leftover)
.bind(self.is_creator_withdraw_surplus)
.bind(self.migration_fee_withdraw_status)
.bind(&self.metrics)
.bind(&self.finish_curve_timestamp)
.bind(&self.creator_base_fee)
.bind(&self.creator_quote_fee)
.bind(self.legacy_creation_fee_bits)
.bind(self.creation_fee_bits)
.bind(self.has_swap)
.bind(&self.padding0)
.bind(self.protocol_liquidity_migration_fee_bps)
.bind(&self.padding1)
.bind(&self.protocol_migration_base_fee_amount)
.bind(&self.protocol_migration_quote_fee_amount)
.bind(&self.padding2)
.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 VirtualPoolRow {
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 virtual_pool_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 VirtualPoolRow {
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 virtual_pool_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 VirtualPoolMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for VirtualPoolMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS virtual_pool_account (
-- Account data
"volatility_tracker" JSONB NOT NULL,
"config" BYTEA NOT NULL,
"creator" BYTEA NOT NULL,
"base_mint" BYTEA NOT NULL,
"base_vault" BYTEA NOT NULL,
"quote_vault" BYTEA NOT NULL,
"base_reserve" NUMERIC(20) NOT NULL,
"quote_reserve" NUMERIC(20) NOT NULL,
"protocol_base_fee" NUMERIC(20) NOT NULL,
"protocol_quote_fee" NUMERIC(20) NOT NULL,
"partner_base_fee" NUMERIC(20) NOT NULL,
"partner_quote_fee" NUMERIC(20) NOT NULL,
"sqrt_price" NUMERIC(39) NOT NULL,
"activation_point" NUMERIC(20) NOT NULL,
"pool_type" INT2 NOT NULL,
"is_migrated" INT2 NOT NULL,
"is_partner_withdraw_surplus" INT2 NOT NULL,
"is_protocol_withdraw_surplus" INT2 NOT NULL,
"migration_progress" INT2 NOT NULL,
"is_withdraw_leftover" INT2 NOT NULL,
"is_creator_withdraw_surplus" INT2 NOT NULL,
"migration_fee_withdraw_status" INT2 NOT NULL,
"metrics" JSONB NOT NULL,
"finish_curve_timestamp" NUMERIC(20) NOT NULL,
"creator_base_fee" NUMERIC(20) NOT NULL,
"creator_quote_fee" NUMERIC(20) NOT NULL,
"legacy_creation_fee_bits" INT2 NOT NULL,
"creation_fee_bits" INT2 NOT NULL,
"has_swap" INT2 NOT NULL,
"padding0" BYTEA NOT NULL,
"protocol_liquidity_migration_fee_bps" INT4 NOT NULL,
"padding1" BYTEA NOT NULL,
"protocol_migration_base_fee_amount" NUMERIC(20) NOT NULL,
"protocol_migration_quote_fee_amount" NUMERIC(20) NOT NULL,
"padding2" 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 virtual_pool_account"#)
.execute(connection)
.await?;
Ok(())
}
}