use {
crate::types::FeeParams,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PoolRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub enabled: bool,
pub owner: Pubkey,
pub mint_a: Pubkey,
pub mint_b: Pubkey,
pub token_a_reserves: U128,
pub token_b_reserves: U128,
pub shift: U128,
pub royalties: U64,
pub vertigo_fees: U64,
pub bump: U8,
pub fee_params: sqlx::types::Json<FeeParams>,
}
impl PoolRow {
pub fn from_parts(source: crate::accounts::pool::Pool, metadata: AccountMetadata) -> Self {
Self {
account_metadata: metadata.into(),
enabled: source.enabled,
owner: source.owner.into(),
mint_a: source.mint_a.into(),
mint_b: source.mint_b.into(),
token_a_reserves: source.token_a_reserves.into(),
token_b_reserves: source.token_b_reserves.into(),
shift: source.shift.into(),
royalties: source.royalties.into(),
vertigo_fees: source.vertigo_fees.into(),
bump: source.bump.into(),
fee_params: sqlx::types::Json(source.fee_params),
}
}
}
impl TryFrom<PoolRow> for crate::accounts::pool::Pool {
type Error = carbon_core::error::Error;
fn try_from(source: PoolRow) -> Result<Self, Self::Error> {
Ok(Self {
enabled: source.enabled,
owner: *source.owner,
mint_a: *source.mint_a,
mint_b: *source.mint_b,
token_a_reserves: *source.token_a_reserves,
token_b_reserves: *source.token_b_reserves,
shift: *source.shift,
royalties: *source.royalties,
vertigo_fees: *source.vertigo_fees,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
fee_params: source.fee_params.0,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::pool::Pool {
fn table() -> &'static str {
"pool_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"enabled",
"owner",
"mint_a",
"mint_b",
"token_a_reserves",
"token_b_reserves",
"shift",
"royalties",
"vertigo_fees",
"bump",
"fee_params",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PoolRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO pool_account (
"enabled",
"owner",
"mint_a",
"mint_b",
"token_a_reserves",
"token_b_reserves",
"shift",
"royalties",
"vertigo_fees",
"bump",
"fee_params",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
)"#,
)
.bind(self.enabled)
.bind(self.owner)
.bind(self.mint_a)
.bind(self.mint_b)
.bind(&self.token_a_reserves)
.bind(&self.token_b_reserves)
.bind(&self.shift)
.bind(&self.royalties)
.bind(&self.vertigo_fees)
.bind(self.bump)
.bind(&self.fee_params)
.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 PoolRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO pool_account (
"enabled",
"owner",
"mint_a",
"mint_b",
"token_a_reserves",
"token_b_reserves",
"shift",
"royalties",
"vertigo_fees",
"bump",
"fee_params",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"enabled" = EXCLUDED."enabled",
"owner" = EXCLUDED."owner",
"mint_a" = EXCLUDED."mint_a",
"mint_b" = EXCLUDED."mint_b",
"token_a_reserves" = EXCLUDED."token_a_reserves",
"token_b_reserves" = EXCLUDED."token_b_reserves",
"shift" = EXCLUDED."shift",
"royalties" = EXCLUDED."royalties",
"vertigo_fees" = EXCLUDED."vertigo_fees",
"bump" = EXCLUDED."bump",
"fee_params" = EXCLUDED."fee_params",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.enabled)
.bind(self.owner)
.bind(self.mint_a)
.bind(self.mint_b)
.bind(&self.token_a_reserves)
.bind(&self.token_b_reserves)
.bind(&self.shift)
.bind(&self.royalties)
.bind(&self.vertigo_fees)
.bind(self.bump)
.bind(&self.fee_params)
.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 PoolRow {
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_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 PoolRow {
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_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 PoolMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PoolMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS pool_account (
-- Account data
"enabled" BOOLEAN NOT NULL,
"owner" BYTEA NOT NULL,
"mint_a" BYTEA NOT NULL,
"mint_b" BYTEA NOT NULL,
"token_a_reserves" NUMERIC(39) NOT NULL,
"token_b_reserves" NUMERIC(39) NOT NULL,
"shift" NUMERIC(39) NOT NULL,
"royalties" NUMERIC(20) NOT NULL,
"vertigo_fees" NUMERIC(20) NOT NULL,
"bump" INT2 NOT NULL,
"fee_params" 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 pool_account"#)
.execute(connection)
.await?;
Ok(())
}
}