use {
crate::types::{BuilderInfo, RevenueShareOrder},
carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U32},
},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct RevenueShareEscrowRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub authority: Pubkey,
pub referrer: Pubkey,
pub referrer_boost_expire_ts: U32,
pub referrer_reward_offset: i8,
pub referee_fee_numerator_offset: i8,
pub referrer_boost_numerator: i8,
pub reserved_fixed: Vec<u8>,
pub padding0: U32,
pub orders: sqlx::types::Json<Vec<RevenueShareOrder>>,
pub padding1: U32,
pub approved_builders: sqlx::types::Json<Vec<BuilderInfo>>,
}
impl RevenueShareEscrowRow {
pub fn from_parts(
source: crate::accounts::revenue_share_escrow::RevenueShareEscrow,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
authority: source.authority.into(),
referrer: source.referrer.into(),
referrer_boost_expire_ts: source.referrer_boost_expire_ts.into(),
referrer_reward_offset: source.referrer_reward_offset,
referee_fee_numerator_offset: source.referee_fee_numerator_offset,
referrer_boost_numerator: source.referrer_boost_numerator,
reserved_fixed: source.reserved_fixed.to_vec(),
padding0: source.padding0.into(),
orders: sqlx::types::Json(source.orders.to_vec()),
padding1: source.padding1.into(),
approved_builders: sqlx::types::Json(source.approved_builders.to_vec()),
}
}
}
impl TryFrom<RevenueShareEscrowRow> for crate::accounts::revenue_share_escrow::RevenueShareEscrow {
type Error = carbon_core::error::Error;
fn try_from(source: RevenueShareEscrowRow) -> Result<Self, Self::Error> {
Ok(Self {
authority: *source.authority,
referrer: *source.referrer,
referrer_boost_expire_ts: source.referrer_boost_expire_ts.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
referrer_reward_offset: source.referrer_reward_offset,
referee_fee_numerator_offset: source.referee_fee_numerator_offset,
referrer_boost_numerator: source.referrer_boost_numerator,
reserved_fixed: source.reserved_fixed.as_slice().try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert padding from postgres primitive: expected 17 bytes"
.to_string(),
)
})?,
padding0: source.padding0.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
orders: source.orders.0,
padding1: source.padding1.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
approved_builders: source.approved_builders.0,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::revenue_share_escrow::RevenueShareEscrow
{
fn table() -> &'static str {
"revenue_share_escrow_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"authority",
"referrer",
"referrer_boost_expire_ts",
"referrer_reward_offset",
"referee_fee_numerator_offset",
"referrer_boost_numerator",
"reserved_fixed",
"padding0",
"orders",
"padding1",
"approved_builders",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for RevenueShareEscrowRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO revenue_share_escrow_account (
"authority",
"referrer",
"referrer_boost_expire_ts",
"referrer_reward_offset",
"referee_fee_numerator_offset",
"referrer_boost_numerator",
"reserved_fixed",
"padding0",
"orders",
"padding1",
"approved_builders",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
)"#,
)
.bind(self.authority)
.bind(self.referrer)
.bind(self.referrer_boost_expire_ts)
.bind(self.referrer_reward_offset)
.bind(self.referee_fee_numerator_offset)
.bind(self.referrer_boost_numerator)
.bind(&self.reserved_fixed)
.bind(self.padding0)
.bind(&self.orders)
.bind(self.padding1)
.bind(&self.approved_builders)
.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 RevenueShareEscrowRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO revenue_share_escrow_account (
"authority",
"referrer",
"referrer_boost_expire_ts",
"referrer_reward_offset",
"referee_fee_numerator_offset",
"referrer_boost_numerator",
"reserved_fixed",
"padding0",
"orders",
"padding1",
"approved_builders",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"authority" = EXCLUDED."authority",
"referrer" = EXCLUDED."referrer",
"referrer_boost_expire_ts" = EXCLUDED."referrer_boost_expire_ts",
"referrer_reward_offset" = EXCLUDED."referrer_reward_offset",
"referee_fee_numerator_offset" = EXCLUDED."referee_fee_numerator_offset",
"referrer_boost_numerator" = EXCLUDED."referrer_boost_numerator",
"reserved_fixed" = EXCLUDED."reserved_fixed",
"padding0" = EXCLUDED."padding0",
"orders" = EXCLUDED."orders",
"padding1" = EXCLUDED."padding1",
"approved_builders" = EXCLUDED."approved_builders",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.authority)
.bind(self.referrer)
.bind(self.referrer_boost_expire_ts)
.bind(self.referrer_reward_offset)
.bind(self.referee_fee_numerator_offset)
.bind(self.referrer_boost_numerator)
.bind(&self.reserved_fixed)
.bind(self.padding0)
.bind(&self.orders)
.bind(self.padding1)
.bind(&self.approved_builders)
.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 RevenueShareEscrowRow {
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 revenue_share_escrow_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 RevenueShareEscrowRow {
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 revenue_share_escrow_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 RevenueShareEscrowMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for RevenueShareEscrowMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS revenue_share_escrow_account (
-- Account data
"authority" BYTEA NOT NULL,
"referrer" BYTEA NOT NULL,
"referrer_boost_expire_ts" INT8 NOT NULL,
"referrer_reward_offset" INT2 NOT NULL,
"referee_fee_numerator_offset" INT2 NOT NULL,
"referrer_boost_numerator" INT2 NOT NULL,
"reserved_fixed" BYTEA NOT NULL,
"padding0" INT8 NOT NULL,
"orders" JSONB NOT NULL,
"padding1" INT8 NOT NULL,
"approved_builders" JSONB 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 revenue_share_escrow_account"#)
.execute(connection)
.await?;
Ok(())
}
}