replay-core 0.1.0

Core engine for the Replay time-travel debugger for Solana transactions.
Documentation
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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
//! IDL-aware account decoder.
//!
//! Three layers:
//!   1. Owner-program dispatch in `AccountDecoder::decode` — known native
//!      programs (SPL Token, Token-2022, System) get manual decoders that
//!      don't need an IDL. Everything else falls through to the Anchor path.
//!   2. `IdlCache` resolution order: bundled (assets/idls/) → on-disk cache
//!      (~/.replay/idl-cache or `REPLAY_IDL_CACHE_DIR`) → on-chain Anchor
//!      IDL account (PDA derived from program_id) → None.
//!   3. Anchor Borsh-via-IDL decoder — walks the IDL's account schema and
//!      consumes the input bytes recursively, emitting `serde_json::Value`.
//!
//! The decoder never returns `Err`; failure cases land as `DecodedAccount`
//! variants (`UnknownDiscriminator`, `NoIdl`, `NotAnchor`) so the UI always
//! has *something* to render — a hex blob is better than a swallowed error.

use crate::error::ReplayError;
use crate::rpc::HeliusClient;
use flate2::read::ZlibDecoder;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use solana_program::hash::hash;
use solana_sdk::{account::Account, pubkey::Pubkey};
use std::collections::HashMap;
use std::io::Read;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::{Duration, SystemTime};
use tracing::{debug, warn};

// Known native program IDs we decode without an IDL.
const SPL_TOKEN: &str = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
const SPL_TOKEN_2022: &str = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb";
const SYSTEM_PROGRAM: &str = "11111111111111111111111111111111";

/// Output of `AccountDecoder::decode`. Always succeeds — failure cases are
/// just other variants. The UI dispatches on the variant.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum DecodedAccount {
    /// We decoded against an IDL successfully.
    Decoded {
        type_name: String,
        value: Value,
        idl_source: IdlSource,
    },
    /// Decoded by a hand-written native decoder (SPL Token, etc.) — no IDL involved.
    Native {
        type_name: String,
        value: Value,
    },
    /// We had an IDL but no account schema matched the 8-byte discriminator.
    UnknownDiscriminator { hex: String },
    /// No IDL available for the owning program.
    NoIdl { owner: String, hex: String },
    /// Account is too short to be Anchor (< 8 bytes for the discriminator).
    NotAnchor { owner: String, hex: String },
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum IdlSource {
    OnChain,
    Manual,
    Cached,
    Bundled,
}

/// Lightweight wrapper around the parsed IDL JSON.
#[derive(Debug, Clone)]
pub struct Idl {
    pub raw: Value,
    pub source: IdlSource,
}

/// On-disk cache of fetched IDLs, plus an in-memory bundled set seeded
/// from `assets/idls/` at construction time.
#[derive(Debug, Clone)]
pub struct IdlCache {
    pub dir: PathBuf,
    pub ttl: Duration,
    bundled: HashMap<Pubkey, Value>,
}

impl Default for IdlCache {
    fn default() -> Self {
        let dir = std::env::var("REPLAY_IDL_CACHE_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|_| {
                let home = std::env::var("HOME")
                    .or_else(|_| std::env::var("USERPROFILE"))
                    .unwrap_or_else(|_| ".".into());
                PathBuf::from(home).join(".replay/idl-cache")
            });
        // Source-tree convention: replay-core/assets/idls/<program_id>.json
        // contains pre-fetched IDLs we ship with the crate. The path is
        // baked at compile time, so it only works when running from the
        // source tree — installed binaries see an empty bundled set and
        // fall through to disk-cache → on-chain fetch.
        let bundled_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets/idls");
        let bundled = load_bundled_idls(&bundled_dir);
        Self {
            dir,
            ttl: Duration::from_secs(7 * 24 * 60 * 60), // 7 days per docs/03-spec
            bundled,
        }
    }
}

impl IdlCache {
    pub fn new(dir: PathBuf) -> Self {
        Self {
            dir,
            ttl: Duration::from_secs(7 * 24 * 60 * 60),
            bundled: HashMap::new(),
        }
    }

    /// Add bundled IDLs. Pass a map from program_id → IDL JSON.
    pub fn with_bundled(mut self, bundled: HashMap<Pubkey, Value>) -> Self {
        self.bundled = bundled;
        self
    }

    /// Manually insert an IDL from a JSON string. Validates the JSON is an
    /// object before writing; persists to disk so subsequent runs don't
    /// need to re-paste.
    pub fn manual_insert_from_json(
        &self,
        program_id: &Pubkey,
        idl_json: &str,
    ) -> Result<(), ReplayError> {
        let value: Value = serde_json::from_str(idl_json)
            .map_err(|e| ReplayError::Idl {
                program_id: program_id.to_string(),
                detail: format!("invalid JSON: {e}"),
            })?;
        if !value.is_object() {
            return Err(ReplayError::Idl {
                program_id: program_id.to_string(),
                detail: "IDL must be a JSON object".into(),
            });
        }
        self.write_disk(program_id, &value)?;
        Ok(())
    }

    /// Persist a fetched/parsed IDL to disk for next-run reuse.
    pub fn insert(&self, program_id: &Pubkey, idl: &Idl) -> Result<(), ReplayError> {
        self.write_disk(program_id, &idl.raw)
    }

    /// Lookup order: bundled → disk cache (within TTL) → on-chain.
    /// Returns Ok(None) when none of the three yield an IDL — *not* Err,
    /// because "no IDL" is a normal state for many programs.
    pub async fn get_or_fetch<C: HeliusClient>(
        &self,
        client: &C,
        program_id: &Pubkey,
    ) -> Result<Option<Idl>, ReplayError> {
        if let Some(raw) = self.bundled.get(program_id) {
            return Ok(Some(Idl {
                raw: raw.clone(),
                source: IdlSource::Bundled,
            }));
        }
        if let Some(idl) = self.read_disk(program_id) {
            return Ok(Some(idl));
        }
        match fetch_anchor_idl(client, program_id).await? {
            Some(raw) => {
                let idl = Idl {
                    raw,
                    source: IdlSource::OnChain,
                };
                let _ = self.write_disk(program_id, &idl.raw); // best-effort persist
                Ok(Some(idl))
            }
            None => Ok(None),
        }
    }

