use {
crate::types::BondingCurveStatus,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct BondingCurveRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub creator: Pubkey,
pub mint: Pubkey,
pub virtual_sol_reserves: U64,
pub virtual_token_reserves: U64,
pub graduation_target: U64,
pub graduation_fee: U64,
pub sol_reserves: U64,
pub token_reserves: U64,
pub damping_term: U8,
pub swap_fee_basis_points: U8,
pub token_for_stakers_basis_points: U16,
pub status: sqlx::types::Json<BondingCurveStatus>,
}
impl BondingCurveRow {
pub fn from_parts(
source: crate::accounts::bonding_curve::BondingCurve,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
creator: source.creator.into(),
mint: source.mint.into(),
virtual_sol_reserves: source.virtual_sol_reserves.into(),
virtual_token_reserves: source.virtual_token_reserves.into(),
graduation_target: source.graduation_target.into(),
graduation_fee: source.graduation_fee.into(),
sol_reserves: source.sol_reserves.into(),
token_reserves: source.token_reserves.into(),
damping_term: source.damping_term.into(),
swap_fee_basis_points: source.swap_fee_basis_points.into(),
token_for_stakers_basis_points: source.token_for_stakers_basis_points.into(),
status: sqlx::types::Json(source.status),
}
}
}
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 {
creator: *source.creator,
mint: *source.mint,
virtual_sol_reserves: *source.virtual_sol_reserves,
virtual_token_reserves: *source.virtual_token_reserves,
graduation_target: *source.graduation_target,
graduation_fee: *source.graduation_fee,
sol_reserves: *source.sol_reserves,
token_reserves: *source.token_reserves,
damping_term: source.damping_term.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
swap_fee_basis_points: source.swap_fee_basis_points.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
token_for_stakers_basis_points: source
.token_for_stakers_basis_points
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
status: source.status.0,
})
}
}
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",
"creator",
"mint",
"virtual_sol_reserves",
"virtual_token_reserves",
"graduation_target",
"graduation_fee",
"sol_reserves",
"token_reserves",
"damping_term",
"swap_fee_basis_points",
"token_for_stakers_basis_points",
"status",
]
}
}
#[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 (
"creator",
"mint",
"virtual_sol_reserves",
"virtual_token_reserves",
"graduation_target",
"graduation_fee",
"sol_reserves",
"token_reserves",
"damping_term",
"swap_fee_basis_points",
"token_for_stakers_basis_points",
"status",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)"#,
)
.bind(self.creator)
.bind(self.mint)
.bind(&self.virtual_sol_reserves)
.bind(&self.virtual_token_reserves)
.bind(&self.graduation_target)
.bind(&self.graduation_fee)
.bind(&self.sol_reserves)
.bind(&self.token_reserves)
.bind(self.damping_term)
.bind(self.swap_fee_basis_points)
.bind(self.token_for_stakers_basis_points)
.bind(&self.status)
.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 (
"creator",
"mint",
"virtual_sol_reserves",
"virtual_token_reserves",
"graduation_target",
"graduation_fee",
"sol_reserves",
"token_reserves",
"damping_term",
"swap_fee_basis_points",
"token_for_stakers_basis_points",
"status",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"creator" = EXCLUDED."creator",
"mint" = EXCLUDED."mint",
"virtual_sol_reserves" = EXCLUDED."virtual_sol_reserves",
"virtual_token_reserves" = EXCLUDED."virtual_token_reserves",
"graduation_target" = EXCLUDED."graduation_target",
"graduation_fee" = EXCLUDED."graduation_fee",
"sol_reserves" = EXCLUDED."sol_reserves",
"token_reserves" = EXCLUDED."token_reserves",
"damping_term" = EXCLUDED."damping_term",
"swap_fee_basis_points" = EXCLUDED."swap_fee_basis_points",
"token_for_stakers_basis_points" = EXCLUDED."token_for_stakers_basis_points",
"status" = EXCLUDED."status",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.creator)
.bind(self.mint)
.bind(&self.virtual_sol_reserves)
.bind(&self.virtual_token_reserves)
.bind(&self.graduation_target)
.bind(&self.graduation_fee)
.bind(&self.sol_reserves)
.bind(&self.token_reserves)
.bind(self.damping_term)
.bind(self.swap_fee_basis_points)
.bind(self.token_for_stakers_basis_points)
.bind(&self.status)
.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
"creator" BYTEA NOT NULL,
"mint" BYTEA NOT NULL,
"virtual_sol_reserves" NUMERIC(20) NOT NULL,
"virtual_token_reserves" NUMERIC(20) NOT NULL,
"graduation_target" NUMERIC(20) NOT NULL,
"graduation_fee" NUMERIC(20) NOT NULL,
"sol_reserves" NUMERIC(20) NOT NULL,
"token_reserves" NUMERIC(20) NOT NULL,
"damping_term" INT2 NOT NULL,
"swap_fee_basis_points" INT2 NOT NULL,
"token_for_stakers_basis_points" INT4 NOT NULL,
"status" 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 bonding_curve_account"#)
.execute(connection)
.await?;
Ok(())
}
}