use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct GlobalRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub initialized: bool,
pub authority: Pubkey,
pub fee_recipient: Pubkey,
pub initial_virtual_token_reserves: U64,
pub initial_virtual_sol_reserves: U64,
pub initial_real_token_reserves: U64,
pub token_total_supply: U64,
pub fee_basis_points: U64,
pub withdraw_authority: Pubkey,
pub enable_migrate: bool,
pub pool_migration_fee: U64,
pub creator_fee_basis_points: U64,
pub fee_recipients: Vec<Pubkey>,
pub set_creator_authority: Pubkey,
pub admin_set_creator_authority: Pubkey,
pub create_v2_enabled: bool,
pub whitelist_pda: Pubkey,
pub reserved_fee_recipient: Pubkey,
pub mayhem_mode_enabled: bool,
pub reserved_fee_recipients: Vec<Pubkey>,
}
impl GlobalRow {
pub fn from_parts(source: crate::accounts::global::Global, metadata: AccountMetadata) -> Self {
Self {
account_metadata: metadata.into(),
initialized: source.initialized,
authority: source.authority.into(),
fee_recipient: source.fee_recipient.into(),
initial_virtual_token_reserves: source.initial_virtual_token_reserves.into(),
initial_virtual_sol_reserves: source.initial_virtual_sol_reserves.into(),
initial_real_token_reserves: source.initial_real_token_reserves.into(),
token_total_supply: source.token_total_supply.into(),
fee_basis_points: source.fee_basis_points.into(),
withdraw_authority: source.withdraw_authority.into(),
enable_migrate: source.enable_migrate,
pool_migration_fee: source.pool_migration_fee.into(),
creator_fee_basis_points: source.creator_fee_basis_points.into(),
fee_recipients: source
.fee_recipients
.into_iter()
.map(|element| element.into())
.collect(),
set_creator_authority: source.set_creator_authority.into(),
admin_set_creator_authority: source.admin_set_creator_authority.into(),
create_v2_enabled: source.create_v2_enabled,
whitelist_pda: source.whitelist_pda.into(),
reserved_fee_recipient: source.reserved_fee_recipient.into(),
mayhem_mode_enabled: source.mayhem_mode_enabled,
reserved_fee_recipients: source
.reserved_fee_recipients
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<GlobalRow> for crate::accounts::global::Global {
type Error = carbon_core::error::Error;
fn try_from(source: GlobalRow) -> Result<Self, Self::Error> {
Ok(Self {
initialized: source.initialized,
authority: *source.authority,
fee_recipient: *source.fee_recipient,
initial_virtual_token_reserves: *source.initial_virtual_token_reserves,
initial_virtual_sol_reserves: *source.initial_virtual_sol_reserves,
initial_real_token_reserves: *source.initial_real_token_reserves,
token_total_supply: *source.token_total_supply,
fee_basis_points: *source.fee_basis_points,
withdraw_authority: *source.withdraw_authority,
enable_migrate: source.enable_migrate,
pool_migration_fee: *source.pool_migration_fee,
creator_fee_basis_points: *source.creator_fee_basis_points,
fee_recipients: source
.fee_recipients
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
set_creator_authority: *source.set_creator_authority,
admin_set_creator_authority: *source.admin_set_creator_authority,
create_v2_enabled: source.create_v2_enabled,
whitelist_pda: *source.whitelist_pda,
reserved_fee_recipient: *source.reserved_fee_recipient,
mayhem_mode_enabled: source.mayhem_mode_enabled,
reserved_fee_recipients: source
.reserved_fee_recipients
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::global::Global {
fn table() -> &'static str {
"global_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"initialized",
"authority",
"fee_recipient",
"initial_virtual_token_reserves",
"initial_virtual_sol_reserves",
"initial_real_token_reserves",
"token_total_supply",
"fee_basis_points",
"withdraw_authority",
"enable_migrate",
"pool_migration_fee",
"creator_fee_basis_points",
"fee_recipients",
"set_creator_authority",
"admin_set_creator_authority",
"create_v2_enabled",
"whitelist_pda",
"reserved_fee_recipient",
"mayhem_mode_enabled",
"reserved_fee_recipients",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for GlobalRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"
INSERT INTO global_account (
"initialized",
"authority",
"fee_recipient",
"initial_virtual_token_reserves",
"initial_virtual_sol_reserves",
"initial_real_token_reserves",
"token_total_supply",
"fee_basis_points",
"withdraw_authority",
"enable_migrate",
"pool_migration_fee",
"creator_fee_basis_points",
"fee_recipients",
"set_creator_authority",
"admin_set_creator_authority",
"create_v2_enabled",
"whitelist_pda",
"reserved_fee_recipient",
"mayhem_mode_enabled",
"reserved_fee_recipients",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22
)"#)
.bind(self.initialized)
.bind(self.authority)
.bind(self.fee_recipient)
.bind(&self.initial_virtual_token_reserves)
.bind(&self.initial_virtual_sol_reserves)
.bind(&self.initial_real_token_reserves)
.bind(&self.token_total_supply)
.bind(&self.fee_basis_points)
.bind(self.withdraw_authority)
.bind(self.enable_migrate)
.bind(&self.pool_migration_fee)
.bind(&self.creator_fee_basis_points)
.bind(&self.fee_recipients)
.bind(self.set_creator_authority)
.bind(self.admin_set_creator_authority)
.bind(self.create_v2_enabled)
.bind(self.whitelist_pda)
.bind(self.reserved_fee_recipient)
.bind(self.mayhem_mode_enabled)
.bind(&self.reserved_fee_recipients)
.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 GlobalRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO global_account (
"initialized",
"authority",
"fee_recipient",
"initial_virtual_token_reserves",
"initial_virtual_sol_reserves",
"initial_real_token_reserves",
"token_total_supply",
"fee_basis_points",
"withdraw_authority",
"enable_migrate",
"pool_migration_fee",
"creator_fee_basis_points",
"fee_recipients",
"set_creator_authority",
"admin_set_creator_authority",
"create_v2_enabled",
"whitelist_pda",
"reserved_fee_recipient",
"mayhem_mode_enabled",
"reserved_fee_recipients",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"initialized" = EXCLUDED."initialized",
"authority" = EXCLUDED."authority",
"fee_recipient" = EXCLUDED."fee_recipient",
"initial_virtual_token_reserves" = EXCLUDED."initial_virtual_token_reserves",
"initial_virtual_sol_reserves" = EXCLUDED."initial_virtual_sol_reserves",
"initial_real_token_reserves" = EXCLUDED."initial_real_token_reserves",
"token_total_supply" = EXCLUDED."token_total_supply",
"fee_basis_points" = EXCLUDED."fee_basis_points",
"withdraw_authority" = EXCLUDED."withdraw_authority",
"enable_migrate" = EXCLUDED."enable_migrate",
"pool_migration_fee" = EXCLUDED."pool_migration_fee",
"creator_fee_basis_points" = EXCLUDED."creator_fee_basis_points",
"fee_recipients" = EXCLUDED."fee_recipients",
"set_creator_authority" = EXCLUDED."set_creator_authority",
"admin_set_creator_authority" = EXCLUDED."admin_set_creator_authority",
"create_v2_enabled" = EXCLUDED."create_v2_enabled",
"whitelist_pda" = EXCLUDED."whitelist_pda",
"reserved_fee_recipient" = EXCLUDED."reserved_fee_recipient",
"mayhem_mode_enabled" = EXCLUDED."mayhem_mode_enabled",
"reserved_fee_recipients" = EXCLUDED."reserved_fee_recipients",
__slot = EXCLUDED.__slot
"#)
.bind(self.initialized)
.bind(self.authority)
.bind(self.fee_recipient)
.bind(&self.initial_virtual_token_reserves)
.bind(&self.initial_virtual_sol_reserves)
.bind(&self.initial_real_token_reserves)
.bind(&self.token_total_supply)
.bind(&self.fee_basis_points)
.bind(self.withdraw_authority)
.bind(self.enable_migrate)
.bind(&self.pool_migration_fee)
.bind(&self.creator_fee_basis_points)
.bind(&self.fee_recipients)
.bind(self.set_creator_authority)
.bind(self.admin_set_creator_authority)
.bind(self.create_v2_enabled)
.bind(self.whitelist_pda)
.bind(self.reserved_fee_recipient)
.bind(self.mayhem_mode_enabled)
.bind(&self.reserved_fee_recipients)
.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 GlobalRow {
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 global_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 GlobalRow {
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 global_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 GlobalMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for GlobalMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS global_account (
-- Account data
"initialized" BOOLEAN NOT NULL,
"authority" BYTEA NOT NULL,
"fee_recipient" BYTEA NOT NULL,
"initial_virtual_token_reserves" NUMERIC(20) NOT NULL,
"initial_virtual_sol_reserves" NUMERIC(20) NOT NULL,
"initial_real_token_reserves" NUMERIC(20) NOT NULL,
"token_total_supply" NUMERIC(20) NOT NULL,
"fee_basis_points" NUMERIC(20) NOT NULL,
"withdraw_authority" BYTEA NOT NULL,
"enable_migrate" BOOLEAN NOT NULL,
"pool_migration_fee" NUMERIC(20) NOT NULL,
"creator_fee_basis_points" NUMERIC(20) NOT NULL,
"fee_recipients" BYTEA[] NOT NULL,
"set_creator_authority" BYTEA NOT NULL,
"admin_set_creator_authority" BYTEA NOT NULL,
"create_v2_enabled" BOOLEAN NOT NULL,
"whitelist_pda" BYTEA NOT NULL,
"reserved_fee_recipient" BYTEA NOT NULL,
"mayhem_mode_enabled" BOOLEAN NOT NULL,
"reserved_fee_recipients" 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 global_account"#)
.execute(connection)
.await?;
Ok(())
}
}