1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use account_compression::program::AccountCompression;
use anchor_lang::{
    prelude::*, solana_program::pubkey::Pubkey, system_program::System, AnchorDeserialize,
    AnchorSerialize,
};

use crate::{
    invoke::{processor::CompressedProof, sol_compression::COMPRESSED_SOL_PDA_SEED},
    sdk::{
        accounts::{InvokeAccounts, InvokeCpiAccounts, SignerAccounts},
        compressed_account::{CompressedAccount, PackedCompressedAccountWithMerkleContext},
        CompressedCpiContext,
    },
    NewAddressParamsPacked,
};

use super::account::CpiContextAccount;
/// These are the base accounts additionally Merkle tree and queue accounts are
/// required. These additional accounts are passed as remaining accounts. One
/// Merkle tree for each input compressed account, one queue and Merkle tree
/// account each for each output compressed account.
#[derive(Accounts)]
pub struct InvokeCpiInstruction<'info> {
    #[account(mut)]
    pub fee_payer: Signer<'info>,
    pub authority: Signer<'info>,
    /// CHECK: this account
    #[account(
    seeds = [&crate::ID.to_bytes()], bump, seeds::program = &account_compression::ID,
    )]
    pub registered_program_pda:
        Account<'info, account_compression::instructions::register_program::RegisteredProgram>,
    /// CHECK: this account
    pub noop_program: UncheckedAccount<'info>,
    /// CHECK: this account in psp account compression program
    #[account(seeds = [b"cpi_authority"], bump)]
    pub account_compression_authority: UncheckedAccount<'info>,
    /// CHECK: this account in psp account compression program
    pub account_compression_program: Program<'info, AccountCompression>,
    /// CHECK: is checked in signer checks to derive the authority pubkey
    pub invoking_program: UncheckedAccount<'info>,
    #[account(
        mut,
        seeds = [COMPRESSED_SOL_PDA_SEED], bump
    )]
    pub compressed_sol_pda: Option<UncheckedAccount<'info>>,
    #[account(mut)]
    pub compression_recipient: Option<UncheckedAccount<'info>>,
    pub system_program: Program<'info, System>,
    #[account(mut)]
    pub cpi_context_account: Option<Account<'info, CpiContextAccount>>,
}

impl<'info> InvokeCpiAccounts<'info> for InvokeCpiInstruction<'info> {
    fn get_invoking_program(&self) -> &UncheckedAccount<'info> {
        &self.invoking_program
    }
    fn get_cpi_context_account(&mut self) -> &mut Option<Account<'info, CpiContextAccount>> {
        &mut self.cpi_context_account
    }
}

impl<'info> SignerAccounts<'info> for InvokeCpiInstruction<'info> {
    fn get_fee_payer(&self) -> &Signer<'info> {
        &self.fee_payer
    }

    fn get_authority(&self) -> &Signer<'info> {
        &self.authority
    }
}

impl<'info> InvokeAccounts<'info> for InvokeCpiInstruction<'info> {
    fn get_registered_program_pda(
        &self,
    ) -> &Account<'info, account_compression::instructions::register_program::RegisteredProgram>
    {
        &self.registered_program_pda
    }

    fn get_noop_program(&self) -> &UncheckedAccount<'info> {
        &self.noop_program
    }

    fn get_account_compression_authority(&self) -> &UncheckedAccount<'info> {
        &self.account_compression_authority
    }

    fn get_account_compression_program(&self) -> &Program<'info, AccountCompression> {
        &self.account_compression_program
    }

    fn get_compressed_sol_pda(&self) -> Option<&UncheckedAccount<'info>> {
        self.compressed_sol_pda.as_ref()
    }

    fn get_compression_recipient(&self) -> Option<&UncheckedAccount<'info>> {
        self.compression_recipient.as_ref()
    }

    fn get_system_program(&self) -> &Program<'info, System> {
        &self.system_program
    }
}

#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct InstructionDataInvokeCpi {
    pub proof: Option<CompressedProof>,
    pub new_address_params: Vec<NewAddressParamsPacked>,
    pub input_root_indices: Vec<u16>,
    pub input_compressed_accounts_with_merkle_context:
        Vec<PackedCompressedAccountWithMerkleContext>,
    pub output_compressed_accounts: Vec<CompressedAccount>,
    /// The indices of the accounts in the output state merkle tree.
    pub output_state_merkle_tree_account_indices: Vec<u8>,
    pub relay_fee: Option<u64>,
    pub compression_lamports: Option<u64>,
    pub is_compress: bool,
    pub signer_seeds: Vec<Vec<u8>>,
    pub cpi_context: Option<CompressedCpiContext>,
}

impl InstructionDataInvokeCpi {
    pub fn combine(&mut self, other: &[InstructionDataInvokeCpi]) {
        for other in other {
            self.new_address_params
                .extend_from_slice(&other.new_address_params);
            self.input_root_indices
                .extend_from_slice(&other.input_root_indices);
            self.input_compressed_accounts_with_merkle_context
                .extend_from_slice(&other.input_compressed_accounts_with_merkle_context);
            self.output_compressed_accounts
                .extend_from_slice(&other.output_compressed_accounts);
            self.output_state_merkle_tree_account_indices
                .extend_from_slice(&other.output_state_merkle_tree_account_indices);
        }
    }
}
#[cfg(test)]
mod tests {
    use std::vec;

    use crate::{
        invoke::processor::CompressedProof,
        sdk::compressed_account::{CompressedAccount, PackedCompressedAccountWithMerkleContext},
        InstructionDataInvokeCpi, NewAddressParamsPacked,
    };

    // test combine instruction data transfer
    #[test]
    fn test_combine_instruction_data_transfer() {
        let mut instruction_data_transfer = InstructionDataInvokeCpi {
            proof: Some(CompressedProof {
                a: [0; 32],
                b: [0; 64],
                c: [0; 32],
            }),
            new_address_params: vec![NewAddressParamsPacked::default()],
            input_root_indices: vec![1],
            input_compressed_accounts_with_merkle_context: vec![
                PackedCompressedAccountWithMerkleContext::default(),
            ],
            output_compressed_accounts: vec![CompressedAccount::default()],
            output_state_merkle_tree_account_indices: vec![1],
            relay_fee: Some(1),
            compression_lamports: Some(1),
            is_compress: true,
            signer_seeds: vec![vec![0; 32], vec![1; 32]],
            cpi_context: None,
        };
        let other = InstructionDataInvokeCpi {
            proof: Some(CompressedProof {
                a: [0; 32],
                b: [0; 64],
                c: [0; 32],
            }),
            input_root_indices: vec![1],
            input_compressed_accounts_with_merkle_context: vec![
                PackedCompressedAccountWithMerkleContext::default(),
            ],
            output_compressed_accounts: vec![CompressedAccount::default()],
            output_state_merkle_tree_account_indices: vec![1],
            relay_fee: Some(1),
            compression_lamports: Some(1),
            is_compress: true,
            new_address_params: vec![NewAddressParamsPacked::default()],
            signer_seeds: vec![],
            cpi_context: None,
        };
        instruction_data_transfer.combine(&[other]);
        assert_eq!(instruction_data_transfer.new_address_params.len(), 2);
        assert_eq!(instruction_data_transfer.input_root_indices.len(), 2);
        assert_eq!(
            instruction_data_transfer
                .input_compressed_accounts_with_merkle_context
                .len(),
            2
        );
        assert_eq!(
            instruction_data_transfer.output_compressed_accounts.len(),
            2
        );
        assert_eq!(
            instruction_data_transfer
                .output_state_merkle_tree_account_indices
                .len(),
            2
        );
    }
}