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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
use crate::{
    create_account_instruction, create_and_send_transaction,
    create_and_send_transaction_with_event, get_hash_set,
    test_env::COMPRESSED_TOKEN_PROGRAM_PROGRAM_ID,
    test_indexer::{StateMerkleTreeAccounts, TestIndexer, TokenDataWithContext},
    AccountZeroCopy, TransactionParams,
};
use account_compression::{
    initialize_nullifier_queue::NullifierQueueAccount, StateMerkleTreeAccount,
};
use light_compressed_token::{
    get_cpi_authority_pda, get_token_authority_pda, get_token_pool_pda,
    mint_sdk::{create_initialize_mint_instruction, create_mint_to_instruction},
    transfer_sdk::create_transfer_instruction,
    TokenTransferOutputData,
};
use light_hasher::Poseidon;
use light_system_program::sdk::{compressed_account::MerkleContext, event::PublicTransactionEvent};
use num_bigint::BigUint;
use num_traits::FromBytes;
use solana_program_test::{BanksClientError, ProgramTestContext};
use solana_sdk::{
    instruction::Instruction,
    program_pack::Pack,
    pubkey::Pubkey,
    signature::{Keypair, Signer},
};
use spl_token::instruction::initialize_mint;
use spl_token::state::Mint;
// TODO: replace with borsh serialize
use anchor_lang::AnchorSerialize;

