use carbon_core::{
account::AccountMetadata,
postgres::{metadata::AccountRowMetadata, primitives::Pubkey},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct OpenOrdersMapRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub user_key: Pubkey,
}
impl OpenOrdersMapRow {
pub fn from_parts(
source: crate::accounts::open_orders_map::OpenOrdersMap,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
user_key: source.user_key.into(),
}
}
}
impl TryFrom<OpenOrdersMapRow> for crate::accounts::open_orders_map::OpenOrdersMap {
type Error = carbon_core::error::Error;
fn try_from(source: OpenOrdersMapRow) -> Result<Self, Self::Error> {
Ok(Self {
user_key: *source.user_key,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::open_orders_map::OpenOrdersMap {
fn table() -> &'static str {
"open_orders_map_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "user_key"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for OpenOrdersMapRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO open_orders_map_account (
"user_key",
__pubkey, __slot
) VALUES (
$1, $2, $3
)"#,
)
.bind(self.user_key)
.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 OpenOrdersMapRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO open_orders_map_account (
"user_key",
__pubkey, __slot
) VALUES (
$1, $2, $3
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"user_key" = EXCLUDED."user_key",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.user_key)
.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 OpenOrdersMapRow {
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 open_orders_map_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 OpenOrdersMapRow {
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 open_orders_map_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 OpenOrdersMapMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for OpenOrdersMapMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS open_orders_map_account (
-- Account data
"user_key" 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 open_orders_map_account"#)
.execute(connection)
.await?;
Ok(())
}
}