use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct IfRebalanceConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub pubkey: Pubkey,
pub total_in_amount: U64,
pub current_in_amount: U64,
pub current_out_amount: U64,
pub current_out_amount_transferred: U64,
pub current_in_amount_since_last_transfer: U64,
pub epoch_start_ts: i64,
pub epoch_in_amount: U64,
pub epoch_max_in_amount: U64,
pub epoch_duration: i64,
pub out_market_index: U16,
pub in_market_index: U16,
pub max_slippage_bps: U16,
pub swap_mode: U8,
pub status: U8,
pub padding2: Vec<u8>,
}
impl IfRebalanceConfigRow {
pub fn from_parts(
source: crate::accounts::if_rebalance_config::IfRebalanceConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
pubkey: source.pubkey.into(),
total_in_amount: source.total_in_amount.into(),
current_in_amount: source.current_in_amount.into(),
current_out_amount: source.current_out_amount.into(),
current_out_amount_transferred: source.current_out_amount_transferred.into(),
current_in_amount_since_last_transfer: source
.current_in_amount_since_last_transfer
.into(),
epoch_start_ts: source.epoch_start_ts,
epoch_in_amount: source.epoch_in_amount.into(),
epoch_max_in_amount: source.epoch_max_in_amount.into(),
epoch_duration: source.epoch_duration,
out_market_index: source.out_market_index.into(),
in_market_index: source.in_market_index.into(),
max_slippage_bps: source.max_slippage_bps.into(),
swap_mode: source.swap_mode.into(),
status: source.status.into(),
padding2: source.padding2.to_vec(),
}
}
}
impl TryFrom<IfRebalanceConfigRow> for crate::accounts::if_rebalance_config::IfRebalanceConfig {
type Error = carbon_core::error::Error;
fn try_from(source: IfRebalanceConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
pubkey: *source.pubkey,
total_in_amount: *source.total_in_amount,
current_in_amount: *source.current_in_amount,
current_out_amount: *source.current_out_amount,
current_out_amount_transferred: *source.current_out_amount_transferred,
current_in_amount_since_last_transfer: *source.current_in_amount_since_last_transfer,
epoch_start_ts: source.epoch_start_ts,
epoch_in_amount: *source.epoch_in_amount,
epoch_max_in_amount: *source.epoch_max_in_amount,
epoch_duration: source.epoch_duration,
out_market_index: source.out_market_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
in_market_index: source.in_market_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
max_slippage_bps: source.max_slippage_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
swap_mode: source.swap_mode.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
status: source.status.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
padding2: source.padding2.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 32 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::if_rebalance_config::IfRebalanceConfig
{
fn table() -> &'static str {
"if_rebalance_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"pubkey",
"total_in_amount",
"current_in_amount",
"current_out_amount",
"current_out_amount_transferred",
"current_in_amount_since_last_transfer",
"epoch_start_ts",
"epoch_in_amount",
"epoch_max_in_amount",
"epoch_duration",
"out_market_index",
"in_market_index",
"max_slippage_bps",
"swap_mode",
"status",
"padding2",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for IfRebalanceConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO if_rebalance_config_account (
"pubkey",
"total_in_amount",
"current_in_amount",
"current_out_amount",
"current_out_amount_transferred",
"current_in_amount_since_last_transfer",
"epoch_start_ts",
"epoch_in_amount",
"epoch_max_in_amount",
"epoch_duration",
"out_market_index",
"in_market_index",
"max_slippage_bps",
"swap_mode",
"status",
"padding2",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
)"#,
)
.bind(self.pubkey)
.bind(&self.total_in_amount)
.bind(&self.current_in_amount)
.bind(&self.current_out_amount)
.bind(&self.current_out_amount_transferred)
.bind(&self.current_in_amount_since_last_transfer)
.bind(self.epoch_start_ts)
.bind(&self.epoch_in_amount)
.bind(&self.epoch_max_in_amount)
.bind(self.epoch_duration)
.bind(self.out_market_index)
.bind(self.in_market_index)
.bind(self.max_slippage_bps)
.bind(self.swap_mode)
.bind(self.status)
.bind(&self.padding2)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.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 IfRebalanceConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO if_rebalance_config_account (
"pubkey",
"total_in_amount",
"current_in_amount",
"current_out_amount",
"current_out_amount_transferred",
"current_in_amount_since_last_transfer",
"epoch_start_ts",
"epoch_in_amount",
"epoch_max_in_amount",
"epoch_duration",
"out_market_index",
"in_market_index",
"max_slippage_bps",
"swap_mode",
"status",
"padding2",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"pubkey" = EXCLUDED."pubkey",
"total_in_amount" = EXCLUDED."total_in_amount",
"current_in_amount" = EXCLUDED."current_in_amount",
"current_out_amount" = EXCLUDED."current_out_amount",
"current_out_amount_transferred" = EXCLUDED."current_out_amount_transferred",
"current_in_amount_since_last_transfer" = EXCLUDED."current_in_amount_since_last_transfer",
"epoch_start_ts" = EXCLUDED."epoch_start_ts",
"epoch_in_amount" = EXCLUDED."epoch_in_amount",
"epoch_max_in_amount" = EXCLUDED."epoch_max_in_amount",
"epoch_duration" = EXCLUDED."epoch_duration",
"out_market_index" = EXCLUDED."out_market_index",
"in_market_index" = EXCLUDED."in_market_index",
"max_slippage_bps" = EXCLUDED."max_slippage_bps",
"swap_mode" = EXCLUDED."swap_mode",
"status" = EXCLUDED."status",
"padding2" = EXCLUDED."padding2",
__slot = EXCLUDED.__slot
"#)
.bind(self.pubkey)
.bind(&self.total_in_amount)
.bind(&self.current_in_amount)
.bind(&self.current_out_amount)
.bind(&self.current_out_amount_transferred)
.bind(&self.current_in_amount_since_last_transfer)
.bind(self.epoch_start_ts)
.bind(&self.epoch_in_amount)
.bind(&self.epoch_max_in_amount)
.bind(self.epoch_duration)
.bind(self.out_market_index)
.bind(self.in_market_index)
.bind(self.max_slippage_bps)
.bind(self.swap_mode)
.bind(self.status)
.bind(&self.padding2)
.bind(self.account_metadata.pubkey)
.bind(&self.account_metadata.slot)
.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 IfRebalanceConfigRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM if_rebalance_config_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.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 IfRebalanceConfigRow {
type Key = carbon_core::postgres::primitives::Pubkey;
async fn lookup(
key: Self::Key,
pool: &sqlx::PgPool,
) -> carbon_core::error::CarbonResult<Option<Self>> {
let row = sqlx::query_as(
r#"SELECT * FROM if_rebalance_config_account WHERE
__pubkey = $1
"#,
)
.bind(key)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct IfRebalanceConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for IfRebalanceConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS if_rebalance_config_account (
-- Account data
"pubkey" BYTEA NOT NULL,
"total_in_amount" NUMERIC(20) NOT NULL,
"current_in_amount" NUMERIC(20) NOT NULL,
"current_out_amount" NUMERIC(20) NOT NULL,
"current_out_amount_transferred" NUMERIC(20) NOT NULL,
"current_in_amount_since_last_transfer" NUMERIC(20) NOT NULL,
"epoch_start_ts" INT8 NOT NULL,
"epoch_in_amount" NUMERIC(20) NOT NULL,
"epoch_max_in_amount" NUMERIC(20) NOT NULL,
"epoch_duration" INT8 NOT NULL,
"out_market_index" INT4 NOT NULL,
"in_market_index" INT4 NOT NULL,
"max_slippage_bps" INT4 NOT NULL,
"swap_mode" INT2 NOT NULL,
"status" INT2 NOT NULL,
"padding2" BYTEA NOT NULL,
-- Account metadata
__pubkey BYTEA NOT NULL,
__slot NUMERIC(20),
PRIMARY KEY (__pubkey)
)"#,
)
.execute(connection)
.await?;
Ok(())
}
async fn down(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(r#"DROP TABLE IF EXISTS if_rebalance_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}