use {
crate::types::OpenPositionWithMetadataBumps,
carbon_core::{instruction::InstructionMetadata, postgres::metadata::InstructionRowMetadata},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct OpenPositionWithMetadataRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub bumps: sqlx::types::Json<OpenPositionWithMetadataBumps>,
pub tick_lower_index: i32,
pub tick_upper_index: i32,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl OpenPositionWithMetadataRow {
pub fn from_parts(
source: crate::instructions::open_position_with_metadata::OpenPositionWithMetadata,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
bumps: sqlx::types::Json(source.bumps),
tick_lower_index: source.tick_lower_index,
tick_upper_index: source.tick_upper_index,
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<OpenPositionWithMetadataRow>
for crate::instructions::open_position_with_metadata::OpenPositionWithMetadata
{
type Error = carbon_core::error::Error;
fn try_from(source: OpenPositionWithMetadataRow) -> Result<Self, Self::Error> {
Ok(Self {
bumps: source.bumps.0,
tick_lower_index: source.tick_lower_index,
tick_upper_index: source.tick_upper_index,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::instructions::open_position_with_metadata::OpenPositionWithMetadata
{
fn table() -> &'static str {
"open_position_with_metadata_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"bumps",
"tick_lower_index",
"tick_upper_index",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for OpenPositionWithMetadataRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO open_position_with_metadata_instruction (
"bumps",
"tick_lower_index",
"tick_upper_index",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)"#,
)
.bind(&self.bumps)
.bind(self.tick_lower_index)
.bind(self.tick_upper_index)
.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 OpenPositionWithMetadataRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO open_position_with_metadata_instruction (
"bumps",
"tick_lower_index",
"tick_upper_index",
__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
"bumps" = EXCLUDED."bumps",
"tick_lower_index" = EXCLUDED."tick_lower_index",
"tick_upper_index" = EXCLUDED."tick_upper_index",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#,
)
.bind(&self.bumps)
.bind(self.tick_lower_index)
.bind(self.tick_upper_index)
.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 OpenPositionWithMetadataRow {
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_position_with_metadata_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 OpenPositionWithMetadataRow {
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_position_with_metadata_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 OpenPositionWithMetadataMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for OpenPositionWithMetadataMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS open_position_with_metadata_instruction (
-- Instruction data
"bumps" JSONB NOT NULL,
"tick_lower_index" INT4 NOT NULL,
"tick_upper_index" INT4 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 open_position_with_metadata_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}