use {
crate::types::{Asset, Side},
carbon_core::{
instruction::InstructionMetadata,
postgres::{metadata::InstructionRowMetadata, primitives::U128},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CancelOrderHaltedRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub side: sqlx::types::Json<Side>,
pub order_id: U128,
pub asset: sqlx::types::Json<Asset>,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl CancelOrderHaltedRow {
pub fn from_parts(
source: crate::instructions::cancel_order_halted::CancelOrderHalted,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
side: sqlx::types::Json(source.side),
order_id: source.order_id.into(),
asset: sqlx::types::Json(source.asset),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<CancelOrderHaltedRow> for crate::instructions::cancel_order_halted::CancelOrderHalted {
type Error = carbon_core::error::Error;
fn try_from(source: CancelOrderHaltedRow) -> Result<Self, Self::Error> {
Ok(Self {
side: source.side.0,
order_id: *source.order_id,
asset: source.asset.0,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::instructions::cancel_order_halted::CancelOrderHalted
{
fn table() -> &'static str {
"cancel_order_halted_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"side",
"order_id",
"asset",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CancelOrderHaltedRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO cancel_order_halted_instruction (
"side",
"order_id",
"asset",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)"#,
)
.bind(&self.side)
.bind(&self.order_id)
.bind(&self.asset)
.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 CancelOrderHaltedRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO cancel_order_halted_instruction (
"side",
"order_id",
"asset",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"side" = EXCLUDED."side",
"order_id" = EXCLUDED."order_id",
"asset" = EXCLUDED."asset",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.bind(&self.side)
.bind(&self.order_id)
.bind(&self.asset)
.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 CancelOrderHaltedRow {
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 cancel_order_halted_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 CancelOrderHaltedRow {
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 cancel_order_halted_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 CancelOrderHaltedMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CancelOrderHaltedMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS cancel_order_halted_instruction (
-- Instruction data
"side" JSONB NOT NULL,
"order_id" NUMERIC(39) NOT NULL,
"asset" JSONB 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 cancel_order_halted_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}