use {
crate::types::{AdaptiveFeeConstants, AdaptiveFeeVariables},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct OracleRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub whirlpool: Pubkey,
pub trade_enable_timestamp: U64,
pub adaptive_fee_constants: sqlx::types::Json<AdaptiveFeeConstants>,
pub adaptive_fee_variables: sqlx::types::Json<AdaptiveFeeVariables>,
pub reserved: Vec<u8>,
}
impl OracleRow {
pub fn from_parts(source: crate::accounts::oracle::Oracle, metadata: AccountMetadata) -> Self {
Self {
account_metadata: metadata.into(),
whirlpool: source.whirlpool.into(),
trade_enable_timestamp: source.trade_enable_timestamp.into(),
adaptive_fee_constants: sqlx::types::Json(source.adaptive_fee_constants),
adaptive_fee_variables: sqlx::types::Json(source.adaptive_fee_variables),
reserved: source.reserved.to_vec(),
}
}
}
impl TryFrom<OracleRow> for crate::accounts::oracle::Oracle {
type Error = carbon_core::error::Error;
fn try_from(source: OracleRow) -> Result<Self, Self::Error> {
Ok(Self {
whirlpool: *source.whirlpool,
trade_enable_timestamp: *source.trade_enable_timestamp,
adaptive_fee_constants: source.adaptive_fee_constants.0,
adaptive_fee_variables: source.adaptive_fee_variables.0,
reserved: source.reserved.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 128 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::oracle::Oracle {
fn table() -> &'static str {
"oracle_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"whirlpool",
"trade_enable_timestamp",
"adaptive_fee_constants",
"adaptive_fee_variables",
"reserved",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for OracleRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO oracle_account (
"whirlpool",
"trade_enable_timestamp",
"adaptive_fee_constants",
"adaptive_fee_variables",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7
)"#,
)
.bind(self.whirlpool)
.bind(&self.trade_enable_timestamp)
.bind(&self.adaptive_fee_constants)
.bind(&self.adaptive_fee_variables)
.bind(&self.reserved)
.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 OracleRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO oracle_account (
"whirlpool",
"trade_enable_timestamp",
"adaptive_fee_constants",
"adaptive_fee_variables",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"whirlpool" = EXCLUDED."whirlpool",
"trade_enable_timestamp" = EXCLUDED."trade_enable_timestamp",
"adaptive_fee_constants" = EXCLUDED."adaptive_fee_constants",
"adaptive_fee_variables" = EXCLUDED."adaptive_fee_variables",
"reserved" = EXCLUDED."reserved",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.whirlpool)
.bind(&self.trade_enable_timestamp)
.bind(&self.adaptive_fee_constants)
.bind(&self.adaptive_fee_variables)
.bind(&self.reserved)
.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 OracleRow {
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 oracle_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 OracleRow {
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 oracle_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 OracleMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for OracleMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS oracle_account (
-- Account data
"whirlpool" BYTEA NOT NULL,
"trade_enable_timestamp" NUMERIC(20) NOT NULL,
"adaptive_fee_constants" JSONB NOT NULL,
"adaptive_fee_variables" JSONB NOT NULL,
"reserved" 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 oracle_account"#)
.execute(connection)
.await?;
Ok(())
}
}