use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct UserVolumeAccumulatorRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub user: Pubkey,
pub needs_claim: bool,
pub total_unclaimed_tokens: U64,
pub total_claimed_tokens: U64,
pub current_sol_volume: U64,
pub last_update_timestamp: i64,
pub has_total_claimed_tokens: bool,
pub cashback_earned: U64,
pub total_cashback_claimed: U64,
}
impl UserVolumeAccumulatorRow {
pub fn from_parts(
source: crate::accounts::user_volume_accumulator::UserVolumeAccumulator,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
user: source.user.into(),
needs_claim: source.needs_claim,
total_unclaimed_tokens: source.total_unclaimed_tokens.into(),
total_claimed_tokens: source.total_claimed_tokens.into(),
current_sol_volume: source.current_sol_volume.into(),
last_update_timestamp: source.last_update_timestamp,
has_total_claimed_tokens: source.has_total_claimed_tokens,
cashback_earned: source.cashback_earned.into(),
total_cashback_claimed: source.total_cashback_claimed.into(),
}
}
}
impl TryFrom<UserVolumeAccumulatorRow>
for crate::accounts::user_volume_accumulator::UserVolumeAccumulator
{
type Error = carbon_core::error::Error;
fn try_from(source: UserVolumeAccumulatorRow) -> Result<Self, Self::Error> {
Ok(Self {
user: *source.user,
needs_claim: source.needs_claim,
total_unclaimed_tokens: *source.total_unclaimed_tokens,
total_claimed_tokens: *source.total_claimed_tokens,
current_sol_volume: *source.current_sol_volume,
last_update_timestamp: source.last_update_timestamp,
has_total_claimed_tokens: source.has_total_claimed_tokens,
cashback_earned: *source.cashback_earned,
total_cashback_claimed: *source.total_cashback_claimed,
})
}
}
impl carbon_core::postgres::operations::Table
for crate::accounts::user_volume_accumulator::UserVolumeAccumulator
{
fn table() -> &'static str {
"user_volume_accumulator_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"user",
"needs_claim",
"total_unclaimed_tokens",
"total_claimed_tokens",
"current_sol_volume",
"last_update_timestamp",
"has_total_claimed_tokens",
"cashback_earned",
"total_cashback_claimed",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for UserVolumeAccumulatorRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO user_volume_accumulator_account (
"user",
"needs_claim",
"total_unclaimed_tokens",
"total_claimed_tokens",
"current_sol_volume",
"last_update_timestamp",
"has_total_claimed_tokens",
"cashback_earned",
"total_cashback_claimed",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
)"#,
)
.bind(self.user)
.bind(self.needs_claim)
.bind(&self.total_unclaimed_tokens)
.bind(&self.total_claimed_tokens)
.bind(&self.current_sol_volume)
.bind(self.last_update_timestamp)
.bind(self.has_total_claimed_tokens)
.bind(&self.cashback_earned)
.bind(&self.total_cashback_claimed)
.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 UserVolumeAccumulatorRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO user_volume_accumulator_account (
"user",
"needs_claim",
"total_unclaimed_tokens",
"total_claimed_tokens",
"current_sol_volume",
"last_update_timestamp",
"has_total_claimed_tokens",
"cashback_earned",
"total_cashback_claimed",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"user" = EXCLUDED."user",
"needs_claim" = EXCLUDED."needs_claim",
"total_unclaimed_tokens" = EXCLUDED."total_unclaimed_tokens",
"total_claimed_tokens" = EXCLUDED."total_claimed_tokens",
"current_sol_volume" = EXCLUDED."current_sol_volume",
"last_update_timestamp" = EXCLUDED."last_update_timestamp",
"has_total_claimed_tokens" = EXCLUDED."has_total_claimed_tokens",
"cashback_earned" = EXCLUDED."cashback_earned",
"total_cashback_claimed" = EXCLUDED."total_cashback_claimed",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.user)
.bind(self.needs_claim)
.bind(&self.total_unclaimed_tokens)
.bind(&self.total_claimed_tokens)
.bind(&self.current_sol_volume)
.bind(self.last_update_timestamp)
.bind(self.has_total_claimed_tokens)
.bind(&self.cashback_earned)
.bind(&self.total_cashback_claimed)
.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 UserVolumeAccumulatorRow {
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 user_volume_accumulator_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 UserVolumeAccumulatorRow {
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 user_volume_accumulator_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 UserVolumeAccumulatorMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for UserVolumeAccumulatorMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS user_volume_accumulator_account (
-- Account data
"user" BYTEA NOT NULL,
"needs_claim" BOOLEAN NOT NULL,
"total_unclaimed_tokens" NUMERIC(20) NOT NULL,
"total_claimed_tokens" NUMERIC(20) NOT NULL,
"current_sol_volume" NUMERIC(20) NOT NULL,
"last_update_timestamp" INT8 NOT NULL,
"has_total_claimed_tokens" BOOLEAN NOT NULL,
"cashback_earned" NUMERIC(20) NOT NULL,
"total_cashback_claimed" 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 user_volume_accumulator_account"#)
.execute(connection)
.await?;
Ok(())
}
}