use {
crate::types::Currency,
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U32, U64, U8},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ConfigAccountRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub migration_authority: Pubkey,
pub backend_authority: Pubkey,
pub config_authority: Pubkey,
pub helio_fee: Pubkey,
pub dex_fee: Pubkey,
pub fee_bps: U16,
pub dex_fee_share: U8,
pub migration_fee: U64,
pub marketcap_threshold: U64,
pub marketcap_currency: sqlx::types::Json<Currency>,
pub min_supported_decimal_places: U8,
pub max_supported_decimal_places: U8,
pub min_supported_token_supply: U64,
pub max_supported_token_supply: U64,
pub bump: U8,
pub coef_b: U32,
}
impl ConfigAccountRow {
pub fn from_parts(
source: crate::accounts::config_account::ConfigAccount,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
migration_authority: source.migration_authority.into(),
backend_authority: source.backend_authority.into(),
config_authority: source.config_authority.into(),
helio_fee: source.helio_fee.into(),
dex_fee: source.dex_fee.into(),
fee_bps: source.fee_bps.into(),
dex_fee_share: source.dex_fee_share.into(),
migration_fee: source.migration_fee.into(),
marketcap_threshold: source.marketcap_threshold.into(),
marketcap_currency: sqlx::types::Json(source.marketcap_currency),
min_supported_decimal_places: source.min_supported_decimal_places.into(),
max_supported_decimal_places: source.max_supported_decimal_places.into(),
min_supported_token_supply: source.min_supported_token_supply.into(),
max_supported_token_supply: source.max_supported_token_supply.into(),
bump: source.bump.into(),
coef_b: source.coef_b.into(),
}
}
}
impl TryFrom<ConfigAccountRow> for crate::accounts::config_account::ConfigAccount {
type Error = carbon_core::error::Error;
fn try_from(source: ConfigAccountRow) -> Result<Self, Self::Error> {
Ok(Self {
migration_authority: *source.migration_authority,
backend_authority: *source.backend_authority,
config_authority: *source.config_authority,
helio_fee: *source.helio_fee,
dex_fee: *source.dex_fee,
fee_bps: source.fee_bps.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
dex_fee_share: source.dex_fee_share.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
migration_fee: *source.migration_fee,
marketcap_threshold: *source.marketcap_threshold,
marketcap_currency: source.marketcap_currency.0,
min_supported_decimal_places: source.min_supported_decimal_places.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
max_supported_decimal_places: source.max_supported_decimal_places.try_into().map_err(
|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
},
)?,
min_supported_token_supply: *source.min_supported_token_supply,
max_supported_token_supply: *source.max_supported_token_supply,
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
coef_b: source.coef_b.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::config_account::ConfigAccount {
fn table() -> &'static str {
"config_account_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"migration_authority",
"backend_authority",
"config_authority",
"helio_fee",
"dex_fee",
"fee_bps",
"dex_fee_share",
"migration_fee",
"marketcap_threshold",
"marketcap_currency",
"min_supported_decimal_places",
"max_supported_decimal_places",
"min_supported_token_supply",
"max_supported_token_supply",
"bump",
"coef_b",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ConfigAccountRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO config_account_account (
"migration_authority",
"backend_authority",
"config_authority",
"helio_fee",
"dex_fee",
"fee_bps",
"dex_fee_share",
"migration_fee",
"marketcap_threshold",
"marketcap_currency",
"min_supported_decimal_places",
"max_supported_decimal_places",
"min_supported_token_supply",
"max_supported_token_supply",
"bump",
"coef_b",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
)"#,
)
.bind(self.migration_authority)
.bind(self.backend_authority)
.bind(self.config_authority)
.bind(self.helio_fee)
.bind(self.dex_fee)
.bind(self.fee_bps)
.bind(self.dex_fee_share)
.bind(&self.migration_fee)
.bind(&self.marketcap_threshold)
.bind(&self.marketcap_currency)
.bind(self.min_supported_decimal_places)
.bind(self.max_supported_decimal_places)
.bind(&self.min_supported_token_supply)
.bind(&self.max_supported_token_supply)
.bind(self.bump)
.bind(self.coef_b)
.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 ConfigAccountRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO config_account_account (
"migration_authority",
"backend_authority",
"config_authority",
"helio_fee",
"dex_fee",
"fee_bps",
"dex_fee_share",
"migration_fee",
"marketcap_threshold",
"marketcap_currency",
"min_supported_decimal_places",
"max_supported_decimal_places",
"min_supported_token_supply",
"max_supported_token_supply",
"bump",
"coef_b",
__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
"migration_authority" = EXCLUDED."migration_authority",
"backend_authority" = EXCLUDED."backend_authority",
"config_authority" = EXCLUDED."config_authority",
"helio_fee" = EXCLUDED."helio_fee",
"dex_fee" = EXCLUDED."dex_fee",
"fee_bps" = EXCLUDED."fee_bps",
"dex_fee_share" = EXCLUDED."dex_fee_share",
"migration_fee" = EXCLUDED."migration_fee",
"marketcap_threshold" = EXCLUDED."marketcap_threshold",
"marketcap_currency" = EXCLUDED."marketcap_currency",
"min_supported_decimal_places" = EXCLUDED."min_supported_decimal_places",
"max_supported_decimal_places" = EXCLUDED."max_supported_decimal_places",
"min_supported_token_supply" = EXCLUDED."min_supported_token_supply",
"max_supported_token_supply" = EXCLUDED."max_supported_token_supply",
"bump" = EXCLUDED."bump",
"coef_b" = EXCLUDED."coef_b",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.migration_authority)
.bind(self.backend_authority)
.bind(self.config_authority)
.bind(self.helio_fee)
.bind(self.dex_fee)
.bind(self.fee_bps)
.bind(self.dex_fee_share)
.bind(&self.migration_fee)
.bind(&self.marketcap_threshold)
.bind(&self.marketcap_currency)
.bind(self.min_supported_decimal_places)
.bind(self.max_supported_decimal_places)
.bind(&self.min_supported_token_supply)
.bind(&self.max_supported_token_supply)
.bind(self.bump)
.bind(self.coef_b)
.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 ConfigAccountRow {
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 config_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 ConfigAccountRow {
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 config_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 ConfigAccountMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ConfigAccountMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS config_account_account (
-- Account data
"migration_authority" BYTEA NOT NULL,
"backend_authority" BYTEA NOT NULL,
"config_authority" BYTEA NOT NULL,
"helio_fee" BYTEA NOT NULL,
"dex_fee" BYTEA NOT NULL,
"fee_bps" INT4 NOT NULL,
"dex_fee_share" INT2 NOT NULL,
"migration_fee" NUMERIC(20) NOT NULL,
"marketcap_threshold" NUMERIC(20) NOT NULL,
"marketcap_currency" JSONB NOT NULL,
"min_supported_decimal_places" INT2 NOT NULL,
"max_supported_decimal_places" INT2 NOT NULL,
"min_supported_token_supply" NUMERIC(20) NOT NULL,
"max_supported_token_supply" NUMERIC(20) NOT NULL,
"bump" INT2 NOT NULL,
"coef_b" INT8 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 config_account_account"#)
.execute(connection)
.await?;
Ok(())
}
}