light-program-test 0.18.0

A fast local test environment for Solana programs using compressed accounts and tokens.
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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! Instruction decoder for Light Protocol and common Solana programs

use borsh::BorshDeserialize;
use light_compressed_account::instruction_data::{
    data::InstructionDataInvoke, invoke_cpi::InstructionDataInvokeCpi,
    with_account_info::InstructionDataInvokeCpiWithAccountInfo,
    with_readonly::InstructionDataInvokeCpiWithReadOnly,
};
use solana_sdk::{instruction::AccountMeta, pubkey::Pubkey, system_program};

use super::types::ParsedInstructionData;

/// Helper to resolve merkle tree and queue pubkeys from instruction accounts
/// For InvokeCpi instructions, tree accounts start 2 positions after the system program
fn resolve_tree_and_queue_pubkeys(
    accounts: &[AccountMeta],
    merkle_tree_index: Option<u8>,
    nullifier_queue_index: Option<u8>,
) -> (Option<Pubkey>, Option<Pubkey>) {
    let mut tree_pubkey = None;
    let mut queue_pubkey = None;

    // Find the system program account position
    let mut system_program_pos = None;
    for (i, account) in accounts.iter().enumerate() {
        if account.pubkey == system_program::ID {
            system_program_pos = Some(i);
            break;
        }
    }

    if let Some(system_pos) = system_program_pos {
        // Tree accounts start 2 positions after system program
        let tree_accounts_start = system_pos + 2;

        if let Some(tree_idx) = merkle_tree_index {
            let tree_account_pos = tree_accounts_start + tree_idx as usize;
            if tree_account_pos < accounts.len() {
                tree_pubkey = Some(accounts[tree_account_pos].pubkey);
            }
        }

        if let Some(queue_idx) = nullifier_queue_index {
            let queue_account_pos = tree_accounts_start + queue_idx as usize;
            if queue_account_pos < accounts.len() {
                queue_pubkey = Some(accounts[queue_account_pos].pubkey);
            }
        }
    }

    (tree_pubkey, queue_pubkey)
}

/// Decode instruction data for known programs
pub fn decode_instruction(
    program_id: &Pubkey,
    data: &[u8],
    accounts: &[AccountMeta],
) -> Option<ParsedInstructionData> {
    match program_id.to_string().as_str() {
        // Light System Program
        "SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7" => {
            decode_light_system_instruction(data, accounts, program_id)
        }

        // Compute Budget Program
        "ComputeBudget111111111111111111111111111111" => decode_compute_budget_instruction(data),

        // System Program
        id if id == system_program::ID.to_string() => decode_system_instruction(data),

        // Account Compression Program
        "compr6CUsB5m2jS4Y3831ztGSTnDpnKJTKS95d64XVq" => decode_compression_instruction(data),

        // Compressed Token Program
        "cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m" => decode_compressed_token_instruction(data),

        _ => Some(ParsedInstructionData::Unknown {
            program_name: get_program_name(program_id),
            data_preview: bs58::encode(&data[..data.len().min(16)]).into_string(),
        }),
    }
}

