account_compression/
sdk.rs

1#![cfg(not(target_os = "solana"))]
2
3use anchor_lang::{system_program, InstructionData, ToAccountMetas};
4use solana_sdk::{
5    instruction::{AccountMeta, Instruction},
6    pubkey::Pubkey,
7};
8
9use crate::{
10    instruction::{
11        InitializeAddressMerkleTreeAndQueue, InitializeStateMerkleTreeAndNullifierQueue,
12    },
13    AddressMerkleTreeConfig, AddressQueueConfig, NullifierQueueConfig, StateMerkleTreeConfig,
14};
15
16pub fn create_initialize_merkle_tree_instruction(
17    payer: Pubkey,
18    registered_program_pda: Option<Pubkey>,
19    merkle_tree_pubkey: Pubkey,
20    nullifier_queue_pubkey: Pubkey,
21    state_merkle_tree_config: StateMerkleTreeConfig,
22    nullifier_queue_config: NullifierQueueConfig,
23    program_owner: Option<Pubkey>,
24    forester: Option<Pubkey>,
25    index: u64,
26) -> Instruction {
27    let instruction_data = InitializeStateMerkleTreeAndNullifierQueue {
28        index,
29        program_owner,
30        forester,
31        state_merkle_tree_config,
32        nullifier_queue_config,
33        additional_bytes: 0,
34    };
35    let registered_program = match registered_program_pda {
36        Some(registered_program_pda) => AccountMeta::new(registered_program_pda, false),
37        None => AccountMeta::new(crate::ID, false),
38    };
39    Instruction {
40        program_id: crate::ID,
41        accounts: vec![
42            AccountMeta::new(payer, true),
43            AccountMeta::new(merkle_tree_pubkey, false),
44            AccountMeta::new(nullifier_queue_pubkey, false),
45            registered_program,
46        ],
47        data: instruction_data.data(),
48    }
49}
50
51pub fn create_insert_leaves_instruction(
52    leaves: Vec<(u8, [u8; 32])>,
53    fee_payer: Pubkey,
54    authority: Pubkey,
55    merkle_tree_pubkeys: Vec<Pubkey>,
56) -> Instruction {
57    let instruction_data = crate::instruction::AppendLeavesToMerkleTrees { leaves };
58
59    let accounts = crate::accounts::AppendLeaves {
60        fee_payer,
61        authority,
62        registered_program_pda: None,
63        system_program: system_program::ID,
64    };
65    let merkle_tree_account_metas = merkle_tree_pubkeys
66        .iter()
67        .map(|pubkey| AccountMeta::new(*pubkey, false))
68        .collect::<Vec<AccountMeta>>();
69
70    Instruction {
71        program_id: crate::ID,
72        accounts: [
73            accounts.to_account_metas(Some(true)),
74            merkle_tree_account_metas,
75        ]
76        .concat(),
77        data: instruction_data.data(),
78    }
79}
80
81pub fn create_initialize_address_merkle_tree_and_queue_instruction(
82    index: u64,
83    payer: Pubkey,
84    registered_program_pda: Option<Pubkey>,
85    program_owner: Option<Pubkey>,
86    forester: Option<Pubkey>,
87    merkle_tree_pubkey: Pubkey,
88    queue_pubkey: Pubkey,
89    address_merkle_tree_config: AddressMerkleTreeConfig,
90    address_queue_config: AddressQueueConfig,
91) -> Instruction {
92    let instruction_data = InitializeAddressMerkleTreeAndQueue {
93        index,
94        program_owner,
95        forester,
96        address_merkle_tree_config,
97        address_queue_config,
98    };
99    let registered_program = match registered_program_pda {
100        Some(registered_program_pda) => AccountMeta::new(registered_program_pda, false),
101        None => AccountMeta::new(crate::ID, false),
102    };
103    Instruction {
104        program_id: crate::ID,
105        accounts: vec![
106            AccountMeta::new(payer, true),
107            AccountMeta::new(merkle_tree_pubkey, false),
108            AccountMeta::new(queue_pubkey, false),
109            registered_program,
110        ],
111        data: instruction_data.data(),
112    }
113}