pub async fn mint_tokens_helper(
    context: &mut ProgramTestContext,
    test_indexer: &mut TestIndexer,
    merkle_tree_pubkey: &Pubkey,
    mint_authority: &Keypair,
    mint: &Pubkey,
    amounts: Vec<u64>,
    recipients: Vec<Pubkey>,
) {
    let payer_pubkey = mint_authority.pubkey();
    let instruction = create_mint_to_instruction(
        &payer_pubkey,
        &payer_pubkey,
        mint,
        merkle_tree_pubkey,
        amounts.clone(),
        recipients.clone(),
    );
    let snapshots = get_merkle_tree_snapshots(
        context,
        test_indexer,
        &vec![*merkle_tree_pubkey; amounts.len()],
    )
    .await;
    let previous_mint_supply = spl_token::state::Mint::unpack(
        &context
            .banks_client
            .get_account(*mint)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap()
    .supply;

    let pool: Pubkey = get_token_pool_pda(mint);
    let previous_pool_amount = spl_token::state::Account::unpack(
        &context
            .banks_client
            .get_account(pool)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap()
    .amount;
    let event = create_and_send_transaction_with_event::<PublicTransactionEvent>(
        context,
        &[instruction],
        &payer_pubkey,
        &[mint_authority],
        None,
    )
    .await
    .unwrap()
    .unwrap();
    let (_, created_token_accounts) = test_indexer.add_event_and_compressed_accounts(event);

    assert_mint_to(
        context,
        test_indexer,
        &recipients,
        *mint,
        amounts.as_slice(),
        &snapshots,
        &created_token_accounts,
        previous_mint_supply,
        previous_pool_amount,
    )
    .await;
}

pub async fn create_mint(
    context: &mut ProgramTestContext,
    payer: &Keypair,
    mint_authority: &Pubkey,
    decimals: u8,
    freeze_authority: Option<&Pubkey>,
    mint_keypair: Option<&Keypair>,
) -> Pubkey {
    let keypair = Keypair::new();
    let mint_keypair = match mint_keypair {
        Some(mint_keypair) => mint_keypair,
        None => &keypair,
    };
    let mint_pubkey = (*mint_keypair).pubkey();
    let rent = context.banks_client.get_rent().await.unwrap();
    let mint_rent = rent.minimum_balance(Mint::LEN);

    let account_create_ix = crate::create_account_instruction(
        &payer.pubkey(),
        Mint::LEN,
        mint_rent,
        &COMPRESSED_TOKEN_PROGRAM_PROGRAM_ID,
        Some(mint_keypair),
    );

    let create_mint_ix = spl_token::instruction::initialize_mint2(
        &spl_token::id(),
        &mint_pubkey,
        mint_authority,
        freeze_authority,
        decimals,
    )
    .unwrap();
    create_and_send_transaction(
        context,
        &[account_create_ix, create_mint_ix],
        &payer.pubkey(),
        &[payer],
    )
    .await
    .unwrap();
    mint_pubkey
}
pub async fn create_mint_helper(context: &mut ProgramTestContext, payer: &Keypair) -> Pubkey {
    let payer_pubkey = payer.pubkey();
    let rent = context
        .banks_client
        .get_rent()
        .await
        .unwrap()
        .minimum_balance(anchor_spl::token::Mint::LEN);
    let mint = Keypair::new();

    let (instructions, pool) =
        create_initialize_mint_instructions(&payer_pubkey, &payer_pubkey, rent, 2, &mint);

    create_and_send_transaction(context, &instructions, &payer_pubkey, &[payer, &mint])
        .await
        .unwrap();
    assert_create_mint(context, &payer_pubkey, &mint.pubkey(), &pool).await;
    mint.pubkey()
}
pub fn create_initialize_mint_instructions(
    payer: &Pubkey,
    authority: &Pubkey,
    rent: u64,
    decimals: u8,
    mint_keypair: &Keypair,
) -> ([Instruction; 4], Pubkey) {
    let account_create_ix = create_account_instruction(
        payer,
        anchor_spl::token::Mint::LEN,
        rent,
        &anchor_spl::token::ID,
        Some(mint_keypair),
    );

    let mint_pubkey = mint_keypair.pubkey();
    let mint_authority = get_token_authority_pda(authority, &mint_pubkey).0;
    let create_mint_instruction = initialize_mint(
        &anchor_spl::token::ID,
        &mint_keypair.pubkey(),
        &mint_authority,
        None,
        decimals,
    )
    .unwrap();
    let transfer_ix =
        anchor_lang::solana_program::system_instruction::transfer(payer, &mint_pubkey, rent);

    let instruction = create_initialize_mint_instruction(payer, authority, &mint_pubkey);
    let pool_pubkey = get_token_pool_pda(&mint_pubkey);
    (
        [
            account_create_ix,
            create_mint_instruction,
            transfer_ix,
            instruction,
        ],
        pool_pubkey,
    )
}

pub async fn assert_create_mint(
    context: &mut ProgramTestContext,
    authority: &Pubkey,
    mint: &Pubkey,
    pool: &Pubkey,
) {
    let mint_account: spl_token::state::Mint = spl_token::state::Mint::unpack(
        &context
            .banks_client
            .get_account(*mint)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();
    let mint_authority = get_token_authority_pda(authority, mint).0;
    assert_eq!(mint_account.supply, 0);
    assert_eq!(mint_account.decimals, 2);
    assert_eq!(mint_account.mint_authority.unwrap(), mint_authority);
    assert_eq!(mint_account.freeze_authority, None.into());
    assert!(mint_account.is_initialized);
    let mint_account: spl_token::state::Account = spl_token::state::Account::unpack(
        &context
            .banks_client
            .get_account(*pool)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();

    assert_eq!(mint_account.amount, 0);
    assert_eq!(mint_account.delegate, None.into());
    assert_eq!(mint_account.mint, *mint);
    assert_eq!(mint_account.owner, get_cpi_authority_pda().0);
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct MerkleTreeTestSnapShot {
    pub accounts: StateMerkleTreeAccounts,
    pub root: [u8; 32],
    pub next_index: usize,
    pub num_added_accounts: usize,
}

pub async fn assert_merkle_tree_after_tx(
    context: &mut ProgramTestContext,
    snapshots: &[MerkleTreeTestSnapShot],
    test_indexer: &mut TestIndexer,
) {
    let mut deduped_snapshots = snapshots.to_vec();
    deduped_snapshots.sort();
    deduped_snapshots.dedup();
    for (i, snapshot) in deduped_snapshots.iter().enumerate() {
        let merkle_tree_account =
            AccountZeroCopy::<StateMerkleTreeAccount>::new(context, snapshot.accounts.merkle_tree)
                .await;
        let merkle_tree = merkle_tree_account
            .deserialized()
            .copy_merkle_tree()
            .unwrap();
        if merkle_tree.root() == snapshot.root {
            println!("deduped_snapshots: {:?}", deduped_snapshots);
            println!("i: {:?}", i);
            panic!("merkle tree root update failed");
        }
        assert_eq!(
            merkle_tree.next_index(),
            snapshot.next_index + snapshot.num_added_accounts
        );
        let test_indexer_merkle_tree = test_indexer
            .state_merkle_trees
            .iter_mut()
            .find(|x| x.accounts.merkle_tree == snapshot.accounts.merkle_tree)
            .expect("merkle tree not found in test indexer");

        if merkle_tree.root() != test_indexer_merkle_tree.merkle_tree.root() {
            println!("Merkle tree pubkey {:?}", snapshot.accounts.merkle_tree);
            for (i, leaf) in test_indexer_merkle_tree.merkle_tree.layers[0]
                .iter()
                .enumerate()
            {
                println!("test_indexer_merkle_tree index {} leaf: {:?}", i, leaf);
            }
            let merkle_tree_roots = merkle_tree_account.deserialized().load_roots().unwrap();
            for i in 0..16 {
                println!("root {} {:?}", i, merkle_tree_roots.get(i));
            }
            for i in 0..5 {
                test_indexer_merkle_tree
                    .merkle_tree
                    .update(&[0u8; 32], 15 - i)
                    .unwrap();
                println!(
                    "roll back root {} {:?}",
                    15 - i,
                    test_indexer_merkle_tree.merkle_tree.root()
                );
            }

            panic!("merkle tree root update failed");
        }
    }
}

pub async fn assert_transfer(
    context: &mut ProgramTestContext,
    test_indexer: &mut TestIndexer,
    out_compressed_accounts: &[TokenTransferOutputData],
    input_compressed_account_hashes: &[[u8; 32]],
    output_merkle_tree_test_snapshots: &[MerkleTreeTestSnapShot],
    input_merkle_tree_test_snapshots: &[MerkleTreeTestSnapShot],
) {
    assert_merkle_tree_after_tx(context, output_merkle_tree_test_snapshots, test_indexer).await;
    let mut tree = Pubkey::default();
    let mut index = 0;
    for (i, out_compressed_account) in out_compressed_accounts.iter().enumerate() {
        if output_merkle_tree_test_snapshots[i].accounts.merkle_tree != tree {
            tree = output_merkle_tree_test_snapshots[i].accounts.merkle_tree;
            index = 0;
        } else {
            index += 1;
        }
        let pos = test_indexer
            .token_compressed_accounts
            .iter()
            .position(|x| {
                x.token_data.owner == out_compressed_account.owner
                    && x.token_data.amount == out_compressed_account.amount
            })
            .expect("transfer recipient compressed account not found in mock indexer");
        let transfer_recipient_token_compressed_account =
            test_indexer.token_compressed_accounts[pos].clone();
        assert_eq!(
            transfer_recipient_token_compressed_account
                .token_data
                .amount,
            out_compressed_account.amount
        );
        assert_eq!(
            transfer_recipient_token_compressed_account.token_data.owner,
            out_compressed_account.owner
        );
        assert_eq!(
            transfer_recipient_token_compressed_account
                .token_data
                .delegate,
            None
        );
        assert_eq!(
            transfer_recipient_token_compressed_account
                .token_data
                .is_native,
            None
        );
        assert_eq!(
            transfer_recipient_token_compressed_account
                .token_data
                .delegated_amount,
            0
        );

        let transfer_recipient_compressed_account = transfer_recipient_token_compressed_account
            .compressed_account
            .clone();
        assert_eq!(
            transfer_recipient_compressed_account
                .compressed_account
                .lamports,
            0
        );
        assert!(transfer_recipient_compressed_account
            .compressed_account
            .data
            .is_some());
        let mut data = Vec::new();
        transfer_recipient_token_compressed_account
            .token_data
            .serialize(&mut data)
            .unwrap();
        assert_eq!(
            transfer_recipient_compressed_account
                .compressed_account
                .data
                .as_ref()
                .unwrap()
                .data,
            data
        );
        assert_eq!(
            transfer_recipient_compressed_account
                .compressed_account
                .owner,
            light_compressed_token::ID
        );

        if !test_indexer.token_compressed_accounts.iter().any(|x| {
            x.compressed_account.merkle_context.leaf_index as usize
                == output_merkle_tree_test_snapshots[i].next_index + index
        }) {
            println!(
                "token_compressed_accounts {:?}",
                test_indexer.token_compressed_accounts
            );
            println!("snapshot {:?}", output_merkle_tree_test_snapshots[i]);
            println!("index {:?}", index);
            panic!("transfer recipient compressed account not found in mock indexer");
        };
    }
    assert_nullifiers_exist_in_hash_sets(
        context,
        input_merkle_tree_test_snapshots,
        input_compressed_account_hashes,
    )
    .await;
}

pub async fn assert_nullifiers_exist_in_hash_sets(
    context: &mut ProgramTestContext,
    snapshots: &[MerkleTreeTestSnapShot],
    input_compressed_account_hashes: &[[u8; 32]],
) {
    for (i, hash) in input_compressed_account_hashes.iter().enumerate() {
        let nullifier_queue = unsafe {
            get_hash_set::<u16, NullifierQueueAccount>(
                context,
                snapshots[i].accounts.nullifier_queue,
            )
            .await
        };
        assert!(nullifier_queue
            .contains(&BigUint::from_be_bytes(hash.as_slice()), 0)
            .unwrap());
    }
}

#[allow(clippy::too_many_arguments)]
pub async fn assert_mint_to<'a>(
    context: &mut ProgramTestContext,
    test_indexer: &'a mut TestIndexer,
    recipients: &[Pubkey],
    mint: Pubkey,
    amounts: &[u64],
    snapshots: &[MerkleTreeTestSnapShot],
    created_token_accounts: &[TokenDataWithContext],
    previous_mint_supply: u64,
    previous_sol_pool_amount: u64,
) {
    let mut created_token_accounts = created_token_accounts.to_vec();
    for (recipient, amount) in recipients.iter().zip(amounts) {
        let pos = created_token_accounts
            .iter()
            .position(|x| {
                x.token_data.owner == *recipient
                    && x.token_data.amount == *amount
                    && x.token_data.mint == mint
                    && x.token_data.delegate.is_none()
                    && x.token_data.is_native.is_none()
                    && x.token_data.delegated_amount == 0
            })
            .expect("Mint to failed to create expected compressed token account.");
        created_token_accounts.remove(pos);
    }
    assert_merkle_tree_after_tx(context, snapshots, test_indexer).await;
    let mint_account: spl_token::state::Mint = spl_token::state::Mint::unpack(
        &context
            .banks_client
            .get_account(mint)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();
    let sum_amounts = amounts.iter().sum::<u64>();
    assert_eq!(mint_account.supply, previous_mint_supply + sum_amounts);

    let pool = get_token_pool_pda(&mint);
    let pool_account = spl_token::state::Account::unpack(
        &context
            .banks_client
            .get_account(pool)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();
    assert_eq!(pool_account.amount, previous_sol_pool_amount + sum_amounts);
}

/// Creates an spl token account and initializes it with the given mint and owner.
/// This function is useful to create token accounts for spl compression and decompression tests.
pub async fn create_token_account(
    context: &mut ProgramTestContext,
    mint: &Pubkey,
    account_keypair: &Keypair,
    owner: &Keypair,
) -> Result<(), BanksClientError> {
    let rent = context
        .banks_client
        .get_rent()
        .await
        .unwrap()
        .minimum_balance(anchor_spl::token::TokenAccount::LEN);
    let account_create_ix = create_account_instruction(
        &owner.pubkey(),
        anchor_spl::token::TokenAccount::LEN,
        rent,
        &anchor_spl::token::ID,
        Some(account_keypair),
    );
    let instruction = spl_token::instruction::initialize_account(
        &spl_token::ID,
        &account_keypair.pubkey(),
        mint,
        &owner.pubkey(),
    )
    .unwrap();
    create_and_send_transaction(
        context,
        &[account_create_ix, instruction],
        &owner.pubkey(),
        &[account_keypair, owner],
    )
    .await
    .unwrap();
    Ok(())
}

pub async fn get_merkle_tree_snapshots(
    context: &mut ProgramTestContext,
    test_indexer: &TestIndexer,
    pubkeys: &[Pubkey],
) -> Vec<MerkleTreeTestSnapShot> {
    let mut snapshots = Vec::new();
    for pubkey in pubkeys.iter() {
        let merkle_tree_account =
            AccountZeroCopy::<StateMerkleTreeAccount>::new(context, *pubkey).await;
        let merkle_tree = merkle_tree_account
            .deserialized()
            .copy_merkle_tree()
            .unwrap();
        let accounts = test_indexer
            .state_merkle_trees
            .iter()
            .find(|x| x.accounts.merkle_tree == *pubkey)
            .expect("merkle tree not found in test indexer");
        snapshots.push(MerkleTreeTestSnapShot {
            accounts: accounts.accounts,
            root: merkle_tree.root(),
            next_index: merkle_tree.next_index(),
            num_added_accounts: pubkeys.iter().filter(|x| **x == *pubkey).count(),
        });
    }
    snapshots
}

#[allow(clippy::too_many_arguments)]
pub async fn compressed_transfer_test(
    payer: &Keypair,
    context: &mut ProgramTestContext,
    test_indexer: &mut TestIndexer,
    mint: &Pubkey,
    from: &Keypair,
    recipients: &[Pubkey],
    amounts: &[u64],
    input_compressed_accounts: &[TokenDataWithContext],
    output_merkle_tree_pubkeys: &[Pubkey],
    transaction_params: Option<TransactionParams>,
) {
    if recipients.len() != amounts.len() && amounts.len() != output_merkle_tree_pubkeys.len() {
        panic!("recipients, amounts, and output_merkle_tree_pubkeys must have the same length");
    }
    let mut input_merkle_tree_context = Vec::new();
    let mut input_compressed_account_token_data = Vec::new();
    let mut input_compressed_account_hashes = Vec::new();
    let mut sum_input_amounts = 0;
    for account in input_compressed_accounts {
        let leaf_index = account.compressed_account.merkle_context.leaf_index;
        input_compressed_account_token_data.push(account.token_data);
        input_compressed_account_hashes.push(
            account
                .compressed_account
                .compressed_account
                .hash::<Poseidon>(
                    &account.compressed_account.merkle_context.merkle_tree_pubkey,
                    &leaf_index,
                )
                .unwrap(),
        );
        sum_input_amounts += account.token_data.amount;
        input_merkle_tree_context.push(MerkleContext {
            merkle_tree_pubkey: account.compressed_account.merkle_context.merkle_tree_pubkey,
            nullifier_queue_pubkey: account
                .compressed_account
                .merkle_context
                .nullifier_queue_pubkey,
            leaf_index,
        });
    }
    let mut output_compressed_accounts = Vec::new();
    for (recipient, amount) in recipients.iter().zip(amounts) {
        let account = TokenTransferOutputData {
            amount: *amount,
            owner: *recipient,
            lamports: None,
        };
        sum_input_amounts -= amount;
        output_compressed_accounts.push(account);
    }
    // add change compressed account if tokens are left
    if sum_input_amounts > 0 {
        let account = TokenTransferOutputData {
            amount: sum_input_amounts,
            owner: from.pubkey(),
            lamports: None,
        };
        output_compressed_accounts.push(account);
    }
    let input_merkle_tree_pubkeys: Vec<Pubkey> = input_merkle_tree_context
        .iter()
        .map(|x| x.merkle_tree_pubkey)
        .collect();

    let proof_rpc_result = test_indexer
        .create_proof_for_compressed_accounts(
            Some(&input_compressed_account_hashes),
            Some(&input_merkle_tree_pubkeys),
            None,
            None,
            context,
        )
        .await;

    let instruction = light_compressed_token::transfer_sdk::create_transfer_instruction(
        &payer.pubkey(),
        &from.pubkey(), // authority
        &input_merkle_tree_context,
        output_merkle_tree_pubkeys, // output_compressed_account_merkle_tree_pubkeys
        &output_compressed_accounts, // output_compressed_accounts
        &proof_rpc_result.root_indices,
        &Some(proof_rpc_result.proof),
        input_compressed_account_token_data.as_slice(), // input_token_data
        *mint,
        None,  // owner_if_delegate_is_signer
        false, // is_compress
        None,  // compression_amount
        None,  // token_pool_pda
        None,  // decompress_token_account
    )
    .unwrap();

    let snapshots =
        get_merkle_tree_snapshots(context, test_indexer, output_merkle_tree_pubkeys).await;
    let input_snapshots =
        get_merkle_tree_snapshots(context, test_indexer, &input_merkle_tree_pubkeys).await;
    let event = create_and_send_transaction_with_event(
        context,
        &[instruction],
        &payer.pubkey(),
        &[payer, from],
        transaction_params,
        // Some(TransactionParams {
        //     num_new_addresses: 0,
        //     num_input_compressed_accounts: input_compressed_account_hashes.len() as u8,
        //     num_output_compressed_accounts: output_compressed_accounts.len() as u8,
        //     compress: 5000, // for second signer
        //     fee_config: crate::FeeConfig::default(),
        // }),
    )
    .await
    .unwrap()
    .unwrap();

    test_indexer.add_compressed_accounts_with_token_data(event);
    assert_transfer(
        context,
        test_indexer,
        &output_compressed_accounts,
        &input_compressed_account_hashes,
        &snapshots,
        &input_snapshots,
    )
    .await;
}

#[allow(clippy::too_many_arguments)]
pub async fn decompress_test(
    payer: &Keypair,
    context: &mut ProgramTestContext,
    test_indexer: &mut TestIndexer,
    input_compressed_accounts: Vec<TokenDataWithContext>,
    amount: u64,
    output_merkle_tree_pubkey: &Pubkey,
    recipient_token_account: &Pubkey,
    transaction_params: Option<TransactionParams>,
) {
    let max_amount: u64 = input_compressed_accounts
        .iter()
        .map(|x| x.token_data.amount)
        .sum();
    let change_out_compressed_account = TokenTransferOutputData {
        amount: max_amount - amount,
        owner: payer.pubkey(),
        lamports: None,
    };
    let input_compressed_account_hashes = input_compressed_accounts
        .iter()
        .map(|x| {
            x.compressed_account
                .compressed_account
                .hash::<Poseidon>(
                    &x.compressed_account.merkle_context.merkle_tree_pubkey,
                    &x.compressed_account.merkle_context.leaf_index,
                )
                .unwrap()
        })
        .collect::<Vec<_>>();
    let input_merkle_tree_pubkeys = input_compressed_accounts
        .iter()
        .map(|x| x.compressed_account.merkle_context.merkle_tree_pubkey)
        .collect::<Vec<_>>();

    let proof_rpc_result = test_indexer
        .create_proof_for_compressed_accounts(
            Some(&input_compressed_account_hashes),
            Some(&input_merkle_tree_pubkeys),
            None,
            None,
            context,
        )
        .await;
    let mint = input_compressed_accounts[0].token_data.mint;
    let output_merkle_tree_pubkeys = vec![*output_merkle_tree_pubkey];
    let instruction = create_transfer_instruction(
        &context.payer.pubkey(),
        &payer.pubkey(), // authority
        &input_compressed_accounts
            .iter()
            .map(|x| x.compressed_account.merkle_context)
            .collect::<Vec<_>>(), // input_compressed_account_merkle_tree_pubkeys
        &output_merkle_tree_pubkeys, // output_cmerkle_contextmerkle_tree_pubkeys
        &[change_out_compressed_account], // output_compressed_accounts
        &proof_rpc_result.root_indices, // root_indices
        &Some(proof_rpc_result.proof),
        input_compressed_accounts
            .iter()
            .map(|x| x.token_data)
            .collect::<Vec<_>>()
            .as_slice(), // input_token_data
        mint,                            // mint
        None,                            // owner_if_delegate_is_signer
        false,                           // is_compress
        Some(amount),                    // compression_amount
        Some(get_token_pool_pda(&mint)), // token_pool_pda
        Some(*recipient_token_account),  // decompress_token_account
    )
    .unwrap();
    let output_merkle_tree_test_snapshots =
        get_merkle_tree_snapshots(context, test_indexer, &output_merkle_tree_pubkeys).await;
    let input_merkle_tree_test_snapshots =
        get_merkle_tree_snapshots(context, test_indexer, &input_merkle_tree_pubkeys).await;
    let recipient_token_account_data_pre = spl_token::state::Account::unpack(
        &context
            .banks_client
            .get_account(*recipient_token_account)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();
    let context_payer = context.payer.insecure_clone();
    let event = create_and_send_transaction_with_event(
        context,
        &[instruction],
        &payer.pubkey(),
        &[&context_payer, payer],
        transaction_params,
    )
    .await
    .unwrap()
    .unwrap();

    test_indexer.add_compressed_accounts_with_token_data(event);

    assert_transfer(
        context,
        test_indexer,
        &[change_out_compressed_account],
        input_compressed_account_hashes.as_slice(),
        &output_merkle_tree_test_snapshots,
        &input_merkle_tree_test_snapshots,
    )
    .await;

    let recipient_token_account_data = spl_token::state::Account::unpack(
        &context
            .banks_client
            .get_account(*recipient_token_account)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();
    assert_eq!(
        recipient_token_account_data.amount,
        recipient_token_account_data_pre.amount + amount
    );
}

#[allow(clippy::too_many_arguments)]
pub async fn compress_test(
    payer: &Keypair,
    context: &mut ProgramTestContext,
    test_indexer: &mut TestIndexer,
    amount: u64,
    mint: &Pubkey,
    output_merkle_tree_pubkey: &Pubkey,
    sender_token_account: &Pubkey,
    transaction_params: Option<TransactionParams>,
) {
    let output_compressed_account = TokenTransferOutputData {
        amount,
        owner: payer.pubkey(),
        lamports: None,
    };
    let approve_instruction = spl_token::instruction::approve(
        &anchor_spl::token::ID,
        sender_token_account,
        &get_cpi_authority_pda().0,
        &payer.pubkey(),
        &[&payer.pubkey()],
        amount,
    )
    .unwrap();

    let output_merkle_tree_pubkeys = vec![*output_merkle_tree_pubkey];
    let instruction = create_transfer_instruction(
        &context.payer.pubkey(),
        &payer.pubkey(),              // authority
        &Vec::new(),                  // input_compressed_account_merkle_tree_pubkeys
        &output_merkle_tree_pubkeys,  // output_cmerkle_contextmerkle_tree_pubkeys
        &[output_compressed_account], // output_compressed_accounts
        &Vec::new(),                  // root_indices
        &None,
        &Vec::new(),                    // input_token_data
        *mint,                          // mint
        None,                           // owner_if_delegate_is_signer
        true,                           // is_compress
        Some(amount),                   // compression_amount
        Some(get_token_pool_pda(mint)), // token_pool_pda
        Some(*sender_token_account),    // decompress_token_account
    )
    .unwrap();
    let output_merkle_tree_test_snapshots =
        get_merkle_tree_snapshots(context, test_indexer, &output_merkle_tree_pubkeys).await;
    let input_merkle_tree_test_snapshots = Vec::new();
    let recipient_token_account_data_pre = spl_token::state::Account::unpack(
        &context
            .banks_client
            .get_account(*sender_token_account)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();
    let context_payer = context.payer.insecure_clone();
    let event = create_and_send_transaction_with_event(
        context,
        &[approve_instruction, instruction],
        &payer.pubkey(),
        &[&context_payer, payer],
        transaction_params,
    )
    .await
    .unwrap()
    .unwrap();

    test_indexer.add_compressed_accounts_with_token_data(event);

    assert_transfer(
        context,
        test_indexer,
        &[output_compressed_account],
        Vec::new().as_slice(),
        &output_merkle_tree_test_snapshots,
        &input_merkle_tree_test_snapshots,
    )
    .await;

    let recipient_token_account_data = spl_token::state::Account::unpack(
        &context
            .banks_client
            .get_account(*sender_token_account)
            .await
            .unwrap()
            .unwrap()
            .data,
    )
    .unwrap();
    assert_eq!(
        recipient_token_account_data.amount,
        recipient_token_account_data_pre.amount - amount
    );
}