    /// Synchronous lookup: bundled set first, then disk cache (within TTL).
    /// Does NOT make any network requests. Returns `None` when neither source
    /// has an IDL — the caller can fall back to `get_or_fetch` if it has a client.
    pub fn get_local(&self, program_id: &Pubkey) -> Option<Idl> {
        if let Some(raw) = self.bundled.get(program_id) {
            return Some(Idl {
                raw: raw.clone(),
                source: IdlSource::Bundled,
            });
        }
        self.read_disk(program_id)
    }

    fn cache_path(&self, program_id: &Pubkey) -> PathBuf {
        self.dir.join(format!("{}.json", program_id))
    }

    fn read_disk(&self, program_id: &Pubkey) -> Option<Idl> {
        let path = self.cache_path(program_id);
        let metadata = std::fs::metadata(&path).ok()?;
        let modified = metadata.modified().ok()?;
        if SystemTime::now().duration_since(modified).ok()? > self.ttl {
            debug!(?program_id, "disk-cached IDL expired");
            return None;
        }
        let bytes = std::fs::read(&path).ok()?;
        let raw: Value = serde_json::from_slice(&bytes).ok()?;
        Some(Idl {
            raw,
            source: IdlSource::Cached,
        })
    }

    fn write_disk(&self, program_id: &Pubkey, raw: &Value) -> Result<(), ReplayError> {
        std::fs::create_dir_all(&self.dir)?;
        let path = self.cache_path(program_id);
        let bytes = serde_json::to_vec_pretty(raw)?;
        std::fs::write(&path, bytes)?;
        Ok(())
    }
}

/// Anchor stores its IDL at a deterministic address derived from the
/// program ID. The derivation is: base = find_program_address(&[], program_id),
/// then create_with_seed(base, "anchor:idl", program_id).
pub fn anchor_idl_address(program_id: &Pubkey) -> Pubkey {
    let (base, _bump) = Pubkey::find_program_address(&[], program_id);
    Pubkey::create_with_seed(&base, "anchor:idl", program_id)
        .expect("create_with_seed cannot fail with a valid base + short seed")
}

/// Fetch and parse an Anchor IDL from on-chain. Returns Ok(None) when the
/// program has no IDL account (the address is empty), Err only on fetch /
/// decompression failures that suggest a real bug.
async fn fetch_anchor_idl<C: HeliusClient>(
    client: &C,
    program_id: &Pubkey,
) -> Result<Option<Value>, ReplayError> {
    let idl_address = anchor_idl_address(program_id);
    let account = match client.get_account_info(&idl_address).await? {
        Some(a) if !a.data.is_empty() => a,
        _ => {
            debug!(?program_id, ?idl_address, "no on-chain IDL account");
            return Ok(None);
        }
    };

    // Layout: [discriminator: 8][authority: 32][len: u32 LE][zlib(idl_json)]
    if account.data.len() < 44 {
        warn!(
            ?program_id,
            data_len = account.data.len(),
            "IDL account too short for Anchor header; skipping"
        );
        return Ok(None);
    }
    let len = u32::from_le_bytes(
        account.data[40..44]
            .try_into()
            .expect("4 bytes"),
    ) as usize;
    if 44 + len > account.data.len() {
        return Err(ReplayError::Idl {
            program_id: program_id.to_string(),
            detail: format!(
                "IDL header claims len={} but account has {} bytes after header",
                len,
                account.data.len() - 44
            ),
        });
    }
    let compressed = &account.data[44..44 + len];

    let mut decoder = ZlibDecoder::new(compressed);
    let mut decompressed = Vec::with_capacity(len * 8);
    decoder
        .read_to_end(&mut decompressed)
        .map_err(|e| ReplayError::Idl {
            program_id: program_id.to_string(),
            detail: format!("zlib decompress: {e}"),
        })?;

    let raw: Value = serde_json::from_slice(&decompressed).map_err(|e| ReplayError::Idl {
        program_id: program_id.to_string(),
        detail: format!("IDL JSON parse: {e}"),
    })?;
    Ok(Some(raw))
}

// ---------- decoder dispatch ----------

pub struct AccountDecoder<'a> {
    idl_cache: &'a IdlCache,
}

impl<'a> AccountDecoder<'a> {
    pub fn new(idl_cache: &'a IdlCache) -> Self {
        Self { idl_cache }
    }

    /// Synchronous instruction decode using only bundled + disk-cached IDLs.
    /// Returns `(instruction_name, decoded_args, account_role_names)`.
    /// Used by the trace builder which cannot block on network fetches.
    pub fn decode_instruction_local(
        &self,
        program_id: &Pubkey,
        data: &[u8],
    ) -> Option<(String, Value, Vec<String>)> {
        let idl = self.idl_cache.get_local(program_id)?;
        decode_instruction_data(&idl, data)
    }

    /// Decode an account against the best-available decoder. Never errors;
    /// failure surfaces as `DecodedAccount::{NoIdl, UnknownDiscriminator, NotAnchor}`.
    pub async fn decode<C: HeliusClient>(
        &self,
        _pubkey: &Pubkey,
        account: &Account,
        client: &C,
    ) -> DecodedAccount {
        let owner_str = account.owner.to_string();
        let hex_data = || hex::encode(&account.data);

        // 1. Owner-program dispatch for known native programs.
        if owner_str == SPL_TOKEN || owner_str == SPL_TOKEN_2022 {
            if let Some((type_name, value)) = decode_spl_token(&account.data) {
                return DecodedAccount::Native { type_name, value };
            }
            // Recognised owner but unfamiliar size: fall through to NoIdl below.
        }

        if owner_str == SYSTEM_PROGRAM {
            return DecodedAccount::Native {
                type_name: "SystemAccount".into(),
                value: json!({
                    "lamports": account.lamports,
                    "data_len": account.data.len(),
                    "data_hex": hex_data(),
                }),
            };
        }

        // 2. Fall through to Anchor IDL.
        let idl = match self.idl_cache.get_or_fetch(client, &account.owner).await {
            Ok(Some(idl)) => idl,
            Ok(None) => {
                return DecodedAccount::NoIdl {
                    owner: owner_str,
                    hex: hex_data(),
                };
            }
            Err(e) => {
                warn!(owner = %owner_str, error = %e, "IDL fetch failed; treating as no-IDL");
                return DecodedAccount::NoIdl {
                    owner: owner_str,
                    hex: hex_data(),
                };
            }
        };

        if account.data.len() < 8 {
            return DecodedAccount::NotAnchor {
                owner: owner_str,
                hex: hex_data(),
            };
        }

        match decode_anchor(&idl, &account.data) {
            Some((type_name, value)) => DecodedAccount::Decoded {
                type_name,
                value,
                idl_source: idl.source,
            },
            None => DecodedAccount::UnknownDiscriminator { hex: hex_data() },
        }
    }
}

