use {
crate::types::{Currency, CurveType, MigrationTarget},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U32, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct CurveAccountRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub total_supply: U64,
pub curve_amount: U64,
pub mint: Pubkey,
pub decimals: U8,
pub collateral_currency: sqlx::types::Json<Currency>,
pub curve_type: sqlx::types::Json<CurveType>,
pub marketcap_threshold: U64,
pub marketcap_currency: sqlx::types::Json<Currency>,
pub migration_fee: U64,
pub coef_b: U32,
pub bump: U8,
pub migration_target: sqlx::types::Json<MigrationTarget>,
}
impl CurveAccountRow {
pub fn from_parts(
source: crate::accounts::curve_account::CurveAccount,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
total_supply: source.total_supply.into(),
curve_amount: source.curve_amount.into(),
mint: source.mint.into(),
decimals: source.decimals.into(),
collateral_currency: sqlx::types::Json(source.collateral_currency),
curve_type: sqlx::types::Json(source.curve_type),
marketcap_threshold: source.marketcap_threshold.into(),
marketcap_currency: sqlx::types::Json(source.marketcap_currency),
migration_fee: source.migration_fee.into(),
coef_b: source.coef_b.into(),
bump: source.bump.into(),
migration_target: sqlx::types::Json(source.migration_target),
}
}
}
impl TryFrom<CurveAccountRow> for crate::accounts::curve_account::CurveAccount {
type Error = carbon_core::error::Error;
fn try_from(source: CurveAccountRow) -> Result<Self, Self::Error> {
Ok(Self {
total_supply: *source.total_supply,
curve_amount: *source.curve_amount,
mint: *source.mint,
decimals: source.decimals.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
collateral_currency: source.collateral_currency.0,
curve_type: source.curve_type.0,
marketcap_threshold: *source.marketcap_threshold,
marketcap_currency: source.marketcap_currency.0,
migration_fee: *source.migration_fee,
coef_b: source.coef_b.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
migration_target: source.migration_target.0,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::curve_account::CurveAccount {
fn table() -> &'static str {
"curve_account_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"total_supply",
"curve_amount",
"mint",
"decimals",
"collateral_currency",
"curve_type",
"marketcap_threshold",
"marketcap_currency",
"migration_fee",
"coef_b",
"bump",
"migration_target",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for CurveAccountRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO curve_account_account (
"total_supply",
"curve_amount",
"mint",
"decimals",
"collateral_currency",
"curve_type",
"marketcap_threshold",
"marketcap_currency",
"migration_fee",
"coef_b",
"bump",
"migration_target",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)"#,
)
.bind(&self.total_supply)
.bind(&self.curve_amount)
.bind(self.mint)
.bind(self.decimals)
.bind(&self.collateral_currency)
.bind(&self.curve_type)
.bind(&self.marketcap_threshold)
.bind(&self.marketcap_currency)
.bind(&self.migration_fee)
.bind(self.coef_b)
.bind(self.bump)
.bind(&self.migration_target)
.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 CurveAccountRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO curve_account_account (
"total_supply",
"curve_amount",
"mint",
"decimals",
"collateral_currency",
"curve_type",
"marketcap_threshold",
"marketcap_currency",
"migration_fee",
"coef_b",
"bump",
"migration_target",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"total_supply" = EXCLUDED."total_supply",
"curve_amount" = EXCLUDED."curve_amount",
"mint" = EXCLUDED."mint",
"decimals" = EXCLUDED."decimals",
"collateral_currency" = EXCLUDED."collateral_currency",
"curve_type" = EXCLUDED."curve_type",
"marketcap_threshold" = EXCLUDED."marketcap_threshold",
"marketcap_currency" = EXCLUDED."marketcap_currency",
"migration_fee" = EXCLUDED."migration_fee",
"coef_b" = EXCLUDED."coef_b",
"bump" = EXCLUDED."bump",
"migration_target" = EXCLUDED."migration_target",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.total_supply)
.bind(&self.curve_amount)
.bind(self.mint)
.bind(self.decimals)
.bind(&self.collateral_currency)
.bind(&self.curve_type)
.bind(&self.marketcap_threshold)
.bind(&self.marketcap_currency)
.bind(&self.migration_fee)
.bind(self.coef_b)
.bind(self.bump)
.bind(&self.migration_target)
.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 CurveAccountRow {
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 curve_account_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 CurveAccountRow {
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 curve_account_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 CurveAccountMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for CurveAccountMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS curve_account_account (
-- Account data
"total_supply" NUMERIC(20) NOT NULL,
"curve_amount" NUMERIC(20) NOT NULL,
"mint" BYTEA NOT NULL,
"decimals" INT2 NOT NULL,
"collateral_currency" JSONB NOT NULL,
"curve_type" JSONB NOT NULL,
"marketcap_threshold" NUMERIC(20) NOT NULL,
"marketcap_currency" JSONB NOT NULL,
"migration_fee" NUMERIC(20) NOT NULL,
"coef_b" INT8 NOT NULL,
"bump" INT2 NOT NULL,
"migration_target" JSONB 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 curve_account_account"#)
.execute(connection)
.await?;
Ok(())
}
}