use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct BondingCurveRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub virtual_token_reserves: U64,
pub virtual_sol_reserves: U64,
pub real_token_reserves: U64,
pub real_sol_reserves: U64,
pub token_total_supply: U64,
pub complete: bool,
pub creator: Pubkey,
pub is_mayhem_mode: bool,
pub is_cashback_coin: bool,
}
impl BondingCurveRow {
pub fn from_parts(
source: crate::accounts::bonding_curve::BondingCurve,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
virtual_token_reserves: source.virtual_token_reserves.into(),
virtual_sol_reserves: source.virtual_sol_reserves.into(),
real_token_reserves: source.real_token_reserves.into(),
real_sol_reserves: source.real_sol_reserves.into(),
token_total_supply: source.token_total_supply.into(),
complete: source.complete,
creator: source.creator.into(),
is_mayhem_mode: source.is_mayhem_mode,
is_cashback_coin: source.is_cashback_coin,
}
}
}
impl TryFrom<BondingCurveRow> for crate::accounts::bonding_curve::BondingCurve {
type Error = carbon_core::error::Error;
fn try_from(source: BondingCurveRow) -> Result<Self, Self::Error> {
Ok(Self {
virtual_token_reserves: *source.virtual_token_reserves,
virtual_sol_reserves: *source.virtual_sol_reserves,
real_token_reserves: *source.real_token_reserves,
real_sol_reserves: *source.real_sol_reserves,
token_total_supply: *source.token_total_supply,
complete: source.complete,
creator: *source.creator,
is_mayhem_mode: source.is_mayhem_mode,
is_cashback_coin: source.is_cashback_coin,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::bonding_curve::BondingCurve {
fn table() -> &'static str {
"bonding_curve_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"virtual_token_reserves",
"virtual_sol_reserves",
"real_token_reserves",
"real_sol_reserves",
"token_total_supply",
"complete",
"creator",
"is_mayhem_mode",
"is_cashback_coin",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for BondingCurveRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO bonding_curve_account (
"virtual_token_reserves",
"virtual_sol_reserves",
"real_token_reserves",
"real_sol_reserves",
"token_total_supply",
"complete",
"creator",
"is_mayhem_mode",
"is_cashback_coin",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
)"#,
)
.bind(&self.virtual_token_reserves)
.bind(&self.virtual_sol_reserves)
.bind(&self.real_token_reserves)
.bind(&self.real_sol_reserves)
.bind(&self.token_total_supply)
.bind(self.complete)
.bind(self.creator)
.bind(self.is_mayhem_mode)
.bind(self.is_cashback_coin)
.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 BondingCurveRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO bonding_curve_account (
"virtual_token_reserves",
"virtual_sol_reserves",
"real_token_reserves",
"real_sol_reserves",
"token_total_supply",
"complete",
"creator",
"is_mayhem_mode",
"is_cashback_coin",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"virtual_token_reserves" = EXCLUDED."virtual_token_reserves",
"virtual_sol_reserves" = EXCLUDED."virtual_sol_reserves",
"real_token_reserves" = EXCLUDED."real_token_reserves",
"real_sol_reserves" = EXCLUDED."real_sol_reserves",
"token_total_supply" = EXCLUDED."token_total_supply",
"complete" = EXCLUDED."complete",
"creator" = EXCLUDED."creator",
"is_mayhem_mode" = EXCLUDED."is_mayhem_mode",
"is_cashback_coin" = EXCLUDED."is_cashback_coin",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.virtual_token_reserves)
.bind(&self.virtual_sol_reserves)
.bind(&self.real_token_reserves)
.bind(&self.real_sol_reserves)
.bind(&self.token_total_supply)
.bind(self.complete)
.bind(self.creator)
.bind(self.is_mayhem_mode)
.bind(self.is_cashback_coin)
.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 BondingCurveRow {
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 bonding_curve_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 BondingCurveRow {
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 bonding_curve_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 BondingCurveMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for BondingCurveMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS bonding_curve_account (
-- Account data
"virtual_token_reserves" NUMERIC(20) NOT NULL,
"virtual_sol_reserves" NUMERIC(20) NOT NULL,
"real_token_reserves" NUMERIC(20) NOT NULL,
"real_sol_reserves" NUMERIC(20) NOT NULL,
"token_total_supply" NUMERIC(20) NOT NULL,
"complete" BOOLEAN NOT NULL,
"creator" BYTEA NOT NULL,
"is_mayhem_mode" BOOLEAN NOT NULL,
"is_cashback_coin" BOOLEAN 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 bonding_curve_account"#)
.execute(connection)
.await?;
Ok(())
}
}