// ---------- non-Anchor decoders (SPL Token & Token-2022) ----------

/// Dispatch by data length. Returns `(type_name, value)` on success.
/// Token-2022 with extensions appends data after byte 165; we ignore
/// extensions for now and decode the base layout.
fn decode_spl_token(data: &[u8]) -> Option<(String, Value)> {
    match data.len() {
        165 => decode_spl_token_account(data).map(|v| ("TokenAccount".into(), v)),
        82 => decode_spl_mint(data).map(|v| ("Mint".into(), v)),
        355 => decode_spl_multisig(data).map(|v| ("Multisig".into(), v)),
        n if n > 165 => {
            // Token-2022 account with extensions; decode the base + flag.
            decode_spl_token_account(&data[..165]).map(|mut v| {
                if let Some(obj) = v.as_object_mut() {
                    obj.insert("has_extensions".into(), json!(true));
                    obj.insert("extension_bytes".into(), json!(n - 165));
                }
                ("TokenAccount".into(), v)
            })
        }
        _ => None,
    }
}

fn decode_spl_token_account(data: &[u8]) -> Option<Value> {
    if data.len() < 165 {
        return None;
    }
    let mint = pubkey_from_slice(&data[0..32])?.to_string();
    let owner = pubkey_from_slice(&data[32..64])?.to_string();
    let amount = read_u64_le(&data[64..72])?;
    let delegate_tag = read_u32_le(&data[72..76])?;
    let delegate = if delegate_tag != 0 {
        Some(pubkey_from_slice(&data[76..108])?.to_string())
    } else {
        None
    };
    let state = match data[108] {
        0 => "uninitialized",
        1 => "initialized",
        2 => "frozen",
        _ => "unknown",
    };
    let is_native_tag = read_u32_le(&data[109..113])?;
    let is_native = if is_native_tag != 0 {
        Some(read_u64_le(&data[113..121])?)
    } else {
        None
    };
    let delegated_amount = read_u64_le(&data[121..129])?;
    let close_authority_tag = read_u32_le(&data[129..133])?;
    let close_authority = if close_authority_tag != 0 {
        Some(pubkey_from_slice(&data[133..165])?.to_string())
    } else {
        None
    };
    Some(json!({
        "mint": mint,
        "owner": owner,
        "amount": amount.to_string(),
        "delegate": delegate,
        "state": state,
        "is_native_lamports": is_native.map(|v| v.to_string()),
        "delegated_amount": delegated_amount.to_string(),
        "close_authority": close_authority,
    }))
}

fn decode_spl_mint(data: &[u8]) -> Option<Value> {
    if data.len() < 82 {
        return None;
    }
    let mint_authority_tag = read_u32_le(&data[0..4])?;
    let mint_authority = if mint_authority_tag != 0 {
        Some(pubkey_from_slice(&data[4..36])?.to_string())
    } else {
        None
    };
    let supply = read_u64_le(&data[36..44])?;
    let decimals = data[44];
    let is_initialized = data[45] != 0;
    let freeze_authority_tag = read_u32_le(&data[46..50])?;
    let freeze_authority = if freeze_authority_tag != 0 {
        Some(pubkey_from_slice(&data[50..82])?.to_string())
    } else {
        None
    };
    Some(json!({
        "mint_authority": mint_authority,
        "supply": supply.to_string(),
        "decimals": decimals,
        "is_initialized": is_initialized,
        "freeze_authority": freeze_authority,
    }))
}

fn decode_spl_multisig(data: &[u8]) -> Option<Value> {
    if data.len() < 355 {
        return None;
    }
    let m = data[0];
    let n = data[1];
    let is_initialized = data[2] != 0;
    let mut signers = Vec::new();
    for i in 0..(n as usize).min(11) {
        let off = 3 + 32 * i;
        if let Some(pk) = pubkey_from_slice(&data[off..off + 32]) {
            signers.push(pk.to_string());
        }
    }
    Some(json!({
        "m": m,
        "n": n,
        "is_initialized": is_initialized,
        "signers": signers,
    }))
}

fn pubkey_from_slice(bytes: &[u8]) -> Option<Pubkey> {
    let arr: [u8; 32] = bytes.try_into().ok()?;
    Some(Pubkey::from(arr))
}

fn read_u32_le(bytes: &[u8]) -> Option<u32> {
    let arr: [u8; 4] = bytes.try_into().ok()?;
    Some(u32::from_le_bytes(arr))
}

fn read_u64_le(bytes: &[u8]) -> Option<u64> {
    let arr: [u8; 8] = bytes.try_into().ok()?;
    Some(u64::from_le_bytes(arr))
}

// ---------- Anchor IDL decoder ----------

#[derive(Debug)]
struct BorshErr(String);

fn anchor_account_discriminator(name: &str) -> [u8; 8] {
    // sha256("account:<Name>")[..8]
    let h = hash(format!("account:{name}").as_bytes());
    let bytes = h.to_bytes();
    let mut disc = [0u8; 8];
    disc.copy_from_slice(&bytes[..8]);
    disc
}

fn decode_anchor(idl: &Idl, data: &[u8]) -> Option<(String, Value)> {
    let accounts = idl.raw.get("accounts").and_then(|v| v.as_array())?;
    let disc = &data[..8];
    for acct in accounts {
        let name = acct.get("name").and_then(|v| v.as_str())?;
        if anchor_account_discriminator(name) != disc {
            continue;
        }
        let type_def = acct.get("type")?;
        let mut cursor: &[u8] = &data[8..];
        match decode_type(idl, type_def, &mut cursor) {
            Ok(v) => return Some((name.to_string(), v)),
            Err(e) => {
                warn!(account = name, err = %e.0, "anchor decode failed");
                return None;
            }
        }
    }
    None
}

