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