pub fn add_together(
ctx: Context<AddTogether>,
computation_offset: u64,
ciphertext_0: [u8; 32],
ciphertext_1: [u8; 32],
pub_key: [u8; 32],
nonce: u128,
) -> Result<()> {
// Note: Using `create_program_address` with the bump would be more efficient than `find_program_address`.
// Since this PDA is constant, you could also derive it at compile time and save it as a constant.
// We use find_program_address here for simplicity.
let addition_result_pda = Pubkey::find_program_address(&[b"AdditionResult"], ctx.program_id).0;
// Build the args the confidential instruction expects using ArgBuilder
let args = ArgBuilder::new()
.x25519_pubkey(pub_key)
.plaintext_u128(nonce)
.encrypted_u8(ciphertext_0)
.encrypted_u8(ciphertext_1)
.build();
// Set the bump for the sign_pda_account
ctx.accounts.sign_pda_account.bump = ctx.bumps.sign_pda_account;
// Build & queue our computation (via CPI to the Arcium program)
queue_computation(
ctx.accounts,
// Random offset for the computation
computation_offset,
// The one-time inputs our confidential instruction expects
args,
// Callback server address
// None here because the output of the confidential instruction can fit into a solana transaction
// as its just 1 ciphertext which is 32 bytes
None,
// Manual approach: Define which callback instruction to call when the computation is complete.
// We specify the program ID, instruction discriminator, and all accounts needed
// for the callback, including our result account which we want to be writable.
vec![CallbackInstruction {
program_id: ID_CONST,
discriminator: instruction::AddTogetherCallback::DISCRIMINATOR.to_vec(),
accounts: vec![
// Standard accounts (always required, in this order)
CallbackAccount {
pubkey: ARCIUM_PROGRAM_ID,
is_writable: false,
},
CallbackAccount {
pubkey: derive_comp_def_pda!(COMP_DEF_OFFSET_ADD_TOGETHER),
is_writable: false,
},
CallbackAccount {
pubkey: ctx.accounts.mxe_account.key(),
is_writable: false,
},
CallbackAccount {
pubkey: ctx.accounts.computation_account.key(),
is_writable: false,
},
CallbackAccount {
pubkey: derive_cluster_pda!(ctx.accounts.mxe_account, ErrorCode::ClusterNotSet),
is_writable: false,
},
CallbackAccount {
pubkey: INSTRUCTIONS_SYSVAR_ID,
is_writable: false,
},
// Custom accounts (your callback-specific accounts)
CallbackAccount {
pubkey: addition_result_pda,
is_writable: true, // Tells nodes to mark this account as writable in the transaction
}
]
}],
1, // Number of transactions needed for callback (1 for simple computations)
0, // cu_price_micro: priority fee in microlamports (0 = no priority fee)
)?;
Ok(())
}
/* The AddTogether accounts struct stays exactly the same as shown in the basic guide */