fn decode_type(idl: &Idl, ty: &Value, bytes: &mut &[u8]) -> Result<Value, BorshErr> {
    if let Some(s) = ty.as_str() {
        return decode_primitive(s, bytes);
    }
    let obj = ty
        .as_object()
        .ok_or_else(|| BorshErr(format!("unsupported type shape: {ty}")))?;

    if let Some(kind) = obj.get("kind").and_then(|v| v.as_str()) {
        match kind {
            "struct" => {
                let fields = obj
                    .get("fields")
                    .and_then(|v| v.as_array())
                    .ok_or_else(|| BorshErr("struct missing fields".into()))?;
                let mut out = serde_json::Map::new();
                for f in fields {
                    let name = f
                        .get("name")
                        .and_then(|v| v.as_str())
                        .ok_or_else(|| BorshErr("struct field missing name".into()))?;
                    let f_ty = f
                        .get("type")
                        .ok_or_else(|| BorshErr("struct field missing type".into()))?;
                    out.insert(name.into(), decode_type(idl, f_ty, bytes)?);
                }
                return Ok(Value::Object(out));
            }
            "enum" => {
                let variants = obj
                    .get("variants")
                    .and_then(|v| v.as_array())
                    .ok_or_else(|| BorshErr("enum missing variants".into()))?;
                let tag = take_u8(bytes)?;
                let variant = variants
                    .get(tag as usize)
                    .ok_or_else(|| BorshErr(format!("enum tag {tag} out of range")))?;
                let name = variant
                    .get("name")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| BorshErr("variant missing name".into()))?;
                let payload = if let Some(fields) = variant.get("fields").and_then(|v| v.as_array())
                {
                    if fields.is_empty() {
                        Value::Null
                    } else if let Some(name0) = fields[0].get("name") {
                        // Named-field variant.
                        let _ = name0;
                        let mut out = serde_json::Map::new();
                        for f in fields {
                            let n = f
                                .get("name")
                                .and_then(|v| v.as_str())
                                .ok_or_else(|| BorshErr("variant field missing name".into()))?;
                            let f_ty = f
                                .get("type")
                                .ok_or_else(|| BorshErr("variant field missing type".into()))?;
                            out.insert(n.into(), decode_type(idl, f_ty, bytes)?);
                        }
                        Value::Object(out)
                    } else {
                        // Tuple variant — `fields` is a list of types.
                        let mut arr = Vec::with_capacity(fields.len());
                        for f_ty in fields {
                            arr.push(decode_type(idl, f_ty, bytes)?);
                        }
                        Value::Array(arr)
                    }
                } else {
                    Value::Null
                };
                return Ok(json!({ "variant": name, "payload": payload }));
            }
            other => return Err(BorshErr(format!("unsupported kind: {other}"))),
        }
    }

    if let Some(inner) = obj.get("option") {
        let tag = take_u8(bytes)?;
        return if tag == 0 {
            Ok(Value::Null)
        } else {
            decode_type(idl, inner, bytes)
        };
    }
    if let Some(inner) = obj.get("vec") {
        let len = take_u32(bytes)? as usize;
        let mut arr = Vec::with_capacity(len);
        for _ in 0..len {
            arr.push(decode_type(idl, inner, bytes)?);
        }
        return Ok(Value::Array(arr));
    }
    if let Some(spec) = obj.get("array").and_then(|v| v.as_array()) {
        if spec.len() != 2 {
            return Err(BorshErr("array spec must be [type, length]".into()));
        }
        let n = spec[1]
            .as_u64()
            .ok_or_else(|| BorshErr("array length not an int".into()))? as usize;
        let mut arr = Vec::with_capacity(n);
        for _ in 0..n {
            arr.push(decode_type(idl, &spec[0], bytes)?);
        }
        return Ok(Value::Array(arr));
    }
    if let Some(name) = obj.get("defined").and_then(|v| v.as_str()) {
        let types = idl
            .raw
            .get("types")
            .and_then(|v| v.as_array())
            .ok_or_else(|| BorshErr(format!("type `{name}` referenced but no `types` in IDL")))?;
        let user_type = types
            .iter()
            .find(|t| t.get("name").and_then(|n| n.as_str()) == Some(name))
            .ok_or_else(|| BorshErr(format!("type `{name}` not found in IDL")))?;
        let inner = user_type
            .get("type")
            .ok_or_else(|| BorshErr(format!("type `{name}` missing `type` field")))?;
        return decode_type(idl, inner, bytes);
    }

    Err(BorshErr(format!("unrecognised type shape: {ty}")))
}

fn decode_primitive(name: &str, bytes: &mut &[u8]) -> Result<Value, BorshErr> {
    match name {
        "u8" => Ok(json!(take_u8(bytes)?)),
        "i8" => Ok(json!(take_u8(bytes)? as i8)),
        "u16" => Ok(json!(u16::from_le_bytes(take_n::<2>(bytes)?))),
        "i16" => Ok(json!(i16::from_le_bytes(take_n::<2>(bytes)?))),
        "u32" => Ok(json!(u32::from_le_bytes(take_n::<4>(bytes)?))),
        "i32" => Ok(json!(i32::from_le_bytes(take_n::<4>(bytes)?))),
        "u64" => Ok(json!(u64::from_le_bytes(take_n::<8>(bytes)?).to_string())),
        "i64" => Ok(json!(i64::from_le_bytes(take_n::<8>(bytes)?).to_string())),
        // u128/i128 don't fit JSON's f64 without loss; serialize as strings.
        "u128" => Ok(json!(u128::from_le_bytes(take_n::<16>(bytes)?).to_string())),
        "i128" => Ok(json!(i128::from_le_bytes(take_n::<16>(bytes)?).to_string())),
        "f32" => Ok(json!(f32::from_le_bytes(take_n::<4>(bytes)?))),
        "f64" => Ok(json!(f64::from_le_bytes(take_n::<8>(bytes)?))),
        "bool" => Ok(json!(take_u8(bytes)? != 0)),
        "string" => {
            let len = take_u32(bytes)? as usize;
            let raw = take_slice(bytes, len)?;
            let s = String::from_utf8(raw.to_vec())
                .map_err(|e| BorshErr(format!("invalid utf-8 in string: {e}")))?;
            Ok(json!(s))
        }
        "publicKey" | "pubkey" => {
            let arr = take_n::<32>(bytes)?;
            Ok(json!(Pubkey::from(arr).to_string()))
        }
        "bytes" => {
            let len = take_u32(bytes)? as usize;
            let raw = take_slice(bytes, len)?;
            Ok(json!(hex::encode(raw)))
        }
        other => Err(BorshErr(format!("unsupported primitive: {other}"))),
    }
}

