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