use carbon_core::{
account::AccountMetadata,
postgres::{metadata::AccountRowMetadata, primitives::Pubkey},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ClaimFeeOperatorRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub operator: Pubkey,
pub padding: Vec<u8>,
}
impl ClaimFeeOperatorRow {
pub fn from_parts(
source: crate::accounts::claim_fee_operator::ClaimFeeOperator,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
operator: source.operator.into(),
padding: source.padding.to_vec(),
}
}
}
impl TryFrom<ClaimFeeOperatorRow> for crate::accounts::claim_fee_operator::ClaimFeeOperator {
type Error = carbon_core::error::Error;
fn try_from(source: ClaimFeeOperatorRow) -> Result<Self, Self::Error> {
Ok(Self {
operator: *source.operator,
padding: source.padding.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::claim_fee_operator::ClaimFeeOperator
{
fn table() -> &'static str {
"claim_fee_operator_account"
}
fn columns() -> Vec<&'static str> {
vec!["__pubkey", "__slot", "operator", "padding"]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ClaimFeeOperatorRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO claim_fee_operator_account (
"operator",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4
)"#,
)
.bind(self.operator)
.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 ClaimFeeOperatorRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO claim_fee_operator_account (
"operator",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"operator" = EXCLUDED."operator",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.operator)
.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 ClaimFeeOperatorRow {
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 claim_fee_operator_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 ClaimFeeOperatorRow {
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 claim_fee_operator_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 ClaimFeeOperatorMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ClaimFeeOperatorMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS claim_fee_operator_account (
-- Account data
"operator" BYTEA 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 claim_fee_operator_account"#)
.execute(connection)
.await?;
Ok(())
}
}