use {
crate::types::DecryptableBalance,
carbon_core::{
instruction::InstructionMetadata,
postgres::{metadata::InstructionRowMetadata, primitives::U64},
},
};
#[derive(sqlx::FromRow, Debug, Clone)]
pub struct ConfigureConfidentialTransferAccountRow {
#[sqlx(flatten)]
pub instruction_metadata: InstructionRowMetadata,
pub decryptable_zero_balance: sqlx::types::Json<DecryptableBalance>,
pub maximum_pending_balance_credit_counter: U64,
pub proof_instruction_offset: i8,
#[sqlx(rename = "__accounts")]
pub accounts: sqlx::types::Json<Vec<solana_instruction::AccountMeta>>,
}
impl ConfigureConfidentialTransferAccountRow {
pub fn from_parts(
source: crate::instructions::configure_confidential_transfer_account::ConfigureConfidentialTransferAccount,
metadata: InstructionMetadata,
accounts: Vec<solana_instruction::AccountMeta>,
) -> Self {
Self {
instruction_metadata: metadata.into(),
decryptable_zero_balance: sqlx::types::Json(source.decryptable_zero_balance),
maximum_pending_balance_credit_counter: source
.maximum_pending_balance_credit_counter
.into(),
proof_instruction_offset: source.proof_instruction_offset,
accounts: sqlx::types::Json(accounts),
}
}
}
impl TryFrom<ConfigureConfidentialTransferAccountRow> for crate::instructions::configure_confidential_transfer_account::ConfigureConfidentialTransferAccount {
type Error = carbon_core::error::Error;
fn try_from(source: ConfigureConfidentialTransferAccountRow) -> Result<Self, Self::Error> {
Ok(Self {
decryptable_zero_balance: source.decryptable_zero_balance.0,
maximum_pending_balance_credit_counter: *source.maximum_pending_balance_credit_counter,
proof_instruction_offset: source.proof_instruction_offset,
})
}
}
impl carbon_core::postgres::operations::Table for crate::instructions::configure_confidential_transfer_account::ConfigureConfidentialTransferAccount {
fn table() -> &'static str {
"configure_confidential_transfer_account_instruction"
}
fn columns() -> Vec<&'static str> {
vec![
"__signature",
"__instruction_index",
"__stack_height",
"__slot",
"decryptable_zero_balance",
"maximum_pending_balance_credit_counter",
"proof_instruction_offset",
"__accounts",
]
}
}
#[async_trait::async_trait]
impl carbon_core::postgres::operations::Insert for ConfigureConfidentialTransferAccountRow {
async fn insert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"
INSERT INTO configure_confidential_transfer_account_instruction (
"decryptable_zero_balance",
"maximum_pending_balance_credit_counter",
"proof_instruction_offset",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)"#,
)
.bind(&self.decryptable_zero_balance)
.bind(&self.maximum_pending_balance_credit_counter)
.bind(self.proof_instruction_offset)
.bind(&self.instruction_metadata.signature)
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(&self.instruction_metadata.slot)
.bind(&self.accounts)
.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 ConfigureConfidentialTransferAccountRow {
async fn upsert(&self, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(r#"INSERT INTO configure_confidential_transfer_account_instruction (
"decryptable_zero_balance",
"maximum_pending_balance_credit_counter",
"proof_instruction_offset",
__signature, __instruction_index, __stack_height, __slot, __accounts
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
) ON CONFLICT (
__signature, __instruction_index, __stack_height
) DO UPDATE SET
"decryptable_zero_balance" = EXCLUDED."decryptable_zero_balance",
"maximum_pending_balance_credit_counter" = EXCLUDED."maximum_pending_balance_credit_counter",
"proof_instruction_offset" = EXCLUDED."proof_instruction_offset",
__instruction_index = EXCLUDED.__instruction_index,
__stack_height = EXCLUDED.__stack_height,
__slot = EXCLUDED.__slot,
__accounts = EXCLUDED.__accounts
"#)
.bind(&self.decryptable_zero_balance)
.bind(&self.maximum_pending_balance_credit_counter)
.bind(self.proof_instruction_offset)
.bind(&self.instruction_metadata.signature)
.bind(self.instruction_metadata.instruction_index)
.bind(self.instruction_metadata.stack_height)
.bind(&self.instruction_metadata.slot)
.bind(&self.accounts)
.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 ConfigureConfidentialTransferAccountRow {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
async fn delete(key: Self::Key, pool: &sqlx::PgPool) -> carbon_core::error::CarbonResult<()> {
sqlx::query(
r#"DELETE FROM configure_confidential_transfer_account_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.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 ConfigureConfidentialTransferAccountRow {
type Key = (
String,
carbon_core::postgres::primitives::U32,
carbon_core::postgres::primitives::U32,
);
async fn lookup(
key: Self::Key,
pool: &sqlx::PgPool,
) -> carbon_core::error::CarbonResult<Option<Self>> {
let row = sqlx::query_as(
r#"SELECT * FROM configure_confidential_transfer_account_instruction WHERE
__signature = $1 AND __instruction_index = $2 AND __stack_height = $3
"#,
)
.bind(key.0)
.bind(key.1)
.bind(key.2)
.fetch_optional(pool)
.await
.map_err(|e| carbon_core::error::Error::Custom(e.to_string()))?;
Ok(row)
}
}
pub struct ConfigureConfidentialTransferAccountMigrationOperation;
#[async_trait::async_trait]
impl sqlx_migrator::Operation<sqlx::Postgres>
for ConfigureConfidentialTransferAccountMigrationOperation
{
async fn up(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(
r#"CREATE TABLE IF NOT EXISTS configure_confidential_transfer_account_instruction (
-- Instruction data
"decryptable_zero_balance" JSONB NOT NULL,
"maximum_pending_balance_credit_counter" NUMERIC(20) NOT NULL,
"proof_instruction_offset" INT2 NOT NULL,
-- Instruction metadata
__signature TEXT NOT NULL,
__instruction_index BIGINT NOT NULL,
__stack_height BIGINT NOT NULL,
__slot NUMERIC(20),
__accounts JSONB NOT NULL,
PRIMARY KEY (__signature, __instruction_index, __stack_height)
)"#,
)
.execute(connection)
.await?;
Ok(())
}
async fn down(
&self,
connection: &mut sqlx::PgConnection,
) -> Result<(), sqlx_migrator::error::Error> {
sqlx::query(r#"DROP TABLE IF EXISTS configure_confidential_transfer_account_instruction"#)
.execute(connection)
.await?;
Ok(())
}
}