use carbon_core::{
instruction::InstructionMetadata,
postgres::{metadata::InstructionRowMetadata, primitives::U64},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct TokenSellExactOutRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub amount_out: U64,
pub allow_partial_fill: bool,
pub price_threshold: Option<sqlx::types::Json<serde_json::Value>>,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl TokenSellExactOutRow {
pub fn from_parts(
source: crate::instructions::token_sell_exact_out::TokenSellExactOut,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
amount_out: source.amount_out.into(),
allow_partial_fill: source.allow_partial_fill,
price_threshold: source
.price_threshold
.map(|value| sqlx::types::Json(serde_json::to_value(value).unwrap())),
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<TokenSellExactOutRow>
for crate::instructions::token_sell_exact_out::TokenSellExactOut
{
type Error = carbon_core::error::Error;
fn try_from(source: TokenSellExactOutRow) -> Result<Self, Self::Error> {
Ok(Self {
amount_out: *source.amount_out,
allow_partial_fill: source.allow_partial_fill,
price_threshold: source
.price_threshold
.map(|value| {
serde_json::from_value(value.0).map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to deserialize tuple from JSON".to_string(),
)
})
})
.transpose()?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::instructions::token_sell_exact_out::TokenSellExactOut
{
fn table() -> &'static str {
"token_sell_exact_out_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"amount_out",
"allow_partial_fill",
"price_threshold",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for TokenSellExactOutRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO token_sell_exact_out_instruction (
"amount_out",
"allow_partial_fill",
"price_threshold",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)"#,
)
.bind(&self.amount_out)
.bind(self.allow_partial_fill)
.bind(&self.price_threshold)
.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 TokenSellExactOutRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO token_sell_exact_out_instruction (
"amount_out",
"allow_partial_fill",
"price_threshold",
__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
"amount_out" = EXCLUDED."amount_out",
"allow_partial_fill" = EXCLUDED."allow_partial_fill",
"price_threshold" = EXCLUDED."price_threshold",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.bind(&self.amount_out)
.bind(self.allow_partial_fill)
.bind(&self.price_threshold)
.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 TokenSellExactOutRow {
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 token_sell_exact_out_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 TokenSellExactOutRow {
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 token_sell_exact_out_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 TokenSellExactOutMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for TokenSellExactOutMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS token_sell_exact_out_instruction (
-- Instruction data
"amount_out" NUMERIC(20) NOT NULL,
"allow_partial_fill" BOOLEAN NOT NULL,
"price_threshold" JSONB,
-- 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 token_sell_exact_out_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}