use {
crate::types::GraduationMethodData,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U128, U16, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct BondingCurveRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub base_mint: Pubkey,
pub quote_mint: Pubkey,
pub creator: Pubkey,
pub retain_mint_authority: bool,
pub buy_requires_permission: bool,
pub buy_permission_bitmap: Vec<U8>,
pub sell_requires_permission: bool,
pub sell_permission_bitmap: Vec<U8>,
pub quote_fee_bps: U16,
pub base_fee_bps: U16,
pub control_points: Vec<U16>,
pub start_price: U128,
pub end_price: U128,
pub quote_amount: U64,
pub base_amount: U64,
pub launch_time: i64,
pub creator_reward: U64,
pub graduation_target: U64,
pub graduation_time: i64,
pub graduation_reward: U64,
pub max_buy_amount: U64,
pub max_sell_amount: U64,
pub swap_fee_bps: U16,
pub base_allocation_bps: U16,
pub graduation_methods: sqlx::types::Json<Vec<GraduationMethodData>>,
pub min_reserve_bps: U16,
pub padding1: Vec<U8>,
pub preminted_supply: U64,
pub padding2: Vec<U8>,
}
impl BondingCurveRow {
pub fn from_parts(
source: crate::accounts::bonding_curve::BondingCurve,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
base_mint: source.base_mint.into(),
quote_mint: source.quote_mint.into(),
creator: source.creator.into(),
retain_mint_authority: source.retain_mint_authority,
buy_requires_permission: source.buy_requires_permission,
buy_permission_bitmap: source
.buy_permission_bitmap
.into_iter()
.map(|element| element.into())
.collect(),
sell_requires_permission: source.sell_requires_permission,
sell_permission_bitmap: source
.sell_permission_bitmap
.into_iter()
.map(|element| element.into())
.collect(),
quote_fee_bps: source.quote_fee_bps.into(),
base_fee_bps: source.base_fee_bps.into(),
control_points: source
.control_points
.into_iter()
.map(|element| element.into())
.collect(),
start_price: source.start_price.into(),
end_price: source.end_price.into(),
quote_amount: source.quote_amount.into(),
base_amount: source.base_amount.into(),
launch_time: source.launch_time,
creator_reward: source.creator_reward.into(),
graduation_target: source.graduation_target.into(),
graduation_time: source.graduation_time,
graduation_reward: source.graduation_reward.into(),
max_buy_amount: source.max_buy_amount.into(),
max_sell_amount: source.max_sell_amount.into(),
swap_fee_bps: source.swap_fee_bps.into(),
base_allocation_bps: source.base_allocation_bps.into(),
graduation_methods: sqlx::types::Json(source.graduation_methods.to_vec()),
min_reserve_bps: source.min_reserve_bps.into(),
padding1: source
.padding1
.into_iter()
.map(|element| element.into())
.collect(),
preminted_supply: source.preminted_supply.into(),
padding2: source
.padding2
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
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 {
base_mint: *source.base_mint,
quote_mint: *source.quote_mint,
creator: *source.creator,
retain_mint_authority: source.retain_mint_authority,
buy_requires_permission: source.buy_requires_permission,
buy_permission_bitmap: source
.buy_permission_bitmap
.into_iter()
.map(|element| {
element.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.collect::<Result<Vec<_>, carbon_core::error::Error>>()?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
sell_requires_permission: source.sell_requires_permission,
sell_permission_bitmap: source
.sell_permission_bitmap
.into_iter()
.map(|element| {
element.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.collect::<Result<Vec<_>, carbon_core::error::Error>>()?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
quote_fee_bps: source.quote_fee_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
base_fee_bps: source.base_fee_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
control_points: source
.control_points
.into_iter()
.map(|element| {
element.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.collect::<Result<Vec<_>, carbon_core::error::Error>>()?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
start_price: *source.start_price,
end_price: *source.end_price,
quote_amount: *source.quote_amount,
base_amount: *source.base_amount,
launch_time: source.launch_time,
creator_reward: *source.creator_reward,
graduation_target: *source.graduation_target,
graduation_time: source.graduation_time,
graduation_reward: *source.graduation_reward,
max_buy_amount: *source.max_buy_amount,
max_sell_amount: *source.max_sell_amount,
swap_fee_bps: source.swap_fee_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
base_allocation_bps: source.base_allocation_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
graduation_methods: source
.graduation_methods
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
min_reserve_bps: source.min_reserve_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding1: source
.padding1
.into_iter()
.map(|element| {
element.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.collect::<Result<Vec<_>, carbon_core::error::Error>>()?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
preminted_supply: *source.preminted_supply,
padding2: source
.padding2
.into_iter()
.map(|element| {
element.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.collect::<Result<Vec<_>, carbon_core::error::Error>>()?
.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::bonding_curve::BondingCurve {
fn table() -> &'static str {
"bonding_curve_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"base_mint",
"quote_mint",
"creator",
"retain_mint_authority",
"buy_requires_permission",
"buy_permission_bitmap",
"sell_requires_permission",
"sell_permission_bitmap",
"quote_fee_bps",
"base_fee_bps",
"control_points",
"start_price",
"end_price",
"quote_amount",
"base_amount",
"launch_time",
"creator_reward",
"graduation_target",
"graduation_time",
"graduation_reward",
"max_buy_amount",
"max_sell_amount",
"swap_fee_bps",
"base_allocation_bps",
"graduation_methods",
"min_reserve_bps",
"padding1",
"preminted_supply",
"padding2",
]
}
}
#[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 (
"base_mint",
"quote_mint",
"creator",
"retain_mint_authority",
"buy_requires_permission",
"buy_permission_bitmap",
"sell_requires_permission",
"sell_permission_bitmap",
"quote_fee_bps",
"base_fee_bps",
"control_points",
"start_price",
"end_price",
"quote_amount",
"base_amount",
"launch_time",
"creator_reward",
"graduation_target",
"graduation_time",
"graduation_reward",
"max_buy_amount",
"max_sell_amount",
"swap_fee_bps",
"base_allocation_bps",
"graduation_methods",
"min_reserve_bps",
"padding1",
"preminted_supply",
"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
)"#)
.bind(self.base_mint)
.bind(self.quote_mint)
.bind(self.creator)
.bind(self.retain_mint_authority)
.bind(self.buy_requires_permission)
.bind(&self.buy_permission_bitmap)
.bind(self.sell_requires_permission)
.bind(&self.sell_permission_bitmap)
.bind(self.quote_fee_bps)
.bind(self.base_fee_bps)
.bind(&self.control_points)
.bind(&self.start_price)
.bind(&self.end_price)
.bind(&self.quote_amount)
.bind(&self.base_amount)
.bind(self.launch_time)
.bind(&self.creator_reward)
.bind(&self.graduation_target)
.bind(self.graduation_time)
.bind(&self.graduation_reward)
.bind(&self.max_buy_amount)
.bind(&self.max_sell_amount)
.bind(self.swap_fee_bps)
.bind(self.base_allocation_bps)
.bind(&self.graduation_methods)
.bind(self.min_reserve_bps)
.bind(&self.padding1)
.bind(&self.preminted_supply)
.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 BondingCurveRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO bonding_curve_account (
"base_mint",
"quote_mint",
"creator",
"retain_mint_authority",
"buy_requires_permission",
"buy_permission_bitmap",
"sell_requires_permission",
"sell_permission_bitmap",
"quote_fee_bps",
"base_fee_bps",
"control_points",
"start_price",
"end_price",
"quote_amount",
"base_amount",
"launch_time",
"creator_reward",
"graduation_target",
"graduation_time",
"graduation_reward",
"max_buy_amount",
"max_sell_amount",
"swap_fee_bps",
"base_allocation_bps",
"graduation_methods",
"min_reserve_bps",
"padding1",
"preminted_supply",
"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
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"base_mint" = EXCLUDED."base_mint",
"quote_mint" = EXCLUDED."quote_mint",
"creator" = EXCLUDED."creator",
"retain_mint_authority" = EXCLUDED."retain_mint_authority",
"buy_requires_permission" = EXCLUDED."buy_requires_permission",
"buy_permission_bitmap" = EXCLUDED."buy_permission_bitmap",
"sell_requires_permission" = EXCLUDED."sell_requires_permission",
"sell_permission_bitmap" = EXCLUDED."sell_permission_bitmap",
"quote_fee_bps" = EXCLUDED."quote_fee_bps",
"base_fee_bps" = EXCLUDED."base_fee_bps",
"control_points" = EXCLUDED."control_points",
"start_price" = EXCLUDED."start_price",
"end_price" = EXCLUDED."end_price",
"quote_amount" = EXCLUDED."quote_amount",
"base_amount" = EXCLUDED."base_amount",
"launch_time" = EXCLUDED."launch_time",
"creator_reward" = EXCLUDED."creator_reward",
"graduation_target" = EXCLUDED."graduation_target",
"graduation_time" = EXCLUDED."graduation_time",
"graduation_reward" = EXCLUDED."graduation_reward",
"max_buy_amount" = EXCLUDED."max_buy_amount",
"max_sell_amount" = EXCLUDED."max_sell_amount",
"swap_fee_bps" = EXCLUDED."swap_fee_bps",
"base_allocation_bps" = EXCLUDED."base_allocation_bps",
"graduation_methods" = EXCLUDED."graduation_methods",
"min_reserve_bps" = EXCLUDED."min_reserve_bps",
"padding1" = EXCLUDED."padding1",
"preminted_supply" = EXCLUDED."preminted_supply",
"padding2" = EXCLUDED."padding2",
__slot = EXCLUDED.__slot
"#)
.bind(self.base_mint)
.bind(self.quote_mint)
.bind(self.creator)
.bind(self.retain_mint_authority)
.bind(self.buy_requires_permission)
.bind(&self.buy_permission_bitmap)
.bind(self.sell_requires_permission)
.bind(&self.sell_permission_bitmap)
.bind(self.quote_fee_bps)
.bind(self.base_fee_bps)
.bind(&self.control_points)
.bind(&self.start_price)
.bind(&self.end_price)
.bind(&self.quote_amount)
.bind(&self.base_amount)
.bind(self.launch_time)
.bind(&self.creator_reward)
.bind(&self.graduation_target)
.bind(self.graduation_time)
.bind(&self.graduation_reward)
.bind(&self.max_buy_amount)
.bind(&self.max_sell_amount)
.bind(self.swap_fee_bps)
.bind(self.base_allocation_bps)
.bind(&self.graduation_methods)
.bind(self.min_reserve_bps)
.bind(&self.padding1)
.bind(&self.preminted_supply)
.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 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
"base_mint" BYTEA NOT NULL,
"quote_mint" BYTEA NOT NULL,
"creator" BYTEA NOT NULL,
"retain_mint_authority" BOOLEAN NOT NULL,
"buy_requires_permission" BOOLEAN NOT NULL,
"buy_permission_bitmap" INT2[] NOT NULL,
"sell_requires_permission" BOOLEAN NOT NULL,
"sell_permission_bitmap" INT2[] NOT NULL,
"quote_fee_bps" INT4 NOT NULL,
"base_fee_bps" INT4 NOT NULL,
"control_points" INT4[] NOT NULL,
"start_price" NUMERIC(39) NOT NULL,
"end_price" NUMERIC(39) NOT NULL,
"quote_amount" NUMERIC(20) NOT NULL,
"base_amount" NUMERIC(20) NOT NULL,
"launch_time" INT8 NOT NULL,
"creator_reward" NUMERIC(20) NOT NULL,
"graduation_target" NUMERIC(20) NOT NULL,
"graduation_time" INT8 NOT NULL,
"graduation_reward" NUMERIC(20) NOT NULL,
"max_buy_amount" NUMERIC(20) NOT NULL,
"max_sell_amount" NUMERIC(20) NOT NULL,
"swap_fee_bps" INT4 NOT NULL,
"base_allocation_bps" INT4 NOT NULL,
"graduation_methods" JSONB NOT NULL,
"min_reserve_bps" INT4 NOT NULL,
"padding1" INT2[] NOT NULL,
"preminted_supply" NUMERIC(20) NOT NULL,
"padding2" INT2[] 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(())
}
}