use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ReferrerAccountRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub nonce: U8,
pub has_alias: bool,
pub referrer: Pubkey,
pub pending_rewards: U64,
pub claimed_rewards: U64,
}
impl ReferrerAccountRow {
pub fn from_parts(
source: crate::accounts::referrer_account::ReferrerAccount,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
nonce: source.nonce.into(),
has_alias: source.has_alias,
referrer: source.referrer.into(),
pending_rewards: source.pending_rewards.into(),
claimed_rewards: source.claimed_rewards.into(),
}
}
}
impl TryFrom<ReferrerAccountRow> for crate::accounts::referrer_account::ReferrerAccount {
type Error = carbon_core::error::Error;
fn try_from(source: ReferrerAccountRow) -> Result<Self, Self::Error> {
Ok(Self {
nonce: source.nonce.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
has_alias: source.has_alias,
referrer: *source.referrer,
pending_rewards: *source.pending_rewards,
claimed_rewards: *source.claimed_rewards,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::referrer_account::ReferrerAccount
{
fn table() -> &'static str {
"referrer_account_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"nonce",
"has_alias",
"referrer",
"pending_rewards",
"claimed_rewards",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ReferrerAccountRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO referrer_account_account (
"nonce",
"has_alias",
"referrer",
"pending_rewards",
"claimed_rewards",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7
)"#,
)
.bind(self.nonce)
.bind(self.has_alias)
.bind(self.referrer)
.bind(&self.pending_rewards)
.bind(&self.claimed_rewards)
.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 ReferrerAccountRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO referrer_account_account (
"nonce",
"has_alias",
"referrer",
"pending_rewards",
"claimed_rewards",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"nonce" = EXCLUDED."nonce",
"has_alias" = EXCLUDED."has_alias",
"referrer" = EXCLUDED."referrer",
"pending_rewards" = EXCLUDED."pending_rewards",
"claimed_rewards" = EXCLUDED."claimed_rewards",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.nonce)
.bind(self.has_alias)
.bind(self.referrer)
.bind(&self.pending_rewards)
.bind(&self.claimed_rewards)
.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 ReferrerAccountRow {
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 referrer_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 ReferrerAccountRow {
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 referrer_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 ReferrerAccountMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for ReferrerAccountMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS referrer_account_account (
-- Account data
"nonce" INT2 NOT NULL,
"has_alias" BOOLEAN NOT NULL,
"referrer" BYTEA NOT NULL,
"pending_rewards" NUMERIC(20) NOT NULL,
"claimed_rewards" 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 referrer_account_account"#)
.execute(connection)
.await?;
Ok(())
}
}