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