use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct FeeTierRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub whirlpools_config: Pubkey,
pub tick_spacing: U16,
pub default_fee_rate: U16,
}
impl FeeTierRow {
pub fn from_parts(
source: crate::accounts::fee_tier::FeeTier,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
whirlpools_config: source.whirlpools_config.into(),
tick_spacing: source.tick_spacing.into(),
default_fee_rate: source.default_fee_rate.into(),
}
}
}
impl TryFrom<FeeTierRow> for crate::accounts::fee_tier::FeeTier {
type Error = carbon_core::error::Error;
fn try_from(source: FeeTierRow) -> Result<Self, Self::Error> {
Ok(Self {
whirlpools_config: *source.whirlpools_config,
tick_spacing: source.tick_spacing.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
default_fee_rate: source.default_fee_rate.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::fee_tier::FeeTier {
fn table() -> &'static str {
"fee_tier_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"whirlpools_config",
"tick_spacing",
"default_fee_rate",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for FeeTierRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO fee_tier_account (
"whirlpools_config",
"tick_spacing",
"default_fee_rate",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
)"#,
)
.bind(self.whirlpools_config)
.bind(self.tick_spacing)
.bind(self.default_fee_rate)
.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 FeeTierRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO fee_tier_account (
"whirlpools_config",
"tick_spacing",
"default_fee_rate",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"whirlpools_config" = EXCLUDED."whirlpools_config",
"tick_spacing" = EXCLUDED."tick_spacing",
"default_fee_rate" = EXCLUDED."default_fee_rate",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.whirlpools_config)
.bind(self.tick_spacing)
.bind(self.default_fee_rate)
.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 FeeTierRow {
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 fee_tier_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 FeeTierRow {
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 fee_tier_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 FeeTierMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for FeeTierMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS fee_tier_account (
-- Account data
"whirlpools_config" BYTEA NOT NULL,
"tick_spacing" INT4 NOT NULL,
"default_fee_rate" INT4 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 fee_tier_account"#)
.execute(connection)
.await?;
Ok(())
}
}