use {
crate::types::{SpotFulfillmentConfigStatus, SpotFulfillmentType},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U16, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SerumV3FulfillmentConfigRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub pubkey: Pubkey,
pub serum_program_id: Pubkey,
pub serum_market: Pubkey,
pub serum_request_queue: Pubkey,
pub serum_event_queue: Pubkey,
pub serum_bids: Pubkey,
pub serum_asks: Pubkey,
pub serum_base_vault: Pubkey,
pub serum_quote_vault: Pubkey,
pub serum_open_orders: Pubkey,
pub serum_signer_nonce: U64,
pub market_index: U16,
pub fulfillment_type: sqlx::types::Json<SpotFulfillmentType>,
pub status: sqlx::types::Json<SpotFulfillmentConfigStatus>,
pub padding: Vec<u8>,
}
impl SerumV3FulfillmentConfigRow {
pub fn from_parts(
source: crate::accounts::serum_v3_fulfillment_config::SerumV3FulfillmentConfig,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
pubkey: source.pubkey.into(),
serum_program_id: source.serum_program_id.into(),
serum_market: source.serum_market.into(),
serum_request_queue: source.serum_request_queue.into(),
serum_event_queue: source.serum_event_queue.into(),
serum_bids: source.serum_bids.into(),
serum_asks: source.serum_asks.into(),
serum_base_vault: source.serum_base_vault.into(),
serum_quote_vault: source.serum_quote_vault.into(),
serum_open_orders: source.serum_open_orders.into(),
serum_signer_nonce: source.serum_signer_nonce.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<SerumV3FulfillmentConfigRow>
for crate::accounts::serum_v3_fulfillment_config::SerumV3FulfillmentConfig
{
type Error = carbon_core::error::Error;
fn try_from(source: SerumV3FulfillmentConfigRow) -> Result<Self, Self::Error> {
Ok(Self {
pubkey: *source.pubkey,
serum_program_id: *source.serum_program_id,
serum_market: *source.serum_market,
serum_request_queue: *source.serum_request_queue,
serum_event_queue: *source.serum_event_queue,
serum_bids: *source.serum_bids,
serum_asks: *source.serum_asks,
serum_base_vault: *source.serum_base_vault,
serum_quote_vault: *source.serum_quote_vault,
serum_open_orders: *source.serum_open_orders,
serum_signer_nonce: *source.serum_signer_nonce,
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::serum_v3_fulfillment_config::SerumV3FulfillmentConfig
{
fn table() -> &'static str {
"serum_v3_fulfillment_config_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"pubkey",
"serum_program_id",
"serum_market",
"serum_request_queue",
"serum_event_queue",
"serum_bids",
"serum_asks",
"serum_base_vault",
"serum_quote_vault",
"serum_open_orders",
"serum_signer_nonce",
"market_index",
"fulfillment_type",
"status",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SerumV3FulfillmentConfigRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO serum_v3_fulfillment_config_account (
"pubkey",
"serum_program_id",
"serum_market",
"serum_request_queue",
"serum_event_queue",
"serum_bids",
"serum_asks",
"serum_base_vault",
"serum_quote_vault",
"serum_open_orders",
"serum_signer_nonce",
"market_index",
"fulfillment_type",
"status",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
)"#,
)
.bind(self.pubkey)
.bind(self.serum_program_id)
.bind(self.serum_market)
.bind(self.serum_request_queue)
.bind(self.serum_event_queue)
.bind(self.serum_bids)
.bind(self.serum_asks)
.bind(self.serum_base_vault)
.bind(self.serum_quote_vault)
.bind(self.serum_open_orders)
.bind(&self.serum_signer_nonce)
.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 SerumV3FulfillmentConfigRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO serum_v3_fulfillment_config_account (
"pubkey",
"serum_program_id",
"serum_market",
"serum_request_queue",
"serum_event_queue",
"serum_bids",
"serum_asks",
"serum_base_vault",
"serum_quote_vault",
"serum_open_orders",
"serum_signer_nonce",
"market_index",
"fulfillment_type",
"status",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"pubkey" = EXCLUDED."pubkey",
"serum_program_id" = EXCLUDED."serum_program_id",
"serum_market" = EXCLUDED."serum_market",
"serum_request_queue" = EXCLUDED."serum_request_queue",
"serum_event_queue" = EXCLUDED."serum_event_queue",
"serum_bids" = EXCLUDED."serum_bids",
"serum_asks" = EXCLUDED."serum_asks",
"serum_base_vault" = EXCLUDED."serum_base_vault",
"serum_quote_vault" = EXCLUDED."serum_quote_vault",
"serum_open_orders" = EXCLUDED."serum_open_orders",
"serum_signer_nonce" = EXCLUDED."serum_signer_nonce",
"market_index" = EXCLUDED."market_index",
"fulfillment_type" = EXCLUDED."fulfillment_type",
"status" = EXCLUDED."status",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.pubkey)
.bind(self.serum_program_id)
.bind(self.serum_market)
.bind(self.serum_request_queue)
.bind(self.serum_event_queue)
.bind(self.serum_bids)
.bind(self.serum_asks)
.bind(self.serum_base_vault)
.bind(self.serum_quote_vault)
.bind(self.serum_open_orders)
.bind(&self.serum_signer_nonce)
.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 SerumV3FulfillmentConfigRow {
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 serum_v3_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 SerumV3FulfillmentConfigRow {
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 serum_v3_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 SerumV3FulfillmentConfigMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SerumV3FulfillmentConfigMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS serum_v3_fulfillment_config_account (
-- Account data
"pubkey" BYTEA NOT NULL,
"serum_program_id" BYTEA NOT NULL,
"serum_market" BYTEA NOT NULL,
"serum_request_queue" BYTEA NOT NULL,
"serum_event_queue" BYTEA NOT NULL,
"serum_bids" BYTEA NOT NULL,
"serum_asks" BYTEA NOT NULL,
"serum_base_vault" BYTEA NOT NULL,
"serum_quote_vault" BYTEA NOT NULL,
"serum_open_orders" BYTEA NOT NULL,
"serum_signer_nonce" NUMERIC(20) 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 serum_v3_fulfillment_config_account"#)
.execute(connection)
.await?;
Ok(())
}
}