use {
crate::types::GraduationMethod,
carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{Pubkey, U128, U16, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct BondingCurveInitializeRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub start_price: U128,
pub end_price: U128,
pub control_points: Vec<U16>,
pub creator: Pubkey,
pub graduation_methods: sqlx::types::Json<Vec<GraduationMethod>>,
pub swap_fee_bps: U16,
pub quote_fee_bps: U16,
pub base_fee_bps: U16,
pub launch_time: i64,
pub creator_reward: U64,
pub graduation_reward: U64,
pub graduation_target: U64,
pub graduation_time: i64,
pub min_reserve_bps: U16,
pub buy_requires_permission: bool,
pub buy_permission_bitmap: Vec<U8>,
pub sell_requires_permission: bool,
pub sell_permission_bitmap: Vec<U8>,
pub max_buy_amount: U64,
pub max_sell_amount: U64,
pub retain_mint_authority: bool,
pub base_allocation_bps: U16,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl BondingCurveInitializeRow {
pub fn from_parts(
source: crate::instructions::bonding_curve_initialize::BondingCurveInitialize,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
start_price: source.start_price.into(),
end_price: source.end_price.into(),
control_points: source
.control_points
.into_iter()
.map(|element| element.into())
.collect(),
creator: source.creator.into(),
graduation_methods: sqlx::types::Json(source.graduation_methods.to_vec()),
swap_fee_bps: source.swap_fee_bps.into(),
quote_fee_bps: source.quote_fee_bps.into(),
base_fee_bps: source.base_fee_bps.into(),
launch_time: source.launch_time,
creator_reward: source.creator_reward.into(),
graduation_reward: source.graduation_reward.into(),
graduation_target: source.graduation_target.into(),
graduation_time: source.graduation_time,
min_reserve_bps: source.min_reserve_bps.into(),
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(),
max_buy_amount: source.max_buy_amount.into(),
max_sell_amount: source.max_sell_amount.into(),
retain_mint_authority: source.retain_mint_authority,
base_allocation_bps: source.base_allocation_bps.into(),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<BondingCurveInitializeRow>
for crate::instructions::bonding_curve_initialize::BondingCurveInitialize
{
type Error = carbon_core::error::Error;
fn try_from(source: BondingCurveInitializeRow) -> Result<Self, Self::Error> {
Ok(Self {
start_price: *source.start_price,
end_price: *source.end_price,
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(),
)
})?,
creator: *source.creator,
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(),
)
})?,
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(),
)
})?,
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(),
)
})?,
launch_time: source.launch_time,
creator_reward: *source.creator_reward,
graduation_reward: *source.graduation_reward,
graduation_target: *source.graduation_target,
graduation_time: source.graduation_time,
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(),
)
})?,
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(),
)
})?,
max_buy_amount: *source.max_buy_amount,
max_sell_amount: *source.max_sell_amount,
retain_mint_authority: source.retain_mint_authority,
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(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::instructions::bonding_curve_initialize::BondingCurveInitialize
{
fn table() -> &'static str {
"bonding_curve_initialize_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"start_price",
"end_price",
"control_points",
"creator",
"graduation_methods",
"swap_fee_bps",
"quote_fee_bps",
"base_fee_bps",
"launch_time",
"creator_reward",
"graduation_reward",
"graduation_target",
"graduation_time",
"min_reserve_bps",
"buy_requires_permission",
"buy_permission_bitmap",
"sell_requires_permission",
"sell_permission_bitmap",
"max_buy_amount",
"max_sell_amount",
"retain_mint_authority",
"base_allocation_bps",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for BondingCurveInitializeRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO bonding_curve_initialize_instruction (
"start_price",
"end_price",
"control_points",
"creator",
"graduation_methods",
"swap_fee_bps",
"quote_fee_bps",
"base_fee_bps",
"launch_time",
"creator_reward",
"graduation_reward",
"graduation_target",
"graduation_time",
"min_reserve_bps",
"buy_requires_permission",
"buy_permission_bitmap",
"sell_requires_permission",
"sell_permission_bitmap",
"max_buy_amount",
"max_sell_amount",
"retain_mint_authority",
"base_allocation_bps",
__signature, __instruction_index, __stack_height, __slot, __accounts
) 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
)"#)
.bind(&self.start_price)
.bind(&self.end_price)
.bind(&self.control_points)
.bind(self.creator)
.bind(&self.graduation_methods)
.bind(self.swap_fee_bps)
.bind(self.quote_fee_bps)
.bind(self.base_fee_bps)
.bind(self.launch_time)
.bind(&self.creator_reward)
.bind(&self.graduation_reward)
.bind(&self.graduation_target)
.bind(self.graduation_time)
.bind(self.min_reserve_bps)
.bind(self.buy_requires_permission)
.bind(&self.buy_permission_bitmap)
.bind(self.sell_requires_permission)
.bind(&self.sell_permission_bitmap)
.bind(&self.max_buy_amount)
.bind(&self.max_sell_amount)
.bind(self.retain_mint_authority)
.bind(self.base_allocation_bps)
.bind(&self.instruction_metadata.signature)
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(&self.instruction_metadata.slot)
.bind(&self.accounts)
.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 BondingCurveInitializeRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO bonding_curve_initialize_instruction (
"start_price",
"end_price",
"control_points",
"creator",
"graduation_methods",
"swap_fee_bps",
"quote_fee_bps",
"base_fee_bps",
"launch_time",
"creator_reward",
"graduation_reward",
"graduation_target",
"graduation_time",
"min_reserve_bps",
"buy_requires_permission",
"buy_permission_bitmap",
"sell_requires_permission",
"sell_permission_bitmap",
"max_buy_amount",
"max_sell_amount",
"retain_mint_authority",
"base_allocation_bps",
__signature, __instruction_index, __stack_height, __slot, __accounts
) 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
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"start_price" = EXCLUDED."start_price",
"end_price" = EXCLUDED."end_price",
"control_points" = EXCLUDED."control_points",
"creator" = EXCLUDED."creator",
"graduation_methods" = EXCLUDED."graduation_methods",
"swap_fee_bps" = EXCLUDED."swap_fee_bps",
"quote_fee_bps" = EXCLUDED."quote_fee_bps",
"base_fee_bps" = EXCLUDED."base_fee_bps",
"launch_time" = EXCLUDED."launch_time",
"creator_reward" = EXCLUDED."creator_reward",
"graduation_reward" = EXCLUDED."graduation_reward",
"graduation_target" = EXCLUDED."graduation_target",
"graduation_time" = EXCLUDED."graduation_time",
"min_reserve_bps" = EXCLUDED."min_reserve_bps",
"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",
"max_buy_amount" = EXCLUDED."max_buy_amount",
"max_sell_amount" = EXCLUDED."max_sell_amount",
"retain_mint_authority" = EXCLUDED."retain_mint_authority",
"base_allocation_bps" = EXCLUDED."base_allocation_bps",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#)
.bind(&self.start_price)
.bind(&self.end_price)
.bind(&self.control_points)
.bind(self.creator)
.bind(&self.graduation_methods)
.bind(self.swap_fee_bps)
.bind(self.quote_fee_bps)
.bind(self.base_fee_bps)
.bind(self.launch_time)
.bind(&self.creator_reward)
.bind(&self.graduation_reward)
.bind(&self.graduation_target)
.bind(self.graduation_time)
.bind(self.min_reserve_bps)
.bind(self.buy_requires_permission)
.bind(&self.buy_permission_bitmap)
.bind(self.sell_requires_permission)
.bind(&self.sell_permission_bitmap)
.bind(&self.max_buy_amount)
.bind(&self.max_sell_amount)
.bind(self.retain_mint_authority)
.bind(self.base_allocation_bps)
.bind(&self.instruction_metadata.signature)
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(&self.instruction_metadata.slot)
.bind(&self.accounts)
.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 BondingCurveInitializeRow {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM bonding_curve_initialize_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.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 BondingCurveInitializeRow {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
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_initialize_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct BondingCurveInitializeMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for BondingCurveInitializeMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS bonding_curve_initialize_instruction (
-- Instruction data
"start_price" NUMERIC(39) NOT NULL,
"end_price" NUMERIC(39) NOT NULL,
"control_points" INT4[] NOT NULL,
"creator" BYTEA NOT NULL,
"graduation_methods" JSONB NOT NULL,
"swap_fee_bps" INT4 NOT NULL,
"quote_fee_bps" INT4 NOT NULL,
"base_fee_bps" INT4 NOT NULL,
"launch_time" INT8 NOT NULL,
"creator_reward" NUMERIC(20) NOT NULL,
"graduation_reward" NUMERIC(20) NOT NULL,
"graduation_target" NUMERIC(20) NOT NULL,
"graduation_time" INT8 NOT NULL,
"min_reserve_bps" INT4 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,
"max_buy_amount" NUMERIC(20) NOT NULL,
"max_sell_amount" NUMERIC(20) NOT NULL,
"retain_mint_authority" BOOLEAN NOT NULL,
"base_allocation_bps" INT4 NOT NULL,
-- Instruction metadata
__signature TEXT NOT NULL,
__instruction_index BIGINT NOT NULL,
__stack_height BIGINT NOT NULL,
__slot NUMERIC(20),
__accounts JSONB NOT NULL,
PRIMARY KEY (__signature, __instruction_index, __stack_height)
)"#,
)
.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_initialize_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}