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