use carbon_core::{
instruction::InstructionMetadata,
postgres::{metadata::InstructionRowMetadata, primitives::U64},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct OpenDcaRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub application_idx: U64,
pub in_amount: U64,
pub in_amount_per_cycle: U64,
pub cycle_frequency: i64,
pub min_out_amount: Option<U64>,
pub max_out_amount: Option<U64>,
pub start_at: Option<i64>,
pub close_wsol_in_ata: Option<bool>,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl OpenDcaRow {
pub fn from_parts(
source: crate::instructions::open_dca::OpenDca,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
application_idx: source.application_idx.into(),
in_amount: source.in_amount.into(),
in_amount_per_cycle: source.in_amount_per_cycle.into(),
cycle_frequency: source.cycle_frequency,
min_out_amount: source.min_out_amount.map(|value| value.into()),
max_out_amount: source.max_out_amount.map(|value| value.into()),
start_at: source.start_at,
close_wsol_in_ata: source.close_wsol_in_ata,
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<OpenDcaRow> for crate::instructions::open_dca::OpenDca {
type Error = carbon_core::error::Error;
fn try_from(source: OpenDcaRow) -> Result<Self, Self::Error> {
Ok(Self {
application_idx: *source.application_idx,
in_amount: *source.in_amount,
in_amount_per_cycle: *source.in_amount_per_cycle,
cycle_frequency: source.cycle_frequency,
min_out_amount: source.min_out_amount.map(|value| *value),
max_out_amount: source.max_out_amount.map(|value| *value),
start_at: source.start_at,
close_wsol_in_ata: source.close_wsol_in_ata,
})
}
}
impl carbon_core::postgres::operations::Table for crate::instructions::open_dca::OpenDca {
fn table() -> &'static str {
"open_dca_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"application_idx",
"in_amount",
"in_amount_per_cycle",
"cycle_frequency",
"min_out_amount",
"max_out_amount",
"start_at",
"close_wsol_in_ata",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for OpenDcaRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO open_dca_instruction (
"application_idx",
"in_amount",
"in_amount_per_cycle",
"cycle_frequency",
"min_out_amount",
"max_out_amount",
"start_at",
"close_wsol_in_ata",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
)"#,
)
.bind(&self.application_idx)
.bind(&self.in_amount)
.bind(&self.in_amount_per_cycle)
.bind(self.cycle_frequency)
.bind(&self.min_out_amount)
.bind(&self.max_out_amount)
.bind(self.start_at)
.bind(self.close_wsol_in_ata)
.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 OpenDcaRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO open_dca_instruction (
"application_idx",
"in_amount",
"in_amount_per_cycle",
"cycle_frequency",
"min_out_amount",
"max_out_amount",
"start_at",
"close_wsol_in_ata",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"application_idx" = EXCLUDED."application_idx",
"in_amount" = EXCLUDED."in_amount",
"in_amount_per_cycle" = EXCLUDED."in_amount_per_cycle",
"cycle_frequency" = EXCLUDED."cycle_frequency",
"min_out_amount" = EXCLUDED."min_out_amount",
"max_out_amount" = EXCLUDED."max_out_amount",
"start_at" = EXCLUDED."start_at",
"close_wsol_in_ata" = EXCLUDED."close_wsol_in_ata",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.bind(&self.application_idx)
.bind(&self.in_amount)
.bind(&self.in_amount_per_cycle)
.bind(self.cycle_frequency)
.bind(&self.min_out_amount)
.bind(&self.max_out_amount)
.bind(self.start_at)
.bind(self.close_wsol_in_ata)
.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 OpenDcaRow {
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 open_dca_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 OpenDcaRow {
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 open_dca_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 OpenDcaMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for OpenDcaMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS open_dca_instruction (
-- Instruction data
"application_idx" NUMERIC(20) NOT NULL,
"in_amount" NUMERIC(20) NOT NULL,
"in_amount_per_cycle" NUMERIC(20) NOT NULL,
"cycle_frequency" INT8 NOT NULL,
"min_out_amount" NUMERIC(20),
"max_out_amount" NUMERIC(20),
"start_at" INT8,
"close_wsol_in_ata" BOOLEAN,
-- 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 open_dca_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}