use {
crate::types::GraduationMethod,
carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{U128, U16, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CreateLaunchRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub name: String,
pub symbol: String,
pub uri: String,
pub start_price: Option<U128>,
pub end_price: Option<U128>,
pub control_points: Option<Vec<U16>>,
pub graduation_target: Option<U64>,
pub graduation_methods: Option<sqlx::types::Json<Vec<GraduationMethod>>>,
pub launch_time: Option<i64>,
pub graduation_time: Option<i64>,
pub base_allocation_bps: Option<U16>,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl CreateLaunchRow {
pub fn from_parts(
source: crate::instructions::create_launch::CreateLaunch,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
name: source.name,
symbol: source.symbol,
uri: source.uri,
start_price: source.start_price.map(|value| value.into()),
end_price: source.end_price.map(|value| value.into()),
control_points: source
.control_points
.map(|value| value.into_iter().map(|element| element.into()).collect()),
graduation_target: source.graduation_target.map(|value| value.into()),
graduation_methods: source
.graduation_methods
.map(|value| sqlx::types::Json(value.to_vec())),
launch_time: source.launch_time,
graduation_time: source.graduation_time,
base_allocation_bps: source.base_allocation_bps.map(|value| value.into()),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<CreateLaunchRow> for crate::instructions::create_launch::CreateLaunch {
type Error = carbon_core::error::Error;
fn try_from(source: CreateLaunchRow) -> Result<Self, Self::Error> {
Ok(Self {
name: source.name,
symbol: source.symbol,
uri: source.uri,
start_price: source.start_price.map(|value| *value),
end_price: source.end_price.map(|value| *value),
control_points: source
.control_points
.map(|value| {
value
.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(),
)
})
})
.transpose()?,
graduation_target: source.graduation_target.map(|value| *value),
graduation_methods: source
.graduation_methods
.map(|value| {
value
.0
.into_iter()
.collect::<Vec<_>>()
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.transpose()?,
launch_time: source.launch_time,
graduation_time: source.graduation_time,
base_allocation_bps: source
.base_allocation_bps
.map(|value| {
value.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.transpose()?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::instructions::create_launch::CreateLaunch {
fn table() -> &'static str {
"create_launch_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"name",
"symbol",
"uri",
"start_price",
"end_price",
"control_points",
"graduation_target",
"graduation_methods",
"launch_time",
"graduation_time",
"base_allocation_bps",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CreateLaunchRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO create_launch_instruction (
"name",
"symbol",
"uri",
"start_price",
"end_price",
"control_points",
"graduation_target",
"graduation_methods",
"launch_time",
"graduation_time",
"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
)"#,
)
.bind(&self.name)
.bind(&self.symbol)
.bind(&self.uri)
.bind(&self.start_price)
.bind(&self.end_price)
.bind(&self.control_points)
.bind(&self.graduation_target)
.bind(&self.graduation_methods)
.bind(self.launch_time)
.bind(self.graduation_time)
.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 CreateLaunchRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO create_launch_instruction (
"name",
"symbol",
"uri",
"start_price",
"end_price",
"control_points",
"graduation_target",
"graduation_methods",
"launch_time",
"graduation_time",
"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
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"name" = EXCLUDED."name",
"symbol" = EXCLUDED."symbol",
"uri" = EXCLUDED."uri",
"start_price" = EXCLUDED."start_price",
"end_price" = EXCLUDED."end_price",
"control_points" = EXCLUDED."control_points",
"graduation_target" = EXCLUDED."graduation_target",
"graduation_methods" = EXCLUDED."graduation_methods",
"launch_time" = EXCLUDED."launch_time",
"graduation_time" = EXCLUDED."graduation_time",
"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.name)
.bind(&self.symbol)
.bind(&self.uri)
.bind(&self.start_price)
.bind(&self.end_price)
.bind(&self.control_points)
.bind(&self.graduation_target)
.bind(&self.graduation_methods)
.bind(self.launch_time)
.bind(self.graduation_time)
.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 CreateLaunchRow {
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 create_launch_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 CreateLaunchRow {
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 create_launch_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 CreateLaunchMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CreateLaunchMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS create_launch_instruction (
-- Instruction data
"name" TEXT NOT NULL,
"symbol" TEXT NOT NULL,
"uri" TEXT NOT NULL,
"start_price" NUMERIC(39),
"end_price" NUMERIC(39),
"control_points" INT4[],
"graduation_target" NUMERIC(20),
"graduation_methods" JSONB,
"launch_time" INT8,
"graduation_time" INT8,
"base_allocation_bps" INT4,
-- 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 create_launch_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}