|
| 1 | +use light_compressed_account::{ |
| 2 | + compressed_account::PackedMerkleContext, instruction_data::cpi_context::CompressedCpiContext, |
| 3 | +}; |
| 4 | +use light_ctoken_types::instructions::transfer2::MultiInputTokenDataWithContext; |
| 5 | +use light_profiler::profile; |
| 6 | +use light_sdk::{ |
| 7 | + error::LightSdkError, |
| 8 | + instruction::{AccountMetasVec, PackedAccounts, PackedStateTreeInfo, SystemAccountMetaConfig}, |
| 9 | + token::TokenData, |
| 10 | +}; |
| 11 | +use solana_account_info::AccountInfo; |
| 12 | +use solana_instruction::{AccountMeta, Instruction}; |
| 13 | +use solana_pubkey::Pubkey; |
| 14 | + |
| 15 | +use crate::{ |
| 16 | + account2::CTokenAccount2, |
| 17 | + error::TokenSdkError, |
| 18 | + instructions::{ |
| 19 | + transfer2::{ |
| 20 | + account_metas::Transfer2AccountsMetaConfig, create_transfer2_instruction, |
| 21 | + Transfer2Config, Transfer2Inputs, |
| 22 | + }, |
| 23 | + CTokenDefaultAccounts, |
| 24 | + }, |
| 25 | + ValidityProof, |
| 26 | +}; |
| 27 | + |
| 28 | +/// Struct to hold all the data needed for DecompressFull operation |
| 29 | +/// Contains the complete compressed account data and destination index |
| 30 | +#[derive(Debug, Clone, crate::AnchorSerialize, crate::AnchorDeserialize)] |
| 31 | +pub struct DecompressFullIndices { |
| 32 | + pub source: MultiInputTokenDataWithContext, // Complete compressed account data with merkle context |
| 33 | + pub destination_index: u8, // Destination ctoken Solana account (must exist) |
| 34 | +} |
| 35 | + |
| 36 | +/// Decompress full balance from compressed token accounts with pre-computed indices |
| 37 | +/// |
| 38 | +/// # Arguments |
| 39 | +/// * `fee_payer` - The fee payer pubkey |
| 40 | +/// * `validity_proof` - Validity proof for the compressed accounts (zkp or index) |
| 41 | +/// * `cpi_context_pubkey` - Optional CPI context account for optimized multi-program transactions |
| 42 | +/// * `indices` - Slice of source/destination pairs for decompress operations |
| 43 | +/// * `packed_accounts` - Slice of all accounts that will be used in the instruction |
| 44 | +/// |
| 45 | +/// # Returns |
| 46 | +/// An instruction that decompresses the full balance of all provided token accounts |
| 47 | +#[profile] |
| 48 | +pub fn decompress_full_ctoken_accounts_with_indices<'info>( |
| 49 | + fee_payer: Pubkey, |
| 50 | + validity_proof: ValidityProof, |
| 51 | + cpi_context_pubkey: Option<Pubkey>, |
| 52 | + indices: &[DecompressFullIndices], |
| 53 | + packed_accounts: &[AccountInfo<'info>], |
| 54 | +) -> Result<Instruction, TokenSdkError> { |
| 55 | + if indices.is_empty() { |
| 56 | + return Err(TokenSdkError::InvalidAccountData); |
| 57 | + } |
| 58 | + |
| 59 | + // Process each set of indices |
| 60 | + let mut token_accounts = Vec::with_capacity(indices.len()); |
| 61 | + |
| 62 | + for idx in indices.iter() { |
| 63 | + // Create CTokenAccount2 with the source data |
| 64 | + // For decompress_full, we don't have an output tree since everything goes to the destination |
| 65 | + let mut token_account = CTokenAccount2::new( |
| 66 | + vec![idx.source], |
| 67 | + 0, // No output tree for full decompress |
| 68 | + )?; |
| 69 | + |
| 70 | + // Set up decompress_full - decompress entire balance to destination ctoken account |
| 71 | + token_account.decompress(idx.source.amount, idx.destination_index)?; |
| 72 | + token_accounts.push(token_account); |
| 73 | + } |
| 74 | + |
| 75 | + // Convert packed_accounts to AccountMetas |
| 76 | + let mut packed_account_metas = Vec::with_capacity(packed_accounts.len()); |
| 77 | + for info in packed_accounts.iter() { |
| 78 | + packed_account_metas.push(AccountMeta { |
| 79 | + pubkey: *info.key, |
| 80 | + is_signer: info.is_signer, |
| 81 | + is_writable: info.is_writable, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + let (meta_config, transfer_config) = if let Some(cpi_context) = cpi_context_pubkey { |
| 86 | + let cpi_context_config = CompressedCpiContext { |
| 87 | + set_context: false, |
| 88 | + first_set_context: false, |
| 89 | + cpi_context_account_index: 0, |
| 90 | + }; |
| 91 | + |
| 92 | + ( |
| 93 | + Transfer2AccountsMetaConfig { |
| 94 | + fee_payer: Some(fee_payer), |
| 95 | + cpi_context: Some(cpi_context), |
| 96 | + decompressed_accounts_only: false, |
| 97 | + sol_pool_pda: None, |
| 98 | + sol_decompression_recipient: None, |
| 99 | + with_sol_pool: false, |
| 100 | + packed_accounts: Some(packed_account_metas), |
| 101 | + }, |
| 102 | + Transfer2Config::default() |
| 103 | + .filter_zero_amount_outputs() |
| 104 | + .with_cpi_context(cpi_context, cpi_context_config), |
| 105 | + ) |
| 106 | + } else { |
| 107 | + ( |
| 108 | + Transfer2AccountsMetaConfig::new(fee_payer, packed_account_metas), |
| 109 | + Transfer2Config::default().filter_zero_amount_outputs(), |
| 110 | + ) |
| 111 | + }; |
| 112 | + |
| 113 | + // Create the transfer2 instruction with all decompress operations |
| 114 | + let inputs = Transfer2Inputs { |
| 115 | + meta_config, |
| 116 | + token_accounts, |
| 117 | + transfer_config, |
| 118 | + validity_proof, |
| 119 | + ..Default::default() |
| 120 | + }; |
| 121 | + |
| 122 | + create_transfer2_instruction(inputs) |
| 123 | +} |
| 124 | + |
| 125 | +/// Helper function to pack compressed token accounts into DecompressFullIndices |
| 126 | +/// Used in tests to build indices for multiple compressed accounts to decompress |
| 127 | +/// |
| 128 | +/// # Arguments |
| 129 | +/// * `token_data` - Slice of TokenData from compressed accounts |
| 130 | +/// * `tree_infos` - Packed tree info for each compressed account |
| 131 | +/// * `destination_indices` - Destination account indices for each decompression |
| 132 | +/// * `packed_accounts` - PackedAccounts that will be used to insert/get indices |
| 133 | +/// |
| 134 | +/// # Returns |
| 135 | +/// Vec of DecompressFullIndices ready to use with decompress_full_ctoken_accounts_with_indices |
| 136 | +#[profile] |
| 137 | +pub fn pack_for_decompress_full( |
| 138 | + token: &TokenData, |
| 139 | + tree_info: &PackedStateTreeInfo, |
| 140 | + destination: Pubkey, |
| 141 | + packed_accounts: &mut PackedAccounts, |
| 142 | +) -> DecompressFullIndices { |
| 143 | + let source = MultiInputTokenDataWithContext { |
| 144 | + owner: packed_accounts.insert_or_get_config(token.owner, true, false), |
| 145 | + amount: token.amount, |
| 146 | + has_delegate: token.delegate.is_some(), |
| 147 | + delegate: token |
| 148 | + .delegate |
| 149 | + .map(|d| packed_accounts.insert_or_get(d)) |
| 150 | + .unwrap_or(0), |
| 151 | + mint: packed_accounts.insert_or_get(token.mint), |
| 152 | + version: 2, |
| 153 | + merkle_context: PackedMerkleContext { |
| 154 | + merkle_tree_pubkey_index: tree_info.merkle_tree_pubkey_index, |
| 155 | + queue_pubkey_index: tree_info.queue_pubkey_index, |
| 156 | + prove_by_index: tree_info.prove_by_index, |
| 157 | + leaf_index: tree_info.leaf_index, |
| 158 | + }, |
| 159 | + root_index: tree_info.root_index, |
| 160 | + }; |
| 161 | + |
| 162 | + DecompressFullIndices { |
| 163 | + source, |
| 164 | + destination_index: packed_accounts.insert_or_get(destination), |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +pub struct DecompressFullAccounts { |
| 169 | + pub compressed_token_program: Pubkey, |
| 170 | + pub cpi_authority_pda: Pubkey, |
| 171 | + pub cpi_context: Option<Pubkey>, |
| 172 | + pub self_program: Pubkey, |
| 173 | +} |
| 174 | + |
| 175 | +impl DecompressFullAccounts { |
| 176 | + pub fn new(self_program: Pubkey, cpi_context: Option<Pubkey>) -> Self { |
| 177 | + Self { |
| 178 | + compressed_token_program: CTokenDefaultAccounts::default().compressed_token_program, |
| 179 | + cpi_authority_pda: CTokenDefaultAccounts::default().cpi_authority_pda, |
| 180 | + cpi_context, |
| 181 | + self_program, |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +impl AccountMetasVec for DecompressFullAccounts { |
| 187 | + /// Adds: |
| 188 | + /// 1. system accounts if not set |
| 189 | + /// 2. compressed token program and ctoken cpi authority pda to pre accounts |
| 190 | + fn get_account_metas_vec(&self, accounts: &mut PackedAccounts) -> Result<(), LightSdkError> { |
| 191 | + if !accounts.system_accounts_set() { |
| 192 | + let config = SystemAccountMetaConfig { |
| 193 | + self_program: self.self_program, |
| 194 | + cpi_context: self.cpi_context, |
| 195 | + ..Default::default() |
| 196 | + }; |
| 197 | + accounts.add_system_accounts_small(config)?; |
| 198 | + } |
| 199 | + // Add both accounts in one operation for better performance |
| 200 | + accounts.pre_accounts.extend_from_slice(&[ |
| 201 | + AccountMeta { |
| 202 | + pubkey: self.compressed_token_program, |
| 203 | + is_signer: false, |
| 204 | + is_writable: false, |
| 205 | + }, |
| 206 | + AccountMeta { |
| 207 | + pubkey: self.cpi_authority_pda, |
| 208 | + is_signer: false, |
| 209 | + is_writable: false, |
| 210 | + }, |
| 211 | + ]); |
| 212 | + Ok(()) |
| 213 | + } |
| 214 | +} |
0 commit comments