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