/// Decode Light System Program instructions
fn decode_light_system_instruction(
    data: &[u8],
    accounts: &[AccountMeta],
    program_id: &Pubkey,
) -> Option<ParsedInstructionData> {
    if data.is_empty() {
        return None;
    }

    // Light System Program uses 8-byte discriminators
    if data.len() < 8 {
        return Some(ParsedInstructionData::LightSystemProgram {
            instruction_type: "Invalid".to_string(),
            compressed_accounts: None,
            proof_info: None,
            address_params: None,
            fee_info: None,
            input_account_data: None,
            output_account_data: None,
        });
    }

    // Extract the 8-byte discriminator
    let discriminator: [u8; 8] = data[0..8].try_into().unwrap();

    // Light Protocol discriminators from compressed-account/src/discriminators.rs
    let (
        instruction_type,
        compressed_accounts,
        proof_info,
        address_params,
        fee_info,
        input_account_data,
        output_account_data,
    ) = match discriminator {
        [26, 16, 169, 7, 21, 202, 242, 25] => {
            // DISCRIMINATOR_INVOKE
            match parse_invoke_instruction(&data[8..], accounts) {
                Ok(parsed) => parsed,
                Err(_) => (
                    "Invoke (parse error)".to_string(),
                    None,
                    None,
                    None,
                    None,
                    None,
                    None,
                ),
            }
        }
        [49, 212, 191, 129, 39, 194, 43, 196] => {
            // DISCRIMINATOR_INVOKE_CPI
            match parse_invoke_cpi_instruction(&data[8..], accounts) {
                Ok(parsed) => parsed,
                Err(_) => (
                    "InvokeCpi (parse error)".to_string(),
                    None,
                    None,
                    None,
                    None,
                    None,
                    None,
                ),
            }
        }
        [86, 47, 163, 166, 21, 223, 92, 8] => {
            // DISCRIMINATOR_INVOKE_CPI_WITH_READ_ONLY
            match parse_invoke_cpi_readonly_instruction(&data[8..], accounts) {
                Ok(parsed) => parsed,
                Err(_) => (
                    "InvokeCpiWithReadOnly (parse error)".to_string(),
                    None,
                    None,
                    None,
                    None,
                    None,
                    None,
                ),
            }
        }
        [228, 34, 128, 84, 47, 139, 86, 240] => {
            // INVOKE_CPI_WITH_ACCOUNT_INFO_INSTRUCTION
            match parse_invoke_cpi_account_info_instruction(&data[8..], accounts, program_id) {
                Ok(parsed) => parsed,
                Err(_) => (
                    "InvokeCpiWithAccountInfo (parse error)".to_string(),
                    None,
                    None,
                    None,
                    None,
                    None,
                    None,
                ),
            }
        }
        _ => {
            // Unknown discriminator - show the discriminator bytes for debugging
            let discriminator_str = format!("{:?}", discriminator);
            (
                format!("Unknown({})", discriminator_str),
                None,
                None,
                None,
                None,
                None,
                None,
            )
        }
    };

    Some(ParsedInstructionData::LightSystemProgram {
        instruction_type,
        compressed_accounts,
        proof_info,
        address_params,
        fee_info,
        input_account_data,
        output_account_data,
    })
}

type InstructionParseResult = Result<
    (
        String,
        Option<super::types::CompressedAccountSummary>,
        Option<super::types::ProofSummary>,
        Option<Vec<super::types::AddressParam>>,
        Option<super::types::FeeSummary>,
        Option<Vec<super::types::InputAccountData>>,
        Option<Vec<super::types::OutputAccountData>>,
    ),
    Box<dyn std::error::Error>,
>;