fn take_u8(bytes: &mut &[u8]) -> Result<u8, BorshErr> {
    if bytes.is_empty() {
        return Err(BorshErr("EOF reading u8".into()));
    }
    let v = bytes[0];
    *bytes = &bytes[1..];
    Ok(v)
}

fn take_u32(bytes: &mut &[u8]) -> Result<u32, BorshErr> {
    Ok(u32::from_le_bytes(take_n::<4>(bytes)?))
}

fn take_n<const N: usize>(bytes: &mut &[u8]) -> Result<[u8; N], BorshErr> {
    if bytes.len() < N {
        return Err(BorshErr(format!("EOF reading {N} bytes (have {})", bytes.len())));
    }
    let mut buf = [0u8; N];
    buf.copy_from_slice(&bytes[..N]);
    *bytes = &bytes[N..];
    Ok(buf)
}

fn take_slice<'a>(bytes: &mut &'a [u8], n: usize) -> Result<&'a [u8], BorshErr> {
    if bytes.len() < n {
        return Err(BorshErr(format!("EOF reading {n} bytes (have {})", bytes.len())));
    }
    let s = &bytes[..n];
    *bytes = &bytes[n..];
    Ok(s)
}

// ---------- Anchor IDL encoder (mirrors the decoder above) ----------

/// Apply a field mutation to an Anchor account's raw data.
///
/// Steps:
/// 1. Locate the account type by discriminator.
/// 2. Decode the full account to JSON with the existing decoder.
/// 3. Navigate to `path` (dot-separated) and overwrite the value.
/// 4. Re-encode back to Borsh bytes (discriminator preserved verbatim).
pub fn apply_field_mutation(
    idl: &Idl,
    data: &[u8],
    path: &str,
    new_value: &Value,
) -> Result<Vec<u8>, ReplayError> {
    if data.len() < 8 {
        return Err(ReplayError::Decoder(
            "account data too short for Anchor discriminator".into(),
        ));
    }
    let disc_bytes: [u8; 8] = data[..8].try_into().expect("8 bytes");

    let accounts = idl
        .raw
        .get("accounts")
        .and_then(|v| v.as_array())
        .ok_or_else(|| ReplayError::Decoder("IDL has no `accounts` array".into()))?;

    let acct_def = accounts
        .iter()
        .find(|a| {
            let name = a.get("name").and_then(|v| v.as_str()).unwrap_or("");
            anchor_account_discriminator(name) == disc_bytes
        })
        .ok_or_else(|| ReplayError::InvalidMutationPath {
            path: path.into(),
            type_name: "unknown_discriminator".into(),
        })?;

    let type_name = acct_def
        .get("name")
        .and_then(|v| v.as_str())
        .unwrap_or("unknown")
        .to_string();
    let type_def = acct_def
        .get("type")
        .ok_or_else(|| ReplayError::Decoder("account missing `type` in IDL".into()))?;

    let mut cursor: &[u8] = &data[8..];
    let mut decoded = decode_type(idl, type_def, &mut cursor)
        .map_err(|e| ReplayError::Decoder(format!("decode for field mutation: {}", e.0)))?;

    set_json_path(&mut decoded, path, new_value.clone()).map_err(|e| {
        ReplayError::InvalidMutationPath {
            path: path.into(),
            type_name: format!("{type_name}: {e}"),
        }
    })?;

    let mut buf = Vec::with_capacity(data.len());
    buf.extend_from_slice(&disc_bytes);
    encode_type(idl, type_def, &decoded, &mut buf)
        .map_err(|e| ReplayError::Decoder(format!("re-encode after field mutation: {}", e.0)))?;

    Ok(buf)
}

fn set_json_path(val: &mut Value, path: &str, new_val: Value) -> Result<(), String> {
    let (head, tail) = match path.find('.') {
        Some(i) => (&path[..i], Some(&path[i + 1..])),
        None => (path, None),
    };
    match val {
        Value::Object(map) => {
            if let Some(rest) = tail {
                let inner = map
                    .get_mut(head)
                    .ok_or_else(|| format!("field `{head}` not found"))?;
                set_json_path(inner, rest, new_val)
            } else {
                if !map.contains_key(head) {
                    return Err(format!("field `{head}` not found"));
                }
                map.insert(head.into(), new_val);
                Ok(())
            }
        }
        _ => Err(format!("cannot navigate into non-object at `{head}`")),
    }
}

