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