use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{U64, U8},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct SocialFeePdaRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub bump: U8,
pub version: U8,
pub user_id: String,
pub platform: U8,
pub total_claimed: U64,
pub last_claimed: U64,
pub reserved: Vec<U8>,
}
impl SocialFeePdaRow {
pub fn from_parts(
source: crate::accounts::social_fee_pda::SocialFeePda,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
bump: source.bump.into(),
version: source.version.into(),
user_id: source.user_id,
platform: source.platform.into(),
total_claimed: source.total_claimed.into(),
last_claimed: source.last_claimed.into(),
reserved: source
.reserved
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<SocialFeePdaRow> for crate::accounts::social_fee_pda::SocialFeePda {
type Error = carbon_core::error::Error;
fn try_from(source: SocialFeePdaRow) -> Result<Self, Self::Error> {
Ok(Self {
bump: source.bump.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
version: source.version.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
user_id: source.user_id,
platform: source.platform.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})?,
total_claimed: *source.total_claimed,
last_claimed: *source.last_claimed,
reserved: source
.reserved
.into_iter()
.map(|element| {
element.try_into().map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert value from postgres primitive".to_string(),
)
})
})
.collect::<Result<Vec<_>, carbon_core::error::Error>>()?
.try_into()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to convert array element to primitive".to_string(),
)
})?,
})
}
}
impl carbon_core::postgres::operations::Table for crate::accounts::social_fee_pda::SocialFeePda {
fn table() -> &'static str {
"social_fee_pda_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"bump",
"version",
"user_id",
"platform",
"total_claimed",
"last_claimed",
"reserved",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for SocialFeePdaRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO social_fee_pda_account (
"bump",
"version",
"user_id",
"platform",
"total_claimed",
"last_claimed",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
)"#,
)
.bind(self.bump)
.bind(self.version)
.bind(&self.user_id)
.bind(self.platform)
.bind(&self.total_claimed)
.bind(&self.last_claimed)
.bind(&self.reserved)
.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 SocialFeePdaRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO social_fee_pda_account (
"bump",
"version",
"user_id",
"platform",
"total_claimed",
"last_claimed",
"reserved",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"bump" = EXCLUDED."bump",
"version" = EXCLUDED."version",
"user_id" = EXCLUDED."user_id",
"platform" = EXCLUDED."platform",
"total_claimed" = EXCLUDED."total_claimed",
"last_claimed" = EXCLUDED."last_claimed",
"reserved" = EXCLUDED."reserved",
__slot = EXCLUDED.__slot
"#,
)
.bind(self.bump)
.bind(self.version)
.bind(&self.user_id)
.bind(self.platform)
.bind(&self.total_claimed)
.bind(&self.last_claimed)
.bind(&self.reserved)
.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 SocialFeePdaRow {
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 social_fee_pda_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 SocialFeePdaRow {
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 social_fee_pda_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 SocialFeePdaMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for SocialFeePdaMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS social_fee_pda_account (
-- Account data
"bump" INT2 NOT NULL,
"version" INT2 NOT NULL,
"user_id" TEXT NOT NULL,
"platform" INT2 NOT NULL,
"total_claimed" NUMERIC(20) NOT NULL,
"last_claimed" NUMERIC(20) NOT NULL,
"reserved" INT2[] 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 social_fee_pda_account"#)
.execute(connection)
.await?;
Ok(())
}
}