use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct StubOracleRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub owner: Pubkey,
pub mint: Pubkey,
pub price: f64,
pub last_update_ts: i64,
pub last_update_slot: U64,
pub deviation: f64,
pub reserved: Vec<u8>,
}
impl StubOracleRow {
pub fn from_parts(
source: crate::accounts::stub_oracle::StubOracle,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
owner: source.owner.into(),
mint: source.mint.into(),
price: source.price,
last_update_ts: source.last_update_ts,
last_update_slot: source.last_update_slot.into(),
deviation: source.deviation,
reserved: source.reserved.to_vec(),
}
}
}
impl TryFrom<StubOracleRow> for crate::accounts::stub_oracle::StubOracle {
type Error = carbon_core::error::Error;
fn try_from(source: StubOracleRow) -> Result<Self, Self::Error> {
Ok(Self {
owner: *source.owner,
mint: *source.mint,
price: source.price,
last_update_ts: source.last_update_ts,
last_update_slot: *source.last_update_slot,
deviation: source.deviation,
reserved: source.reserved.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 104 bytes"
.to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::stub_oracle::StubOracle {
fn table() -> &'static str {
"stub_oracle_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"owner",
"mint",
"price",
"last_update_ts",
"last_update_slot",
"deviation",
"reserved",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for StubOracleRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO stub_oracle_account (
"owner",
"mint",
"price",
"last_update_ts",
"last_update_slot",
"deviation",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
)"#,
)
.bind(self.owner)
.bind(self.mint)
.bind(self.price)
.bind(self.last_update_ts)
.bind(&self.last_update_slot)
.bind(self.deviation)
.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 StubOracleRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO stub_oracle_account (
"owner",
"mint",
"price",
"last_update_ts",
"last_update_slot",
"deviation",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"owner" = EXCLUDED."owner",
"mint" = EXCLUDED."mint",
"price" = EXCLUDED."price",
"last_update_ts" = EXCLUDED."last_update_ts",
"last_update_slot" = EXCLUDED."last_update_slot",
"deviation" = EXCLUDED."deviation",
"reserved" = EXCLUDED."reserved",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.owner)
.bind(self.mint)
.bind(self.price)
.bind(self.last_update_ts)
.bind(&self.last_update_slot)
.bind(self.deviation)
.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 StubOracleRow {
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 stub_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 StubOracleRow {
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 stub_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 StubOracleMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for StubOracleMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS stub_oracle_account (
-- Account data
"owner" BYTEA NOT NULL,
"mint" BYTEA NOT NULL,
"price" DOUBLE PRECISION NOT NULL,
"last_update_ts" INT8 NOT NULL,
"last_update_slot" NUMERIC(20) NOT NULL,
"deviation" DOUBLE PRECISION 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 stub_oracle_account"#)
.execute(connection)
.await?;
Ok(())
}
}