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