use carbon_core::{
instruction::InstructionMetadata,
postgres::{
metadata::InstructionRowMetadata,
primitives::{U128, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SwapRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub amount: U64,
pub other_amount_threshold: U64,
pub sqrt_price_limit: U128,
pub amount_specified_is_input: bool,
pub a_to_b: bool,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl SwapRow {
pub fn from_parts(
source: crate::instructions::swap::Swap,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
amount: source.amount.into(),
other_amount_threshold: source.other_amount_threshold.into(),
sqrt_price_limit: source.sqrt_price_limit.into(),
amount_specified_is_input: source.amount_specified_is_input,
a_to_b: source.a_to_b,
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<SwapRow> for crate::instructions::swap::Swap {
type Error = carbon_core::error::Error;
fn try_from(source: SwapRow) -> Result<Self, Self::Error> {
Ok(Self {
amount: *source.amount,
other_amount_threshold: *source.other_amount_threshold,
sqrt_price_limit: *source.sqrt_price_limit,
amount_specified_is_input: source.amount_specified_is_input,
a_to_b: source.a_to_b,
})
}
}
impl carbon_core::postgres::operations::Table for crate::instructions::swap::Swap {
fn table() -> &'static str {
"swap_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"amount",
"other_amount_threshold",
"sqrt_price_limit",
"amount_specified_is_input",
"a_to_b",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SwapRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO swap_instruction (
"amount",
"other_amount_threshold",
"sqrt_price_limit",
"amount_specified_is_input",
"a_to_b",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
)"#,
)
.bind(&self.amount)
.bind(&self.other_amount_threshold)
.bind(&self.sqrt_price_limit)
.bind(self.amount_specified_is_input)
.bind(self.a_to_b)
.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 SwapRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO swap_instruction (
"amount",
"other_amount_threshold",
"sqrt_price_limit",
"amount_specified_is_input",
"a_to_b",
__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
"amount" = EXCLUDED."amount",
"other_amount_threshold" = EXCLUDED."other_amount_threshold",
"sqrt_price_limit" = EXCLUDED."sqrt_price_limit",
"amount_specified_is_input" = EXCLUDED."amount_specified_is_input",
"a_to_b" = EXCLUDED."a_to_b",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.bind(&self.amount)
.bind(&self.other_amount_threshold)
.bind(&self.sqrt_price_limit)
.bind(self.amount_specified_is_input)
.bind(self.a_to_b)
.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 SwapRow {
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 swap_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 SwapRow {
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 swap_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 SwapMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SwapMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS swap_instruction (
-- Instruction data
"amount" NUMERIC(20) NOT NULL,
"other_amount_threshold" NUMERIC(20) NOT NULL,
"sqrt_price_limit" NUMERIC(39) NOT NULL,
"amount_specified_is_input" BOOLEAN NOT NULL,
"a_to_b" BOOLEAN 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 swap_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}