fn encode_type(idl: &Idl, ty: &Value, value: &Value, buf: &mut Vec<u8>) -> Result<(), BorshErr> {
    if let Some(s) = ty.as_str() {
        return encode_primitive(s, value, buf);
    }
    let obj = ty
        .as_object()
        .ok_or_else(|| BorshErr(format!("unsupported type shape: {ty}")))?;

    if let Some(kind) = obj.get("kind").and_then(|v| v.as_str()) {
        match kind {
            "struct" => {
                let fields = obj
                    .get("fields")
                    .and_then(|v| v.as_array())
                    .ok_or_else(|| BorshErr("struct missing fields".into()))?;
                let map = value
                    .as_object()
                    .ok_or_else(|| BorshErr("expected object for struct value".into()))?;
                for f in fields {
                    let name = f
                        .get("name")
                        .and_then(|v| v.as_str())
                        .ok_or_else(|| BorshErr("struct field missing name".into()))?;
                    let f_ty = f
                        .get("type")
                        .ok_or_else(|| BorshErr("struct field missing type".into()))?;
                    let fval = map
                        .get(name)
                        .ok_or_else(|| BorshErr(format!("struct missing field `{name}` in value")))?;
                    encode_type(idl, f_ty, fval, buf)?;
                }
                return Ok(());
            }
            "enum" => {
                let variants = obj
                    .get("variants")
                    .and_then(|v| v.as_array())
                    .ok_or_else(|| BorshErr("enum missing variants".into()))?;
                let variant_name = value
                    .get("variant")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| BorshErr("enum value missing `variant` key".into()))?;
                let (tag, variant) = variants
                    .iter()
                    .enumerate()
                    .find(|(_, v)| v.get("name").and_then(|n| n.as_str()) == Some(variant_name))
                    .ok_or_else(|| BorshErr(format!("enum variant `{variant_name}` not found")))?;
                buf.push(tag as u8);
                let payload = value.get("payload").unwrap_or(&Value::Null);
                if let Some(fields) = variant.get("fields").and_then(|v| v.as_array()) {
                    if !fields.is_empty() {
                        if fields[0].get("name").is_some() {
                            let fmap = payload.as_object().ok_or_else(|| {
                                BorshErr("named variant payload must be object".into())
                            })?;
                            for f in fields {
                                let n = f
                                    .get("name")
                                    .and_then(|v| v.as_str())
                                    .ok_or_else(|| BorshErr("variant field missing name".into()))?;
                                let f_ty = f
                                    .get("type")
                                    .ok_or_else(|| BorshErr("variant field missing type".into()))?;
                                let fval = fmap.get(n).ok_or_else(|| {
                                    BorshErr(format!("variant missing field `{n}`"))
                                })?;
                                encode_type(idl, f_ty, fval, buf)?;
                            }
                        } else {
                            let arr = payload.as_array().ok_or_else(|| {
                                BorshErr("tuple variant payload must be array".into())
                            })?;
                            for (f_ty, val) in fields.iter().zip(arr.iter()) {
                                encode_type(idl, f_ty, val, buf)?;
                            }
                        }
                    }
                }
                return Ok(());
            }
            other => return Err(BorshErr(format!("unsupported kind: {other}"))),
        }
    }

    if let Some(inner) = obj.get("option") {
        if value.is_null() {
            buf.push(0);
        } else {
            buf.push(1);
            encode_type(idl, inner, value, buf)?;
        }
        return Ok(());
    }
    if let Some(inner) = obj.get("vec") {
        let arr = value
            .as_array()
            .ok_or_else(|| BorshErr("expected array for vec".into()))?;
        buf.extend_from_slice(&(arr.len() as u32).to_le_bytes());
        for item in arr {
            encode_type(idl, inner, item, buf)?;
        }
        return Ok(());
    }
    if let Some(spec) = obj.get("array").and_then(|v| v.as_array()) {
        if spec.len() != 2 {
            return Err(BorshErr("array spec must be [type, length]".into()));
        }
        let n = spec[1]
            .as_u64()
            .ok_or_else(|| BorshErr("array length not an int".into()))? as usize;
        let arr = value
            .as_array()
            .ok_or_else(|| BorshErr("expected array for fixed array".into()))?;
        if arr.len() != n {
            return Err(BorshErr(format!(
                "array length mismatch: expected {n}, got {}",
                arr.len()
            )));
        }
        for item in arr {
            encode_type(idl, &spec[0], item, buf)?;
        }
        return Ok(());
    }
    if let Some(name) = obj.get("defined").and_then(|v| v.as_str()) {
        let types = idl
            .raw
            .get("types")
            .and_then(|v| v.as_array())
            .ok_or_else(|| BorshErr(format!("type `{name}` referenced but no `types` in IDL")))?;
        let user_type = types
            .iter()
            .find(|t| t.get("name").and_then(|n| n.as_str()) == Some(name))
            .ok_or_else(|| BorshErr(format!("type `{name}` not found in IDL")))?;
        let inner = user_type
            .get("type")
            .ok_or_else(|| BorshErr(format!("type `{name}` missing `type` field")))?;
        return encode_type(idl, inner, value, buf);
    }

    Err(BorshErr(format!("unrecognised type shape: {ty}")))
}

fn encode_primitive(name: &str, value: &Value, buf: &mut Vec<u8>) -> Result<(), BorshErr> {
    match name {
        "u8" => buf.push(coerce_u64(value)? as u8),
        "i8" => buf.push(coerce_i64(value)? as i8 as u8),
        "u16" => buf.extend_from_slice(&(coerce_u64(value)? as u16).to_le_bytes()),
        "i16" => buf.extend_from_slice(&(coerce_i64(value)? as i16).to_le_bytes()),
        "u32" => buf.extend_from_slice(&(coerce_u64(value)? as u32).to_le_bytes()),
        "i32" => buf.extend_from_slice(&(coerce_i64(value)? as i32).to_le_bytes()),
        "u64" => buf.extend_from_slice(&coerce_u64(value)?.to_le_bytes()),
        "i64" => buf.extend_from_slice(&coerce_i64(value)?.to_le_bytes()),
        "u128" => buf.extend_from_slice(&coerce_u128(value)?.to_le_bytes()),
        "i128" => buf.extend_from_slice(&coerce_i128(value)?.to_le_bytes()),
        "f32" => {
            let v = value
                .as_f64()
                .ok_or_else(|| BorshErr("expected f32".into()))? as f32;
            buf.extend_from_slice(&v.to_le_bytes());
        }
        "f64" => {
            let v = value
                .as_f64()
                .ok_or_else(|| BorshErr("expected f64".into()))?;
            buf.extend_from_slice(&v.to_le_bytes());
        }
        "bool" => buf.push(
            value
                .as_bool()
                .ok_or_else(|| BorshErr("expected bool".into()))? as u8,
        ),
        "string" => {
            let s = value
                .as_str()
                .ok_or_else(|| BorshErr("expected string".into()))?;
            buf.extend_from_slice(&(s.len() as u32).to_le_bytes());
            buf.extend_from_slice(s.as_bytes());
        }
        "publicKey" | "pubkey" => {
            let s = value
                .as_str()
                .ok_or_else(|| BorshErr("expected pubkey string".into()))?;
            let pk =
                Pubkey::from_str(s).map_err(|e| BorshErr(format!("bad pubkey `{s}`: {e}")))?;
            buf.extend_from_slice(pk.as_ref());
        }
        "bytes" => {
            let s = value
                .as_str()
                .ok_or_else(|| BorshErr("expected hex string for bytes".into()))?;
            let raw =
                hex::decode(s).map_err(|e| BorshErr(format!("bad hex in bytes: {e}")))?;
            buf.extend_from_slice(&(raw.len() as u32).to_le_bytes());
            buf.extend_from_slice(&raw);
        }
        other => return Err(BorshErr(format!("unsupported primitive: {other}"))),
    }
    Ok(())
}

fn coerce_u64(v: &Value) -> Result<u64, BorshErr> {
    v.as_u64()
        .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        .ok_or_else(|| BorshErr(format!("expected u64-compatible value, got {v}")))
}

