use carbon_core::{
account::AccountMetadata,
postgres::{
metadata::AccountRowMetadata,
primitives::{Pubkey, U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct VestingRecordRow {
#[sqlx(flatten)]
pub account_metadata: AccountRowMetadata,
pub epoch: U64,
pub pool: Pubkey,
pub beneficiary: Pubkey,
pub claimed_amount: U64,
pub token_share_amount: U64,
pub padding: Vec<U64>,
}
impl VestingRecordRow {
pub fn from_parts(
source: crate::accounts::vesting_record::VestingRecord,
metadata: AccountMetadata,
) -> Self {
Self {
account_metadata: metadata.into(),
epoch: source.epoch.into(),
pool: source.pool.into(),
beneficiary: source.beneficiary.into(),
claimed_amount: source.claimed_amount.into(),
token_share_amount: source.token_share_amount.into(),
padding: source
.padding
.into_iter()
.map(|element| element.into())
.collect(),
}
}
}
impl TryFrom<VestingRecordRow> for crate::accounts::vesting_record::VestingRecord {
type Error = carbon_core::error::Error;
fn try_from(source: VestingRecordRow) -> Result<Self, Self::Error> {
Ok(Self {
epoch: *source.epoch,
pool: *source.pool,
beneficiary: *source.beneficiary,
claimed_amount: *source.claimed_amount,
token_share_amount: *source.token_share_amount,
padding: source
.padding
.into_iter()
.map(|element| Ok(*element))
.collect::<Result<Vec<_>, carbon_core::error::Error>>()
.map_err(|_| {
carbon_core::error::Error::Custom(
"Failed to collect array elements".to_string(),
)
})?
.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::vesting_record::VestingRecord {
fn table() -> &'static str {
"vesting_record_account"
}
fn columns() -> Vec<&'static str> {
vec![
"__pubkey",
"__slot",
"epoch",
"pool",
"beneficiary",
"claimed_amount",
"token_share_amount",
"padding",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for VestingRecordRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO vesting_record_account (
"epoch",
"pool",
"beneficiary",
"claimed_amount",
"token_share_amount",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)"#,
)
.bind(&self.epoch)
.bind(self.pool)
.bind(self.beneficiary)
.bind(&self.claimed_amount)
.bind(&self.token_share_amount)
.bind(&self.padding)
.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 VestingRecordRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"INSERT INTO vesting_record_account (
"epoch",
"pool",
"beneficiary",
"claimed_amount",
"token_share_amount",
"padding",
__pubkey, __slot
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
) ON CONFLICT (
__pubkey
) DO UPDATE SET
"epoch" = EXCLUDED."epoch",
"pool" = EXCLUDED."pool",
"beneficiary" = EXCLUDED."beneficiary",
"claimed_amount" = EXCLUDED."claimed_amount",
"token_share_amount" = EXCLUDED."token_share_amount",
"padding" = EXCLUDED."padding",
__slot = EXCLUDED.__slot
"#,
)
.bind(&self.epoch)
.bind(self.pool)
.bind(self.beneficiary)
.bind(&self.claimed_amount)
.bind(&self.token_share_amount)
.bind(&self.padding)
.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 VestingRecordRow {
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 vesting_record_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 VestingRecordRow {
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 vesting_record_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 VestingRecordMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres> for VestingRecordMigrationOperation {
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS vesting_record_account (
-- Account data
"epoch" NUMERIC(20) NOT NULL,
"pool" BYTEA NOT NULL,
"beneficiary" BYTEA NOT NULL,
"claimed_amount" NUMERIC(20) NOT NULL,
"token_share_amount" NUMERIC(20) NOT NULL,
"padding" 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 vesting_record_account"#)
.execute(connection)
.await?;
Ok(())
}
}