use {
crate::types::{SpotFulfillmentConfigStatus, SpotFulfillmentType},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct PhoenixV1FulfillmentConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub pubkey: Pubkey,
pub phoenix_program_id: Pubkey,
pub phoenix_log_authority: Pubkey,
pub phoenix_market: Pubkey,
pub phoenix_base_vault: Pubkey,
pub phoenix_quote_vault: Pubkey,
pub market_index: U16,
pub fulfillment_type: sqlx::types::Json<SpotFulfillmentType>,
pub status: sqlx::types::Json<SpotFulfillmentConfigStatus>,
pub padding: Vec<u8>,
}
impl PhoenixV1FulfillmentConfigRow {
pub fn from_parts(
source: crate::accounts::phoenix_v1_fulfillment_config::PhoenixV1FulfillmentConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
pubkey: source.pubkey.into(),
phoenix_program_id: source.phoenix_program_id.into(),
phoenix_log_authority: source.phoenix_log_authority.into(),
phoenix_market: source.phoenix_market.into(),
phoenix_base_vault: source.phoenix_base_vault.into(),
phoenix_quote_vault: source.phoenix_quote_vault.into(),
market_index: source.market_index.into(),
fulfillment_type: sqlx::types::Json(source.fulfillment_type),
status: sqlx::types::Json(source.status),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<PhoenixV1FulfillmentConfigRow>
for crate::accounts::phoenix_v1_fulfillment_config::PhoenixV1FulfillmentConfig
{
type Error = carbon_core::error::Error;
fn try_from(source: PhoenixV1FulfillmentConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
pubkey: *source.pubkey,
phoenix_program_id: *source.phoenix_program_id,
phoenix_log_authority: *source.phoenix_log_authority,
phoenix_market: *source.phoenix_market,
phoenix_base_vault: *source.phoenix_base_vault,
phoenix_quote_vault: *source.phoenix_quote_vault,
market_index: source.market_index.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
fulfillment_type: source.fulfillment_type.0,
status: source.status.0,
padding: source.padding.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 4 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::phoenix_v1_fulfillment_config::PhoenixV1FulfillmentConfig
{
fn table() -> &'static str {
"phoenix_v1_fulfillment_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"pubkey",
"phoenix_program_id",
"phoenix_log_authority",
"phoenix_market",
"phoenix_base_vault",
"phoenix_quote_vault",
"market_index",
"fulfillment_type",
"status",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for PhoenixV1FulfillmentConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO phoenix_v1_fulfillment_config_account (
"pubkey",
"phoenix_program_id",
"phoenix_log_authority",
"phoenix_market",
"phoenix_base_vault",
"phoenix_quote_vault",
"market_index",
"fulfillment_type",
"status",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
)"#,
)
.bind(self.pubkey)
.bind(self.phoenix_program_id)
.bind(self.phoenix_log_authority)
.bind(self.phoenix_market)
.bind(self.phoenix_base_vault)
.bind(self.phoenix_quote_vault)
.bind(self.market_index)
.bind(&self.fulfillment_type)
.bind(&self.status)
.bind(&self.padding)
.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 PhoenixV1FulfillmentConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO phoenix_v1_fulfillment_config_account (
"pubkey",
"phoenix_program_id",
"phoenix_log_authority",
"phoenix_market",
"phoenix_base_vault",
"phoenix_quote_vault",
"market_index",
"fulfillment_type",
"status",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"pubkey" = EXCLUDED."pubkey",
"phoenix_program_id" = EXCLUDED."phoenix_program_id",
"phoenix_log_authority" = EXCLUDED."phoenix_log_authority",
"phoenix_market" = EXCLUDED."phoenix_market",
"phoenix_base_vault" = EXCLUDED."phoenix_base_vault",
"phoenix_quote_vault" = EXCLUDED."phoenix_quote_vault",
"market_index" = EXCLUDED."market_index",
"fulfillment_type" = EXCLUDED."fulfillment_type",
"status" = EXCLUDED."status",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.pubkey)
.bind(self.phoenix_program_id)
.bind(self.phoenix_log_authority)
.bind(self.phoenix_market)
.bind(self.phoenix_base_vault)
.bind(self.phoenix_quote_vault)
.bind(self.market_index)
.bind(&self.fulfillment_type)
.bind(&self.status)
.bind(&self.padding)
.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 PhoenixV1FulfillmentConfigRow {
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 phoenix_v1_fulfillment_config_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 PhoenixV1FulfillmentConfigRow {
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 phoenix_v1_fulfillment_config_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 PhoenixV1FulfillmentConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for PhoenixV1FulfillmentConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS phoenix_v1_fulfillment_config_account (
-- Account data
"pubkey" BYTEA NOT NULL,
"phoenix_program_id" BYTEA NOT NULL,
"phoenix_log_authority" BYTEA NOT NULL,
"phoenix_market" BYTEA NOT NULL,
"phoenix_base_vault" BYTEA NOT NULL,
"phoenix_quote_vault" BYTEA NOT NULL,
"market_index" INT4 NOT NULL,
"fulfillment_type" JSONB NOT NULL,
"status" JSONB NOT NULL,
"padding" 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 phoenix_v1_fulfillment_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}