fn coerce_i64(v: &Value) -> Result<i64, BorshErr> {
    v.as_i64()
        .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        .ok_or_else(|| BorshErr(format!("expected i64-compatible value, got {v}")))
}

fn coerce_u128(v: &Value) -> Result<u128, BorshErr> {
    v.as_u64()
        .map(|n| n as u128)
        .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        .ok_or_else(|| BorshErr(format!("expected u128-compatible value, got {v}")))
}

fn coerce_i128(v: &Value) -> Result<i128, BorshErr> {
    v.as_i64()
        .map(|n| n as i128)
        .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        .ok_or_else(|| BorshErr(format!("expected i128-compatible value, got {v}")))
}

// ---------- instruction discriminator + decoder ----------

/// Anchor instruction discriminator: sha256("global:<name>")[..8].
/// Different from the account discriminator which uses "account:<name>".
pub fn anchor_instruction_discriminator(name: &str) -> [u8; 8] {
    let h = hash(format!("global:{name}").as_bytes());
    let bytes = h.to_bytes();
    let mut disc = [0u8; 8];
    disc.copy_from_slice(&bytes[..8]);
    disc
}

/// Match instruction data against the IDL's `instructions` array.
/// Returns `(name, decoded_args, account_role_names)` or `None` when
/// the discriminator doesn't match any instruction.
pub fn decode_instruction_data(idl: &Idl, data: &[u8]) -> Option<(String, Value, Vec<String>)> {
    if data.len() < 8 {
        return None;
    }
    let disc = &data[..8];
    let instructions = idl.raw.get("instructions").and_then(|v| v.as_array())?;

    for instr in instructions {
        let name = instr.get("name").and_then(|v| v.as_str())?;
        if anchor_instruction_discriminator(name) != disc {
            continue;
        }

        let decoded_args = if let Some(args) = instr.get("args").and_then(|v| v.as_array()) {
            let mut cursor: &[u8] = &data[8..];
            let mut out = serde_json::Map::new();
            let mut ok = true;
            for arg in args {
                let arg_name = match arg.get("name").and_then(|v| v.as_str()) {
                    Some(n) => n,
                    None => {
                        ok = false;
                        break;
                    }
                };
                let arg_type = match arg.get("type") {
                    Some(t) => t,
                    None => {
                        ok = false;
                        break;
                    }
                };
                match decode_type(idl, arg_type, &mut cursor) {
                    Ok(v) => {
                        out.insert(arg_name.into(), v);
                    }
                    Err(e) => {
                        warn!(instruction = name, arg = arg_name, err = %e.0, "arg decode failed");
                        ok = false;
                        break;
                    }
                }
            }
            if ok {
                Value::Object(out)
            } else {
                Value::Null
            }
        } else {
            Value::Object(serde_json::Map::new())
        };

        let roles: Vec<String> = instr
            .get("accounts")
            .and_then(|v| v.as_array())
            .map(|accts| {
                accts
                    .iter()
                    .filter_map(|a| a.get("name").and_then(|v| v.as_str()).map(str::to_string))
                    .collect()
            })
            .unwrap_or_default();

        return Some((name.to_string(), decoded_args, roles));
    }
    None
}

