use {
crate::types::{AmmCreatorFeeOn, VestingSchedule},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PoolStateRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub epoch: U64,
pub auth_bump: U8,
pub status: U8,
pub base_decimals: U8,
pub quote_decimals: U8,
pub migrate_type: U8,
pub supply: U64,
pub total_base_sell: U64,
pub virtual_base: U64,
pub virtual_quote: U64,
pub real_base: U64,
pub real_quote: U64,
pub total_quote_fund_raising: U64,
pub quote_protocol_fee: U64,
pub platform_fee: U64,
pub migrate_fee: U64,
pub vesting_schedule: sqlx::types::Json<VestingSchedule>,
pub global_config: Pubkey,
pub platform_config: Pubkey,
pub base_mint: Pubkey,
pub quote_mint: Pubkey,
pub base_vault: Pubkey,
pub quote_vault: Pubkey,
pub creator: Pubkey,
pub token_program_flag: U8,
pub amm_creator_fee_on: sqlx::types::Json<AmmCreatorFeeOn>,
pub platform_vesting_share: U64,
pub padding: Vec<u8>,
}
impl PoolStateRow {
pub fn from_parts(
source: crate::accounts::pool_state::PoolState,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
epoch: source.epoch.into(),
auth_bump: source.auth_bump.into(),
status: source.status.into(),
base_decimals: source.base_decimals.into(),
quote_decimals: source.quote_decimals.into(),
migrate_type: source.migrate_type.into(),
supply: source.supply.into(),
total_base_sell: source.total_base_sell.into(),
virtual_base: source.virtual_base.into(),
virtual_quote: source.virtual_quote.into(),
real_base: source.real_base.into(),
real_quote: source.real_quote.into(),
total_quote_fund_raising: source.total_quote_fund_raising.into(),
quote_protocol_fee: source.quote_protocol_fee.into(),
platform_fee: source.platform_fee.into(),
migrate_fee: source.migrate_fee.into(),
vesting_schedule: sqlx::types::Json(source.vesting_schedule),
global_config: source.global_config.into(),
platform_config: source.platform_config.into(),
base_mint: source.base_mint.into(),
quote_mint: source.quote_mint.into(),
base_vault: source.base_vault.into(),
quote_vault: source.quote_vault.into(),
creator: source.creator.into(),
token_program_flag: source.token_program_flag.into(),
amm_creator_fee_on: sqlx::types::Json(source.amm_creator_fee_on),
platform_vesting_share: source.platform_vesting_share.into(),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<PoolStateRow> for crate::accounts::pool_state::PoolState {
type Error = carbon_core::error::Error;
fn try_from(source: PoolStateRow) -> Result<Self, Self::Error> {
Ok(Self {
epoch: *source.epoch,
auth_bump: source.auth_bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
status: source.status.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
base_decimals: source.base_decimals.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
quote_decimals: source.quote_decimals.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
migrate_type: source.migrate_type.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
supply: *source.supply,
total_base_sell: *source.total_base_sell,
virtual_base: *source.virtual_base,
virtual_quote: *source.virtual_quote,
real_base: *source.real_base,
real_quote: *source.real_quote,
total_quote_fund_raising: *source.total_quote_fund_raising,
quote_protocol_fee: *source.quote_protocol_fee,
platform_fee: *source.platform_fee,
migrate_fee: *source.migrate_fee,
vesting_schedule: source.vesting_schedule.0,
global_config: *source.global_config,
platform_config: *source.platform_config,
base_mint: *source.base_mint,
quote_mint: *source.quote_mint,
base_vault: *source.base_vault,
quote_vault: *source.quote_vault,
creator: *source.creator,
token_program_flag: source.token_program_flag.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
amm_creator_fee_on: source.amm_creator_fee_on.0,
platform_vesting_share: *source.platform_vesting_share,
padding: source.padding.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 54 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::pool_state::PoolState {
fn table() -> &'static str {
"pool_state_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"epoch",
"auth_bump",
"status",
"base_decimals",
"quote_decimals",
"migrate_type",
"supply",
"total_base_sell",
"virtual_base",
"virtual_quote",
"real_base",
"real_quote",
"total_quote_fund_raising",
"quote_protocol_fee",
"platform_fee",
"migrate_fee",
"vesting_schedule",
"global_config",
"platform_config",
"base_mint",
"quote_mint",
"base_vault",
"quote_vault",
"creator",
"token_program_flag",
"amm_creator_fee_on",
"platform_vesting_share",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PoolStateRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO pool_state_account (
"epoch",
"auth_bump",
"status",
"base_decimals",
"quote_decimals",
"migrate_type",
"supply",
"total_base_sell",
"virtual_base",
"virtual_quote",
"real_base",
"real_quote",
"total_quote_fund_raising",
"quote_protocol_fee",
"platform_fee",
"migrate_fee",
"vesting_schedule",
"global_config",
"platform_config",
"base_mint",
"quote_mint",
"base_vault",
"quote_vault",
"creator",
"token_program_flag",
"amm_creator_fee_on",
"platform_vesting_share",
"padding",
__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
)"#)
.bind(&self.epoch)
.bind(self.auth_bump)
.bind(self.status)
.bind(self.base_decimals)
.bind(self.quote_decimals)
.bind(self.migrate_type)
.bind(&self.supply)
.bind(&self.total_base_sell)
.bind(&self.virtual_base)
.bind(&self.virtual_quote)
.bind(&self.real_base)
.bind(&self.real_quote)
.bind(&self.total_quote_fund_raising)
.bind(&self.quote_protocol_fee)
.bind(&self.platform_fee)
.bind(&self.migrate_fee)
.bind(&self.vesting_schedule)
.bind(self.global_config)
.bind(self.platform_config)
.bind(self.base_mint)
.bind(self.quote_mint)
.bind(self.base_vault)
.bind(self.quote_vault)
.bind(self.creator)
.bind(self.token_program_flag)
.bind(&self.amm_creator_fee_on)
.bind(&self.platform_vesting_share)
.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 PoolStateRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO pool_state_account (
"epoch",
"auth_bump",
"status",
"base_decimals",
"quote_decimals",
"migrate_type",
"supply",
"total_base_sell",
"virtual_base",
"virtual_quote",
"real_base",
"real_quote",
"total_quote_fund_raising",
"quote_protocol_fee",
"platform_fee",
"migrate_fee",
"vesting_schedule",
"global_config",
"platform_config",
"base_mint",
"quote_mint",
"base_vault",
"quote_vault",
"creator",
"token_program_flag",
"amm_creator_fee_on",
"platform_vesting_share",
"padding",
__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
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"epoch" = EXCLUDED."epoch",
"auth_bump" = EXCLUDED."auth_bump",
"status" = EXCLUDED."status",
"base_decimals" = EXCLUDED."base_decimals",
"quote_decimals" = EXCLUDED."quote_decimals",
"migrate_type" = EXCLUDED."migrate_type",
"supply" = EXCLUDED."supply",
"total_base_sell" = EXCLUDED."total_base_sell",
"virtual_base" = EXCLUDED."virtual_base",
"virtual_quote" = EXCLUDED."virtual_quote",
"real_base" = EXCLUDED."real_base",
"real_quote" = EXCLUDED."real_quote",
"total_quote_fund_raising" = EXCLUDED."total_quote_fund_raising",
"quote_protocol_fee" = EXCLUDED."quote_protocol_fee",
"platform_fee" = EXCLUDED."platform_fee",
"migrate_fee" = EXCLUDED."migrate_fee",
"vesting_schedule" = EXCLUDED."vesting_schedule",
"global_config" = EXCLUDED."global_config",
"platform_config" = EXCLUDED."platform_config",
"base_mint" = EXCLUDED."base_mint",
"quote_mint" = EXCLUDED."quote_mint",
"base_vault" = EXCLUDED."base_vault",
"quote_vault" = EXCLUDED."quote_vault",
"creator" = EXCLUDED."creator",
"token_program_flag" = EXCLUDED."token_program_flag",
"amm_creator_fee_on" = EXCLUDED."amm_creator_fee_on",
"platform_vesting_share" = EXCLUDED."platform_vesting_share",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#)
.bind(&self.epoch)
.bind(self.auth_bump)
.bind(self.status)
.bind(self.base_decimals)
.bind(self.quote_decimals)
.bind(self.migrate_type)
.bind(&self.supply)
.bind(&self.total_base_sell)
.bind(&self.virtual_base)
.bind(&self.virtual_quote)
.bind(&self.real_base)
.bind(&self.real_quote)
.bind(&self.total_quote_fund_raising)
.bind(&self.quote_protocol_fee)
.bind(&self.platform_fee)
.bind(&self.migrate_fee)
.bind(&self.vesting_schedule)
.bind(self.global_config)
.bind(self.platform_config)
.bind(self.base_mint)
.bind(self.quote_mint)
.bind(self.base_vault)
.bind(self.quote_vault)
.bind(self.creator)
.bind(self.token_program_flag)
.bind(&self.amm_creator_fee_on)
.bind(&self.platform_vesting_share)
.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 PoolStateRow {
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 pool_state_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 PoolStateRow {
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 pool_state_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 PoolStateMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PoolStateMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS pool_state_account (
-- Account data
"epoch" NUMERIC(20) NOT NULL,
"auth_bump" INT2 NOT NULL,
"status" INT2 NOT NULL,
"base_decimals" INT2 NOT NULL,
"quote_decimals" INT2 NOT NULL,
"migrate_type" INT2 NOT NULL,
"supply" NUMERIC(20) NOT NULL,
"total_base_sell" NUMERIC(20) NOT NULL,
"virtual_base" NUMERIC(20) NOT NULL,
"virtual_quote" NUMERIC(20) NOT NULL,
"real_base" NUMERIC(20) NOT NULL,
"real_quote" NUMERIC(20) NOT NULL,
"total_quote_fund_raising" NUMERIC(20) NOT NULL,
"quote_protocol_fee" NUMERIC(20) NOT NULL,
"platform_fee" NUMERIC(20) NOT NULL,
"migrate_fee" NUMERIC(20) NOT NULL,
"vesting_schedule" JSONB NOT NULL,
"global_config" BYTEA NOT NULL,
"platform_config" BYTEA NOT NULL,
"base_mint" BYTEA NOT NULL,
"quote_mint" BYTEA NOT NULL,
"base_vault" BYTEA NOT NULL,
"quote_vault" BYTEA NOT NULL,
"creator" BYTEA NOT NULL,
"token_program_flag" INT2 NOT NULL,
"amm_creator_fee_on" JSONB NOT NULL,
"platform_vesting_share" NUMERIC(20) 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 pool_state_account"#)
.execute(connection)
.await?;
Ok(())
}
}