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