/// Parse Invoke instruction data - display data hashes directly
fn parse_invoke_instruction(data: &[u8], accounts: &[AccountMeta]) -> InstructionParseResult {
    // Skip the 4-byte vec length prefix that Anchor adds
    if data.len() < 4 {
        return Err("Instruction data too short for Anchor prefix".into());
    }
    let instruction_data = InstructionDataInvoke::try_from_slice(&data[4..])?;

    let compressed_accounts = Some(super::types::CompressedAccountSummary {
        input_accounts: instruction_data
            .input_compressed_accounts_with_merkle_context
            .len(),
        output_accounts: instruction_data.output_compressed_accounts.len(),
        lamports_change: instruction_data
            .compress_or_decompress_lamports
            .map(|l| l as i64),
    });

    let proof_info = instruction_data
        .proof
        .as_ref()
        .map(|_| super::types::ProofSummary {
            proof_type: "Validity".to_string(),
            has_validity_proof: true,
        });

    // Extract actual address parameters with values
    let address_params = if !instruction_data.new_address_params.is_empty() {
        Some(
            instruction_data
                .new_address_params
                .iter()
                .map(|param| {
                    let tree_idx = Some(param.address_merkle_tree_account_index);
                    let queue_idx = Some(param.address_queue_account_index);
                    let (tree_pubkey, queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, queue_idx);

                    super::types::AddressParam {
                        seed: param.seed,
                        address_queue_index: queue_idx,
                        address_queue_pubkey: queue_pubkey,
                        merkle_tree_index: tree_idx,
                        address_merkle_tree_pubkey: tree_pubkey,
                        root_index: Some(param.address_merkle_tree_root_index),
                        derived_address: None,
                        assigned_account_index: super::types::AddressAssignment::V1,
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    // Extract input account data
    let input_account_data = if !instruction_data
        .input_compressed_accounts_with_merkle_context
        .is_empty()
    {
        Some(
            instruction_data
                .input_compressed_accounts_with_merkle_context
                .iter()
                .map(|acc| {
                    let tree_idx = Some(acc.merkle_context.merkle_tree_pubkey_index);
                    let queue_idx = Some(acc.merkle_context.queue_pubkey_index);
                    let (tree_pubkey, queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, queue_idx);

                    super::types::InputAccountData {
                        lamports: acc.compressed_account.lamports,
                        owner: Some(acc.compressed_account.owner.into()),
                        merkle_tree_index: tree_idx,
                        merkle_tree_pubkey: tree_pubkey,
                        queue_index: queue_idx,
                        queue_pubkey,
                        address: acc.compressed_account.address,
                        data_hash: if let Some(ref data) = acc.compressed_account.data {
                            data.data_hash.to_vec()
                        } else {
                            vec![]
                        },
                        discriminator: if let Some(ref data) = acc.compressed_account.data {
                            data.discriminator.to_vec()
                        } else {
                            vec![]
                        },
                        leaf_index: Some(acc.merkle_context.leaf_index),
                        root_index: Some(acc.root_index),
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    // Extract output account data
    let output_account_data = if !instruction_data.output_compressed_accounts.is_empty() {
        Some(
            instruction_data
                .output_compressed_accounts
                .iter()
                .map(|acc| {
                    let tree_idx = Some(acc.merkle_tree_index);
                    let (tree_pubkey, _queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, None);

                    super::types::OutputAccountData {
                        lamports: acc.compressed_account.lamports,
                        data: acc.compressed_account.data.as_ref().map(|d| d.data.clone()),
                        owner: Some(acc.compressed_account.owner.into()),
                        merkle_tree_index: tree_idx,
                        merkle_tree_pubkey: tree_pubkey,
                        queue_index: None,
                        queue_pubkey: None,
                        address: acc.compressed_account.address,
                        data_hash: if let Some(ref data) = acc.compressed_account.data {
                            data.data_hash.to_vec()
                        } else {
                            vec![]
                        },
                        discriminator: if let Some(ref data) = acc.compressed_account.data {
                            data.discriminator.to_vec()
                        } else {
                            vec![]
                        },
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    let fee_info = instruction_data
        .relay_fee
        .map(|fee| super::types::FeeSummary {
            relay_fee: Some(fee),
            compression_fee: None,
        });

    Ok((
        "Invoke".to_string(),
        compressed_accounts,
        proof_info,
        address_params,
        fee_info,
        input_account_data,
        output_account_data,
    ))
}

/// Parse InvokeCpi instruction data - display data hashes directly
fn parse_invoke_cpi_instruction(data: &[u8], accounts: &[AccountMeta]) -> InstructionParseResult {
    // Skip the 4-byte vec length prefix that Anchor adds
    if data.len() < 4 {
        return Err("Instruction data too short for Anchor prefix".into());
    }
    let instruction_data = InstructionDataInvokeCpi::try_from_slice(&data[4..])?;

    let compressed_accounts = Some(super::types::CompressedAccountSummary {
        input_accounts: instruction_data
            .input_compressed_accounts_with_merkle_context
            .len(),
        output_accounts: instruction_data.output_compressed_accounts.len(),
        lamports_change: instruction_data
            .compress_or_decompress_lamports
            .map(|l| l as i64),
    });

    let proof_info = instruction_data
        .proof
        .as_ref()
        .map(|_| super::types::ProofSummary {
            proof_type: "Validity".to_string(),
            has_validity_proof: true,
        });

    // Extract actual address parameters with values
    let address_params = if !instruction_data.new_address_params.is_empty() {
        Some(
            instruction_data
                .new_address_params
                .iter()
                .map(|param| {
                    let tree_idx = Some(param.address_merkle_tree_account_index);
                    let queue_idx = Some(param.address_queue_account_index);
                    let (tree_pubkey, queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, queue_idx);

                    super::types::AddressParam {
                        seed: param.seed,
                        address_queue_index: queue_idx,
                        address_queue_pubkey: queue_pubkey,
                        merkle_tree_index: tree_idx,
                        address_merkle_tree_pubkey: tree_pubkey,
                        root_index: Some(param.address_merkle_tree_root_index),
                        derived_address: None,
                        assigned_account_index: super::types::AddressAssignment::V1,
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    // Extract input account data
    let input_account_data = if !instruction_data
        .input_compressed_accounts_with_merkle_context
        .is_empty()
    {
        Some(
            instruction_data
                .input_compressed_accounts_with_merkle_context
                .iter()
                .map(|acc| {
                    let tree_idx = Some(acc.merkle_context.merkle_tree_pubkey_index);
                    let queue_idx = Some(acc.merkle_context.queue_pubkey_index);
                    let (tree_pubkey, queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, queue_idx);

                    super::types::InputAccountData {
                        lamports: acc.compressed_account.lamports,
                        owner: Some(acc.compressed_account.owner.into()),
                        merkle_tree_index: tree_idx,
                        merkle_tree_pubkey: tree_pubkey,
                        queue_index: queue_idx,
                        queue_pubkey,
                        address: acc.compressed_account.address,
                        data_hash: if let Some(ref data) = acc.compressed_account.data {
                            data.data_hash.to_vec()
                        } else {
                            vec![]
                        },
                        discriminator: if let Some(ref data) = acc.compressed_account.data {
                            data.discriminator.to_vec()
                        } else {
                            vec![]
                        },
                        leaf_index: Some(acc.merkle_context.leaf_index),
                        root_index: Some(acc.root_index),
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    // Extract output account data
    let output_account_data = if !instruction_data.output_compressed_accounts.is_empty() {
        Some(
            instruction_data
                .output_compressed_accounts
                .iter()
                .map(|acc| {
                    let tree_idx = Some(acc.merkle_tree_index);
                    let (tree_pubkey, _queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, None);

                    super::types::OutputAccountData {
                        lamports: acc.compressed_account.lamports,
                        data: acc.compressed_account.data.as_ref().map(|d| d.data.clone()),
                        owner: Some(acc.compressed_account.owner.into()),
                        merkle_tree_index: tree_idx,
                        merkle_tree_pubkey: tree_pubkey,
                        queue_index: None,
                        queue_pubkey: None,
                        address: acc.compressed_account.address,
                        data_hash: if let Some(ref data) = acc.compressed_account.data {
                            data.data_hash.to_vec()
                        } else {
                            vec![]
                        },
                        discriminator: if let Some(ref data) = acc.compressed_account.data {
                            data.discriminator.to_vec()
                        } else {
                            vec![]
                        },
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    let fee_info = instruction_data
        .relay_fee
        .map(|fee| super::types::FeeSummary {
            relay_fee: Some(fee),
            compression_fee: None,
        });

    Ok((
        "InvokeCpi".to_string(),
        compressed_accounts,
        proof_info,
        address_params,
        fee_info,
        input_account_data,
        output_account_data,
    ))
}

/// Parse InvokeCpiWithReadOnly instruction data - display data hashes directly
fn parse_invoke_cpi_readonly_instruction(
    data: &[u8],
    accounts: &[AccountMeta],
) -> InstructionParseResult {
    let instruction_data = InstructionDataInvokeCpiWithReadOnly::try_from_slice(data)?;

    let compressed_accounts = Some(super::types::CompressedAccountSummary {
        input_accounts: instruction_data.input_compressed_accounts.len(),
        output_accounts: instruction_data.output_compressed_accounts.len(),
        lamports_change: if instruction_data.compress_or_decompress_lamports > 0 {
            Some(instruction_data.compress_or_decompress_lamports as i64)
        } else {
            None
        },
    });

    let proof_info = Some(super::types::ProofSummary {
        proof_type: "Validity".to_string(),
        has_validity_proof: true,
    });

    // Extract actual address parameters with values
    let mut address_params = Vec::new();

    // Add new address parameters with actual values
    for param in &instruction_data.new_address_params {
        let tree_idx = Some(param.address_merkle_tree_account_index);
        let queue_idx = Some(param.address_queue_account_index);
        let (tree_pubkey, queue_pubkey) =
            resolve_tree_and_queue_pubkeys(accounts, tree_idx, queue_idx);

        address_params.push(super::types::AddressParam {
            seed: param.seed,
            address_queue_index: queue_idx,
            address_queue_pubkey: queue_pubkey,
            merkle_tree_index: tree_idx,
            address_merkle_tree_pubkey: tree_pubkey,
            root_index: Some(param.address_merkle_tree_root_index),
            derived_address: None,
            assigned_account_index: if param.assigned_to_account {
                super::types::AddressAssignment::AssignedIndex(param.assigned_account_index)
            } else {
                super::types::AddressAssignment::None
            },
        });
    }

    // Add readonly address parameters
    for readonly_addr in &instruction_data.read_only_addresses {
        let tree_idx = Some(readonly_addr.address_merkle_tree_account_index);
        let (tree_pubkey, _queue_pubkey) = resolve_tree_and_queue_pubkeys(accounts, tree_idx, None);

        address_params.push(super::types::AddressParam {
            seed: [0; 32], // ReadOnly addresses don't have seeds in the same way
            address_queue_index: None,
            address_queue_pubkey: None,
            merkle_tree_index: tree_idx,
            address_merkle_tree_pubkey: tree_pubkey,
            root_index: Some(readonly_addr.address_merkle_tree_root_index),
            derived_address: Some(readonly_addr.address),
            assigned_account_index: super::types::AddressAssignment::None,
        });
    }

    let address_params = if !address_params.is_empty() {
        Some(address_params)
    } else {
        None
    };

    // Extract input account data - use data_hash from InAccount
    let input_account_data = if !instruction_data.input_compressed_accounts.is_empty() {
        Some(
            instruction_data
                .input_compressed_accounts
                .iter()
                .map(|acc| {
                    let tree_idx = Some(acc.merkle_context.merkle_tree_pubkey_index);
                    let queue_idx = Some(acc.merkle_context.queue_pubkey_index);
                    let (tree_pubkey, queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, queue_idx);

                    super::types::InputAccountData {
                        lamports: acc.lamports,
                        owner: Some(instruction_data.invoking_program_id.into()), // Use invoking program as owner
                        merkle_tree_index: tree_idx,
                        merkle_tree_pubkey: tree_pubkey,
                        queue_index: queue_idx,
                        queue_pubkey,
                        address: acc.address,
                        data_hash: acc.data_hash.to_vec(),
                        discriminator: acc.discriminator.to_vec(),
                        leaf_index: Some(acc.merkle_context.leaf_index),
                        root_index: Some(acc.root_index),
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    // Extract output account data
    let output_account_data = if !instruction_data.output_compressed_accounts.is_empty() {
        Some(
            instruction_data
                .output_compressed_accounts
                .iter()
                .map(|acc| {
                    let tree_idx = Some(acc.merkle_tree_index);
                    let (tree_pubkey, _queue_pubkey) =
                        resolve_tree_and_queue_pubkeys(accounts, tree_idx, None);

                    super::types::OutputAccountData {
                        lamports: acc.compressed_account.lamports,
                        data: acc.compressed_account.data.as_ref().map(|d| d.data.clone()),
                        owner: Some(instruction_data.invoking_program_id.into()), // Use invoking program as owner for consistency
                        merkle_tree_index: tree_idx,
                        merkle_tree_pubkey: tree_pubkey,
                        queue_index: None,
                        queue_pubkey: None,
                        address: acc.compressed_account.address,
                        data_hash: if let Some(ref data) = acc.compressed_account.data {
                            data.data_hash.to_vec()
                        } else {
                            vec![]
                        },
                        discriminator: if let Some(ref data) = acc.compressed_account.data {
                            data.discriminator.to_vec()
                        } else {
                            vec![]
                        },
                    }
                })
                .collect(),
        )
    } else {
        None
    };

    Ok((
        "InvokeCpiWithReadOnly".to_string(),
        compressed_accounts,
        proof_info,
        address_params,
        None,
        input_account_data,
        output_account_data,
    ))
}

/// Parse InvokeCpiWithAccountInfo instruction data - display data hashes directly
fn parse_invoke_cpi_account_info_instruction(
    data: &[u8],
    accounts: &[AccountMeta],
    program_id: &Pubkey,
) -> InstructionParseResult {
    let instruction_data = InstructionDataInvokeCpiWithAccountInfo::try_from_slice(data)?;

    let input_accounts = instruction_data
        .account_infos
        .iter()
        .filter(|a| a.input.is_some())
        .count();
    let output_accounts = instruction_data
        .account_infos
        .iter()
        .filter(|a| a.output.is_some())
        .count();

    let compressed_accounts = Some(super::types::CompressedAccountSummary {
        input_accounts,
        output_accounts,
        lamports_change: if instruction_data.compress_or_decompress_lamports > 0 {
            Some(instruction_data.compress_or_decompress_lamports as i64)
        } else {
            None
        },
    });

    let proof_info = Some(super::types::ProofSummary {
        proof_type: "Validity".to_string(),
        has_validity_proof: true,
    });

    // Extract actual address parameters with values
    let mut address_params = Vec::new();

    // Add new address parameters with actual values
    for param in &instruction_data.new_address_params {
        let tree_idx = Some(param.address_merkle_tree_account_index);
        let queue_idx = Some(param.address_queue_account_index);
        let (tree_pubkey, queue_pubkey) =
            resolve_tree_and_queue_pubkeys(accounts, tree_idx, queue_idx);

        address_params.push(super::types::AddressParam {
            seed: param.seed,
            address_queue_index: queue_idx,
            address_queue_pubkey: queue_pubkey,
            merkle_tree_index: tree_idx,
            address_merkle_tree_pubkey: tree_pubkey,
            root_index: Some(param.address_merkle_tree_root_index),
            derived_address: None,
            assigned_account_index: if param.assigned_to_account {
                super::types::AddressAssignment::AssignedIndex(param.assigned_account_index)
            } else {
                super::types::AddressAssignment::None
            },
        });
    }

    // Add readonly address parameters
    for readonly_addr in &instruction_data.read_only_addresses {
        let tree_idx = Some(readonly_addr.address_merkle_tree_account_index);
        let (tree_pubkey, _queue_pubkey) = resolve_tree_and_queue_pubkeys(accounts, tree_idx, None);

        address_params.push(super::types::AddressParam {
            seed: [0; 32], // ReadOnly addresses don't have seeds in the same way
            address_queue_index: None,
            address_queue_pubkey: None,
            merkle_tree_index: tree_idx,
            address_merkle_tree_pubkey: tree_pubkey,
            root_index: Some(readonly_addr.address_merkle_tree_root_index),
            derived_address: Some(readonly_addr.address),
            assigned_account_index: super::types::AddressAssignment::None,
        });
    }

    let address_params = if !address_params.is_empty() {
        Some(address_params)
    } else {
        None
    };

    // Extract input account data from account_infos
    let input_account_data = {
        let mut input_data = Vec::new();
        for account_info in &instruction_data.account_infos {
            if let Some(ref input) = account_info.input {
                input_data.push(super::types::InputAccountData {
                    lamports: input.lamports,
                    owner: Some(*program_id), // Use invoking program as owner
                    merkle_tree_index: None, // Note: merkle tree context not available in CompressedAccountInfo
                    merkle_tree_pubkey: None,
                    queue_index: None,
                    queue_pubkey: None,
                    address: account_info.address, // Use address from CompressedAccountInfo
                    data_hash: input.data_hash.to_vec(),
                    discriminator: input.discriminator.to_vec(),
                    leaf_index: Some(input.merkle_context.leaf_index),
                    root_index: Some(input.root_index),
                });
            }
        }
        if !input_data.is_empty() {
            Some(input_data)
        } else {
            None
        }
    };

    // Extract output account data from account_infos
    let output_account_data = {
        let mut output_data = Vec::new();
        for account_info in &instruction_data.account_infos {
            if let Some(ref output) = account_info.output {
                let tree_idx = Some(output.output_merkle_tree_index);
                let (tree_pubkey, _queue_pubkey) =
                    resolve_tree_and_queue_pubkeys(accounts, tree_idx, None);

                output_data.push(super::types::OutputAccountData {
                    lamports: output.lamports,
                    data: if !output.data.is_empty() {
                        Some(output.data.clone())
                    } else {
                        None
                    },
                    owner: Some(*program_id), // Use invoking program as owner
                    merkle_tree_index: tree_idx,
                    merkle_tree_pubkey: tree_pubkey,
                    queue_index: None,
                    queue_pubkey: None,
                    address: account_info.address, // Use address from CompressedAccountInfo
                    data_hash: output.data_hash.to_vec(),
                    discriminator: output.discriminator.to_vec(),
                });
            }
        }
        if !output_data.is_empty() {
            Some(output_data)
        } else {
            None
        }
    };

    Ok((
        "InvokeCpiWithAccountInfo".to_string(),
        compressed_accounts,
        proof_info,
        address_params,
        None,
        input_account_data,
        output_account_data,
    ))
}

/// Decode Compute Budget Program instructions
fn decode_compute_budget_instruction(data: &[u8]) -> Option<ParsedInstructionData> {
    if data.len() < 4 {
        return None;
    }

    let instruction_discriminator = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);

    match instruction_discriminator {
        0 => {
            // RequestUnitsDeprecated
            if data.len() >= 12 {
                let units = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as u64;
                let _additional_fee =
                    u32::from_le_bytes([data[8], data[9], data[10], data[11]]) as u64;
                Some(ParsedInstructionData::ComputeBudget {
                    instruction_type: "RequestUnitsDeprecated".to_string(),
                    value: Some(units),
                })
            } else {
                None
            }
        }
        1 => {
            // RequestHeapFrame
            if data.len() >= 8 {
                let bytes = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as u64;
                Some(ParsedInstructionData::ComputeBudget {
                    instruction_type: "RequestHeapFrame".to_string(),
                    value: Some(bytes),
                })
            } else {
                None
            }
        }
        2 => {
            // SetComputeUnitLimit
            if data.len() >= 8 {
                let units = u32::from_le_bytes([data[4], data[5], data[6], data[7]]) as u64;
                Some(ParsedInstructionData::ComputeBudget {
                    instruction_type: "SetComputeUnitLimit".to_string(),
                    value: Some(units),
                })
            } else {
                None
            }
        }
        3 => {
            // SetComputeUnitPrice
            if data.len() >= 12 {
                let price = u64::from_le_bytes([
                    data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11],
                ]);
                Some(ParsedInstructionData::ComputeBudget {
                    instruction_type: "SetComputeUnitPrice".to_string(),
                    value: Some(price),
                })
            } else {
                None
            }
        }
        _ => Some(ParsedInstructionData::ComputeBudget {
            instruction_type: "Unknown".to_string(),
            value: None,
        }),
    }
}

/// Decode System Program instructions
fn decode_system_instruction(data: &[u8]) -> Option<ParsedInstructionData> {
    if data.len() < 4 {
        return None;
    }

    let instruction_type = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);

    match instruction_type {
        0 => {
            // CreateAccount
            if data.len() >= 52 {
                let lamports = u64::from_le_bytes([
                    data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11],
                ]);
                let space = u64::from_le_bytes([
                    data[12], data[13], data[14], data[15], data[16], data[17], data[18], data[19],
                ]);

                Some(ParsedInstructionData::System {
                    instruction_type: "CreateAccount".to_string(),
                    lamports: Some(lamports),
                    space: Some(space),
                    new_account: None,
                })
            } else {
                None
            }
        }
        2 => {
            // Transfer
            if data.len() >= 12 {
                let lamports = u64::from_le_bytes([
                    data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11],
                ]);

                Some(ParsedInstructionData::System {
                    instruction_type: "Transfer".to_string(),
                    lamports: Some(lamports),
                    space: None,
                    new_account: None,
                })
            } else {
                None
            }
        }
        8 => {
            // Allocate
            if data.len() >= 12 {
                let space = u64::from_le_bytes([
                    data[4], data[5], data[6], data[7], data[8], data[9], data[10], data[11],
                ]);

                Some(ParsedInstructionData::System {
                    instruction_type: "Allocate".to_string(),
                    lamports: None,
                    space: Some(space),
                    new_account: None,
                })
            } else {
                None
            }
        }
        _ => Some(ParsedInstructionData::System {
            instruction_type: "Unknown".to_string(),
            lamports: None,
            space: None,
            new_account: None,
        }),
    }
}

/// Decode Account Compression Program instructions
fn decode_compression_instruction(data: &[u8]) -> Option<ParsedInstructionData> {
    // Return basic instruction info for account compression
    let instruction_name = if data.len() >= 8 {
        // Common account compression operations
        "InsertIntoQueues"
    } else {
        "Unknown"
    };

    Some(ParsedInstructionData::Unknown {
        program_name: "Account Compression".to_string(),
        data_preview: format!("{}({}bytes)", instruction_name, data.len()),
    })
}

/// Decode Compressed Token Program instructions
fn decode_compressed_token_instruction(data: &[u8]) -> Option<ParsedInstructionData> {
    // Return basic instruction info for compressed token operations
    let instruction_name = if data.len() >= 8 {
        // Common compressed token operations
        "TokenOperation"
    } else {
        "Unknown"
    };

    Some(ParsedInstructionData::Unknown {
        program_name: "Compressed Token".to_string(),
        data_preview: format!("{}({}bytes)", instruction_name, data.len()),
    })
}

/// Get human-readable program name
fn get_program_name(program_id: &Pubkey) -> String {
    match program_id.to_string().as_str() {
        id if id == system_program::ID.to_string() => "System Program".to_string(),
        "ComputeBudget111111111111111111111111111111" => "Compute Budget".to_string(),
        "SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7" => "Light System Program".to_string(),
        "compr6CUsB5m2jS4Y3831ztGSTnDpnKJTKS95d64XVq" => "Account Compression".to_string(),
        "FNt7byTHev1k5x2cXZLBr8TdWiC3zoP5vcnZR4P682Uy" => "Test Program".to_string(),
        _ => {
            let pubkey_str = program_id.to_string();
            format!("Program {}", &pubkey_str[..8])
        }
    }
}

/// Extract Light Protocol events from transaction logs and metadata
pub fn extract_light_events(
    logs: &[String],
    _events: &Option<Vec<String>>, // Light Protocol events for future enhancement
) -> Vec<super::types::LightProtocolEvent> {
    let mut light_events = Vec::new();

    // Parse events from logs
    for log in logs {
        if log.contains("PublicTransactionEvent") || log.contains("BatchPublicTransactionEvent") {
            // Parse Light Protocol events from logs
            light_events.push(super::types::LightProtocolEvent {
                event_type: "PublicTransactionEvent".to_string(),
                compressed_accounts: Vec::new(),
                merkle_tree_changes: Vec::new(),
                nullifiers: Vec::new(),
            });
        }
    }

    light_events
}