use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U32, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct OpenOrdersIndexerRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub bump: U8,
pub created_counter: U32,
pub addresses: Vec<Pubkey>,
}
impl OpenOrdersIndexerRow {
pub fn from_parts(
source: crate::accounts::open_orders_indexer::OpenOrdersIndexer,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
bump: source.bump.into(),
created_counter: source.created_counter.into(),
addresses: source
.addresses
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<OpenOrdersIndexerRow> for crate::accounts::open_orders_indexer::OpenOrdersIndexer {
type Error = carbon_core::error::Error;
fn try_from(source: OpenOrdersIndexerRow) -> Result<Self, Self::Error> {
Ok(Self {
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
created_counter: source.created_counter.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
addresses: source
.addresses
.into_iter()
.map(|element| *element)
.collect(),
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::open_orders_indexer::OpenOrdersIndexer
{
fn table() -> &'static str {
"open_orders_indexer_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "bump", "created_counter", "addresses"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for OpenOrdersIndexerRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO open_orders_indexer_account (
"bump",
"created_counter",
"addresses",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
)"#,
)
.bind(self.bump)
.bind(self.created_counter)
.bind(&self.addresses)
.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 OpenOrdersIndexerRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO open_orders_indexer_account (
"bump",
"created_counter",
"addresses",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"bump" = EXCLUDED."bump",
"created_counter" = EXCLUDED."created_counter",
"addresses" = EXCLUDED."addresses",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.bump)
.bind(self.created_counter)
.bind(&self.addresses)
.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 OpenOrdersIndexerRow {
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_indexer_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 OpenOrdersIndexerRow {
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_indexer_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 OpenOrdersIndexerMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for OpenOrdersIndexerMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS open_orders_indexer_account (
-- Account data
"bump" INT2 NOT NULL,
"created_counter" INT8 NOT NULL,
"addresses" 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_indexer_account"#)
.execute(connection)
.await?;
Ok(())
}
}