use carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{Pubkey, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CreateConfigRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub lp_fee_basis_points: U64,
pub protocol_fee_basis_points: U64,
pub protocol_fee_recipients: Vec<Pubkey>,
pub coin_creator_fee_basis_points: U64,
pub admin_set_coin_creator_authority: Pubkey,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl CreateConfigRow {
pub fn from_parts(
source: crate::instructions::create_config::CreateConfig,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
lp_fee_basis_points: source.lp_fee_basis_points.into(),
protocol_fee_basis_points: source.protocol_fee_basis_points.into(),
protocol_fee_recipients: source
.protocol_fee_recipients
.into_iter()
.map(|element| element.into())
.collect(),
coin_creator_fee_basis_points: source.coin_creator_fee_basis_points.into(),
admin_set_coin_creator_authority: source.admin_set_coin_creator_authority.into(),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<CreateConfigRow> for crate::instructions::create_config::CreateConfig {
type Error = carbon_core::error::Error;
fn try_from(source: CreateConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
lp_fee_basis_points: *source.lp_fee_basis_points,
protocol_fee_basis_points: *source.protocol_fee_basis_points,
protocol_fee_recipients: source
.protocol_fee_recipients
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
coin_creator_fee_basis_points: *source.coin_creator_fee_basis_points,
admin_set_coin_creator_authority: *source.admin_set_coin_creator_authority,
})
}
}
impl carbon_core::postgres::operations::Table for crate::instructions::create_config::CreateConfig {
fn table() -> &'static str {
"create_config_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"lp_fee_basis_points",
"protocol_fee_basis_points",
"protocol_fee_recipients",
"coin_creator_fee_basis_points",
"admin_set_coin_creator_authority",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CreateConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO create_config_instruction (
"lp_fee_basis_points",
"protocol_fee_basis_points",
"protocol_fee_recipients",
"coin_creator_fee_basis_points",
"admin_set_coin_creator_authority",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
)"#,
)
.bind(&self.lp_fee_basis_points)
.bind(&self.protocol_fee_basis_points)
.bind(&self.protocol_fee_recipients)
.bind(&self.coin_creator_fee_basis_points)
.bind(self.admin_set_coin_creator_authority)
.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 CreateConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO create_config_instruction (
"lp_fee_basis_points",
"protocol_fee_basis_points",
"protocol_fee_recipients",
"coin_creator_fee_basis_points",
"admin_set_coin_creator_authority",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"lp_fee_basis_points" = EXCLUDED."lp_fee_basis_points",
"protocol_fee_basis_points" = EXCLUDED."protocol_fee_basis_points",
"protocol_fee_recipients" = EXCLUDED."protocol_fee_recipients",
"coin_creator_fee_basis_points" = EXCLUDED."coin_creator_fee_basis_points",
"admin_set_coin_creator_authority" = EXCLUDED."admin_set_coin_creator_authority",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.bind(&self.lp_fee_basis_points)
.bind(&self.protocol_fee_basis_points)
.bind(&self.protocol_fee_recipients)
.bind(&self.coin_creator_fee_basis_points)
.bind(self.admin_set_coin_creator_authority)
.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 CreateConfigRow {
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_config_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 CreateConfigRow {
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_config_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 CreateConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CreateConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS create_config_instruction (
-- Instruction data
"lp_fee_basis_points" NUMERIC(20) NOT NULL,
"protocol_fee_basis_points" NUMERIC(20) NOT NULL,
"protocol_fee_recipients" BYTEA[] NOT NULL,
"coin_creator_fee_basis_points" NUMERIC(20) NOT NULL,
"admin_set_coin_creator_authority" BYTEA 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 create_config_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}