|
| 1 | +use crate::*; |
| 2 | +use quasar_lang::cpi::{InstructionAccount, InstructionView}; |
| 3 | + |
| 4 | +/// Maximum number of proof nodes for the merkle tree. |
| 5 | +/// Concurrent merkle trees support up to depth 30, but typical depth is 14-20. |
| 6 | +const MAX_PROOF_NODES: usize = 24; |
| 7 | + |
| 8 | +/// Total max accounts for the CPI: 7 fixed + proof nodes. |
| 9 | +const MAX_CPI_ACCOUNTS: usize = 7 + MAX_PROOF_NODES; |
| 10 | + |
| 11 | +/// Accounts for burning a compressed NFT via mpl-bubblegum CPI. |
| 12 | +#[derive(Accounts)] |
| 13 | +pub struct BurnCnft<'info> { |
| 14 | + #[account(mut)] |
| 15 | + pub leaf_owner: &'info Signer, |
| 16 | + /// Tree authority PDA (seeds checked by Bubblegum). |
| 17 | + #[account(mut)] |
| 18 | + pub tree_authority: &'info UncheckedAccount, |
| 19 | + /// Merkle tree account modified by the compression program. |
| 20 | + #[account(mut)] |
| 21 | + pub merkle_tree: &'info UncheckedAccount, |
| 22 | + /// SPL Noop log wrapper. |
| 23 | + pub log_wrapper: &'info UncheckedAccount, |
| 24 | + /// SPL Account Compression program. |
| 25 | + #[account(address = SPL_ACCOUNT_COMPRESSION_ID)] |
| 26 | + pub compression_program: &'info UncheckedAccount, |
| 27 | + /// mpl-bubblegum program. |
| 28 | + #[account(address = MPL_BUBBLEGUM_ID)] |
| 29 | + pub bubblegum_program: &'info UncheckedAccount, |
| 30 | + pub system_program: &'info Program<System>, |
| 31 | +} |
| 32 | + |
| 33 | +impl<'info> BurnCnft<'info> { |
| 34 | + pub fn burn_cnft( |
| 35 | + &self, |
| 36 | + ctx: &CtxWithRemaining<'info, BurnCnft<'info>>, |
| 37 | + ) -> Result<(), ProgramError> { |
| 38 | + // Parse instruction args from raw data: |
| 39 | + // root(32) + data_hash(32) + creator_hash(32) + nonce(8) + index(4) = 108 bytes |
| 40 | + let data = ctx.data; |
| 41 | + if data.len() < 108 { |
| 42 | + return Err(ProgramError::InvalidInstructionData); |
| 43 | + } |
| 44 | + |
| 45 | + // Build instruction data: discriminator + args |
| 46 | + // 8 + 32 + 32 + 32 + 8 + 4 = 116 bytes |
| 47 | + let mut ix_data = [0u8; 116]; |
| 48 | + ix_data[0..8].copy_from_slice(&BURN_DISCRIMINATOR); |
| 49 | + ix_data[8..116].copy_from_slice(&data[0..108]); |
| 50 | + |
| 51 | + // Collect remaining accounts (proof nodes) into a stack buffer |
| 52 | + let remaining = ctx.remaining_accounts(); |
| 53 | + let placeholder = self.system_program.to_account_view().clone(); |
| 54 | + let mut proof_views: [AccountView; MAX_PROOF_NODES] = |
| 55 | + core::array::from_fn(|_| placeholder.clone()); |
| 56 | + let mut proof_count = 0usize; |
| 57 | + for result in remaining.iter() { |
| 58 | + if proof_count >= MAX_PROOF_NODES { |
| 59 | + break; |
| 60 | + } |
| 61 | + proof_views[proof_count] = result?; |
| 62 | + proof_count += 1; |
| 63 | + } |
| 64 | + |
| 65 | + let total_accounts = 7 + proof_count; |
| 66 | + |
| 67 | + // Build instruction account metas. |
| 68 | + // Layout matches mpl-bubblegum Burn: tree_authority, leaf_owner (signer), |
| 69 | + // leaf_delegate (= leaf_owner, not signer), merkle_tree, log_wrapper, |
| 70 | + // compression_program, system_program, then proof nodes. |
| 71 | + let sys_addr = self.system_program.address(); |
| 72 | + let mut ix_accounts: [InstructionAccount; MAX_CPI_ACCOUNTS] = core::array::from_fn(|_| { |
| 73 | + InstructionAccount::readonly(sys_addr) |
| 74 | + }); |
| 75 | + |
| 76 | + ix_accounts[0] = InstructionAccount::readonly(self.tree_authority.address()); |
| 77 | + ix_accounts[1] = InstructionAccount::readonly_signer(self.leaf_owner.address()); |
| 78 | + // leaf_delegate = leaf_owner, not a signer in this call |
| 79 | + ix_accounts[2] = InstructionAccount::readonly(self.leaf_owner.address()); |
| 80 | + ix_accounts[3] = InstructionAccount::writable(self.merkle_tree.address()); |
| 81 | + ix_accounts[4] = InstructionAccount::readonly(self.log_wrapper.address()); |
| 82 | + ix_accounts[5] = InstructionAccount::readonly(self.compression_program.address()); |
| 83 | + ix_accounts[6] = InstructionAccount::readonly(self.system_program.address()); |
| 84 | + |
| 85 | + for i in 0..proof_count { |
| 86 | + ix_accounts[7 + i] = InstructionAccount::readonly(proof_views[i].address()); |
| 87 | + } |
| 88 | + |
| 89 | + // Build account views array for the CPI |
| 90 | + let sys_view = self.system_program.to_account_view().clone(); |
| 91 | + let mut views: [AccountView; MAX_CPI_ACCOUNTS] = core::array::from_fn(|_| sys_view.clone()); |
| 92 | + |
| 93 | + views[0] = self.tree_authority.to_account_view().clone(); |
| 94 | + views[1] = self.leaf_owner.to_account_view().clone(); |
| 95 | + views[2] = self.leaf_owner.to_account_view().clone(); // leaf_delegate = leaf_owner |
| 96 | + views[3] = self.merkle_tree.to_account_view().clone(); |
| 97 | + views[4] = self.log_wrapper.to_account_view().clone(); |
| 98 | + views[5] = self.compression_program.to_account_view().clone(); |
| 99 | + views[6] = self.system_program.to_account_view().clone(); |
| 100 | + |
| 101 | + for i in 0..proof_count { |
| 102 | + views[7 + i] = proof_views[i].clone(); |
| 103 | + } |
| 104 | + |
| 105 | + let instruction = InstructionView { |
| 106 | + program_id: &MPL_BUBBLEGUM_ID, |
| 107 | + data: &ix_data, |
| 108 | + accounts: &ix_accounts[..total_accounts], |
| 109 | + }; |
| 110 | + |
| 111 | + solana_instruction_view::cpi::invoke_with_bounds::<MAX_CPI_ACCOUNTS, AccountView>( |
| 112 | + &instruction, |
| 113 | + &views[..total_accounts], |
| 114 | + ) |
| 115 | + } |
| 116 | +} |
0 commit comments