/// Load any `assets/idls/<program_id>.json` files at the workspace root
/// into a bundled-IDL map. Returns an empty map if the directory doesn't
/// exist (typical: nothing bundled yet).
pub fn load_bundled_idls(dir: &std::path::Path) -> HashMap<Pubkey, Value> {
    let mut out = HashMap::new();
    let Ok(entries) = std::fs::read_dir(dir) else { return out };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.extension().and_then(|s| s.to_str()) != Some("json") {
            continue;
        }
        let Some(stem) = path.file_stem().and_then(|s| s.to_str()) else { continue };
        let Ok(pk) = Pubkey::from_str(stem) else {
            warn!(?path, "bundled IDL filename is not a valid pubkey; skipping");
            continue;
        };
        match std::fs::read(&path).ok().and_then(|b| serde_json::from_slice(&b).ok()) {
            Some(raw) => {
                out.insert(pk, raw);
            }
            None => warn!(?path, "failed to parse bundled IDL"),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::MockHeliusClient;

    #[test]
    fn anchor_idl_pda_is_deterministic() {
        let pid = Pubkey::from_str("JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4").unwrap();
        let a = anchor_idl_address(&pid);
        let b = anchor_idl_address(&pid);
        assert_eq!(a, b);
    }

    #[test]
    fn anchor_account_discriminator_matches_expected() {
        // sha256("account:Position")[..8] = e8 90 92 9d 8c 35 7a 0a (well-known)
        let d = anchor_account_discriminator("Position");
        assert_eq!(d.len(), 8);
        // Self-consistency: same input → same output.
        assert_eq!(d, anchor_account_discriminator("Position"));
    }

    #[test]
    fn spl_mint_decoder_round_trips_a_known_layout() {
        // Synthesize an 82-byte mint: no mint_authority, supply=12345, decimals=6,
        // initialized, freeze_authority present.
        let mut data = vec![0u8; 82];
        // mint_authority absent (tag = 0)
        data[0..4].copy_from_slice(&0u32.to_le_bytes());
        // supply
        data[36..44].copy_from_slice(&12345u64.to_le_bytes());
        // decimals
        data[44] = 6;
        // is_initialized
        data[45] = 1;
        // freeze_authority present (tag = 1)
        data[46..50].copy_from_slice(&1u32.to_le_bytes());
        let fz = Pubkey::new_unique();
        data[50..82].copy_from_slice(fz.as_ref());

        let v = decode_spl_mint(&data).unwrap();
        assert_eq!(v["supply"], "12345");
        assert_eq!(v["decimals"], 6);
        assert_eq!(v["is_initialized"], true);
        assert_eq!(v["mint_authority"], Value::Null);
        assert_eq!(v["freeze_authority"], json!(fz.to_string()));
    }

    #[test]
    fn spl_token_account_decoder_round_trips() {
        // 165-byte token account: mint X, owner Y, amount 1_000_000, delegate absent,
        // state initialized, no native, delegated_amount 0, close_authority absent.
        let mut data = vec![0u8; 165];
        let mint = Pubkey::new_unique();
        let owner = Pubkey::new_unique();
        data[0..32].copy_from_slice(mint.as_ref());
        data[32..64].copy_from_slice(owner.as_ref());
        data[64..72].copy_from_slice(&1_000_000u64.to_le_bytes());
        data[72..76].copy_from_slice(&0u32.to_le_bytes()); // delegate absent
        data[108] = 1; // initialized
        data[109..113].copy_from_slice(&0u32.to_le_bytes()); // is_native absent
        data[121..129].copy_from_slice(&0u64.to_le_bytes());
        data[129..133].copy_from_slice(&0u32.to_le_bytes()); // close_authority absent

        let v = decode_spl_token_account(&data).unwrap();
        assert_eq!(v["mint"], mint.to_string());
        assert_eq!(v["owner"], owner.to_string());
        assert_eq!(v["amount"], "1000000");
        assert_eq!(v["state"], "initialized");
        assert_eq!(v["delegate"], Value::Null);
    }

    #[tokio::test]
    async fn decode_dispatches_to_spl_token() {
        let cache = IdlCache::new(std::env::temp_dir().join("replay-idl-test"));
        let dec = AccountDecoder::new(&cache);
        let mut data = vec![0u8; 165];
        let mint = Pubkey::new_unique();
        let owner = Pubkey::new_unique();
        data[0..32].copy_from_slice(mint.as_ref());
        data[32..64].copy_from_slice(owner.as_ref());
        data[64..72].copy_from_slice(&42u64.to_le_bytes());
        data[108] = 1;

        let acct = Account {
            lamports: 2_039_280,
            data,
            owner: Pubkey::from_str(SPL_TOKEN).unwrap(),
            executable: false,
            rent_epoch: 0,
        };
        let client = MockHeliusClient::default();
        let res = dec.decode(&Pubkey::new_unique(), &acct, &client).await;
        match res {
            DecodedAccount::Native { type_name, value } => {
                assert_eq!(type_name, "TokenAccount");
                assert_eq!(value["amount"], "42");
            }
            other => panic!("expected Native, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn decode_returns_no_idl_for_unknown_owner() {
        let cache = IdlCache::new(std::env::temp_dir().join("replay-idl-test"));
        let dec = AccountDecoder::new(&cache);
        let acct = Account {
            lamports: 1,
            data: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            owner: Pubkey::new_unique(),
            executable: false,
            rent_epoch: 0,
        };
        let client = MockHeliusClient::default();
        let res = dec.decode(&Pubkey::new_unique(), &acct, &client).await;
        assert!(matches!(res, DecodedAccount::NoIdl { .. }));
    }

    #[test]
    fn anchor_borsh_decode_struct_with_primitives() {
        // Synthetic IDL: one account `Counter { count: u64, owner: pubkey, label: string }`.
        let owner = Pubkey::new_unique();
        let mut data = anchor_account_discriminator("Counter").to_vec();
        data.extend_from_slice(&7u64.to_le_bytes());
        data.extend_from_slice(owner.as_ref());
        let label = "hello";
        data.extend_from_slice(&(label.len() as u32).to_le_bytes());
        data.extend_from_slice(label.as_bytes());

        let idl = Idl {
            raw: json!({
                "version": "0.1.0",
                "name": "demo",
                "accounts": [{
                    "name": "Counter",
                    "type": {
                        "kind": "struct",
                        "fields": [
                            { "name": "count", "type": "u64" },
                            { "name": "owner", "type": "publicKey" },
                            { "name": "label", "type": "string" },
                        ]
                    }
                }],
                "types": []
            }),
            source: IdlSource::Manual,
        };

        let (name, value) = decode_anchor(&idl, &data).expect("decode succeeds");
        assert_eq!(name, "Counter");
        assert_eq!(value["count"], "7");
        assert_eq!(value["owner"], owner.to_string());
        assert_eq!(value["label"], "hello");
    }

    #[test]
    fn anchor_borsh_decode_option_vec_array_defined() {
        // `Position { holders: Vec<pubkey>, fee: Option<u32>, fixed: [u8; 4], cfg: Config }`
        // `Config { active: bool }`
        let h0 = Pubkey::new_unique();
        let h1 = Pubkey::new_unique();
        let mut data = anchor_account_discriminator("Position").to_vec();
        // Vec<pubkey> length 2
        data.extend_from_slice(&2u32.to_le_bytes());
        data.extend_from_slice(h0.as_ref());
        data.extend_from_slice(h1.as_ref());
        // Option<u32> = Some(99)
        data.push(1);
        data.extend_from_slice(&99u32.to_le_bytes());
        // [u8; 4] = [1,2,3,4]
        data.extend_from_slice(&[1, 2, 3, 4]);
        // Config { active: true }
        data.push(1);

        let idl = Idl {
            raw: json!({
                "accounts": [{
                    "name": "Position",
                    "type": {
                        "kind": "struct",
                        "fields": [
                            { "name": "holders", "type": { "vec": "publicKey" } },
                            { "name": "fee", "type": { "option": "u32" } },
                            { "name": "fixed", "type": { "array": ["u8", 4] } },
                            { "name": "cfg", "type": { "defined": "Config" } },
                        ]
                    }
                }],
                "types": [{
                    "name": "Config",
                    "type": { "kind": "struct", "fields": [{ "name": "active", "type": "bool" }] }
                }]
            }),
            source: IdlSource::Manual,
        };

        let (name, v) = decode_anchor(&idl, &data).expect("decode succeeds");
        assert_eq!(name, "Position");
        assert_eq!(v["holders"][0], h0.to_string());
        assert_eq!(v["holders"][1], h1.to_string());
        assert_eq!(v["fee"], 99);
        assert_eq!(v["fixed"], json!([1, 2, 3, 4]));
        assert_eq!(v["cfg"]["active"], true);
    }

    #[test]
    fn idl_cache_manual_insert_and_read_disk() {
        let dir = std::env::temp_dir().join(format!("replay-idl-test-{}", rand_suffix()));
        let cache = IdlCache::new(dir.clone());
        let pid = Pubkey::new_unique();
        cache
            .manual_insert_from_json(&pid, r#"{"version":"0.1.0","name":"x"}"#)
            .unwrap();
        let idl = cache.read_disk(&pid).expect("written IDL is readable");
        assert_eq!(idl.raw["name"], "x");
        assert_eq!(idl.source, IdlSource::Cached);
        let _ = std::fs::remove_dir_all(&dir);
    }

    fn rand_suffix() -> u64 {
        use std::time::SystemTime;
        SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_nanos() as u64
    }
}