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
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
use {
    crate::config::{
        get_default_ledger_path, BootstrapMode, BuildConfig, Config, ConfigOverride, HookType,
        Manifest, PackageManager, ProgramDeployment, ProgramWorkspace, ScriptsConfig,
        SurfnetInfoResponse, SurfpoolConfig, TestValidator, Validator, ValidatorType, WithPath,
        SHUTDOWN_WAIT, STARTUP_WAIT, SURFPOOL_HOST,
    },
    abs_path::AbsolutePath,
    anchor_cli_macros::AbsolutePath,
    anchor_client::Cluster,
    anchor_lang::{
        prelude::UpgradeableLoaderState, solana_program::bpf_loader_upgradeable, AnchorDeserialize,
    },
    anchor_lang_idl::{
        convert::{convert_idl, convert_idl_to_legacy},
        types::{Idl, IdlArrayLen, IdlDefinedFields, IdlType, IdlTypeDefTy},
    },
    anyhow::{anyhow, bail, Context, Result},
    base64::{engine::general_purpose::STANDARD, Engine},
    cargo_metadata::{DependencyKind, MetadataCommand},
    checks::{check_anchor_version, check_deps, check_idl_build_feature, check_overflow},
    clap::{CommandFactory, Parser},
    dirs::home_dir,
    heck::{ToKebabCase, ToLowerCamelCase, ToPascalCase, ToSnakeCase},
    regex::{Regex, RegexBuilder},
    semver::{Version, VersionReq},
    serde::Deserialize,
    serde_json::{json, Map, Value as JsonValue},
    solana_cli_config::Config as SolanaCliConfig,
    solana_commitment_config::CommitmentConfig,
    solana_compute_budget_interface::ComputeBudgetInstruction,
    solana_instruction::Instruction,
    solana_keypair::Keypair,
    solana_pubkey::Pubkey,
    solana_pubsub_client::pubsub_client::{PubsubClient, PubsubClientSubscription},
    solana_rpc_client::rpc_client::RpcClient,
    solana_rpc_client_api::{
        config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter},
        request::RpcRequest,
        response::{Response as RpcResponse, RpcLogsResponse},
    },
    solana_signer::{EncodableKey, Signer},
    std::{
        collections::{BTreeMap, HashMap, HashSet},
        ffi::OsString,
        fs::{self, File},
        io::prelude::*,
        path::{Path, PathBuf},
        process::{Child, ExitStatus, Stdio},
        string::ToString,
        sync::{LazyLock, OnceLock},
    },
    template::{AnchorVersion, ProgramTemplate, TestTemplate},
};

mod abs_path;
mod account;
mod checks;
pub mod codama;
pub mod config;
#[cfg(not(windows))]
pub mod coverage;
#[cfg(not(windows))]
pub mod debugger;
pub mod fetch;
#[cfg(not(windows))]
mod flamegraph;
mod keygen;
mod legacy_idl;
mod metadata;
#[cfg(not(windows))]
mod profile;
mod program;
pub mod template;

// Version of the docker image.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const DOCKER_BUILDER_VERSION: &str = VERSION;
/// Default RPC port
pub const DEFAULT_RPC_PORT: u16 = 8899;
const DEFAULT_FAUCET_PORT: u16 = 9900;

/// WebSocket port offset for solana-test-validator (RPC port + 1)
pub const WEBSOCKET_PORT_OFFSET: u16 = 1;

pub static AVM_HOME: LazyLock<PathBuf> = LazyLock::new(|| {
    if let Ok(avm_home) = std::env::var("AVM_HOME") {
        PathBuf::from(avm_home)
    } else {
        let mut user_home = dirs::home_dir().expect("Could not find home directory");
        user_home.push(".avm");
        user_home
    }
});

pub fn support_version_report() -> String {
    let mut lines = vec![format!("anchor-cli {VERSION}")];

    lines.push(command_version_line("solana-cli", "solana"));
    lines.push(command_version_line("cargo", "cargo"));
    lines.push(format!("OS: {}", os_version()));

    lines.join("\n") + "\n"
}

fn command_version_line(label: &str, command: &str) -> String {
    match command_output(command, &["--version"]) {
        Some(version) if version.starts_with(label) => version,
        Some(version) => format!("{label} {version}"),
        None => format!("{label} unavailable"),
    }
}

fn command_output(command: &str, args: &[&str]) -> Option<String> {
    let output = std::process::Command::new(command)
        .args(args)
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }

    String::from_utf8(output.stdout)
        .ok()
        .and_then(|output| output.lines().next().map(str::trim).map(str::to_owned))
        .filter(|line| !line.is_empty())
}

fn os_version() -> String {
    #[cfg(target_os = "macos")]
    if let Some(version) = macos_version() {
        return version;
    }

    #[cfg(target_os = "linux")]
    {
        if let Some(version) = command_output("lsb_release", &["-ds"]) {
            return version.trim_matches('"').to_owned();
        }
        if let Some(version) = linux_os_release() {
            return version;
        }
    }

    #[cfg(target_os = "windows")]
    if let Some(version) = command_output("cmd", &["/C", "ver"]) {
        return version;
    }

    std::env::consts::OS.to_owned()
}

#[cfg(target_os = "macos")]
fn macos_version() -> Option<String> {
    let name = command_output("sw_vers", &["-productName"])?;
    let version = command_output("sw_vers", &["-productVersion"])?;
    let build = command_output("sw_vers", &["-buildVersion"])?;
    Some(format!("{name} {version} {build}"))
}

#[cfg(target_os = "linux")]
fn linux_os_release() -> Option<String> {
    fs::read_to_string("/etc/os-release")
        .ok()?
        .lines()
        .find_map(|line| line.strip_prefix("PRETTY_NAME="))
        .map(|value| value.trim_matches('"').to_owned())
}

#[derive(Debug, Parser, AbsolutePath)]
#[clap(version = VERSION)]
pub struct Opts {
    #[clap(flatten)]
    pub cfg_override: ConfigOverride,
    #[clap(subcommand)]
    pub command: Command,
}

#[derive(Debug, Parser, AbsolutePath)]
pub enum Command {
    /// Initializes a workspace.
    Init {
        /// Workspace name
        name: String,
        /// Use JavaScript instead of TypeScript
        #[clap(short, long)]
        javascript: bool,
        /// Don't install JavaScript dependencies
        #[clap(long)]
        no_install: bool,
        /// Package Manager to use. If omitted, detection cascades
        /// `pnpm` -> `yarn` -> `npm` and picks the first one on PATH. When
        /// set explicitly, the chosen binary must be installed.
        #[clap(value_enum, long)]
        package_manager: Option<PackageManager>,
        /// Don't initialize git
        #[clap(long)]
        no_git: bool,
        /// Rust program template to use
        #[clap(value_enum, short, long, default_value = "multiple")]
        template: ProgramTemplate,
        /// Anchor template version to generate
        #[clap(value_enum, long, default_value = "v1")]
        anchor_version: AnchorVersion,
        /// Test template to use
        #[clap(value_enum, long, default_value = "litesvm")]
        test_template: TestTemplate,
        /// Initialize even if there are files
        #[clap(long, action)]
        force: bool,
        /// Install Solana agent skills
        #[clap(long)]
        install_agent_skills: bool,
    },
    /// Builds the workspace.
    #[clap(name = "build", alias = "b")]
    Build {
        /// True if the build should not fail even if there are no "CHECK" comments
        #[clap(long)]
        skip_lint: bool,
        /// Skip checking for program ID mismatch between keypair and declare_id
        #[clap(long)]
        ignore_keys: bool,
        /// Do not build the IDL
        #[clap(long)]
        no_idl: bool,
        /// Output directory for the IDL.
        #[clap(short, long)]
        idl: Option<String>,
        /// Output directory for the TypeScript IDL.
        #[clap(short = 't', long)]
        idl_ts: Option<String>,
        /// True if the build artifact needs to be deterministic and verifiable.
        #[clap(short, long)]
        verifiable: bool,
        /// Name of the program to build
        #[clap(short, long)]
        program_name: Option<String>,
        /// Version of the Solana toolchain to use. For --verifiable builds
        /// only.
        #[clap(short, long)]
        solana_version: Option<String>,
        /// Docker image to use. For --verifiable builds only.
        #[clap(short, long)]
        docker_image: Option<String>,
        /// Bootstrap docker image from scratch, installing all requirements for
        /// verifiable builds. Only works for debian-based images.
        #[clap(value_enum, short, long, default_value = "none")]
        bootstrap: BootstrapMode,
        /// Environment variables to pass into the docker container
        #[clap(short, long, required = false)]
        env: Vec<String>,
        /// Arguments to pass to the underlying `cargo build-sbf` command
        #[clap(required = false, last = true)]
        cargo_args: Vec<String>,
        /// Suppress doc strings in IDL output
        #[clap(long)]
        no_docs: bool,
    },
    /// Expands macros (wrapper around cargo expand)
    ///
    /// Use it in a program folder to expand program
    ///
    /// Use it in a workspace but outside a program
    /// folder to expand the entire workspace
    Expand {
        /// Expand only this program
        #[clap(short, long)]
        program_name: Option<String>,
        /// Write to stdout
        #[clap(long)]
        stdout: bool,
        /// Arguments to pass to the underlying `cargo expand` command
        #[clap(required = false, last = true)]
        cargo_args: Vec<String>,
    },
    /// Verifies the on-chain bytecode matches the locally compiled artifact.
    /// Run this command inside a program subdirectory, i.e., in the dir
    /// containing the program's Cargo.toml.
    Verify {
        /// The program ID to verify.
        program_id: Pubkey,
        /// The URL of the repository to verify against. Conflicts with `--current-dir`.
        #[clap(long, conflicts_with = "current_dir")]
        repo_url: Option<String>,
        /// The commit hash to verify against. Requires `--repo-url`.
        #[clap(long, requires = "repo_url")]
        commit_hash: Option<String>,
        /// Verify against the source code in the current directory. Conflicts with `--repo-url`.
        #[clap(long)]
        current_dir: bool,
        /// Name of the program to run the command on. Defaults to the package name.
        #[clap(long)]
        program_name: Option<String>,
        /// Any additional arguments to pass to `solana-verify`.
        #[clap(raw = true)]
        args: Vec<String>,
    },
    #[clap(name = "test", alias = "t")]
    /// Runs integration tests.
    Test {
        /// Build and test only this program
        #[clap(short, long)]
        program_name: Option<String>,
        /// Use this flag if you want to run tests against previously deployed
        /// programs.
        #[clap(long)]
        skip_deploy: bool,
        /// True if the build should not fail even if there are
        /// no "CHECK" comments where normally required
        #[clap(long)]
        skip_lint: bool,
        /// Flag to skip starting a local validator, if the configured cluster
        /// url is a localnet.
        #[clap(long)]
        skip_local_validator: bool,
        /// Flag to skip building the program in the workspace,
        /// use this to save time when running test and the program code is not altered.
        #[clap(long)]
        skip_build: bool,
        /// Do not build the IDL
        #[clap(long)]
        no_idl: bool,
        /// Flag to keep the local validator running after tests
        /// to be able to check the transactions.
        #[clap(long)]
        detach: bool,
        /// Run the test suites under the specified path
        #[clap(long)]
        run: Vec<String>,
        /// Name of the script to run from [scripts] section (defaults to "test")
        #[clap(long)]
        script: Option<String>,
        /// Validator type to use for local testing
        #[clap(value_enum, long, default_value = "surfpool")]
        validator: ValidatorType,
        /// Profile each test: record per-test SBF register traces and render flamegraph SVGs under target/anchor-v2-profile.
        #[clap(long)]
        profile: bool,
        args: Vec<String>,
        /// Environment variables to pass into the docker container
        #[clap(short, long, required = false)]
        env: Vec<String>,
        /// Arguments to pass to the underlying `cargo build-sbf` command.
        #[clap(required = false, last = true)]
        cargo_args: Vec<String>,
    },
    /// Coverage-guided fuzzing for Solana programs (powered by Crucible).
    Fuzz(crucible_fuzz_cli::Cli),
    /// Creates a new program.
    New {
        /// Program name
        name: String,
        /// Rust program template to use
        #[clap(value_enum, short, long, default_value = "multiple")]
        template: ProgramTemplate,
        /// Anchor template version to generate
        #[clap(value_enum, long, default_value = "v1")]
        anchor_version: AnchorVersion,
        /// Create new program even if there is already one
        #[clap(long, action)]
        force: bool,
    },
    /// Run tests under an instruction-level debugger.
    #[cfg(not(windows))]
    Debugger {
        /// Filter captured traces to tests whose name contains this substring.
        test_name: Option<String>,
        /// Skip the build+test phase and open the TUI over existing traces.
        #[clap(long)]
        skip_run: bool,
        /// Skip `cargo build-sbf`.
        #[clap(long)]
        skip_build: bool,
        /// Forwarded to the underlying `anchor test` invocation.
        #[clap(long)]
        skip_lint: bool,
        /// Drive tests over sbpf's gdb-stub instead of reading dumped trace files.
        #[clap(long)]
        gdb: bool,
        /// Arguments to pass to the underlying `cargo build-sbf` command.
        #[clap(required = false, last = true)]
        cargo_args: Vec<String>,
    },
    /// Generate source-level coverage from SBF register traces.
    #[cfg(not(windows))]
    Coverage {
        /// Skip the build+test phase and generate coverage from existing traces.
        #[clap(long)]
        skip_run: bool,
        /// Skip `cargo build-sbf`.
        #[clap(long)]
        skip_build: bool,
        /// Output path for the LCOV file.
        #[clap(long, default_value = "target/coverage/sbf.lcov")]
        output: String,
        /// Directory containing register trace files.
        #[clap(long, default_value = "target/coverage/traces")]
        trace_dir: String,
        /// Arguments to pass to the underlying `cargo build-sbf` command.
        #[clap(required = false, last = true)]
        cargo_args: Vec<String>,
    },
    /// Commands for interacting with interface definitions.
    Idl {
        #[clap(subcommand)]
        subcmd: IdlCommand,
    },
    /// Remove all artifacts from the generated directories except program keypairs.
    Clean,
    /// Deploys each program in the workspace.
    #[clap(hide = true)]
    #[deprecated(since = "0.32.0", note = "use `anchor program deploy` instead")]
    Deploy {
        /// Only deploy this program
        #[clap(short, long)]
        program_name: Option<String>,
        /// Keypair of the program (filepath) (requires program-name)
        #[clap(long, requires = "program_name")]
        program_keypair: Option<PathBuf>,
        /// If true, deploy from path target/verifiable
        #[clap(short, long)]
        verifiable: bool,
        /// Don't upload IDL during deployment (IDL is uploaded by default)
        #[clap(long)]
        no_idl: bool,
        /// Arguments to pass to the underlying `solana program deploy` command.
        #[clap(required = false, last = true)]
        solana_args: Vec<String>,
    },
    /// Runs the deploy migration script.
    Migrate,
    /// Deploys, initializes an IDL, and migrates all in one command.
    /// Upgrades a single program. The configured wallet must be the upgrade
    /// authority.
    #[clap(hide = true)]
    #[deprecated(since = "0.32.0", note = "use `anchor program upgrade` instead")]
    Upgrade {
        /// The program to upgrade.
        #[clap(short, long)]
        program_id: Pubkey,
        /// Filepath to the new program binary.
        program_filepath: PathBuf,
        /// Max times to retry on failure.
        #[clap(long, default_value = "0")]
        max_retries: u32,
        /// Arguments to pass to the underlying `solana program deploy` command.
        #[clap(required = false, last = true)]
        solana_args: Vec<String>,
    },
    /// Request an airdrop of SOL
    Airdrop {
        /// Amount of SOL to airdrop
        amount: f64,
        /// Recipient address (defaults to configured wallet)
        pubkey: Option<Pubkey>,
    },
    /// Cluster commands.
    Cluster {
        #[clap(subcommand)]
        subcmd: ClusterCommand,
    },
    /// Configuration management commands.
    Config {
        #[clap(subcommand)]
        subcmd: ConfigCommand,
    },
    /// Starts a node shell with an Anchor client setup according to the local
    /// config.
    Shell,
    /// Runs the script defined by the current workspace's Anchor.toml.
    #[clap(alias = "r")]
    Run {
        /// The name of the script to run.
        script: String,
        /// Argument to pass to the underlying script.
        #[clap(required = false, last = true)]
        script_args: Vec<String>,
    },
    /// Program keypair commands.
    Keys {
        #[clap(subcommand)]
        subcmd: KeysCommand,
    },
    /// Localnet commands.
    Localnet {
        /// Flag to skip building the program in the workspace,
        /// use this to save time when running test and the program code is not altered.
        #[clap(long)]
        skip_build: bool,
        /// Use this flag if you want to run tests against previously deployed
        /// programs.
        #[clap(long)]
        skip_deploy: bool,
        /// True if the build should not fail even if there are
        /// no "CHECK" comments where normally required
        #[clap(long)]
        skip_lint: bool,
        /// Skip checking for program ID mismatch between keypair and declare_id
        #[clap(long)]
        ignore_keys: bool,
        /// Validator type to use for local testing
        #[clap(value_enum, long, default_value = "surfpool")]
        validator: ValidatorType,
        /// Environment variables to pass into the docker container
        #[clap(short, long, required = false)]
        env: Vec<String>,
        /// Arguments to pass to the underlying `cargo build-sbf` command.
        #[clap(required = false, last = true)]
        cargo_args: Vec<String>,
    },
    /// Fetch and deserialize an account using the IDL provided.
    Account {
        /// Account struct to deserialize (format: <program_name>.<Account>)
        account_type: String,
        /// Address of the account to deserialize
        address: Pubkey,
        /// Path of IDL to use (defaults to workspace IDL)
        #[clap(long)]
        idl: Option<PathBuf>,
    },
    /// Generates shell completions.
    Completions {
        #[clap(value_enum)]
        shell: clap_complete::Shell,
    },
    /// Get your public key
    Address,
    /// Get your balance
    Balance {
        /// Account to check balance for (defaults to configured wallet)
        pubkey: Option<Pubkey>,
        /// Display balance in lamports instead of SOL
        #[clap(long)]
        lamports: bool,
    },
    /// Get current epoch
    Epoch,
    /// Get information about the current epoch
    #[clap(name = "epoch-info")]
    EpochInfo,
    /// Stream transaction logs
    Logs {
        /// Include vote transactions when monitoring all transactions
        #[clap(long)]
        include_votes: bool,
        /// Addresses to filter logs by
        #[clap(long)]
        address: Option<Vec<Pubkey>>,
    },
    /// Show the contents of an account
    ShowAccount {
        #[clap(flatten)]
        cmd: account::ShowAccountCommand,
    },
    /// Keypair generation and management
    Keygen {
        #[clap(subcommand)]
        subcmd: KeygenCommand,
    },
    /// Program deployment and management commands
    Program {
        #[clap(subcommand)]
        subcmd: ProgramCommand,
    },
    /// Codama IDL integration commands
    Codama {
        #[clap(subcommand)]
        subcmd: codama::CodamaCommand,
    },
    /// [DEPRECATED] Manage legacy on-chain IDL accounts.
    /// These commands interact with the old Anchor IDL instruction protocol and will be removed
    /// in a future release. Migrate to Program Metadata-based IDL management (`anchor idl`).
    LegacyIdl {
        #[clap(subcommand)]
        subcmd: legacy_idl::LegacyIdlCommand,
    },
}

#[derive(Debug, Parser, AbsolutePath)]
pub enum KeygenCommand {
    /// Generate a new keypair
    New {
        /// Path to generated keypair file
        #[clap(short = 'o', long)]
        outfile: Option<PathBuf>,
        /// Overwrite the output file if it exists
        #[clap(short, long)]
        force: bool,
        /// Do not prompt for a passphrase
        #[clap(long)]
        no_passphrase: bool,
        /// Do not display the generated pubkey
        #[clap(long)]
        silent: bool,
        /// Number of words in the mnemonic phrase [possible values: 12, 15, 18, 21, 24]
        #[clap(short = 'w', long, default_value = "12")]
        word_count: usize,
    },
    /// Display the pubkey for a given keypair
    Pubkey {
        /// Keypair filepath
        keypair: Option<PathBuf>,
    },
    /// Recover a keypair from a seed phrase
    Recover {
        /// Path to recovered keypair file
        #[clap(short = 'o', long)]
        outfile: Option<PathBuf>,
        /// Overwrite the output file if it exists
        #[clap(short, long)]
        force: bool,
        /// Skip seed phrase validation
        #[clap(long)]
        skip_seed_phrase_validation: bool,
        /// Do not prompt for a passphrase
        #[clap(long)]
        no_passphrase: bool,
    },
    /// Verify a keypair can sign and verify a message
    Verify {
        /// Public key to verify
        pubkey: Pubkey,
        /// Keypair filepath (defaults to configured wallet)
        keypair: Option<PathBuf>,
    },
}

#[derive(Debug, Parser, AbsolutePath)]
pub enum KeysCommand {
    /// List all of the program keys.
    List,
    /// Sync program `declare_id!` pubkeys with the program's actual pubkey.
    Sync {
        /// Only sync the given program instead of all programs
        #[clap(short, long)]
        program_name: Option<String>,
    },
}

#[derive(Debug, Parser, AbsolutePath)]
pub enum ProgramCommand {
    /// Deploy an upgradeable program
    Deploy {
        /// Program filepath (e.g., target/deploy/my_program.so).
        /// If not provided, discovers programs from workspace
        program_filepath: Option<PathBuf>,
        /// Program name to deploy (from workspace). Used when program_filepath is not provided
        #[clap(short, long)]
        program_name: Option<String>,
        /// Program keypair filepath (defaults to target/deploy/{program_name}-keypair.json)
        #[clap(long)]
        program_keypair: Option<PathBuf>,
        /// Upgrade authority keypair (defaults to configured wallet)
        #[clap(long)]
        upgrade_authority: Option<String>,
        /// Program id to deploy to (derived from program-keypair if not specified)
        #[clap(long)]
        program_id: Option<Pubkey>,
        /// Buffer account to use for deployment
        #[clap(long)]
        buffer: Option<Pubkey>,
        /// Maximum transaction length (BPF loader upgradeable limit)
        #[clap(long)]
        max_len: Option<usize>,
        /// Send write transactions through RPC instead of TPU.
        #[clap(long)]
        use_rpc: bool,
        /// Don't upload IDL during deployment (IDL is uploaded by default)
        #[clap(long)]
        no_idl: bool,
        /// Make the program immutable after deployment (cannot be upgraded)
        #[clap(long = "final")]
        make_final: bool,
        /// Additional arguments to configure deployment (e.g., --with-compute-unit-price 1000)
        #[clap(required = false, last = true)]
        solana_args: Vec<String>,
    },
    /// Write a program into a buffer account
    WriteBuffer {
        /// Program filepath (e.g., target/deploy/my_program.so).
        /// If not provided, discovers program from workspace using program_name
        program_filepath: Option<PathBuf>,
        /// Program name to write (from workspace). Used when program_filepath is not provided
        #[clap(short, long)]
        program_name: Option<String>,
        /// Buffer account keypair (defaults to new keypair)
        #[clap(long)]
        buffer: Option<String>,
        /// Buffer authority (defaults to configured wallet)
        #[clap(long)]
        buffer_authority: Option<String>,
        /// Maximum transaction length
        #[clap(long)]
        max_len: Option<usize>,
    },
    /// Set a new buffer authority
    SetBufferAuthority {
        /// Buffer account address
        buffer: Pubkey,
        /// New buffer authority
        new_buffer_authority: Pubkey,
    },
    /// Set a new program authority
    SetUpgradeAuthority {
        /// Program id
        program_id: Pubkey,
        /// New upgrade authority pubkey
        #[clap(long)]
        new_upgrade_authority: Option<Pubkey>,
        /// New upgrade authority signer (keypair file). Required unless --skip-new-upgrade-authority-signer-check is used.
        /// When provided, both current and new authority will sign (checked mode, recommended)
        #[clap(long)]
        new_upgrade_authority_signer: Option<String>,
        /// Skip new upgrade authority signer check. Allows setting authority with only current authority signature.
        /// WARNING: Less safe - use only if you're confident the pubkey is correct
        #[clap(long)]
        skip_new_upgrade_authority_signer_check: bool,
        /// Make the program immutable (cannot be upgraded)
        #[clap(long = "final")]
        make_final: bool,
        /// Current upgrade authority keypair (defaults to configured wallet)
        #[clap(long)]
        upgrade_authority: Option<String>,
    },
    /// Display information about a buffer or program
    Show {
        /// Account address (buffer or program)
        account: Pubkey,
        /// Get account information from the Solana config file
        #[clap(long)]
        get_programs: bool,
        /// Get account information from the Solana config file
        #[clap(long)]
        get_buffers: bool,
        /// Show all accounts
        #[clap(long)]
        all: bool,
    },
    /// Upgrade an upgradeable program
    Upgrade {
        /// Program id to upgrade
        program_id: Pubkey,
        /// Program filepath (e.g., target/deploy/my_program.so). If not provided, discovers from workspace
        #[clap(long)]
        program_filepath: Option<PathBuf>,
        /// Program name to upgrade (from workspace). Used when program_filepath is not provided
        #[clap(short, long)]
        program_name: Option<String>,
        /// Existing buffer account to upgrade from. If not provided, auto-discovers program from workspace
        #[clap(long)]
        buffer: Option<Pubkey>,
        /// Upgrade authority (defaults to configured wallet)
        #[clap(long)]
        upgrade_authority: Option<String>,
        /// Max times to retry on failure
        #[clap(long, default_value = "0")]
        max_retries: u32,
        /// Send write transactions through RPC instead of TPU.
        #[clap(long)]
        use_rpc: bool,
        /// Additional arguments to configure deployment (e.g., --with-compute-unit-price 1000)
        #[clap(required = false, last = true)]
        solana_args: Vec<String>,
    },
    /// Write the program data to a file
    Dump {
        /// Program account address
        account: Pubkey,
        /// Output file path
        output_file: String,
    },
    /// Close a program or buffer account and withdraw all lamports
    Close {
        /// Account address to close (buffer or program).
        /// If not provided, discovers program from workspace using program_name
        account: Option<Pubkey>,
        /// Program name to close (from workspace). Used when account is not provided
        #[clap(short, long)]
        program_name: Option<String>,
        /// Authority keypair (defaults to configured wallet)
        #[clap(long)]
        authority: Option<String>,
        /// Recipient address for reclaimed lamports (defaults to authority)
        #[clap(long)]
        recipient: Option<Pubkey>,
        /// Bypass warning prompts
        #[clap(long)]
        bypass_warning: bool,
    },
    /// Extend the length of an upgradeable program
    Extend {
        /// Program id to extend.
        /// If not provided, discovers program from workspace using program_name
        program_id: Option<Pubkey>,
        /// Program name to extend (from workspace). Used when program_id is not provided
        #[clap(short, long)]
        program_name: Option<String>,
        /// Additional bytes to allocate
        additional_bytes: usize,
    },
}

#[derive(Debug, Parser, AbsolutePath)]
pub enum IdlCommand {
    /// Initializes a program's IDL account. Can only be run once.
    Init {
        /// Program id to initialize IDL for.
        /// If not provided, discovers program ID from IDL.
        program_id: Option<Pubkey>,
        #[clap(short, long)]
        filepath: PathBuf,
        #[clap(long)]
        priority_fee: Option<u64>,
        /// Create non-canonical metadata account (third-party metadata)
        #[clap(long)]
        non_canonical: bool,
        /// Allow running against a localnet cluster (disabled by default)
        #[clap(long)]
        #[cfg(feature = "idl-localnet-testing")]
        allow_localnet: bool,
    },
    /// Upgrades the IDL to the new file. An alias for first writing and then
    /// then setting the idl buffer account.
    Upgrade {
        /// Program id to upgrade IDL for.
        /// If not provided, discovers program ID from IDL.
        program_id: Option<Pubkey>,
        #[clap(short, long)]
        filepath: PathBuf,
        #[clap(long)]
        priority_fee: Option<u64>,
        /// Allow running against a localnet cluster (disabled by default)
        #[clap(long)]
        #[cfg(feature = "idl-localnet-testing")]
        allow_localnet: bool,
    },
    /// Generates the IDL for the program using the compilation method.
    #[clap(alias = "b")]
    Build {
        // Program name to build the IDL of(current dir's program if not specified)
        #[clap(short, long)]
        program_name: Option<String>,
        /// Output file for the IDL (stdout if not specified)
        #[clap(short, long)]
        out: Option<String>,
        /// Output file for the TypeScript IDL
        #[clap(short = 't', long)]
        out_ts: Option<String>,
        /// Suppress doc strings in output
        #[clap(long)]
        no_docs: bool,
        /// Do not check for safety comments
        #[clap(long)]
        skip_lint: bool,
        /// Arguments to pass to the underlying `cargo test` command
        #[clap(required = false, last = true)]
        cargo_args: Vec<String>,
    },
    /// Fetches an IDL for the given program from a cluster.
    Fetch {
        program_id: Pubkey,
        /// Output file for the IDL (stdout if not specified).
        #[clap(short, long)]
        out: Option<String>,
        /// Fetch non-canonical metadata account (third-party metadata)
        #[clap(long)]
        non_canonical: bool,
    },
    /// Fetches historical IDL versions for the given program from a cluster.
    ///
    /// With no filters, fetches all historical versions.
    FetchHistorical {
        program_id: Pubkey,
        /// Fetch authority-scoped PMP metadata account history for this authority
        #[clap(long)]
        authority: Option<Pubkey>,
        /// Fetch IDL at specific slot
        #[clap(long, conflicts_with_all = ["before", "after"])]
        slot: Option<u64>,
        /// Fetch IDL before this date (YYYY-MM-DD)
        #[clap(long)]
        before: Option<String>,
        /// Fetch IDL after this date (YYYY-MM-DD)
        #[clap(long)]
        after: Option<String>,
        /// Output directory for fetched versions (defaults to the current directory)
        #[clap(long)]
        out_dir: Option<PathBuf>,
        /// Max parallel RPC workers for transaction fetches.
        #[clap(long)]
        rpc_workers: Option<usize>,
        /// Force sequential transaction fetches (equivalent to --rpc-workers 1).
        #[clap(long, conflicts_with = "rpc_workers")]
        no_parallel: bool,
        /// Max retry attempts per transaction on 429/timeout errors.
        #[clap(long, default_value_t = 5)]
        rpc_max_retries: u32,
        /// Base backoff in milliseconds between retries (doubled each attempt).
        #[clap(long, default_value_t = 500)]
        rpc_retry_backoff_ms: u64,
        /// Hard cap on signatures fetched per history source.
        #[clap(long, default_value_t = 1000)]
        max_signatures: usize,
        /// Print diagnostic progress messages.
        #[clap(long)]
        verbose: bool,
    },
    /// Convert legacy IDLs (pre Anchor 0.30) to the new IDL spec
    Convert {
        /// Path to the IDL file
        path: PathBuf,
        /// Output file for the IDL (stdout if not specified)
        #[clap(short, long)]
        out: Option<PathBuf>,
        /// Program id to initialize IDL for.
        /// If not provided, discovers program ID from IDL.
        #[clap(short, long)]
        program_id: Option<Pubkey>,
        /// Convert a current-spec IDL back to the legacy (pre Anchor
        /// v0.30) format. Without this flag the converter runs in the
        /// default direction (legacy -> current).
        #[clap(long)]
        to_legacy: bool,
    },
    /// Generate TypeScript type for the IDL
    Type {
        /// Path to the IDL file
        path: PathBuf,
        /// Output file for the IDL (stdout if not specified)
        #[clap(short, long)]
        out: Option<PathBuf>,
    },
    /// Close a metadata account and recover rent
    Close {
        /// The program ID
        program_id: Pubkey,
        /// The seed used for the metadata account (default: "idl")
        #[clap(long, default_value = "idl")]
        seed: String,
        /// Priority fees in micro-lamports per compute unit
        #[clap(long)]
        priority_fee: Option<u64>,
    },
    /// Create a buffer account for metadata
    CreateBuffer {
        /// Path to the metadata file
        #[clap(short, long)]
        filepath: PathBuf,
        /// Priority fees in micro-lamports per compute unit
        #[clap(long)]
        priority_fee: Option<u64>,
    },
    /// Set a new authority on a buffer account
    SetBufferAuthority {
        /// The buffer account address
        buffer: Pubkey,
        /// The new authority
        #[clap(short, long)]
        new_authority: Pubkey,
        /// Priority fees in micro-lamports per compute unit
        #[clap(long)]
        priority_fee: Option<u64>,
    },
    /// Write metadata using a buffer account
    WriteBuffer {
        /// The program ID
        program_id: Pubkey,
        /// The buffer account address
        #[clap(short, long)]
        buffer: Pubkey,
        /// The seed to use for the metadata account (default: "idl")
        #[clap(long, default_value = "idl")]
        seed: String,
        /// Close the buffer after writing
        #[clap(long)]
        close_buffer: bool,
        /// Priority fees in micro-lamports per compute unit
        #[clap(long)]
        priority_fee: Option<u64>,
    },
}

#[derive(Debug, Parser, AbsolutePath)]
pub enum ClusterCommand {
    /// Prints common cluster urls.
    List,
}

#[derive(Debug, Parser, AbsolutePath)]
pub enum ConfigCommand {
    /// Get configuration settings from the local Anchor.toml
    Get,
    /// Set configuration settings in the local Anchor.toml
    Set {
        /// Cluster to connect to (custom URL). Use -um, -ud, -ut, -ul for standard clusters
        #[clap(short = 'u', long = "url")]
        url: Option<String>,
        /// Path to wallet keypair file to update the Anchor.toml file with
        #[clap(short = 'k', long = "keypair")]
        keypair: Option<PathBuf>,
    },
}

fn get_keypair(path: &Path) -> Result<Keypair> {
    solana_keypair::read_keypair_file(path)
        .map_err(|_| anyhow!("Unable to read keypair file ({})", path.display()))
}

/// Format lamports as SOL with trailing zeros removed
fn format_sol(lamports: u64) -> String {
    let sol = lamports as f64 / 1_000_000_000.0;
    let formatted = format!("{:.8}", sol);

    // Remove trailing zeros and decimal point if not needed
    let trimmed = formatted.trim_end_matches('0').trim_end_matches('.');
    format!("{} SOL", trimmed)
}

/// Get cluster URL and wallet path from Anchor config, CLI overrides, or Solana CLI config
fn get_cluster_and_wallet(cfg_override: &ConfigOverride) -> Result<(String, String)> {
    // Try to get from Anchor workspace config first
    if let Ok(Some(cfg)) = Config::discover(cfg_override) {
        return Ok((
            cfg.provider.cluster.url().to_string(),
            cfg.provider.wallet.to_string(),
        ));
    }

    // Try to load Solana CLI config
    let (cluster_url, wallet_path) =
        if let Some(config_file) = solana_cli_config::CONFIG_FILE.as_ref() {
            match SolanaCliConfig::load(config_file) {
                Ok(cli_config) => (
                    cli_config.json_rpc_url.clone(),
                    cli_config.keypair_path.clone(),
                ),
                Err(_) => {
                    // Fallback to defaults if Solana CLI config doesn't exist
                    (
                        "https://api.mainnet-beta.solana.com".to_string(),
                        dirs::home_dir()
                            .map(|home| {
                                home.join(".config/solana/id.json")
                                    .to_string_lossy()
                                    .to_string()
                            })
                            .unwrap_or_else(|| "~/.config/solana/id.json".to_string()),
                    )
                }
            }
        } else {
            // If CONFIG_FILE is None, use defaults
            (
                "https://api.mainnet-beta.solana.com".to_string(),
                dirs::home_dir()
                    .map(|home| {
                        home.join(".config/solana/id.json")
                            .to_string_lossy()
                            .to_string()
                    })
                    .unwrap_or_else(|| "~/.config/solana/id.json".to_string()),
            )
        };

    // Apply cluster override if provided
    let final_cluster = if let Some(cluster) = &cfg_override.cluster {
        cluster.url().to_string()
    } else {
        cluster_url
    };

    Ok((final_cluster, wallet_path))
}

/// Get the recommended priority fee from the RPC client, falling back to 0 if unavailable.
/// `write_locked_accounts` scopes the query to txs that write-locked all of these
/// accounts in recent blocks — passing the accounts the upcoming tx will lock
/// gives a contention-aware fee. Pass `&[]` for a global median (often too low
/// for hot mainnet windows).
pub fn get_recommended_micro_lamport_fee(
    client: &RpcClient,
    write_locked_accounts: &[Pubkey],
) -> u64 {
    let mut fees = match client.get_recent_prioritization_fees(write_locked_accounts) {
        // Fees may be empty or query may fail, e.g. on localnet
        Err(e) => {
            eprintln!("Warning: failed to fetch prioritization fees, defaulting to 0: {e}");
            return 0;
        }
        Ok(f) if f.is_empty() => {
            return 0;
        }
        Ok(f) => f,
    };

    // Get the median fee from the most recent 150 slots' prioritization fee
    fees.sort_unstable_by_key(|fee| fee.prioritization_fee);
    let median_index = fees.len() / 2;

    if fees.len() % 2 == 0 {
        (fees[median_index - 1].prioritization_fee + fees[median_index].prioritization_fee) / 2
    } else {
        fees[median_index].prioritization_fee
    }
}

/// Prepend a compute unit ix, if the priority fee is greater than 0.
pub fn prepend_compute_unit_ix(
    instructions: Vec<Instruction>,
    client: &RpcClient,
    priority_fee: Option<u64>,
    write_locked_accounts: &[Pubkey],
) -> Vec<Instruction> {
    let priority_fee = priority_fee
        .unwrap_or_else(|| get_recommended_micro_lamport_fee(client, write_locked_accounts));

    if priority_fee > 0 {
        let mut instructions_appended = instructions.clone();
        instructions_appended.insert(
            0,
            ComputeBudgetInstruction::set_compute_unit_price(priority_fee),
        );
        instructions_appended
    } else {
        instructions
    }
}

pub fn entry(opts: Opts) -> Result<()> {
    let opts = opts.absolute();

    let restore_cbs = override_toolchain(&opts.cfg_override)?;
    let result = process_command(opts);
    restore_toolchain(restore_cbs)?;

    result
}

/// Functions to restore toolchain entries
type RestoreToolchainCallbacks = Vec<Box<dyn FnOnce() -> Result<()>>>;

/// Override the toolchain from `Anchor.toml`.
///
/// Returns the previous versions to restore back to.
fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainCallbacks> {
    let mut restore_cbs: RestoreToolchainCallbacks = vec![];

    let cfg = Config::discover(cfg_override)?;
    if let Some(cfg) = cfg {
        fn parse_version(text: &str) -> Option<String> {
            Some(
                Regex::new(r"(\d+\.\d+\.\S+)")
                    .unwrap()
                    .captures_iter(text)
                    .next()?
                    .get(0)?
                    .as_str()
                    .to_string(),
            )
        }

        fn get_current_version(cmd_name: &str) -> Result<String> {
            let output = std::process::Command::new(cmd_name)
                .arg("--version")
                .output()?;
            if !output.status.success() {
                return Err(anyhow!("Failed to run `{cmd_name} --version`"));
            }

            let output_version = std::str::from_utf8(&output.stdout)?;
            parse_version(output_version)
                .ok_or_else(|| anyhow!("Failed to parse the version of `{cmd_name}`"))
        }

        if let Some(solana_version) = &cfg.toolchain.solana_version {
            let current_version = get_current_version("solana")?;
            if solana_version != &current_version {
                // We are overriding with `solana-install` command instead of using the binaries
                // from `~/.local/share/solana/install/releases` because we use multiple Solana
                // binaries in various commands.
                fn override_solana_version(version: String) -> Result<bool> {
                    // There is a deprecation warning message starting with `1.18.19` which causes
                    // parsing problems https://github.com/otter-sec/anchor/issues/3147
                    let (cmd_name, domain) =
                        if Version::parse(&version)? < Version::parse("1.18.19")? {
                            ("solana-install", "anza.xyz")
                        } else {
                            ("agave-install", "anza.xyz")
                        };

                    // Install the command if it's not installed
                    if get_current_version(cmd_name).is_err() {
                        // `solana-install` and `agave-install` are not usable at the same time i.e.
                        // using one of them makes the other unusable with the default installation,
                        // causing the installation process to run each time users switch between
                        // `agave` supported versions. For example, if the user's active Solana
                        // version is `1.18.17`, and he specifies `solana_version = "2.0.6"`, this
                        // code path will run each time an Anchor command gets executed.
                        eprintln!(
                            "Command not installed: `{cmd_name}`. \
                            See https://github.com/anza-xyz/agave/wiki/Agave-Transition, \
                            installing..."
                        );
                        let install_script = std::process::Command::new("curl")
                            .args([
                                "-sSfL",
                                &format!("https://release.{domain}/v{version}/install"),
                            ])
                            .output()?;
                        let is_successful = std::process::Command::new("sh")
                            .args(["-c", std::str::from_utf8(&install_script.stdout)?])
                            .spawn()?
                            .wait_with_output()?
                            .status
                            .success();
                        if !is_successful {
                            return Err(anyhow!("Failed to install `{cmd_name}`"));
                        }
                    }

                    let output = std::process::Command::new(cmd_name).arg("list").output()?;
                    if !output.status.success() {
                        return Err(anyhow!("Failed to list installed `solana` versions"));
                    }

                    // Hide the installation progress if the version is already installed
                    let is_installed = std::str::from_utf8(&output.stdout)?
                        .lines()
                        .filter_map(parse_version)
                        .any(|line_version| line_version == version);
                    let (stderr, stdout) = if is_installed {
                        (Stdio::null(), Stdio::null())
                    } else {
                        (Stdio::inherit(), Stdio::inherit())
                    };

                    std::process::Command::new(cmd_name)
                        .arg("init")
                        .arg(&version)
                        .stderr(stderr)
                        .stdout(stdout)
                        .spawn()?
                        .wait()
                        .map(|status| status.success())
                        .map_err(|err| anyhow!("Failed to run `{cmd_name}` command: {err}"))
                }

                match override_solana_version(solana_version.to_owned())? {
                    true => restore_cbs.push(Box::new(|| {
                        match override_solana_version(current_version)? {
                            true => Ok(()),
                            false => Err(anyhow!("Failed to restore `solana` version")),
                        }
                    })),
                    false => eprintln!(
                        "Failed to override `solana` version to {solana_version}, using \
                         {current_version} instead"
                    ),
                }
            }
        }

        // Anchor version override should be handled last.
        //
        // When invoked via the AVM proxy (`AVM_ACTIVE=1`), AVM has already resolved
        // the requested toolchain version and spawned the matching binary. Re-execing
        // here would either be a no-op (binary already matches) or fight AVM's
        // resolution (e.g. when AVM resolved via `anchor-lang` in Cargo.toml). Skip.
        if let Some(anchor_version) = &cfg.toolchain.anchor_version {
            if std::env::var("AVM_ACTIVE").is_ok() {
                return Ok(restore_cbs);
            }
            // Anchor binary name prefix(applies to binaries that are installed via `avm`)
            const ANCHOR_BINARY_PREFIX: &str = "anchor-";

            // Get the current version from the executing binary name if possible because commit
            // based toolchain overrides do not have version information.
            let current_version = std::env::args()
                .next()
                .expect("First arg should exist")
                .parse::<PathBuf>()?
                .file_name()
                .and_then(|name| name.to_str())
                .expect("File name should be valid Unicode")
                .split_once(ANCHOR_BINARY_PREFIX)
                .map(|(_, version)| version)
                .unwrap_or(VERSION)
                .to_owned();
            if anchor_version != &current_version {
                let binary_path = home_dir()
                    .unwrap()
                    .join(".avm")
                    .join("bin")
                    .join(format!("{ANCHOR_BINARY_PREFIX}{anchor_version}"));

                if !binary_path.exists() {
                    eprintln!(
                        "`anchor` {anchor_version} is not installed with `avm`. Installing...\n"
                    );

                    if let Err(e) = install_with_avm(anchor_version, false) {
                        eprintln!(
                            "Failed to install `anchor`: {e}, using {current_version} instead"
                        );
                        return Ok(restore_cbs);
                    }
                }

                let exit_code = std::process::Command::new(binary_path)
                    .args(std::env::args_os().skip(1))
                    .spawn()?
                    .wait()?
                    .code()
                    .unwrap_or(1);
                restore_toolchain(restore_cbs)?;
                std::process::exit(exit_code);
            }
        }
    }

    Ok(restore_cbs)
}

/// Installs Anchor using AVM, passing `--force` (and optionally) installing
/// `solana-verify`.
fn install_with_avm(version: &str, verify: bool) -> Result<()> {
    let mut cmd = std::process::Command::new("avm");
    cmd.arg("install");
    cmd.arg(version);
    cmd.arg("--force");
    if verify {
        cmd.arg("--verify");
    }
    let status = cmd.status().context("running AVM")?;
    if !status.success() {
        bail!("failed to install `anchor` {version} with avm");
    }
    Ok(())
}

/// Restore toolchain to how it was before the command was run.
fn restore_toolchain(restore_cbs: RestoreToolchainCallbacks) -> Result<()> {
    for restore_toolchain in restore_cbs {
        if let Err(e) = restore_toolchain() {
            eprintln!("Toolchain error: {e}");
        }
    }

    Ok(())
}

/// Get the system's default license - what 'npm init' would use.
fn get_npm_init_license() -> Result<String> {
    let npm_init_license_output = std::process::Command::new("npm")
        .arg("config")
        .arg("get")
        .arg("init-license")
        .output()?;

    if !npm_init_license_output.status.success() {
        return Err(anyhow!("Failed to get npm init license"));
    }

    let license = String::from_utf8(npm_init_license_output.stdout)?;
    Ok(license.trim().to_string())
}

fn process_command(opts: Opts) -> Result<()> {
    match opts.command {
        Command::Init {
            name,
            javascript,
            no_install,
            package_manager,
            no_git,
            template,
            anchor_version,
            test_template,
            force,
            install_agent_skills,
        } => init(
            &opts.cfg_override,
            name,
            javascript,
            no_install,
            package_manager,
            no_git,
            template,
            anchor_version,
            test_template,
            force,
            install_agent_skills,
        ),
        Command::Fuzz(cli) => crucible_fuzz_cli::run(cli),
        Command::New {
            name,
            template,
            anchor_version,
            force,
        } => new(&opts.cfg_override, name, template, anchor_version, force),
        Command::Build {
            no_idl,
            idl,
            idl_ts,
            verifiable,
            program_name,
            solana_version,
            docker_image,
            bootstrap,
            cargo_args,
            env,
            skip_lint,
            ignore_keys,
            no_docs,
        } => build(
            &opts.cfg_override,
            no_idl,
            idl,
            idl_ts,
            verifiable,
            skip_lint,
            ignore_keys,
            program_name,
            solana_version,
            docker_image,
            bootstrap,
            None,
            None,
            env,
            cargo_args,
            no_docs,
        ),
        Command::Verify {
            program_id,
            repo_url,
            commit_hash,
            current_dir,
            program_name,
            args,
        } => verify(
            program_id,
            repo_url,
            commit_hash,
            current_dir,
            program_name,
            args,
        ),
        Command::Clean => clean(&opts.cfg_override),
        #[allow(deprecated)]
        Command::Deploy {
            program_name,
            program_keypair,
            verifiable,
            no_idl,
            solana_args,
        } => {
            eprintln!(
                "Warning: 'anchor deploy' is deprecated. Use 'anchor program deploy' instead."
            );
            deploy(
                &opts.cfg_override,
                program_name,
                program_keypair,
                verifiable,
                no_idl,
                solana_args,
            )
        }
        Command::Expand {
            program_name,
            stdout,
            cargo_args,
        } => expand(&opts.cfg_override, program_name, stdout, &cargo_args),
        #[allow(deprecated)]
        Command::Upgrade {
            program_id,
            program_filepath,
            max_retries,
            solana_args,
        } => {
            eprintln!(
                "Warning: 'anchor upgrade' is deprecated. Use 'anchor program upgrade' instead."
            );
            upgrade(
                &opts.cfg_override,
                program_id,
                program_filepath,
                max_retries,
                solana_args,
            )
        }
        Command::Idl { subcmd } => idl(&opts.cfg_override, subcmd),
        Command::LegacyIdl { subcmd } => {
            legacy_idl::handle_legacy_idl_command(&opts.cfg_override, subcmd)
        }
        Command::Migrate => migrate(&opts.cfg_override),
        Command::Test {
            program_name,
            skip_deploy,
            skip_local_validator,
            skip_build,
            no_idl,
            detach,
            run,
            script,
            validator,
            profile,
            args,
            env,
            cargo_args,
            skip_lint,
        } => test(
            &opts.cfg_override,
            program_name,
            skip_deploy,
            skip_local_validator,
            skip_build,
            skip_lint,
            no_idl,
            detach,
            run,
            script,
            validator,
            profile,
            false,
            args,
            env,
            cargo_args,
        ),
        #[cfg(not(windows))]
        Command::Debugger {
            test_name,
            skip_run,
            skip_build,
            skip_lint,
            gdb,
            cargo_args,
        } => debugger(
            &opts.cfg_override,
            test_name,
            skip_run,
            skip_build,
            skip_lint,
            gdb,
            cargo_args,
        ),
        #[cfg(not(windows))]
        Command::Coverage {
            skip_run,
            skip_build,
            output,
            trace_dir,
            cargo_args,
        } => run_coverage(
            &opts.cfg_override,
            skip_run,
            skip_build,
            &output,
            &trace_dir,
            cargo_args,
        ),
        Command::Airdrop { amount, pubkey } => airdrop(&opts.cfg_override, amount, pubkey),
        Command::Cluster { subcmd } => cluster(subcmd),
        Command::Config { subcmd } => config_cmd(&opts.cfg_override, subcmd),
        Command::Shell => shell(&opts.cfg_override),
        Command::Run {
            script,
            script_args,
        } => run(&opts.cfg_override, script, script_args),
        Command::Keys { subcmd } => keys(&opts.cfg_override, subcmd),
        Command::Localnet {
            skip_build,
            skip_deploy,
            skip_lint,
            ignore_keys,
            validator,
            env,
            cargo_args,
        } => localnet(
            &opts.cfg_override,
            skip_build,
            skip_deploy,
            skip_lint,
            ignore_keys,
            validator,
            env,
            cargo_args,
        ),
        Command::Account {
            account_type,
            address,
            idl,
        } => account(&opts.cfg_override, account_type, address, idl),
        Command::Completions { shell } => {
            clap_complete::generate(
                shell,
                &mut Opts::command(),
                "anchor",
                &mut std::io::stdout(),
            );
            Ok(())
        }
        Command::Address => address(&opts.cfg_override),
        Command::Balance { pubkey, lamports } => balance(&opts.cfg_override, pubkey, lamports),
        Command::Epoch => epoch(&opts.cfg_override),
        Command::EpochInfo => epoch_info(&opts.cfg_override),
        Command::Logs {
            include_votes,
            address,
        } => logs_subscribe(&opts.cfg_override, include_votes, address),
        Command::ShowAccount { cmd } => account::show_account(&opts.cfg_override, cmd),
        Command::Keygen { subcmd } => keygen::keygen(&opts.cfg_override, subcmd),
        Command::Program { subcmd } => program::program(&opts.cfg_override, subcmd),
        Command::Codama { subcmd } => codama::entry(subcmd),
    }
}

/// Cargo does not support nested workspaces. If `start` lives inside a
/// directory tree containing any `Cargo.toml`, refuse to create a new
/// Anchor workspace here and point at `anchor new`, which is the
/// supported flow for adding a program to an existing project.
fn reject_if_inside_cargo_project(start: PathBuf) -> Result<()> {
    if let Some(parent) = Manifest::discover_from_path(start)? {
        return Err(anyhow!(
            "Cannot run `anchor init` inside an existing Cargo project at `{}`.\nTo add a new \
             program to the existing project, run `anchor new <name>` from the workspace root. To \
             create a fresh Anchor workspace, run `anchor init` outside any Cargo project tree.",
            parent.path().display()
        ));
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn init(
    cfg_override: &ConfigOverride,
    name: String,
    javascript: bool,
    no_install: bool,
    package_manager: Option<PackageManager>,
    no_git: bool,
    template: ProgramTemplate,
    anchor_version: AnchorVersion,
    test_template: TestTemplate,
    force: bool,
    install_agent_skills: bool,
) -> Result<()> {
    if !force {
        if Config::discover(cfg_override)?.is_some() {
            return Err(anyhow!("Workspace already initialized"));
        }
        reject_if_inside_cargo_project(std::env::current_dir()?)?;
    }

    // We need to format different cases for the dir and the name
    let rust_name = name.to_snake_case();
    let project_name = if name == rust_name {
        rust_name.clone()
    } else {
        name.to_kebab_case()
    };

    // Additional keywords that have not been added to the `syn` crate as reserved words
    // https://github.com/dtolnay/syn/pull/1098
    let extra_keywords = ["async", "await", "try"];
    // Anchor converts to snake case before writing the program name
    if syn::parse_str::<syn::Ident>(&rust_name).is_err()
        || extra_keywords.contains(&rust_name.as_str())
    {
        return Err(anyhow!(
            "Anchor workspace name must be a valid Rust identifier. It may not be a Rust reserved word, start with a digit, or include certain disallowed characters. See https://doc.rust-lang.org/reference/identifiers.html for more detail.",
        ));
    }

    if force {
        fs::create_dir_all(&project_name)?;
    } else {
        fs::create_dir(&project_name)?;
    }
    std::env::set_current_dir(&project_name)?;
    fs::create_dir_all("app")?;

    let mut cfg = Config::default();

    let uses_node = test_template.uses_node();
    let package_manager = if uses_node {
        Some(resolve_package_manager(package_manager)?)
    } else {
        None
    };
    let test_script = test_template.get_test_script(javascript, package_manager.as_ref());
    cfg.scripts.insert("test".to_owned(), test_script);

    // In-process test templates drive the Solana VM inside `cargo test`, so
    // auto-starting a validator at `anchor test` time is unnecessary.
    if matches!(test_template, TestTemplate::Litesvm | TestTemplate::Mollusk) {
        cfg.skip_local_validator = Some(true);
    }

    let package_manager_cmd = package_manager.as_ref().map(ToString::to_string);
    if uses_node {
        cfg.toolchain.package_manager = package_manager.clone();
    }

    // Initialize .gitignore file
    fs::write(".gitignore", template::git_ignore())?;

    // Initialize .prettierignore file
    fs::write(".prettierignore", template::prettier_ignore())?;

    // Remove the default program if `--force` is passed
    if force {
        let default_program_dir = std::env::current_dir()?
            .join("programs")
            .join(&project_name);
        if default_program_dir.exists() {
            fs::remove_dir_all(default_program_dir)?;
        }
    }

    // Build the program.
    template::create_program(
        &project_name,
        template,
        Some(&test_template),
        anchor_version,
    )?;

    let program_id = template::get_or_create_program_id(&rust_name, target_dir()?);
    let mut localnet = BTreeMap::new();
    localnet.insert(
        rust_name,
        ProgramDeployment {
            address: program_id,
            path: None,
            idl: None,
        },
    );
    cfg.programs.insert(Cluster::Localnet, localnet);
    let toml = cfg.to_string();
    fs::write("Anchor.toml", toml)?;

    if uses_node {
        // Build the migrations directory.
        let migrations_path = Path::new("migrations");
        fs::create_dir_all(migrations_path)?;

        let license = get_npm_init_license()?;

        let jest = TestTemplate::Jest == test_template;
        if javascript {
            // Build javascript config
            let mut package_json = File::create("package.json")?;
            package_json
                .write_all(template::package_json(jest, license, anchor_version).as_bytes())?;

            let mut deploy = File::create(migrations_path.join("deploy.js"))?;
            deploy.write_all(template::deploy_script().as_bytes())?;
        } else {
            // Build typescript config
            let mut ts_config = File::create("tsconfig.json")?;
            ts_config.write_all(template::ts_config(jest).as_bytes())?;

            let mut ts_package_json = File::create("package.json")?;
            ts_package_json
                .write_all(template::ts_package_json(jest, license, anchor_version).as_bytes())?;

            let mut deploy = File::create(migrations_path.join("deploy.ts"))?;
            deploy.write_all(template::ts_deploy_script().as_bytes())?;
        }
    }

    test_template.create_test_files(
        &project_name,
        javascript,
        &program_id.to_string(),
        anchor_version,
    )?;

    if !no_install && uses_node {
        let package_manager_cmd =
            package_manager_cmd.expect("Node templates resolve a package manager");
        let output = install_node_modules(&package_manager_cmd)?;
        if !output.status.success() {
            return Err(anyhow!(
                "`{package_manager_cmd} install` failed (exit code {:?}). Re-run with \
                 `--no-install` to keep the generated files without installing dependencies.",
                output.status.code()
            ));
        }
    }

    if !no_git {
        let git_result = std::process::Command::new("git")
            .arg("init")
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .output()
            .map_err(|e| anyhow::format_err!("git init failed: {}", e))?;
        if !git_result.status.success() {
            eprintln!("Failed to automatically initialize a new git repository");
        }
    }

    if install_agent_skills {
        install_solana_skill();
    }

    println!("{project_name} initialized");

    Ok(())
}

fn install_solana_skill() {
    const SKILL_REPO: &str = "https://github.com/solana-foundation/solana-dev-skill";
    const SKILL_NAME: &str = "solana-dev";

    // Skip if globally installed (active across all projects already)
    if home_dir().is_some_and(|home| {
        home.join(".agents")
            .join("skills")
            .join(SKILL_NAME)
            .exists()
    }) {
        return;
    }

    // Skip if already project-scoped (could be anchor init --force on existing folder)
    let project_path = Path::new(".agents").join("skills").join(SKILL_NAME);
    if project_path.exists() {
        return;
    }

    println!("Installing Solana dev skill for Agents from {SKILL_REPO}");

    let status = std::process::Command::new("npx")
        .args([
            "--yes",
            "skills@1.4.4",
            "add",
            SKILL_REPO,
            "--skill",
            "*",
            "-y",
        ])
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status();

    match status {
        Ok(s) if s.success() => {
            println!("Solana dev skill installed successfully");
        }
        _ => {
            eprintln!(
                "Warning: Failed to install Solana dev skill. Install manually with:\n  npx \
                 skills add {SKILL_REPO}"
            );
        }
    }
}

const PACKAGE_MANAGER_WATERFALL: &[PackageManager] = &[
    PackageManager::PNPM,
    PackageManager::Yarn,
    PackageManager::NPM,
];

fn package_manager_available(pm: &PackageManager) -> bool {
    let cmd = pm.to_string();
    let mut command = if cfg!(target_os = "windows") {
        let mut command = std::process::Command::new("cmd");
        command.arg(format!("/C {cmd} --version"));
        command
    } else {
        let mut command = std::process::Command::new(&cmd);
        command.arg("--version");
        command
    };
    command
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|status| status.success())
        .unwrap_or(false)
}

fn resolve_package_manager(explicit: Option<PackageManager>) -> Result<PackageManager> {
    if let Some(pm) = explicit {
        if !package_manager_available(&pm) {
            return Err(anyhow!(
                "`{pm}` was requested but is not on PATH. Install it or pick a different package \
                 manager with `--package-manager`."
            ));
        }
        return Ok(pm);
    }

    let mut skipped = Vec::new();
    for candidate in PACKAGE_MANAGER_WATERFALL {
        if package_manager_available(candidate) {
            if !skipped.is_empty() {
                let missing = skipped
                    .iter()
                    .map(|pm: &PackageManager| pm.to_string())
                    .collect::<Vec<_>>()
                    .join(", ");
                eprintln!("warning: {missing} not found on PATH, using `{candidate}` instead");
            }
            return Ok(candidate.clone());
        }
        skipped.push(candidate.clone());
    }

    Err(anyhow!(
        "No supported package manager found on PATH (tried pnpm, yarn, npm). Install one of them, \
         or re-run with `--no-install`."
    ))
}

fn install_node_modules(cmd: &str) -> Result<std::process::Output> {
    if cfg!(target_os = "windows") {
        std::process::Command::new("cmd")
            .arg(format!("/C {cmd} install"))
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .output()
            .map_err(|e| anyhow::format_err!("{} install failed: {}", cmd, e))
    } else {
        std::process::Command::new(cmd)
            .arg("install")
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .output()
            .map_err(|e| anyhow::format_err!("{} install failed: {}", cmd, e))
    }
}

// Creates a new program crate in the `programs/<name>` directory.
fn new(
    cfg_override: &ConfigOverride,
    name: String,
    template: ProgramTemplate,
    anchor_version: AnchorVersion,
    force: bool,
) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        match cfg.path().parent() {
            None => {
                println!("Unable to make new program");
            }
            Some(parent) => {
                std::env::set_current_dir(parent)?;

                let cluster = cfg.provider.cluster.clone();
                let programs = cfg.programs.entry(cluster).or_default();
                if programs.contains_key(&name) {
                    if !force {
                        return Err(anyhow!("Program already exists"));
                    }

                    // Delete all files within the program folder
                    fs::remove_dir_all(std::env::current_dir()?.join("programs").join(&name))?;
                }

                template::create_program(&name, template, None, anchor_version)?;

                programs.insert(
                    name.clone(),
                    ProgramDeployment {
                        address: template::get_or_create_program_id(&name, target_dir()?),
                        path: None,
                        idl: None,
                    },
                );

                let toml = cfg.to_string();
                fs::write("Anchor.toml", toml)?;

                println!("Created new program.");
            }
        };
        Ok(())
    })?
}

/// Array of (path, content) tuple.
pub type Files = Vec<(PathBuf, String)>;

/// Create files from the given (path, content) tuple array.
///
/// # Example
///
/// ```ignore
/// crate_files(vec![("programs/my_program/src/lib.rs".into(), "// Content".into())])?;
/// ```
pub fn create_files(files: &Files) -> Result<()> {
    for (path, content) in files {
        let path = path
            .display()
            .to_string()
            .replace('/', std::path::MAIN_SEPARATOR_STR);
        let path = Path::new(&path);
        if path.exists() {
            continue;
        }

        match path.extension() {
            Some(_) => {
                fs::create_dir_all(path.parent().unwrap())?;
                fs::write(path, content)?;
            }
            None => fs::create_dir_all(path)?,
        }
    }

    Ok(())
}

/// Override or create files from the given (path, content) tuple array.
///
/// # Example
///
/// ```ignore
/// override_or_create_files(vec![("programs/my_program/src/lib.rs".into(), "// Content".into())])?;
/// ```
pub fn override_or_create_files(files: &Files) -> Result<()> {
    for (path, content) in files {
        let path = Path::new(path);
        if path.exists() {
            let mut f = fs::OpenOptions::new()
                .write(true)
                .truncate(true)
                .open(path)?;
            f.write_all(content.as_bytes())?;
            f.flush()?;
        } else {
            fs::create_dir_all(path.parent().unwrap())?;
            fs::write(path, content)?;
        }
    }

    Ok(())
}

pub fn expand(
    cfg_override: &ConfigOverride,
    program_name: Option<String>,
    stdout: bool,
    cargo_args: &[String],
) -> Result<()> {
    // Change to the workspace member directory, if needed.
    if let Some(program_name) = program_name.as_ref() {
        cd_member(cfg_override, program_name)?;
    }

    let workspace_cfg = Config::discover(cfg_override)?
        .ok_or_else(|| anyhow!("The 'anchor expand' command requires an Anchor workspace."))?;
    let cfg_parent = workspace_cfg.path().parent().expect("Invalid Anchor.toml");
    let cargo = Manifest::discover()?;

    let expansions_path = cfg_parent.join(".anchor").join("expanded-macros");
    fs::create_dir_all(&expansions_path)?;

    match cargo {
        // No Cargo.toml found, expand entire workspace
        None => expand_all(&workspace_cfg, expansions_path, stdout, cargo_args),
        // Cargo.toml is at root of workspace, expand entire workspace
        Some(cargo) if cargo.path().parent() == workspace_cfg.path().parent() => {
            expand_all(&workspace_cfg, expansions_path, stdout, cargo_args)
        }
        // Reaching this arm means Cargo.toml belongs to a single package. Expand it.
        Some(cargo) => expand_program(
            // If we found Cargo.toml, it must be in a directory so unwrap is safe
            cargo.path().parent().unwrap().to_path_buf(),
            expansions_path,
            stdout,
            cargo_args,
        ),
    }
}

fn expand_all(
    workspace_cfg: &WithPath<Config>,
    expansions_path: PathBuf,
    stdout: bool,
    cargo_args: &[String],
) -> Result<()> {
    let cur_dir = std::env::current_dir()?;
    for p in workspace_cfg.get_program_list()? {
        expand_program(p, expansions_path.clone(), stdout, cargo_args)?;
    }
    std::env::set_current_dir(cur_dir)?;
    Ok(())
}

fn expand_program(
    program_path: PathBuf,
    expansions_path: PathBuf,
    stdout: bool,
    cargo_args: &[String],
) -> Result<()> {
    let cargo = Manifest::from_path(program_path.join("Cargo.toml"))
        .map_err(|_| anyhow!("Could not find Cargo.toml for program"))?;
    let package_name = &cargo
        .package
        .as_ref()
        .ok_or_else(|| anyhow!("Cargo config is missing a package"))?
        .name;

    let mut cmd = std::process::Command::new("cargo");
    cmd.arg("expand")
        .arg("--target-dir")
        .arg(expansions_path.join("expand-target"))
        .arg("--package")
        .arg(package_name)
        .args(cargo_args);

    let handle_err = |err| anyhow!("Failed to run `cargo expand`: {err}");
    let exit_on_err = |exit_status: ExitStatus| {
        if !exit_status.success() {
            eprintln!("'anchor expand' failed. Perhaps you have not installed 'cargo-expand'? https://github.com/dtolnay/cargo-expand#installation");
            std::process::exit(exit_status.code().unwrap_or(1));
        }
    };

    if stdout {
        let status = cmd.status().map_err(handle_err)?;
        exit_on_err(status);
    } else {
        let output = cmd.stderr(Stdio::inherit()).output().map_err(handle_err)?;
        exit_on_err(output.status);

        let program_expansions_path = expansions_path.join(package_name);
        fs::create_dir_all(&program_expansions_path)?;

        let version = cargo.version();
        let time = chrono::Utc::now().to_string().replace(' ', "_");
        let file_path = program_expansions_path.join(format!("{package_name}-{version}-{time}.rs"));
        fs::write(&file_path, &output.stdout)?;

        println!(
            "Expanded {} into file {}\n",
            package_name,
            file_path.to_string_lossy()
        );
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub fn build(
    cfg_override: &ConfigOverride,
    no_idl: bool,
    idl: Option<String>,
    idl_ts: Option<String>,
    verifiable: bool,
    skip_lint: bool,
    ignore_keys: bool,
    program_name: Option<String>,
    solana_version: Option<String>,
    docker_image: Option<String>,
    bootstrap: BootstrapMode,
    stdout: Option<File>, // Used for the package registry server.
    stderr: Option<File>, // Used for the package registry server.
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
    no_docs: bool,
) -> Result<()> {
    // Change to the workspace member directory, if needed.
    if let Some(program_name) = program_name.as_ref() {
        cd_member(cfg_override, program_name)?;
    }
    let cfg = Config::discover(cfg_override)?
        .ok_or_else(|| anyhow!("The 'anchor build' command requires an Anchor workspace."))?;
    let cfg_parent = cfg.path().parent().expect("Invalid Anchor.toml");

    // Require overflow checks
    let workspace_cargo_toml_path = cfg_parent.join("Cargo.toml");
    if workspace_cargo_toml_path.exists() {
        check_overflow(workspace_cargo_toml_path)?;
    }

    // Check whether there is a mismatch between CLI and crate/package versions
    check_anchor_version(&cfg).ok();
    check_deps(&cfg).ok();

    // Check for program ID mismatches before building (skip if --ignore-keys is used), Always skipped in anchor test
    if !ignore_keys {
        // FIXME: Consider making this a hard error in `deploy` instead
        if let ProgramIdComparison::Mismatch {
            lib_name,
            actual_id,
            declared_id,
        } = check_program_id_mismatch(&cfg, program_name.clone())?
        {
            eprintln!(
                "Program ID mismatch detected for program '{lib_name}':\n  Keypair file has: \
                 {actual_id}\n  Source code has:  {declared_id}\n\nPlease run 'anchor keys sync' \
                 to update the program ID in your source code or use the '--ignore-keys' flag to \
                 skip this check.",
            );
        }
    }

    let idl_out = match idl {
        Some(idl) => Some(PathBuf::from(idl)),
        None => Some(target_dir()?.join("idl")),
    };
    fs::create_dir_all(idl_out.as_ref().unwrap())?;

    let idl_ts_out = match idl_ts {
        Some(idl_ts) => Some(PathBuf::from(idl_ts)),
        None => Some(target_dir()?.join("types")),
    };
    fs::create_dir_all(idl_ts_out.as_ref().unwrap())?;

    if !cfg.workspace.idls.is_empty() {
        fs::create_dir_all(cfg_parent.join(&cfg.workspace.idls))?;
    };
    if !cfg.workspace.types.is_empty() {
        fs::create_dir_all(cfg_parent.join(&cfg.workspace.types))?;
    };

    cfg.run_hooks(HookType::PreBuild)?;

    let cargo = Manifest::discover()?;
    let build_config = BuildConfig {
        verifiable,
        solana_version: solana_version.or_else(|| cfg.toolchain.solana_version.clone()),
        docker_image: docker_image.unwrap_or_else(|| cfg.docker()),
        bootstrap,
    };
    let built_idl_paths = match cargo {
        // No Cargo.toml so build the entire workspace.
        None => build_all(
            &cfg,
            cfg.path(),
            no_idl,
            idl_out.clone(),
            idl_ts_out.clone(),
            &build_config,
            stdout,
            stderr,
            env_vars,
            cargo_args,
            skip_lint,
            no_docs,
        )?,
        // If the Cargo.toml is at the root, build the entire workspace.
        Some(cargo) if cargo.path().parent() == cfg.path().parent() => build_all(
            &cfg,
            cfg.path(),
            no_idl,
            idl_out.clone(),
            idl_ts_out.clone(),
            &build_config,
            stdout,
            stderr,
            env_vars,
            cargo_args,
            skip_lint,
            no_docs,
        )?,
        // Cargo.toml represents a single package. Build it.
        Some(cargo) => build_cwd(
            &cfg,
            cargo.path().to_path_buf(),
            no_idl,
            idl_out.clone(),
            idl_ts_out.clone(),
            &build_config,
            stdout,
            stderr,
            env_vars,
            cargo_args,
            skip_lint,
            no_docs,
        )?,
    };
    cfg.run_hooks(HookType::PostBuild)?;

    if cfg.clients.auto && !no_idl {
        // Only pass IDLs produced by this build. The output directory can
        // contain stale JSON from earlier full builds, especially after
        // `anchor build --program-name ...` from inside a program crate.
        codama::auto_generate_for_workspace(&cfg.clients, cfg_parent, &built_idl_paths)?;
    }

    set_workspace_dir_or_exit();

    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn build_all(
    cfg: &WithPath<Config>,
    cfg_path: &Path,
    no_idl: bool,
    idl_out: Option<PathBuf>,
    idl_ts_out: Option<PathBuf>,
    build_config: &BuildConfig,
    stdout: Option<File>, // Used for the package registry server.
    stderr: Option<File>, // Used for the package registry server.
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
    skip_lint: bool,
    no_docs: bool,
) -> Result<Vec<PathBuf>> {
    let cur_dir = std::env::current_dir()?;
    let r = (|| -> Result<Vec<PathBuf>> {
        match cfg_path.parent() {
            None => Err(anyhow!("Invalid Anchor.toml at {}", cfg_path.display())),
            Some(_parent) => {
                let mut idl_paths = Vec::new();
                for p in get_metadata_ordered_program_list(cfg)? {
                    idl_paths.extend(build_cwd(
                        cfg,
                        p.join("Cargo.toml"),
                        no_idl,
                        idl_out.clone(),
                        idl_ts_out.clone(),
                        build_config,
                        stdout.as_ref().map(|f| f.try_clone()).transpose()?,
                        stderr.as_ref().map(|f| f.try_clone()).transpose()?,
                        env_vars.clone(),
                        cargo_args.clone(),
                        skip_lint,
                        no_docs,
                    )?);
                }
                Ok(idl_paths)
            }
        }
    })();
    std::env::set_current_dir(cur_dir)?;
    r
}

fn get_metadata_ordered_program_list(cfg: &WithPath<Config>) -> Result<Vec<PathBuf>> {
    let programs = cfg.get_program_list()?;
    let ordered = order_programs_by_metadata(cfg, &programs);
    Ok(ordered.unwrap_or(programs))
}

fn order_programs_by_metadata(
    cfg: &WithPath<Config>,
    programs: &[PathBuf],
) -> Result<Vec<PathBuf>> {
    let workspace_dir = cfg
        .path()
        .parent()
        .ok_or_else(|| anyhow!("Invalid Anchor.toml at {}", cfg.path().display()))?;
    let metadata = MetadataCommand::new()
        .current_dir(workspace_dir)
        .exec()
        .context("Failed to run `cargo metadata`")?;

    let mut package_dirs = HashMap::new();
    for (idx, package) in metadata.packages.iter().enumerate() {
        if package.source.is_some() {
            continue;
        }
        let manifest_path = package.manifest_path.clone().into_std_path_buf();
        if let Some(package_dir) = manifest_path.parent() {
            if let Ok(package_dir) = package_dir.canonicalize() {
                package_dirs.insert(package_dir, idx);
            }
        }
    }

    let program_indices = programs
        .iter()
        .filter_map(|program| package_dirs.get(program).copied())
        .collect::<HashSet<_>>();
    if program_indices.len() != programs.len() {
        bail!("Failed to match all Anchor programs in `cargo metadata`");
    }

    let mut local_deps = vec![Vec::new(); metadata.packages.len()];
    for (idx, package) in metadata.packages.iter().enumerate() {
        for dep in &package.dependencies {
            if dep.kind == DependencyKind::Development {
                continue;
            }
            let Some(dep_path) = dep.path.as_ref() else {
                continue;
            };
            if let Ok(dep_path) = dep_path.clone().into_std_path_buf().canonicalize() {
                if let Some(dep_idx) = package_dirs.get(&dep_path) {
                    local_deps[idx].push(*dep_idx);
                }
            }
        }
    }

    let mut program_closures = HashMap::new();
    for idx in &program_indices {
        program_closures.insert(*idx, local_dependency_closure(*idx, &local_deps));
    }

    let original_order_by_package = programs
        .iter()
        .enumerate()
        .map(|(idx, program)| (package_dirs[program], idx))
        .collect::<HashMap<_, _>>();
    let program_by_index = programs
        .iter()
        .map(|program| (package_dirs[program], program.clone()))
        .collect::<HashMap<_, _>>();
    let ordered = order_program_indices_by_dependency_cache_heuristic(
        &program_indices,
        &program_closures,
        &original_order_by_package,
    )
    .into_iter()
    .map(|idx| program_by_index[&idx].clone())
    .collect();

    Ok(ordered)
}

fn order_program_indices_by_dependency_cache_heuristic(
    program_indices: &HashSet<usize>,
    program_closures: &HashMap<usize, HashSet<usize>>,
    original_order: &HashMap<usize, usize>,
) -> Vec<usize> {
    let mut reverse_dependents = HashMap::new();
    for idx in program_indices {
        reverse_dependents.insert(*idx, 0usize);
    }
    for (program_idx, deps) in program_closures {
        for dep_idx in deps {
            if program_indices.contains(dep_idx) && dep_idx != program_idx {
                *reverse_dependents.entry(*dep_idx).or_default() += 1;
            }
        }
    }

    let mut ordered = program_indices.iter().copied().collect::<Vec<_>>();
    ordered.sort_by(|a, b| {
        let a_deps = &program_closures[a];
        let b_deps = &program_closures[b];
        let a_program_deps = a_deps
            .iter()
            .filter(|idx| program_indices.contains(idx))
            .count();
        let b_program_deps = b_deps
            .iter()
            .filter(|idx| program_indices.contains(idx))
            .count();
        let a_reverse = reverse_dependents[a];
        let b_reverse = reverse_dependents[b];
        let a_isolated = a_program_deps == 0 && a_reverse == 0;
        let b_isolated = b_program_deps == 0 && b_reverse == 0;

        b_isolated
            .cmp(&a_isolated)
            .then_with(|| b_program_deps.cmp(&a_program_deps))
            .then_with(|| b_deps.len().cmp(&a_deps.len()))
            .then_with(|| a_reverse.cmp(&b_reverse))
            .then_with(|| original_order[a].cmp(&original_order[b]))
    });

    ordered
}

fn local_dependency_closure(start: usize, deps: &[Vec<usize>]) -> HashSet<usize> {
    let mut seen = HashSet::new();
    let mut stack = deps[start].clone();

    while let Some(idx) = stack.pop() {
        if seen.insert(idx) {
            stack.extend(deps[idx].iter().copied());
        }
    }

    seen
}

// Runs the build command outside of a workspace.
#[allow(clippy::too_many_arguments)]
fn build_cwd(
    cfg: &WithPath<Config>,
    cargo_toml: PathBuf,
    no_idl: bool,
    idl_out: Option<PathBuf>,
    idl_ts_out: Option<PathBuf>,
    build_config: &BuildConfig,
    stdout: Option<File>,
    stderr: Option<File>,
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
    skip_lint: bool,
    no_docs: bool,
) -> Result<Vec<PathBuf>> {
    match cargo_toml.parent() {
        None => return Err(anyhow!("Unable to find parent")),
        Some(p) => std::env::set_current_dir(p)?,
    };
    match build_config.verifiable {
        false => _build_cwd(
            cfg, no_idl, idl_out, idl_ts_out, skip_lint, no_docs, cargo_args,
        ),
        true => build_cwd_verifiable(
            cfg,
            cargo_toml,
            build_config,
            stdout,
            stderr,
            skip_lint,
            env_vars,
            cargo_args,
            no_docs,
        ),
    }
}

// Builds an anchor program in a docker image and copies the build artifacts
// into the `target/` directory.
#[allow(clippy::too_many_arguments)]
fn build_cwd_verifiable(
    cfg: &WithPath<Config>,
    cargo_toml: PathBuf,
    build_config: &BuildConfig,
    stdout: Option<File>,
    stderr: Option<File>,
    skip_lint: bool,
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
    no_docs: bool,
) -> Result<Vec<PathBuf>> {
    // Create output dirs.
    let workspace_dir = cfg.path().parent().unwrap().canonicalize()?;
    let target_dir = target_dir()?;
    fs::create_dir_all(target_dir.join("verifiable"))?;
    fs::create_dir_all(target_dir.join("idl"))?;
    fs::create_dir_all(target_dir.join("types"))?;
    if !&cfg.workspace.idls.is_empty() {
        fs::create_dir_all(workspace_dir.join(&cfg.workspace.idls))?;
    }
    if !&cfg.workspace.types.is_empty() {
        fs::create_dir_all(workspace_dir.join(&cfg.workspace.types))?;
    }

    let container_name = "anchor-program";

    // Build the binary in docker.
    let result = docker_build(
        cfg,
        container_name,
        cargo_toml,
        build_config,
        stdout,
        stderr,
        env_vars,
        cargo_args.clone(),
    );

    match result {
        Err(e) => {
            eprintln!("Error during Docker build: {e:?}");
            Err(e)
        }
        Ok(_) => {
            // Build the idl.
            println!("Extracting the IDL");
            let idl = generate_idl(cfg, skip_lint, no_docs, &cargo_args)?;
            // Write out the JSON file.
            println!("Writing the IDL file");
            let out_file = target_dir
                .join("idl")
                .join(&idl.metadata.name)
                .with_extension("json");
            write_idl(&idl, OutFile::File(out_file.clone()))?;

            if !&cfg.workspace.idls.is_empty() {
                write_idl(
                    &idl,
                    OutFile::File(
                        workspace_dir
                            .join(&cfg.workspace.idls)
                            .join(&idl.metadata.name)
                            .with_extension("json"),
                    ),
                )?;
            }

            // Write out the TypeScript type.
            println!("Writing the .ts file");
            let ts_file = target_dir
                .join("types")
                .join(&idl.metadata.name)
                .with_extension("ts");
            fs::write(&ts_file, idl_ts(&idl)?)?;

            // Copy out the TypeScript type.
            if !&cfg.workspace.types.is_empty() {
                fs::copy(
                    ts_file,
                    workspace_dir
                        .join(&cfg.workspace.types)
                        .join(idl.metadata.name)
                        .with_extension("ts"),
                )?;
            }

            println!("Build success");
            Ok(vec![out_file])
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn docker_build(
    cfg: &WithPath<Config>,
    container_name: &str,
    cargo_toml: PathBuf,
    build_config: &BuildConfig,
    stdout: Option<File>,
    stderr: Option<File>,
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
) -> Result<()> {
    let binary_name = Manifest::from_path(&cargo_toml)?.lib_name()?;

    // Docker vars.
    let workdir = Path::new("/workdir");
    let volume_mount = format!(
        "{}:{}",
        cfg.path().parent().unwrap().canonicalize()?.display(),
        workdir.to_str().unwrap(),
    );
    println!("Using image {:?}", build_config.docker_image);

    // Start the docker image running detached in the background.
    let target_dir = workdir.join("docker-target");
    println!("Run docker image");
    let exit = std::process::Command::new("docker")
        .args([
            "run",
            "-it",
            "-d",
            "--name",
            container_name,
            "--env",
            &format!(
                "CARGO_TARGET_DIR={}",
                target_dir.as_path().to_str().unwrap()
            ),
            "-v",
            &volume_mount,
            "-w",
            workdir.to_str().unwrap(),
            &build_config.docker_image,
            "bash",
        ])
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .map_err(|e| anyhow::format_err!("Docker build failed: {}", e))?;
    if !exit.status.success() {
        return Err(anyhow!("Failed to build program"));
    }

    let result = docker_prep(container_name, build_config).and_then(|_| {
        let cfg_parent = cfg.path().parent().unwrap();
        docker_build_bpf(
            container_name,
            cargo_toml.as_path(),
            cfg_parent,
            target_dir.as_path(),
            binary_name,
            stdout,
            stderr,
            env_vars,
            cargo_args,
        )
    });

    // Cleanup regardless of errors
    docker_cleanup(container_name, target_dir.as_path())?;

    // Done.
    result
}

fn docker_prep(container_name: &str, build_config: &BuildConfig) -> Result<()> {
    // Set the solana version in the container, if given. Otherwise use the
    // default.
    match build_config.bootstrap {
        BootstrapMode::Debian => {
            // Install build requirements
            docker_exec(container_name, &["apt", "update"])?;
            docker_exec(
                container_name,
                &["apt", "install", "-y", "curl", "build-essential"],
            )?;

            // Install Rust
            docker_exec(
                container_name,
                &["curl", "https://sh.rustup.rs", "-sfo", "rustup.sh"],
            )?;
            docker_exec(container_name, &["sh", "rustup.sh", "-y"])?;
            docker_exec(container_name, &["rm", "-f", "rustup.sh"])?;
        }
        BootstrapMode::None => {}
    }

    if let Some(solana_version) = &build_config.solana_version {
        println!("Using solana version: {solana_version}");

        // Install Solana CLI
        docker_exec(
            container_name,
            &[
                "curl",
                "-sSfL",
                &format!("https://release.anza.xyz/v{solana_version}/install",),
                "-o",
                "solana_installer.sh",
            ],
        )?;
        docker_exec(container_name, &["sh", "solana_installer.sh"])?;
        docker_exec(container_name, &["rm", "-f", "solana_installer.sh"])?;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn docker_build_bpf(
    container_name: &str,
    cargo_toml: &Path,
    cfg_parent: &Path,
    target_dir: &Path,
    binary_name: String,
    stdout: Option<File>,
    stderr: Option<File>,
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
) -> Result<()> {
    let manifest_path =
        pathdiff::diff_paths(cargo_toml.canonicalize()?, cfg_parent.canonicalize()?)
            .ok_or_else(|| anyhow!("Unable to diff paths"))?;
    println!(
        "Building {} manifest: {:?}",
        binary_name,
        manifest_path.display()
    );

    // Execute the build.
    let exit = std::process::Command::new("docker")
        .args([
            "exec",
            "--env",
            "PATH=/root/.local/share/solana/install/active_release/bin:/root/.cargo/bin:/usr/\
             local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
        ])
        .args(
            env_vars
                .iter()
                .map(|x| ["--env", x.as_str()])
                .collect::<Vec<[&str; 2]>>()
                .concat(),
        )
        .args([container_name, "cargo"])
        .args(BUILD_SUBCOMMAND)
        .args(["--manifest-path", &manifest_path.display().to_string()])
        .args(cargo_args)
        .stdout(match stdout {
            None => Stdio::inherit(),
            Some(f) => f.into(),
        })
        .stderr(match stderr {
            None => Stdio::inherit(),
            Some(f) => f.into(),
        })
        .output()
        .map_err(|e| anyhow::format_err!("Docker build failed: {}", e))?;
    if !exit.status.success() {
        return Err(anyhow!("Failed to build program"));
    }

    // Copy the binary out of the docker image.
    println!("Copying out the build artifacts");
    let out_file = crate::target_dir()?
        .join("verifiable")
        .join(&binary_name)
        .with_extension("so")
        .display()
        .to_string();

    // This requires the target directory of any built program to be located at
    // the root of the workspace.
    let mut bin_path = target_dir.join("deploy");
    bin_path.push(format!("{binary_name}.so"));
    let bin_artifact = format!(
        "{}:{}",
        container_name,
        bin_path.as_path().to_str().unwrap()
    );
    let exit = std::process::Command::new("docker")
        .args(["cp", &bin_artifact, &out_file])
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .map_err(|e| anyhow::format_err!("{}", e))?;
    if !exit.status.success() {
        Err(anyhow!(
            "Failed to copy binary out of docker. Is the target directory set correctly?"
        ))
    } else {
        Ok(())
    }
}

fn docker_cleanup(container_name: &str, target_dir: &Path) -> Result<()> {
    // Wipe the generated docker-target dir.
    println!("Cleaning up the docker target directory");
    docker_exec(container_name, &["rm", "-rf", target_dir.to_str().unwrap()])?;

    // Remove the docker image.
    println!("Removing the docker container");
    let exit = std::process::Command::new("docker")
        .args(["rm", "-f", container_name])
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .map_err(|e| anyhow::format_err!("{}", e))?;
    if !exit.status.success() {
        println!("Unable to remove the docker container");
        std::process::exit(exit.status.code().unwrap_or(1));
    }
    Ok(())
}

fn docker_exec(container_name: &str, args: &[&str]) -> Result<()> {
    let exit = std::process::Command::new("docker")
        .args([&["exec", container_name], args].concat())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .map_err(|e| anyhow!("Failed to run command \"{:?}\": {:?}", args, e))?;
    if !exit.status.success() {
        Err(anyhow!("Failed to run command: {:?}", args))
    } else {
        Ok(())
    }
}

#[allow(clippy::too_many_arguments)]
fn _build_cwd(
    cfg: &WithPath<Config>,
    no_idl: bool,
    idl_out: Option<PathBuf>,
    idl_ts_out: Option<PathBuf>,
    skip_lint: bool,
    no_docs: bool,
    cargo_args: Vec<String>,
) -> Result<Vec<PathBuf>> {
    let exit = std::process::Command::new("cargo")
        .args(BUILD_SUBCOMMAND)
        .args(cargo_args.clone())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .map_err(|e| anyhow::format_err!("{}", e))?;
    if !exit.status.success() {
        std::process::exit(exit.status.code().unwrap_or(1));
    }

    // Generate IDL
    if !no_idl {
        let idl = generate_idl(cfg, skip_lint, no_docs, &cargo_args)?;
        let cfg_parent = cfg.path().parent().expect("Invalid Anchor.toml");

        // JSON out path.
        let out = match idl_out {
            None => PathBuf::from(".")
                .join(&idl.metadata.name)
                .with_extension("json"),
            Some(o) => PathBuf::from(&o.join(&idl.metadata.name).with_extension("json")),
        };
        // TS out path.
        let ts_out = match idl_ts_out {
            None => PathBuf::from(".")
                .join(&idl.metadata.name)
                .with_extension("ts"),
            Some(o) => PathBuf::from(&o.join(&idl.metadata.name).with_extension("ts")),
        };

        // Write out the JSON file.
        write_idl(&idl, OutFile::File(out.clone()))?;
        if !&cfg.workspace.idls.is_empty() {
            write_idl(
                &idl,
                OutFile::File(
                    cfg_parent
                        .join(&cfg.workspace.idls)
                        .join(&idl.metadata.name)
                        .with_extension("json"),
                ),
            )?;
        }
        // Write out the TypeScript type.
        fs::write(&ts_out, idl_ts(&idl)?)?;

        // Copy out the TypeScript type.
        if !&cfg.workspace.types.is_empty() {
            fs::copy(
                &ts_out,
                cfg_parent
                    .join(&cfg.workspace.types)
                    .join(&idl.metadata.name)
                    .with_extension("ts"),
            )?;
        }
        Ok(vec![out])
    } else {
        Ok(Vec::new())
    }
}

/// Subcommand and any arguments to be passed to cargo
const BUILD_SUBCOMMAND: &[&str] = &["build-sbf", "--tools-version", "v1.52"];

/// Run the configured SBF build command.
pub fn cargo_build_sbf(cwd: Option<&Path>, extra_args: &[String]) -> Result<()> {
    let mut cmd = std::process::Command::new("cargo");
    if let Some(d) = cwd {
        cmd.current_dir(d);
    }
    let status = cmd
        .args(BUILD_SUBCOMMAND)
        .args(extra_args)
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
        .context("running cargo build-sbf")?;
    if !status.success() {
        return Err(anyhow!(
            "`cargo {}` failed with status {status}",
            BUILD_SUBCOMMAND.join(" ")
        ));
    }
    Ok(())
}

pub fn verify(
    program_id: Pubkey,
    repo_url: Option<String>,
    commit_hash: Option<String>,
    current_dir: bool,
    program_name: Option<String>,
    args: Vec<String>,
) -> Result<()> {
    let mut command_args = Vec::new();

    match (current_dir, repo_url) {
        (true, _) => {
            let current_path = std::env::current_dir()?
                .to_str()
                .ok_or_else(|| anyhow!("Invalid current directory path"))?
                .to_owned();
            command_args.push(current_path);
            command_args.push("--current-dir".into());
        }
        (false, Some(url)) => {
            command_args.push(url);
        }
        (false, None) => {
            return Err(anyhow!(
                "You must provide either --repo-url or --current-dir"
            ));
        }
    }

    if let Some(commit) = commit_hash {
        command_args.push("--commit-hash".into());
        command_args.push(commit);
    }

    if let Some(name) = program_name {
        command_args.push("--library-name".into());
        command_args.push(name);
    }

    command_args.push("--program-id".into());
    command_args.push(program_id.to_string());

    command_args.extend(args);

    println!("Verifying program {program_id}");
    let verify_path = AVM_HOME.join("bin").join("solana-verify");
    if !verify_path.exists() {
        install_with_avm(env!("CARGO_PKG_VERSION"), true)
            .context("installing Anchor with solana-verify")?;
    }

    let status = std::process::Command::new(verify_path)
        .arg("verify-from-repo")
        .args(&command_args)
        .stdout(std::process::Stdio::inherit())
        .stderr(std::process::Stdio::inherit())
        .status()
        .with_context(|| "Failed to run `solana-verify`")?;

    if !status.success() {
        return Err(anyhow!("Failed to verify program"));
    }

    Ok(())
}

fn cd_member(cfg_override: &ConfigOverride, program_name: &str) -> Result<()> {
    // Change directories to the given `program_name`, using either Anchor or Cargo workspace
    let programs = program::get_programs_from_workspace(cfg_override, None)?;

    for program in programs {
        let cargo_toml = program.path.join("Cargo.toml");
        if !cargo_toml.exists() {
            return Err(anyhow!(
                "Did not find Cargo.toml at the path: {}",
                program.path.display()
            ));
        }

        let manifest = Manifest::from_path(&cargo_toml)?;
        let pkg_name = manifest.package().name();
        if program_name == pkg_name || program_name == program.lib_name {
            std::env::set_current_dir(&program.path)?;
            return Ok(());
        }
    }

    Err(anyhow!("{} is not part of the workspace", program_name,))
}

fn idl(cfg_override: &ConfigOverride, subcmd: IdlCommand) -> Result<()> {
    match subcmd {
        IdlCommand::Init {
            program_id,
            filepath,
            priority_fee,
            non_canonical,
            #[cfg(feature = "idl-localnet-testing")]
            allow_localnet,
        } => {
            #[cfg(feature = "idl-localnet-testing")]
            let allow_localnet = allow_localnet;
            #[cfg(not(feature = "idl-localnet-testing"))]
            let allow_localnet = false;
            idl_init(
                program_id,
                cfg_override,
                filepath,
                priority_fee,
                non_canonical,
                allow_localnet,
            )
        }
        IdlCommand::Upgrade {
            program_id,
            filepath,
            priority_fee,
            #[cfg(feature = "idl-localnet-testing")]
            allow_localnet,
        } => {
            #[cfg(feature = "idl-localnet-testing")]
            let allow_localnet = allow_localnet;
            #[cfg(not(feature = "idl-localnet-testing"))]
            let allow_localnet = false;
            idl_upgrade(
                program_id,
                cfg_override,
                filepath,
                priority_fee,
                allow_localnet,
            )
        }
        IdlCommand::Build {
            program_name,
            out,
            out_ts,
            no_docs,
            skip_lint,
            cargo_args,
        } => idl_build(
            cfg_override,
            program_name,
            out,
            out_ts,
            no_docs,
            skip_lint,
            cargo_args,
        ),
        IdlCommand::Fetch {
            program_id: address,
            out,
            non_canonical,
        } => idl_fetch(cfg_override, address, out, non_canonical),
        IdlCommand::FetchHistorical {
            program_id: address,
            authority,
            slot,
            before,
            after,
            out_dir,
            rpc_workers,
            no_parallel,
            rpc_max_retries,
            rpc_retry_backoff_ms,
            max_signatures,
            verbose,
        } => fetch::idl_fetch_historical(
            cfg_override,
            address,
            authority,
            slot,
            before,
            after,
            out_dir,
            fetch::FetchTuning {
                workers: rpc_workers,
                no_parallel,
                max_retries: rpc_max_retries,
                retry_backoff_ms: rpc_retry_backoff_ms,
                max_signatures,
                verbose,
            },
        ),
        IdlCommand::Convert {
            path,
            out,
            program_id,
            to_legacy,
        } => idl_convert(path, out, program_id, to_legacy),
        IdlCommand::Type { path, out } => idl_type(path, out),
        IdlCommand::Close {
            program_id,
            seed,
            priority_fee,
        } => idl_close_metadata(cfg_override, program_id, seed, priority_fee),
        IdlCommand::CreateBuffer {
            filepath,
            priority_fee,
        } => idl_create_buffer(cfg_override, filepath, priority_fee),
        IdlCommand::SetBufferAuthority {
            buffer,
            new_authority,
            priority_fee,
        } => idl_set_buffer_authority(cfg_override, buffer, new_authority, priority_fee),
        IdlCommand::WriteBuffer {
            program_id,
            buffer,
            seed,
            close_buffer,
            priority_fee,
        } => idl_write_buffer_metadata(
            cfg_override,
            program_id,
            buffer,
            seed,
            close_buffer,
            priority_fee,
        ),
    }
}

fn idl_init(
    program_id: Option<Pubkey>,
    cfg_override: &ConfigOverride,
    idl_filepath: PathBuf,
    priority_fee: Option<u64>,
    non_canonical: bool,
    allow_localnet: bool,
) -> Result<()> {
    // Get cluster URL and wallet path from Anchor config
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;

    let is_localnet = cluster_url.contains("localhost") || cluster_url.contains("127.0.0.1");
    if is_localnet && !allow_localnet {
        #[cfg(feature = "idl-localnet-testing")]
        println!(
            "Skipping IDL initialization on localnet. To deploy on localnet, use --allow-localnet"
        );
        #[cfg(not(feature = "idl-localnet-testing"))]
        println!("Skipping IDL initialization on localnet");
        return Ok(());
    }

    let program_id = match program_id {
        Some(id) => id.to_string(),
        _ => {
            let idl = fs::read(&idl_filepath)?;
            let idl = convert_idl(&idl)?;
            idl.address
        }
    };

    let command = metadata::IdlCommand::funded(
        cluster_url,
        wallet_path,
        priority_fee,
        metadata::FundedIdlSubcommand::Write {
            program_id,
            idl_filepath: idl_filepath
                .to_str()
                .ok_or_else(|| anyhow!("IDL filepath is not valid UTF-8"))?
                .to_string(),
            non_canonical,
        },
    );

    if !command.status()?.success() {
        return Err(anyhow!("Failed to initialize IDL"));
    }

    println!("IDL initialized.");
    Ok(())
}

// Currently identical to `idl_init`, other than not accepting `non_canonical`
fn idl_upgrade(
    program_id: Option<Pubkey>,
    cfg_override: &ConfigOverride,
    idl_filepath: PathBuf,
    priority_fee: Option<u64>,
    allow_localnet: bool,
) -> Result<()> {
    // Get cluster URL and wallet path from Anchor config
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;

    let is_localnet = cluster_url.contains("localhost") || cluster_url.contains("127.0.0.1");
    if is_localnet && !allow_localnet {
        #[cfg(feature = "idl-localnet-testing")]
        println!("Skipping IDL upgrade on localnet. To deploy on localnet, use --allow-localnet");
        #[cfg(not(feature = "idl-localnet-testing"))]
        println!("Skipping IDL upgrade on localnet");
        return Ok(());
    }

    let program_id = match program_id {
        Some(id) => id.to_string(),
        _ => {
            let idl = fs::read(&idl_filepath)?;
            let idl = convert_idl(&idl)?;
            idl.address
        }
    };

    let command = metadata::IdlCommand::funded(
        cluster_url,
        wallet_path,
        priority_fee,
        metadata::FundedIdlSubcommand::Write {
            program_id,
            idl_filepath: idl_filepath
                .to_str()
                .ok_or_else(|| anyhow!("IDL filepath is not valid UTF-8"))?
                .to_string(),
            non_canonical: false,
        },
    );
    if !command.status()?.success() {
        return Err(anyhow!("Failed to upgrade IDL"));
    }

    println!("IDL upgraded.");
    Ok(())
}

fn idl_build(
    cfg_override: &ConfigOverride,
    program_name: Option<String>,
    out: Option<String>,
    out_ts: Option<String>,
    no_docs: bool,
    skip_lint: bool,
    cargo_args: Vec<String>,
) -> Result<()> {
    let cfg = Config::discover(cfg_override)?
        .ok_or_else(|| anyhow!("The 'anchor idl build' command requires an Anchor workspace."))?;
    let current_dir = std::env::current_dir()?;
    let program_path = match program_name {
        Some(name) => cfg.get_program(&name)?.path,
        None => {
            let programs = cfg.read_all_programs()?;
            if programs.len() == 1 {
                programs.into_iter().next().unwrap().path
            } else {
                programs
                    .into_iter()
                    .find(|program| program.path == current_dir)
                    .ok_or_else(|| anyhow!("Not in a program directory"))?
                    .path
            }
        }
    };
    std::env::set_current_dir(program_path)?;
    let idl = generate_idl(&cfg, skip_lint, no_docs, &cargo_args)?;
    std::env::set_current_dir(current_dir)?;

    let out = match out {
        Some(path) => OutFile::File(PathBuf::from(path)),
        None => OutFile::Stdout,
    };
    write_idl(&idl, out)?;

    if let Some(path) = out_ts {
        fs::write(path, idl_ts(&idl)?)?;
    }

    Ok(())
}

/// Generate IDL with method decided by whether manifest file has `idl-build` feature or not.
fn generate_idl(
    cfg: &WithPath<Config>,
    skip_lint: bool,
    no_docs: bool,
    cargo_args: &[String],
) -> Result<Idl> {
    check_idl_build_feature()?;

    let idl = anchor_lang_idl::build::IdlBuilder::new()
        .resolution(cfg.features.resolution)
        .skip_lint(cfg.features.skip_lint || skip_lint)
        .no_docs(no_docs)
        .cargo_args(cargo_args.into())
        .build()?;

    // Warn users if there is a potential for a conflict between user-defined discriminators and
    // hardcoded `event-cpi` discriminator.
    //
    // Note: Warn independent of whether the user has the `event-cpi` feature enabled to make sure
    // there are no potential conflicts in the future if/when the user decides to enable it.
    idl.instructions
        .iter()
        .filter(|ix| anchor_lang::event::EVENT_IX_TAG_LE.starts_with(&ix.discriminator))
        .for_each(|ix| {
            eprintln!(
                "Warning: Instruction conflicts with `event-cpi` instruction discriminator: `{}`",
                ix.name
            );
        });

    Ok(idl)
}

fn idl_fetch(
    cfg_override: &ConfigOverride,
    address: Pubkey,
    out: Option<String>,
    non_canonical: bool,
) -> Result<()> {
    let (cluster_url, _) = get_cluster_and_wallet(cfg_override)?;
    let command = metadata::IdlCommand::unfunded(
        cluster_url,
        metadata::UnfundedIdlSubcommand::Fetch {
            program_id: address.to_string(),
            out,
            non_canonical,
        },
    );

    if !command.status()?.success() {
        return Err(anyhow!("Failed to fetch IDL"));
    }
    Ok(())
}

/// Apply a `--program-id` override to a raw IDL JSON document. The current
/// and legacy specs store the program address in different places, so we
/// detect the spec by the presence of `metadata.spec` and patch the right
/// field. For the legacy spec we merge into the existing `metadata` object
/// instead of replacing it; replacing it would drop sibling fields, and for
/// a current-spec IDL it would also wipe `metadata.{spec,name,version}` and
/// cause `convert_idl` to mis-detect the file as legacy.
fn apply_program_id_override(idl: &[u8], program_id: Pubkey) -> Result<Vec<u8>> {
    let mut idl = serde_json::from_slice::<serde_json::Value>(idl)?;
    let obj = idl
        .as_object_mut()
        .ok_or_else(|| anyhow!("IDL must be an object"))?;
    let pid = program_id.to_string();
    let is_current_spec = obj.get("metadata").and_then(|m| m.get("spec")).is_some();
    if is_current_spec {
        // Current spec stores the address at the top level.
        obj.insert("address".into(), serde_json::Value::String(pid));
    } else {
        // Legacy spec stores it under `metadata.address`. Merge so we
        // don't drop any sibling metadata fields the file may already
        // carry.
        match obj.get_mut("metadata") {
            Some(serde_json::Value::Object(m)) => {
                m.insert("address".into(), serde_json::Value::String(pid));
            }
            _ => {
                obj.insert("metadata".into(), serde_json::json!({ "address": pid }));
            }
        }
    }
    serde_json::to_vec(&idl).map_err(Into::into)
}

fn idl_convert(
    path: PathBuf,
    out: Option<PathBuf>,
    program_id: Option<Pubkey>,
    to_legacy: bool,
) -> Result<()> {
    let idl = fs::read(path)?;
    let idl = match program_id {
        Some(program_id) => apply_program_id_override(&idl, program_id)?,
        None => idl,
    };

    // Normalize either input spec to a current-spec `Idl`; both output
    // branches need the parsed value.
    let parsed = convert_idl(&idl)?;
    let out = match out {
        None => OutFile::Stdout,
        Some(out) => OutFile::File(out),
    };
    if to_legacy {
        let bytes = convert_idl_to_legacy(&parsed)?;
        match out {
            OutFile::Stdout => {
                let s =
                    std::str::from_utf8(&bytes).context("legacy IDL JSON was not valid UTF-8")?;
                println!("{s}");
                Ok(())
            }
            OutFile::File(path) => fs::write(path, bytes).map_err(Into::into),
        }
    } else {
        write_idl(&parsed, out)
    }
}

fn idl_type(path: PathBuf, out: Option<PathBuf>) -> Result<()> {
    let idl = fs::read(path)?;
    let idl = convert_idl(&idl)?;
    let types = idl_ts(&idl)?;
    match out {
        Some(out) => fs::write(out, types)?,
        _ => println!("{types}"),
    };
    Ok(())
}

fn idl_close_metadata(
    cfg_override: &ConfigOverride,
    program_id: Pubkey,
    seed: String,
    priority_fee: Option<u64>,
) -> Result<()> {
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;
    let command = metadata::IdlCommand::funded(
        cluster_url,
        wallet_path,
        priority_fee,
        metadata::FundedIdlSubcommand::Close {
            program_id: program_id.to_string(),
            seed,
        },
    );

    if !command.status()?.success() {
        return Err(anyhow!("Failed to close metadata account"));
    }

    println!("Metadata account closed successfully.");
    Ok(())
}

fn idl_create_buffer(
    cfg_override: &ConfigOverride,
    filepath: PathBuf,
    priority_fee: Option<u64>,
) -> Result<()> {
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;
    let command = metadata::IdlCommand::funded(
        cluster_url,
        wallet_path,
        priority_fee,
        metadata::FundedIdlSubcommand::CreateBuffer {
            filepath: filepath
                .to_str()
                .ok_or_else(|| anyhow!("IDL filepath is not valid UTF-8"))?
                .to_string(),
        },
    );

    if !command.status()?.success() {
        return Err(anyhow!("Failed to create buffer"));
    }

    println!("Buffer created successfully.");
    Ok(())
}

fn idl_set_buffer_authority(
    cfg_override: &ConfigOverride,
    buffer: Pubkey,
    new_authority: Pubkey,
    priority_fee: Option<u64>,
) -> Result<()> {
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;
    let command = metadata::IdlCommand::funded(
        cluster_url,
        wallet_path,
        priority_fee,
        metadata::FundedIdlSubcommand::SetBufferAuthority {
            buffer: buffer.to_string(),
            new_authority: new_authority.to_string(),
        },
    );

    if !command.status()?.success() {
        return Err(anyhow!("Failed to set buffer authority"));
    }

    println!("Buffer authority set successfully.");
    Ok(())
}

fn idl_write_buffer_metadata(
    cfg_override: &ConfigOverride,
    program_id: Pubkey,
    buffer: Pubkey,
    seed: String,
    close_buffer: bool,
    priority_fee: Option<u64>,
) -> Result<()> {
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;
    let command = metadata::IdlCommand::funded(
        cluster_url,
        wallet_path,
        priority_fee,
        metadata::FundedIdlSubcommand::WriteBuffer {
            program_id: program_id.to_string(),
            buffer: buffer.to_string(),
            seed,
            close_buffer,
        },
    );

    if !command.status()?.success() {
        return Err(anyhow!("Failed to write metadata using buffer"));
    }

    println!("Metadata written successfully using buffer.");
    Ok(())
}

fn idl_ts(idl: &Idl) -> Result<String> {
    let idl_name = &idl.metadata.name;
    let type_name = idl_name.to_pascal_case();
    let mut camel_idl = serde_json::to_value(idl)?;
    camel_case_idl_identifiers(&mut camel_idl);
    let camel_idl = serde_json::to_string_pretty(&serde_json::from_value::<Idl>(camel_idl)?)?;

    Ok(format!(
        r#"/**
 * Program IDL in camelCase format in order to be used in JS/TS.
 *
 * Note that this is only a type helper and is not the actual IDL. The original
 * IDL can be found at `target/idl/{idl_name}.json`.
 */
export type {type_name} = {camel_idl};
"#
    ))
}

fn camel_case_idl_identifiers(value: &mut JsonValue) {
    match value {
        JsonValue::Array(values) => {
            for value in values {
                camel_case_idl_identifiers(value);
            }
        }
        JsonValue::Object(map) => {
            for (key, value) in map {
                if is_idl_identifier_key(key) {
                    camel_case_idl_identifier(value);
                } else {
                    camel_case_idl_identifiers(value);
                }
            }
        }
        _ => {}
    }
}

fn camel_case_idl_identifier(value: &mut JsonValue) {
    match value {
        JsonValue::String(s) => {
            if Pubkey::try_from(s.as_str()).is_err() {
                *s = s
                    .split('.')
                    .map(ToLowerCamelCase::to_lower_camel_case)
                    .collect::<Vec<_>>()
                    .join(".");
            }
        }
        JsonValue::Array(values) => {
            for value in values {
                camel_case_idl_identifier(value);
            }
        }
        _ => camel_case_idl_identifiers(value),
    }
}

fn is_idl_identifier_key(key: &str) -> bool {
    matches!(key, "name" | "path" | "account" | "relations" | "generic")
}

fn write_idl(idl: &Idl, out: OutFile) -> Result<()> {
    let idl_json = serde_json::to_string_pretty(idl)?;
    match out {
        OutFile::Stdout => println!("{idl_json}"),
        OutFile::File(out) => fs::write(out, idl_json)?,
    };

    Ok(())
}
fn account(
    cfg_override: &ConfigOverride,
    account_type: String,
    address: Pubkey,
    idl_filepath: Option<PathBuf>,
) -> Result<()> {
    let (program_name, account_type_name) = account_type
        .split_once('.') // Split at first occurrence of dot
        .and_then(|(x, y)| y.find('.').map_or_else(|| Some((x, y)), |_| None)) // ensures no dots in second substring
        .ok_or_else(|| {
            anyhow!(
                "Please enter the account struct in the following format: <program_name>.<Account>",
            )
        })?;

    let idl = idl_filepath.map_or_else(
        || {
            let config = Config::discover(cfg_override)?.ok_or_else(|| {
                anyhow!(
                    "The 'anchor account' command requires an Anchor workspace with Anchor.toml \
                     for IDL type generation."
                )
            })?;
            let programs = config
                .read_all_programs()
                .expect("Workspace must contain atleast one program.");

            let program = programs
                .iter()
                .find(|p| p.lib_name == *program_name)
                .ok_or_else(|| {
                    let mut available_programs: Vec<String> =
                        programs.iter().map(|p| p.lib_name.clone()).collect();
                    available_programs.sort();

                    if available_programs.is_empty() {
                        anyhow!(
                            "Program '{program_name}' not found in workspace. No programs \
                             available."
                        )
                    } else {
                        anyhow!(
                            "Program '{program_name}' not found in workspace.\n\nAvailable \
                             programs:\n  {}",
                            available_programs.join("\n  ")
                        )
                    }
                })?;

            program.idl.clone().ok_or_else(|| {
                anyhow!("IDL not found. Please build the program atleast once to generate the IDL.")
            })
        },
        |idl_path| {
            let idl = fs::read(idl_path)?;
            let idl = convert_idl(&idl)?;
            if idl.metadata.name != *program_name {
                return Err(anyhow!("IDL does not match program {program_name}."));
            }

            Ok(idl)
        },
    )?;

    let cluster = match &cfg_override.cluster {
        Some(cluster) => cluster.clone(),
        None => Config::discover(cfg_override)?
            .map(|cfg| cfg.provider.cluster.clone())
            .unwrap_or(Cluster::Localnet),
    };

    let data = create_client(cluster.url()).get_account_data(&address)?;
    let disc_len = idl
        .accounts
        .iter()
        .find(|acc| acc.name == *account_type_name)
        .map(|acc| acc.discriminator.len())
        .ok_or_else(|| {
            let mut available_accounts: Vec<String> =
                idl.accounts.iter().map(|acc| acc.name.clone()).collect();
            available_accounts.sort();

            if available_accounts.is_empty() {
                anyhow!(
                    "Account '{account_type_name}' not found in IDL. No accounts available in \
                     program '{program_name}'."
                )
            } else {
                anyhow!(
                    "Account '{account_type_name}' not found in IDL.\n\nAvailable accounts in \
                     program '{program_name}':\n  {}",
                    available_accounts.join("\n  ")
                )
            }
        })?;
    let mut data_view = &data[disc_len..];

    let deserialized_json =
        deserialize_idl_defined_type_to_json(&idl, account_type_name, &mut data_view)?;

    println!(
        "{}",
        serde_json::to_string_pretty(&deserialized_json).unwrap()
    );

    Ok(())
}

// Deserializes user defined IDL types by munching the account data(recursively).
fn deserialize_idl_defined_type_to_json(
    idl: &Idl,
    defined_type_name: &str,
    data: &mut &[u8],
) -> Result<JsonValue, anyhow::Error> {
    let defined_type = &idl
        .accounts
        .iter()
        .find(|acc| acc.name == defined_type_name)
        .and_then(|acc| idl.types.iter().find(|ty| ty.name == acc.name))
        .or_else(|| idl.types.iter().find(|ty| ty.name == defined_type_name))
        .ok_or_else(|| anyhow!("Type `{}` not found in IDL.", defined_type_name))?
        .ty;

    let mut deserialized_fields = Map::new();

    match defined_type {
        IdlTypeDefTy::Struct { fields } => {
            if let Some(fields) = fields {
                match fields {
                    IdlDefinedFields::Named(fields) => {
                        for field in fields {
                            deserialized_fields.insert(
                                field.name.clone(),
                                deserialize_idl_type_to_json(&field.ty, data, idl)?,
                            );
                        }
                    }
                    IdlDefinedFields::Tuple(fields) => {
                        let mut values = Vec::new();
                        for field in fields {
                            values.push(deserialize_idl_type_to_json(field, data, idl)?);
                        }
                        deserialized_fields
                            .insert(defined_type_name.to_owned(), JsonValue::Array(values));
                    }
                }
            }
        }
        IdlTypeDefTy::Enum { variants } => {
            let repr = <u8 as AnchorDeserialize>::deserialize(data)?;

            let variant = variants
                .get(repr as usize)
                .ok_or_else(|| anyhow!("Error while deserializing enum variant {repr}"))?;

            let mut value = json!({});

            if let Some(enum_field) = &variant.fields {
                match enum_field {
                    IdlDefinedFields::Named(fields) => {
                        let mut values = Map::new();
                        for field in fields {
                            values.insert(
                                field.name.clone(),
                                deserialize_idl_type_to_json(&field.ty, data, idl)?,
                            );
                        }
                        value = JsonValue::Object(values);
                    }
                    IdlDefinedFields::Tuple(fields) => {
                        let mut values = Vec::new();
                        for field in fields {
                            values.push(deserialize_idl_type_to_json(field, data, idl)?);
                        }
                        value = JsonValue::Array(values);
                    }
                }
            }

            deserialized_fields.insert(variant.name.clone(), value);
        }
        IdlTypeDefTy::Type { alias } => {
            return deserialize_idl_type_to_json(alias, data, idl);
        }
    }

    Ok(JsonValue::Object(deserialized_fields))
}

// Deserializes a primitive type using AnchorDeserialize
fn deserialize_idl_type_to_json(
    idl_type: &IdlType,
    data: &mut &[u8],
    parent_idl: &Idl,
) -> Result<JsonValue, anyhow::Error> {
    if data.is_empty() {
        return Err(anyhow::anyhow!("Unable to parse from empty bytes"));
    }

    Ok(match idl_type {
        IdlType::Bool => json!(<bool as AnchorDeserialize>::deserialize(data)?),
        IdlType::U8 => {
            json!(<u8 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::I8 => {
            json!(<i8 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::U16 => {
            json!(<u16 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::I16 => {
            json!(<i16 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::U32 => {
            json!(<u32 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::I32 => {
            json!(<i32 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::F32 => json!(<f32 as AnchorDeserialize>::deserialize(data)?),
        IdlType::U64 => {
            json!(<u64 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::I64 => {
            json!(<i64 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::F64 => json!(<f64 as AnchorDeserialize>::deserialize(data)?),
        IdlType::U128 => {
            json!(<u128 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::I128 => {
            json!(<i128 as AnchorDeserialize>::deserialize(data)?)
        }
        IdlType::U256 => todo!("Upon completion of u256 IDL standard"),
        IdlType::I256 => todo!("Upon completion of i256 IDL standard"),
        IdlType::Bytes => JsonValue::Array(
            <Vec<u8> as AnchorDeserialize>::deserialize(data)?
                .iter()
                .map(|i| json!(*i))
                .collect(),
        ),
        IdlType::String => json!(<String as AnchorDeserialize>::deserialize(data)?),
        IdlType::Pubkey => {
            json!(<Pubkey as AnchorDeserialize>::deserialize(data)?.to_string())
        }
        IdlType::Array(ty, size) => match size {
            IdlArrayLen::Value(size) => {
                let mut array_data: Vec<JsonValue> = Vec::with_capacity(*size);

                for _ in 0..*size {
                    array_data.push(deserialize_idl_type_to_json(ty, data, parent_idl)?);
                }

                JsonValue::Array(array_data)
            }
            // TODO:
            IdlArrayLen::Generic(_) => unimplemented!("Generic array length is not yet supported"),
        },
        IdlType::Option(ty) => {
            let is_present = <u8 as AnchorDeserialize>::deserialize(data)?;

            if is_present == 0 {
                JsonValue::String("None".to_string())
            } else {
                deserialize_idl_type_to_json(ty, data, parent_idl)?
            }
        }
        IdlType::Vec(ty) => {
            let size: usize = <u32 as AnchorDeserialize>::deserialize(data)?
                .try_into()
                .unwrap();

            let mut vec_data: Vec<JsonValue> = Vec::with_capacity(size);

            for _ in 0..size {
                vec_data.push(deserialize_idl_type_to_json(ty, data, parent_idl)?);
            }

            JsonValue::Array(vec_data)
        }
        IdlType::Defined {
            name,
            generics: _generics,
        } => {
            // TODO: Generics
            deserialize_idl_defined_type_to_json(parent_idl, name, data)?
        }
        IdlType::Generic(generic) => json!(generic),
        _ => unimplemented!("{idl_type:?}"),
    })
}

enum OutFile {
    Stdout,
    File(PathBuf),
}

// Builds, deploys, and tests all workspace programs in a single command.
#[allow(clippy::too_many_arguments)]
fn test(
    cfg_override: &ConfigOverride,
    program_name: Option<String>,
    skip_deploy: bool,
    skip_local_validator: bool,
    skip_build: bool,
    skip_lint: bool,
    no_idl: bool,
    detach: bool,
    tests_to_run: Vec<String>,
    script_name: Option<String>,
    validator_type: ValidatorType,
    profile: bool,
    gdb: bool,
    extra_args: Vec<String>,
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
) -> Result<()> {
    let test_paths = tests_to_run
        .iter()
        .map(|path| {
            PathBuf::from(path)
                .canonicalize()
                .map_err(|_| anyhow!("Wrong path {}", path))
        })
        .collect::<Result<Vec<_>, _>>()?;

    with_workspace(cfg_override, |cfg| -> Result<()> {
        // Set validator type based on CLI choice
        cfg.validator = Some(validator_type);

        let cli_skip_local_validator = skip_local_validator;
        let config_skip_local_validator = cfg.skip_local_validator.unwrap_or(false);
        let workspace_root = cfg.path().parent().unwrap().to_owned();

        #[cfg(windows)]
        if profile {
            return Err(anyhow!(
                "`anchor test --profile` is not supported on Windows"
            ));
        }
        #[cfg(windows)]
        let _ = gdb;

        #[cfg(not(windows))]
        let profile_dir = workspace_root.join(crate::profile::DEFAULT_PROFILE_DIR);
        #[cfg(not(windows))]
        let _gdb_guard: Option<crate::debugger::gdb::GdbDriver> = if profile {
            let _ = fs::remove_dir_all(&profile_dir);
            std::env::set_var("ANCHOR_PROFILE_DIR", &profile_dir);
            std::env::set_var("CARGO_PROFILE_RELEASE_DEBUG", "2");

            if let Some(test_script) = cfg.scripts.get_mut("test") {
                if test_script.contains("cargo test") {
                    *test_script =
                        test_script.replacen("cargo test", "cargo test --features profile", 1);
                    if gdb {
                        let sep = if test_script.contains(" -- ") {
                            " "
                        } else {
                            " -- "
                        };
                        *test_script = format!("{test_script}{sep}--test-threads=1");
                    }
                } else {
                    eprintln!(
                        "warning: --profile requires the `test` script in Anchor.toml to invoke \
                         `cargo test`; got: {test_script:?}. Profiling will not activate."
                    );
                }
            } else {
                eprintln!(
                    "warning: --profile requires a [scripts] test entry in Anchor.toml; none \
                     found. Profiling will not activate."
                );
            }

            if gdb {
                let driver = crate::debugger::gdb::start_gdb_driver(&profile_dir)?;
                std::env::set_var(crate::debugger::gdb::SOCKET_ENV, driver.sock_path());
                std::env::set_var("RUST_TEST_THREADS", "1");
                Some(driver)
            } else {
                None
            }
        } else {
            None
        };

        // Build if needed.
        if !skip_build {
            build(
                cfg_override,
                no_idl,
                None,
                None,
                false,
                skip_lint,
                true,
                program_name.clone(),
                None,
                None,
                BootstrapMode::None,
                None,
                None,
                env_vars,
                cargo_args,
                false,
            )?;
        }

        cfg.add_test_config(workspace_root, test_paths)?;

        // Deploy to the cluster unless told to skip. For localnet, preserve
        // explicit `--skip-local-validator` deploys because the validator is
        // already running, but don't let generated in-process templates force
        // an RPC deploy through their persisted config.
        let is_localnet = cfg.provider.cluster == Cluster::Localnet;
        let validator_plan = test_validator_plan(
            skip_deploy,
            is_localnet,
            cli_skip_local_validator,
            config_skip_local_validator,
        );
        if validator_plan.predeploy {
            deploy(cfg_override, None, None, false, true, vec![])?;
        }

        cfg.run_hooks(HookType::PreTest)?;

        let mut is_first_suite = true;
        let script_name_to_use = script_name.as_deref().unwrap_or("test");
        if let Some(test_script) = cfg.scripts.get_mut(script_name_to_use) {
            is_first_suite = false;

            match program_name {
                Some(program_name) => {
                    if let Some((from, to)) = Regex::new("\\s(tests/\\S+\\.(js|ts))")
                        .unwrap()
                        .captures_iter(&test_script.clone())
                        .last()
                        .and_then(|c| c.get(1).and_then(|mtch| c.get(2).map(|ext| (mtch, ext))))
                        .map(|(mtch, ext)| {
                            (
                                mtch.as_str(),
                                format!("tests/{program_name}.{}", ext.as_str()),
                            )
                        })
                    {
                        println!("\nRunning tests of program `{program_name}`!");
                        // Replace the last path to the program name's path
                        *test_script = test_script.replace(from, &to);
                    }
                }
                _ => println!(
                    "\nFound a '{}' script in the Anchor.toml. Running it as a test suite!",
                    script_name_to_use
                ),
            }

            run_test_suite(
                cfg,
                cfg.path(),
                is_localnet,
                validator_plan.skip_local_validator,
                skip_deploy,
                detach,
                validator_type,
                &cfg.test_validator,
                &cfg.scripts,
                script_name_to_use,
                validator_plan.stream_program_logs,
                &extra_args,
                &cfg.surfpool_config,
            )?;
        }
        if let Some(test_config) = &cfg.test_config {
            for test_suite in test_config.iter() {
                if !is_first_suite {
                    std::thread::sleep(std::time::Duration::from_millis(
                        test_suite
                            .1
                            .test
                            .as_ref()
                            .map(|val| val.shutdown_wait)
                            .unwrap_or(SHUTDOWN_WAIT) as u64,
                    ));
                } else {
                    is_first_suite = false;
                }

                run_test_suite(
                    cfg,
                    test_suite.0,
                    is_localnet,
                    validator_plan.skip_local_validator,
                    skip_deploy,
                    detach,
                    validator_type,
                    &test_suite.1.test,
                    &test_suite.1.scripts,
                    script_name_to_use,
                    validator_plan.stream_program_logs,
                    &extra_args,
                    &cfg.surfpool_config,
                )?;
            }
        }
        cfg.run_hooks(HookType::PostTest)?;

        #[cfg(not(windows))]
        if profile {
            render_profile(cfg, &profile_dir)?;
        }

        Ok(())
    })?
}

fn should_predeploy_before_test(
    skip_deploy: bool,
    is_localnet: bool,
    cli_skip_local_validator: bool,
) -> bool {
    !skip_deploy && (!is_localnet || cli_skip_local_validator)
}

#[derive(Debug, PartialEq, Eq)]
struct TestValidatorPlan {
    skip_local_validator: bool,
    predeploy: bool,
    stream_program_logs: bool,
}

fn test_validator_plan(
    skip_deploy: bool,
    is_localnet: bool,
    cli_skip_local_validator: bool,
    config_skip_local_validator: bool,
) -> TestValidatorPlan {
    TestValidatorPlan {
        skip_local_validator: cli_skip_local_validator || config_skip_local_validator,
        predeploy: should_predeploy_before_test(skip_deploy, is_localnet, cli_skip_local_validator),
        stream_program_logs: true,
    }
}

/// Run the test suite with profile tracing enabled and then launch the SBF instruction stepper.
#[cfg(not(windows))]
#[allow(clippy::too_many_arguments)]
fn debugger(
    cfg_override: &ConfigOverride,
    test_name: Option<String>,
    skip_run: bool,
    skip_build: bool,
    skip_lint: bool,
    gdb: bool,
    cargo_args: Vec<String>,
) -> Result<()> {
    let has_anchor_toml = match Config::discover(cfg_override) {
        Ok(Some(_)) => true,
        Ok(None) => false,
        Err(e) => return Err(anyhow!("failed to probe for Anchor.toml: {e}")),
    };

    if has_anchor_toml {
        debugger_anchor_workspace(
            cfg_override,
            test_name,
            skip_run,
            skip_build,
            skip_lint,
            gdb,
            cargo_args,
        )
    } else {
        debugger_loose(
            cfg_override,
            test_name,
            skip_run,
            skip_build,
            gdb,
            cargo_args,
        )
    }
}

#[cfg(not(windows))]
#[allow(clippy::too_many_arguments)]
fn debugger_anchor_workspace(
    cfg_override: &ConfigOverride,
    test_name: Option<String>,
    skip_run: bool,
    skip_build: bool,
    skip_lint: bool,
    gdb: bool,
    cargo_args: Vec<String>,
) -> Result<()> {
    if !skip_run {
        test(
            cfg_override,
            None,
            true,
            true,
            skip_build,
            skip_lint,
            true,
            false,
            Vec::new(),
            None, // script_name — debugger drives test execution itself
            ValidatorType::Surfpool,
            true,
            gdb,
            Vec::new(),
            Vec::new(),
            cargo_args,
        )?;
    }

    with_workspace(cfg_override, |cfg| -> Result<()> {
        let workspace_root = cfg.path().parent().unwrap().to_owned();
        let profile_dir = workspace_root.join(crate::profile::DEFAULT_PROFILE_DIR);
        let (pubkey_to_so, sources) = resolve_anchor_workspace_programs(cfg);

        if pubkey_to_so.is_empty() {
            return Err(anyhow!(
                "no programs resolved for the debugger.\n\nEither declare them in Anchor.toml:\n  \
                 [programs.localnet]\n  <name> = \"<pubkey>\"\n\nor run `anchor build` so \
                 `target/deploy/<name>-keypair.json` exists."
            ));
        }

        println!("\nResolved programs:");
        for (pk, so) in &pubkey_to_so {
            let src = sources.get(pk).copied().unwrap_or("unknown");
            println!("  {pk}  ->  {}  [{src}]", display_path_relative_to_cwd(so));
        }

        debugger::run(
            &profile_dir,
            &pubkey_to_so,
            Some(&workspace_root),
            None,
            test_name.as_deref(),
        )
    })?
}

#[cfg(not(windows))]
#[allow(clippy::too_many_arguments)]
fn debugger_loose(
    _cfg_override: &ConfigOverride,
    test_name: Option<String>,
    skip_run: bool,
    skip_build: bool,
    gdb: bool,
    cargo_args: Vec<String>,
) -> Result<()> {
    let cwd = std::env::current_dir().context("read current directory")?;
    let ws = debugger::loose::LooseWorkspace::discover(cwd)?;

    if !skip_run {
        ws.check_dev_dep()?;
    }
    let profile_feature = ws.detect_profile_feature()?;
    let profile_dir = ws.root.join(debugger::loose_profile_dir_name());

    if !skip_run {
        debugger::loose::clear_profile_dir(&profile_dir)?;
        std::env::set_var("CARGO_PROFILE_RELEASE_DEBUG", "2");

        let anchor_exe =
            std::env::current_exe().context("resolve anchor binary path for RUSTC_WRAPPER")?;
        std::env::set_var("RUSTC_WRAPPER", &anchor_exe);
        std::env::set_var(debugger::rustc_wrapper::WRAPPER_SENTINEL, "1");

        if !skip_build {
            let build_cwd = ws.cargo_invocation_dir();
            eprintln!("running `cargo build-sbf` from {}", build_cwd.display());
            cargo_build_sbf(Some(build_cwd), &cargo_args)?;
        }

        std::env::remove_var("RUSTC_WRAPPER");
        std::env::remove_var(debugger::rustc_wrapper::WRAPPER_SENTINEL);

        eprintln!(
            "running `cargo test{gdb} --features {profile_feature}{pkg}{filter}` from {dir}",
            gdb = if gdb { " [gdb mode]" } else { "" },
            pkg = ws
                .current_package
                .as_deref()
                .map(|p| format!(" -p {p}"))
                .unwrap_or_default(),
            filter = test_name
                .as_deref()
                .map(|f| format!(" -- {f}"))
                .unwrap_or_default(),
            dir = ws.cargo_invocation_dir().display(),
        );
        if gdb {
            debugger::gdb::run_gdb_mode(
                ws.cargo_invocation_dir(),
                ws.current_package.as_deref(),
                &profile_feature,
                &profile_dir,
                test_name.as_deref(),
            )?;
        } else {
            debugger::loose::run_cargo_test(
                ws.cargo_invocation_dir(),
                ws.current_package.as_deref(),
                &profile_feature,
                &profile_dir,
                test_name.as_deref(),
            )?;
        }
    }

    let pubkey_to_so = debugger::loose::discover_programs(&ws.root, ws.current_package.as_deref())?;
    if pubkey_to_so.is_empty() {
        eprintln!(
            "warning: no programs found under {}/target/deploy/.\nELFs are required for \
             source/disasm symbolication. The debugger will still open but the static disasm pane \
             will be empty.",
            ws.root.display()
        );
    }

    if !profile_dir.exists() {
        return Err(anyhow!(
            "no traces produced at {}.\n\nDid the test actually run? Check that:\n- the test \
             calls `anchor_v2_testing::svm()` (NOT `LiteSVM::new()`)\n- the `{profile_feature}` \
             feature is enabled in the test build\n- the test sent at least one transaction that \
             hit a BPF program",
            profile_dir.display()
        ));
    }

    debugger::run(
        &profile_dir,
        &pubkey_to_so,
        Some(&ws.root),
        Some(&ws.cwd),
        test_name.as_deref(),
    )
}

#[cfg(not(windows))]
fn run_coverage(
    _cfg_override: &ConfigOverride,
    skip_run: bool,
    skip_build: bool,
    output: &str,
    trace_dir: &str,
    cargo_args: Vec<String>,
) -> Result<()> {
    let cwd = std::env::current_dir().context("read current directory")?;
    let ws = debugger::loose::LooseWorkspace::discover(cwd)?;

    let trace_path = ws.root.join(trace_dir);
    let output_path = ws.root.join(output);

    if !skip_run {
        std::env::set_var("CARGO_PROFILE_RELEASE_DEBUG", "2");

        let anchor_exe =
            std::env::current_exe().context("resolve anchor binary path for RUSTC_WRAPPER")?;
        std::env::set_var("RUSTC_WRAPPER", &anchor_exe);
        std::env::set_var(debugger::rustc_wrapper::WRAPPER_SENTINEL, "1");

        if !skip_build {
            let build_cwd = ws.cargo_invocation_dir();
            eprintln!("building programs with DWARF...");
            cargo_build_sbf(Some(build_cwd), &cargo_args)?;
        }

        if trace_path.exists() {
            fs::remove_dir_all(&trace_path)?;
        }
        fs::create_dir_all(&trace_path)?;

        let profile_feature = ws.detect_profile_feature().ok();
        eprintln!("running tests with register tracing...");
        let mut cmd = std::process::Command::new("cargo");
        cmd.current_dir(ws.cargo_invocation_dir()).arg("test");
        if let Some(feature) = &profile_feature {
            cmd.env("ANCHOR_PROFILE_DIR", &trace_path)
                .arg("--features")
                .arg(feature);
        } else {
            cmd.env("SBF_TRACE_DIR", &trace_path);
        }
        if let Some(pkg) = &ws.current_package {
            cmd.arg("-p").arg(pkg);
        }
        let status = cmd.status().context("spawn cargo test")?;
        if !status.success() {
            return Err(anyhow!("cargo test failed"));
        }
    }

    if !trace_path.exists() {
        return Err(anyhow!(
            "no traces at {}. Run without --skip-run first.",
            trace_path.display()
        ));
    }

    let programs = debugger::loose::discover_programs(&ws.root, ws.current_package.as_deref())?;
    if programs.is_empty() {
        return Err(anyhow!(
            "no programs found. Ensure declare_id!() is present in source.",
        ));
    }

    if let Some(parent) = output_path.parent() {
        fs::create_dir_all(parent)?;
    }
    coverage::generate_lcov(&trace_path, &programs, Some(&ws.root), &output_path)
}

#[cfg(not(windows))]
fn display_path_relative_to_cwd(p: &Path) -> String {
    std::env::current_dir()
        .ok()
        .as_deref()
        .and_then(|c| p.strip_prefix(c).ok())
        .map(|rel| rel.display().to_string())
        .unwrap_or_else(|| p.display().to_string())
}

#[cfg(not(windows))]
fn resolve_anchor_workspace_programs(
    cfg: &WithPath<Config>,
) -> (BTreeMap<String, PathBuf>, BTreeMap<String, &'static str>) {
    let workspace_root = cfg.path().parent().unwrap();
    let deploy_dir = workspace_root.join("target").join("deploy");
    let mut pubkey_to_so: BTreeMap<String, PathBuf> = BTreeMap::new();
    let mut sources: BTreeMap<String, &'static str> = BTreeMap::new();
    for programs in cfg.programs.values() {
        for (name, deployment) in programs {
            let pk = deployment.address.to_string();
            pubkey_to_so.insert(pk.clone(), deploy_dir.join(format!("{name}.so")));
            sources.insert(pk, "Anchor.toml");
        }
    }
    if let Ok(discovered) = debugger::loose::discover_programs(workspace_root, None) {
        for (pk, so) in discovered {
            if !pubkey_to_so.contains_key(&pk) {
                pubkey_to_so.insert(pk.clone(), so);
                sources.insert(pk, "target/deploy");
            }
        }
    }
    (pubkey_to_so, sources)
}

#[cfg(not(windows))]
fn render_profile(cfg: &WithPath<Config>, profile_dir: &Path) -> Result<()> {
    let workspace_root = cfg.path().parent().unwrap().to_owned();
    let (pubkey_to_so, _sources) = resolve_anchor_workspace_programs(cfg);

    let rendered = profile::render_all_tests(profile_dir, Some(&workspace_root), &pubkey_to_so)
        .context("failed to render flamegraphs from trace directory")?;

    if rendered.is_empty() {
        eprintln!(
            "warning: no per-test trace directories found under {}. Did your tests call \
             `anchor_v2_testing::svm()` with the `profile` feature?",
            profile_dir.display()
        );
        return Ok(());
    }

    let mut sorted: Vec<&profile::RenderedTest> = rendered.iter().collect();
    sorted.sort_by(|a, b| a.test_name.cmp(&b.test_name));

    let max_name = sorted
        .iter()
        .filter(|t| t.svg_paths.len() == 1)
        .map(|t| t.test_name.len())
        .max()
        .unwrap_or(0);

    println!("\nFlamegraphs:");
    for test in &sorted {
        if test.svg_paths.len() == 1 {
            println!(
                "  {:<width$}  ->  {}",
                test.test_name,
                display_path_relative_to_cwd(&test.svg_paths[0]),
                width = max_name,
            );
        } else {
            println!("  {}", test.test_name);
            for (i, svg) in test.svg_paths.iter().enumerate() {
                println!("    tx{}  ->  {}", i + 1, display_path_relative_to_cwd(svg));
            }
        }
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn run_test_suite(
    cfg: &WithPath<Config>,
    test_suite_path: impl AsRef<Path>,
    is_localnet: bool,
    skip_local_validator: bool,
    skip_deploy: bool,
    detach: bool,
    validator_type: ValidatorType,
    test_validator: &Option<TestValidator>,
    scripts: &ScriptsConfig,
    script_name: &str,
    stream_program_logs: bool,
    extra_args: &[String],
    surfpool_config: &Option<SurfpoolConfig>,
) -> Result<()> {
    println!("\nRunning test suite: {:#?}\n", test_suite_path.as_ref());
    let mut validator_handle = None;
    if is_localnet && !skip_local_validator {
        let generated_accounts = generated_validator_accounts(cfg, test_validator)?;
        match validator_type {
            ValidatorType::Surfpool => {
                let full_simnet_mode = false;
                let flags = Some(surfpool_flags(
                    cfg,
                    surfpool_config,
                    full_simnet_mode,
                    skip_deploy,
                    Some(test_suite_path.as_ref()),
                    &generated_accounts,
                )?);
                validator_handle = Some(start_surfpool_validator(
                    flags,
                    surfpool_config,
                    full_simnet_mode,
                )?);
            }
            ValidatorType::Legacy => {
                let flags = Some(validator_flags(
                    cfg,
                    test_validator,
                    skip_deploy,
                    &generated_accounts,
                )?);
                validator_handle = Some(start_solana_test_validator(
                    cfg,
                    test_validator,
                    flags,
                    true,
                )?);
            }
        }
    }
    let url = cluster_url(cfg, test_validator, surfpool_config);

    let node_options = format!(
        "{} {}",
        match std::env::var_os("NODE_OPTIONS") {
            Some(value) => value
                .into_string()
                .map_err(std::env::VarError::NotUnicode)?,
            None => "".to_owned(),
        },
        get_node_dns_option(),
    );

    // Setup log reader - kept alive until end of scope
    let log_streams = if stream_program_logs {
        match stream_logs(cfg, &url) {
            Ok(streams) => Some(streams),
            Err(e) => {
                eprintln!("Warning: Failed to setup program log streaming: {:#}", e);
                eprintln!("Program logs will still be visible in the test output.");
                None
            }
        }
    } else {
        None
    };

    // Run the tests.
    let test_result = {
        let Some(cmd) = scripts.get(script_name) else {
            bail!("Not able to find script for `{}`", script_name);
        };
        let cmd = cmd.clone();
        let script_args = format!("{cmd} {}", extra_args.join(" "));

        std::process::Command::new("bash")
            .arg("-c")
            .arg(script_args)
            .env("ANCHOR_PROVIDER_URL", url)
            .env("ANCHOR_WALLET", cfg.provider.wallet.to_string())
            .env("NODE_OPTIONS", node_options)
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .output()
            .map_err(anyhow::Error::from)
            .context(cmd)
    };

    // Keep validator running if needed.
    if test_result.is_ok() && detach {
        println!("Local validator still running. Press Ctrl + C quit.");
        std::io::stdin().lock().lines().next().unwrap().unwrap();
    }

    // Check all errors and shut down.
    if let Some(mut child) = validator_handle {
        if let Err(err) = child.kill() {
            println!("Failed to kill subprocess {}: {}", child.id(), err);
        }
    }

    // Explicitly shutdown log streams - closes WebSocket subscriptions
    if let Some(log_streams) = log_streams {
        for handle in log_streams {
            handle.shutdown();
        }
    }

    // Must exist *after* shutting down the validator and log streams.
    match test_result {
        Ok(exit) => {
            if !exit.status.success() {
                std::process::exit(exit.status.code().unwrap());
            }
        }
        Err(err) => {
            println!("Failed to run test: {err:#}");
            return Err(err);
        }
    }

    Ok(())
}

// Returns the solana-test-validator flags. When `skip_deploy` is false, this
// embeds the workspace programs in the genesis block so we don't have to deploy
// every time. It also allows control of other solana-test-validator features.
fn validator_flags(
    cfg: &WithPath<Config>,
    test_validator: &Option<TestValidator>,
    skip_deploy: bool,
    generated_accounts: &[GeneratedAccount],
) -> Result<Vec<String>> {
    let mut flags = match skip_deploy {
        true => Vec::new(),
        false => validator_deploy_flags(cfg, test_validator)?,
    };
    for acct in generated_accounts {
        flags.push("--account".to_string());
        flags.push(acct.pubkey.to_string());
        flags.push(acct.file_path.display().to_string());
    }
    flags.extend(validator_config_flags(test_validator)?);
    Ok(flags)
}

/// Returns the rent-exempt minimum for the given account size.
fn rent_exempt_minimum(data_len: u64) -> u64 {
    (128 + data_len) * 3480 * 2
}

const RENT_EPOCH_NEVER: u64 = u64::MAX;

/// Writes a keypair file with restrictive permissions.
fn write_keypair_secure(keypair: &Keypair, path: &Path) -> Result<()> {
    use std::io::Write;
    let bytes = keypair.to_bytes().to_vec();
    let json = serde_json::to_string(&bytes)
        .with_context(|| format!("Failed to serialize keypair for {}", path.display()))?;

    let mut opts = std::fs::OpenOptions::new();
    opts.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        opts.mode(0o600);
    }
    let mut file = opts
        .open(path)
        .with_context(|| format!("Failed to create keypair file: {}", path.display()))?;
    file.write_all(json.as_bytes())
        .with_context(|| format!("Failed to write keypair file: {}", path.display()))?;
    Ok(())
}

/// Packs a `COption<Pubkey>` into the canonical on-chain byte layout.
fn pack_coption_pubkey(buf: &mut Vec<u8>, value: Option<Pubkey>) {
    match value {
        Some(pk) => {
            buf.extend_from_slice(&1u32.to_le_bytes());
            buf.extend_from_slice(pk.as_ref());
        }
        None => {
            buf.extend_from_slice(&0u32.to_le_bytes());
            buf.extend_from_slice(&[0u8; 32]);
        }
    }
}

/// Writes a validator account JSON fixture to disk.
fn write_account_json(path: &Path, value: &JsonValue) -> Result<()> {
    let mut file = File::create(path)
        .with_context(|| format!("Failed to create account file: {}", path.display()))?;
    serde_json::to_writer_pretty(&mut file, value)
        .with_context(|| format!("Failed to write account JSON to: {}", path.display()))?;
    Ok(())
}

/// Returns whether a config field requests a freshly generated address.
fn is_new_address(address: &str) -> bool {
    address.eq_ignore_ascii_case("new")
}

#[derive(Debug, Clone)]
struct GeneratedAccount {
    pubkey: Pubkey,
    file_path: PathBuf,
    surfpool_snapshot_value: JsonValue,
}

/// Resolves a config-relative path against the workspace root.
fn resolve_workspace_path(cfg: &WithPath<Config>, path: &str) -> Result<PathBuf> {
    let workspace_root = cfg
        .path()
        .parent()
        .ok_or_else(|| anyhow!("Anchor.toml path has no parent directory"))?;
    let candidate = Path::new(path);
    Ok(if candidate.is_relative() {
        workspace_root.join(candidate)
    } else {
        candidate.to_path_buf()
    })
}

/// Collects pubkeys declared by `account_dir` JSON fixtures.
fn account_dir_pubkeys(cfg: &WithPath<Config>, validator: &Validator) -> Result<HashSet<Pubkey>> {
    let mut pubkeys = HashSet::new();
    for account_dir in validator.account_dir.iter().flatten() {
        let directory = resolve_workspace_path(cfg, &account_dir.directory)?;
        for entry in fs::read_dir(&directory)
            .with_context(|| format!("Failed to read account directory: {}", directory.display()))?
        {
            let path = entry?.path();
            if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
                continue;
            }

            let fixture: JsonValue =
                serde_json::from_reader(File::open(&path).with_context(|| {
                    format!("Failed to open account fixture: {}", path.display())
                })?)
                .with_context(|| format!("Failed to parse account fixture: {}", path.display()))?;

            let Some(pubkey) = fixture.get("pubkey").and_then(JsonValue::as_str) else {
                continue;
            };
            pubkeys.insert(
                Pubkey::try_from(pubkey)
                    .map_err(|_| anyhow!("Invalid pubkey {} in {}", pubkey, path.display()))?,
            );
        }
    }
    Ok(pubkeys)
}

/// Collects the pubkeys the validator will preload before token-account materialization.
fn validator_supplied_account_pubkeys(
    cfg: &WithPath<Config>,
    validator: &Validator,
    created_mints: &[Pubkey],
) -> Result<HashSet<Pubkey>> {
    let mut pubkeys = created_mints.iter().copied().collect::<HashSet<_>>();

    if let Some(accounts) = &validator.account {
        for account in accounts {
            pubkeys.insert(
                Pubkey::try_from(account.address.as_str())
                    .map_err(|_| anyhow!("Invalid account pubkey: {}", account.address))?,
            );
        }
    }

    if let Some(clones) = &validator.clone {
        for clone in clones {
            pubkeys.insert(
                Pubkey::try_from(clone.address.as_str())
                    .map_err(|_| anyhow!("Invalid clone pubkey: {}", clone.address))?,
            );
        }
    }

    pubkeys.extend(account_dir_pubkeys(cfg, validator)?);
    Ok(pubkeys)
}

/// Materializes generated validator accounts for use by both legacy validator flags and Surfpool.
fn materialize_validator_accounts(
    cfg: &WithPath<Config>,
    validator: &Validator,
) -> Result<Vec<GeneratedAccount>> {
    let mut out = Vec::new();
    let needs_dir = validator.mints.is_some()
        || validator.token_accounts.is_some()
        || validator.fund_accounts.is_some();
    if !needs_dir {
        return Ok(out);
    }

    let workspace_root = cfg
        .path()
        .parent()
        .ok_or_else(|| anyhow!("Anchor.toml path has no parent directory"))?;
    let accounts_dir = workspace_root.join(".anchor").join("generated_accounts");
    fs::create_dir_all(&accounts_dir).with_context(|| {
        format!(
            "Failed to create accounts directory: {}",
            accounts_dir.display()
        )
    })?;

    let mut seen_pubkeys: HashSet<Pubkey> = HashSet::new();
    let mut record_pubkey = |pk: Pubkey, section: &str| -> Result<()> {
        if !seen_pubkeys.insert(pk) {
            bail!(
                "Duplicate pubkey {} across [test.validator] sections (collision detected in \
                 `{}`). Each generated account must have a unique address.",
                pk,
                section
            );
        }
        Ok(())
    };

    let mut created_mints: Vec<Pubkey> = Vec::new();

    if let Some(mints) = &validator.mints {
        for token_mint in mints {
            let pubkey = if is_new_address(&token_mint.address) {
                let keypair = Keypair::new();
                let pubkey = keypair.pubkey();
                let keypair_path = accounts_dir.join(format!("{}.mint.json", pubkey));
                write_keypair_secure(&keypair, &keypair_path)?;
                pubkey
            } else {
                Pubkey::try_from(token_mint.address.as_str())
                    .map_err(|_| anyhow!("Invalid mint pubkey address: {}", token_mint.address))?
            };
            record_pubkey(pubkey, "mints")?;
            created_mints.push(pubkey);

            let parse_authority = |opt: &Option<String>, field: &str| -> Result<Option<Pubkey>> {
                opt.as_ref()
                    .map(|s| {
                        Pubkey::try_from(s.as_str()).map_err(|_| {
                            anyhow!("Invalid {} pubkey for mint {}: {}", field, pubkey, s)
                        })
                    })
                    .transpose()
            };
            let mint_authority = parse_authority(&token_mint.mint_authority, "mint_authority")?;
            let freeze_authority =
                parse_authority(&token_mint.freeze_authority, "freeze_authority")?;

            let mut data = Vec::with_capacity(82);
            pack_coption_pubkey(&mut data, mint_authority);
            data.extend_from_slice(&token_mint.supply.unwrap_or(0).to_le_bytes());
            data.push(token_mint.decimals);
            data.push(1u8); // is_initialized
            pack_coption_pubkey(&mut data, freeze_authority);

            let account_json = json!({
                "pubkey": pubkey.to_string(),
                "account": {
                    "lamports": rent_exempt_minimum(82),
                    "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                    "executable": false,
                    "rentEpoch": RENT_EPOCH_NEVER,
                    "data": [STANDARD.encode(&data), "base64"]
                }
            });
            let file_path = accounts_dir.join(format!("{}.json", pubkey));
            write_account_json(&file_path, &account_json)?;
            out.push(GeneratedAccount {
                pubkey,
                file_path,
                surfpool_snapshot_value: json!({
                    "lamports": rent_exempt_minimum(82),
                    "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                    "executable": false,
                    "rentEpoch": RENT_EPOCH_NEVER,
                    "data": STANDARD.encode(&data),
                    "parsedData": JsonValue::Null,
                }),
            });
        }
    }

    if let Some(token_accounts) = &validator.token_accounts {
        let validator_supplied_pubkeys =
            validator_supplied_account_pubkeys(cfg, validator, &created_mints)?;
        for token_account in token_accounts {
            let mint_pubkey = if is_new_address(&token_account.mint) {
                *created_mints.last().ok_or_else(|| {
                    anyhow!(
                        "token_account specifies `mint = \"new\"` but no [[test.validator.mints]] \
                         entries are configured"
                    )
                })?
            } else {
                let mint_pubkey = Pubkey::try_from(token_account.mint.as_str()).map_err(|_| {
                    anyhow!(
                        "Invalid mint pubkey in token_account: {}",
                        token_account.mint
                    )
                })?;
                if !validator_supplied_pubkeys.contains(&mint_pubkey) {
                    bail!(
                        "token_account mint {} is not loaded by the validator. Add it via \
                         [[test.validator.mints]], [[test.validator.clone]], \
                         [[test.validator.account]], or [[test.validator.account_dir]].",
                        mint_pubkey
                    );
                }
                mint_pubkey
            };

            let owner_pubkey = if is_new_address(&token_account.owner) {
                let kp = Keypair::new();
                let pk = kp.pubkey();
                let owner_path = accounts_dir.join(format!("{}.owner.json", pk));
                write_keypair_secure(&kp, &owner_path)?;
                pk
            } else {
                Pubkey::try_from(token_account.owner.as_str()).map_err(|_| {
                    anyhow!(
                        "Invalid owner pubkey in token_account: {}",
                        token_account.owner
                    )
                })?
            };

            let token_account_pubkey = match &token_account.address {
                Some(addr) if !is_new_address(addr) => Pubkey::try_from(addr.as_str())
                    .map_err(|_| anyhow!("Invalid token_account address pubkey: {}", addr))?,
                _ => {
                    let kp = Keypair::new();
                    let pk = kp.pubkey();
                    let ta_path = accounts_dir.join(format!("{}.token_account.json", pk));
                    write_keypair_secure(&kp, &ta_path)?;
                    pk
                }
            };
            record_pubkey(token_account_pubkey, "token_accounts")?;

            let mut data = Vec::with_capacity(165);
            data.extend_from_slice(mint_pubkey.as_ref());
            data.extend_from_slice(owner_pubkey.as_ref());
            data.extend_from_slice(&token_account.amount.to_le_bytes());
            pack_coption_pubkey(&mut data, None); // delegate
            data.push(1u8); // state = initialized
            data.extend_from_slice(&0u32.to_le_bytes()); // is_native None tag
            data.extend_from_slice(&[0u8; 8]); // is_native body
            data.extend_from_slice(&0u64.to_le_bytes()); // delegated_amount
            pack_coption_pubkey(&mut data, None); // close_authority

            let account_json = json!({
                "pubkey": token_account_pubkey.to_string(),
                "account": {
                    "lamports": rent_exempt_minimum(165),
                    "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                    "executable": false,
                    "rentEpoch": RENT_EPOCH_NEVER,
                    "data": [STANDARD.encode(&data), "base64"]
                }
            });
            let file_path = accounts_dir.join(format!("{}.json", token_account_pubkey));
            write_account_json(&file_path, &account_json)?;
            out.push(GeneratedAccount {
                pubkey: token_account_pubkey,
                file_path,
                surfpool_snapshot_value: json!({
                    "lamports": rent_exempt_minimum(165),
                    "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                    "executable": false,
                    "rentEpoch": RENT_EPOCH_NEVER,
                    "data": STANDARD.encode(&data),
                    "parsedData": JsonValue::Null,
                }),
            });
        }
    }

    if let Some(fund_accounts) = &validator.fund_accounts {
        for funded_account in fund_accounts {
            let pubkey = if is_new_address(&funded_account.address) {
                let keypair = Keypair::new();
                let pubkey = keypair.pubkey();
                let keypair_path = accounts_dir.join(format!("{}.keypair.json", pubkey));
                write_keypair_secure(&keypair, &keypair_path)?;
                pubkey
            } else {
                Pubkey::try_from(funded_account.address.as_str())
                    .map_err(|_| anyhow!("Invalid pubkey address: {}", funded_account.address))?
            };
            record_pubkey(pubkey, "fund_accounts")?;

            let lamports = funded_account.lamports.unwrap_or(1_000_000_000);

            let account_json = json!({
                "pubkey": pubkey.to_string(),
                "account": {
                    "lamports": lamports,
                    "owner": "11111111111111111111111111111111",
                    "executable": false,
                    "rentEpoch": RENT_EPOCH_NEVER,
                    "data": ["", "base64"]
                }
            });
            let file_path = accounts_dir.join(format!("{}.json", pubkey));
            write_account_json(&file_path, &account_json)?;
            out.push(GeneratedAccount {
                pubkey,
                file_path,
                surfpool_snapshot_value: json!({
                    "lamports": lamports,
                    "owner": "11111111111111111111111111111111",
                    "executable": false,
                    "rentEpoch": RENT_EPOCH_NEVER,
                    "data": "",
                    "parsedData": JsonValue::Null,
                }),
            });
        }
    }

    Ok(out)
}

/// Computes the generated validator accounts once so all validator backends share them.
fn generated_validator_accounts(
    cfg: &WithPath<Config>,
    test_validator: &Option<TestValidator>,
) -> Result<Vec<GeneratedAccount>> {
    test_validator
        .as_ref()
        .and_then(|test| test.validator.as_ref())
        .map(|validator| materialize_validator_accounts(cfg, validator))
        .transpose()
        .map(|accounts| accounts.unwrap_or_default())
}

/// Writes a Surfpool snapshot file for generated validator accounts and returns its path.
fn write_surfpool_snapshot(
    cfg: &WithPath<Config>,
    generated_accounts: &[GeneratedAccount],
) -> Result<Option<PathBuf>> {
    if generated_accounts.is_empty() {
        return Ok(None);
    }

    let accounts_dir = resolve_workspace_path(cfg, ".anchor/generated_accounts")?;
    fs::create_dir_all(&accounts_dir).with_context(|| {
        format!(
            "Failed to create accounts directory for Surfpool snapshot: {}",
            accounts_dir.display()
        )
    })?;

    let snapshot_path = accounts_dir.join("surfpool.snapshot.json");
    let mut snapshot = Map::new();
    for account in generated_accounts {
        snapshot.insert(
            account.pubkey.to_string(),
            account.surfpool_snapshot_value.clone(),
        );
    }

    let mut file = File::create(&snapshot_path).with_context(|| {
        format!(
            "Failed to create Surfpool snapshot file: {}",
            snapshot_path.display()
        )
    })?;
    serde_json::to_writer_pretty(&mut file, &JsonValue::Object(snapshot)).with_context(|| {
        format!(
            "Failed to write Surfpool snapshot file: {}",
            snapshot_path.display()
        )
    })?;

    Ok(Some(snapshot_path))
}

fn validator_deploy_flags(
    cfg: &WithPath<Config>,
    test_validator: &Option<TestValidator>,
) -> Result<Vec<String>> {
    let programs = cfg.programs.get(&Cluster::Localnet);

    let test_upgradeable_program = test_validator
        .as_ref()
        .map(|test_validator| test_validator.upgradeable)
        .unwrap_or(false);

    let mut flags = Vec::new();
    for mut program in cfg.read_all_programs()? {
        let verifiable = false;
        let binary_path = program.binary_path(verifiable)?.display().to_string();
        // Use the [programs.cluster] override and fallback to the keypair
        // files if no override is given.
        let address = programs
            .and_then(|m| m.get(&program.lib_name))
            .map(|deployment| Ok(deployment.address.to_string()))
            .unwrap_or_else(|| program.pubkey().map(|p| p.to_string()))?;

        if test_upgradeable_program {
            flags.push("--upgradeable-program".to_string());
            flags.push(address.clone());
            flags.push(binary_path);
            flags.push(cfg.wallet_kp()?.pubkey().to_string());
        } else {
            flags.push("--bpf-program".to_string());
            flags.push(address.clone());
            flags.push(binary_path);
        }

        if let Some(idl) = program.idl.as_mut() {
            // Add program address to the IDL.
            idl.address = address;

            // Persist it.
            let idl_out = target_dir()?
                .join("idl")
                .join(&idl.metadata.name)
                .with_extension("json");
            write_idl(idl, OutFile::File(idl_out))?;
        }
    }

    if let Some(test) = test_validator.as_ref() {
        if let Some(genesis) = &test.genesis {
            for entry in genesis {
                let program_path = Path::new(&entry.program);
                if !program_path.exists() {
                    return Err(anyhow!(
                        "Program in genesis configuration does not exist at path: {}",
                        program_path.display()
                    ));
                }
                if entry.upgradeable.unwrap_or(false) {
                    flags.push("--upgradeable-program".to_string());
                    flags.push(entry.address.clone());
                    flags.push(entry.program.clone());
                    flags.push(cfg.wallet_kp()?.pubkey().to_string());
                } else {
                    flags.push("--bpf-program".to_string());
                    flags.push(entry.address.clone());
                    flags.push(entry.program.clone());
                }
            }
        }
    }

    Ok(flags)
}

fn validator_config_flags(test_validator: &Option<TestValidator>) -> Result<Vec<String>> {
    let mut flags = Vec::new();

    if let Some(validator) = test_validator
        .as_ref()
        .and_then(|test| test.validator.as_ref())
    {
        let entries = serde_json::to_value(validator)?;
        for (key, value) in entries.as_object().unwrap() {
            if key == "ledger" {
                // Ledger flag is a special case as it is passed separately to the rest of
                // these validator flags.
                continue;
            };
            if key == "fund_accounts" || key == "mints" || key == "token_accounts" {
                continue;
            }
            if key == "extra_args" {
                for arg in value.as_array().unwrap() {
                    flags.push(arg.as_str().unwrap().to_string());
                }
                continue;
            }
            if key == "account" {
                for entry in value.as_array().unwrap() {
                    // Push the account flag for each array entry
                    flags.push("--account".to_string());
                    flags.push(entry["address"].as_str().unwrap().to_string());
                    flags.push(entry["filename"].as_str().unwrap().to_string());
                }
            } else if key == "account_dir" {
                for entry in value.as_array().unwrap() {
                    flags.push("--account-dir".to_string());
                    flags.push(entry["directory"].as_str().unwrap().to_string());
                }
            } else if key == "clone" {
                // Client for fetching accounts data
                let client = if let Some(url) = entries["url"].as_str() {
                    create_client(url)
                } else {
                    return Err(anyhow!(
                        "Validator url for Solana's JSON RPC should be provided in order to clone \
                         accounts from it"
                    ));
                };

                let pubkeys = value
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|entry| {
                        let address = entry["address"].as_str().unwrap();
                        Pubkey::try_from(address).map_err(|_| anyhow!("Invalid pubkey {}", address))
                    })
                    .collect::<Result<HashSet<Pubkey>>>()?
                    .into_iter()
                    .collect::<Vec<_>>();
                let accounts = client.get_multiple_accounts(&pubkeys)?;

                for (pubkey, account) in pubkeys.into_iter().zip(accounts) {
                    match account {
                        Some(account) => {
                            // Use a different flag for program accounts to fix the problem
                            // described in https://github.com/anza-xyz/agave/issues/522
                            if account.owner == bpf_loader_upgradeable::id()
                                // Only programs are supported with `--clone-upgradeable-program`
                                && matches!(
                                    account.deserialize_data::<UpgradeableLoaderState>()?,
                                    UpgradeableLoaderState::Program { .. }
                                )
                            {
                                flags.push("--clone-upgradeable-program".to_string());
                                flags.push(pubkey.to_string());
                            } else {
                                flags.push("--clone".to_string());
                                flags.push(pubkey.to_string());
                            }
                        }
                        _ => return Err(anyhow!("Account {} not found", pubkey)),
                    }
                }
            } else if key == "deactivate_feature" {
                // Verify that the feature flags are valid pubkeys
                let pubkeys_result: Result<Vec<Pubkey>, _> = value
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|entry| {
                        let feature_flag = entry.as_str().unwrap();
                        Pubkey::try_from(feature_flag)
                            .map_err(|_| anyhow!("Invalid pubkey (feature flag) {}", feature_flag))
                    })
                    .collect();
                let features = pubkeys_result?;
                for feature in features {
                    flags.push("--deactivate-feature".to_string());
                    flags.push(feature.to_string());
                }
            } else {
                // Remaining validator flags are non-array types
                flags.push(format!("--{}", key.replace('_', "-")));
                if let serde_json::Value::String(v) = value {
                    flags.push(v.to_string());
                } else {
                    flags.push(value.to_string());
                }
            }
        }
    }

    Ok(flags)
}

// Returns Surfpool flags.
// This flags will be passed to the Surfpool, it allows to configure the validator.
fn surfpool_flags(
    cfg: &WithPath<Config>,
    surfpool_config: &Option<SurfpoolConfig>,
    full_simnet_mode: bool,
    skip_deploy: bool,
    test_suite_path: Option<&Path>,
    generated_accounts: &[GeneratedAccount],
) -> Result<Vec<String>> {
    let programs = cfg.programs.get(&Cluster::Localnet);
    let mut flags = Vec::new();

    for mut program in cfg.read_all_programs()? {
        let address = programs
            .and_then(|m| m.get(&program.lib_name))
            .map(|deployment| Ok(deployment.address.to_string()))
            .unwrap_or_else(|| program.pubkey().map(|p| p.to_string()))?;
        if let Some(idl) = program.idl.as_mut() {
            // Creating the idl files
            idl.address = address;
            let idl_out = target_dir()?
                .join("idl")
                .join(&idl.metadata.name)
                .with_extension("json");
            write_idl(idl, OutFile::File(idl_out))?;
        }
    }

    if let Some(snapshot_path) = write_surfpool_snapshot(cfg, generated_accounts)? {
        flags.push("--snapshot".to_string());
        flags.push(snapshot_path.display().to_string());
    }

    if let Some(config) = &surfpool_config {
        if let Some(airdrop_addresses) = &config.airdrop_addresses {
            for address in airdrop_addresses {
                flags.push("--airdrop".to_string());
                flags.push(address.to_string());
            }
        }
        if let Some(datasource_rpc_url) = &config.datasource_rpc_url {
            flags.push("--rpc-url".to_string());
            flags.push(datasource_rpc_url.to_string());
        }

        let host = &config.host;
        flags.push("--host".to_string());
        flags.push(host.to_string());

        let rpc_port = &config.rpc_port;
        flags.push("--port".to_string());
        flags.push(rpc_port.to_string());

        if let Some(ws_port) = &config.ws_port {
            flags.push("--ws-port".to_string());
            flags.push(ws_port.to_string());
        }

        if let Some(manifest_file_path) = &config.manifest_file_path {
            flags.push("--manifest-file-path".to_string());
            flags.push(manifest_file_path.to_string());
        }

        if let Some(runbooks) = &config.runbooks {
            for runbook in runbooks {
                flags.push("--runbook".to_string());
                flags.push(runbook.to_string());
            }
        }

        if let Some(slot_time) = &config.slot_time {
            flags.push("--slot-time".to_string());
            flags.push(slot_time.to_string());
        }
    }

    let online = surfpool_config
        .as_ref()
        .and_then(|c| c.online)
        .unwrap_or(false);
    if !online {
        flags.push("--offline".to_string());
    }

    let block_production_mode = surfpool_config
        .as_ref()
        .and_then(|c| c.block_production_mode.clone())
        .unwrap_or("transaction".into());
    flags.push("--block-production-mode".to_string());
    flags.push(block_production_mode);

    flags.push("--log-level".to_string());
    flags.push(
        surfpool_config
            .as_ref()
            .and_then(|c| c.log_level.clone())
            .unwrap_or("none".into()),
    );

    if !full_simnet_mode {
        flags.push("--no-tui".to_string());
        flags.push("--disable-instruction-profiling".to_string());
        flags.push("--max-profiles".to_string());
        flags.push("1".to_string());
        flags.push("--no-studio".to_string());
    }

    match skip_deploy {
        true => flags.push("--no-deploy".to_string()),
        false => {
            // automatically generate in-memory runbooks
            flags.push("--legacy-anchor-compatibility".to_string());
            if let Some(test_suite_path) = test_suite_path {
                flags.push("--anchor-test-config-path".to_string());
                flags.push(test_suite_path.display().to_string());
            }
        }
    }

    Ok(flags)
}

/// Handle for a log streaming thread.
///
/// Manages a WebSocket subscription and its associated receiver thread.
/// Call `shutdown()` to cleanly stop the thread.
struct LogStreamHandle {
    subscription: PubsubClientSubscription<RpcResponse<RpcLogsResponse>>,
}

impl LogStreamHandle {
    /// Explicitly shutdown the log stream
    fn shutdown(self) {
        // Send unsubscribe in a background thread to avoid blocking
        // PubsubClientSubscription::send_unsubscribe() can block indefinitely if WebSocket is stuck
        // The receiver threads will exit when the subscription closes
        std::thread::spawn(move || {
            let _ = self.subscription.send_unsubscribe();
        });
    }
}

/// Spawns a thread to receive logs from a subscription and write them to a file
fn spawn_log_receiver_thread<R>(receiver: R, log_file_path: PathBuf)
where
    R: IntoIterator<Item = RpcResponse<RpcLogsResponse>> + Send + 'static,
{
    std::thread::spawn(move || {
        if let Ok(mut file) = File::create(&log_file_path) {
            for response in receiver {
                let _ = writeln!(
                    file,
                    "Transaction executed in slot {}:",
                    response.context.slot
                );
                let _ = writeln!(file, "  Signature: {}", response.value.signature);
                let _ = writeln!(
                    file,
                    "  Status: {}",
                    response
                        .value
                        .err
                        .map(|err| err.to_string())
                        .unwrap_or_else(|| "Ok".to_string())
                );
                let _ = writeln!(file, "  Log Messages:");
                for log in response.value.logs {
                    let _ = writeln!(file, "    {}", log);
                }
                let _ = writeln!(file); // Empty line between transactions
                let _ = file.flush();
            }
        } else {
            eprintln!("Failed to create log file: {:?}", log_file_path);
        }
    });
}

fn stream_logs(config: &WithPath<Config>, rpc_url: &str) -> Result<Vec<LogStreamHandle>> {
    // Determine validator type to use appropriate logging
    match &config.validator {
        Some(ValidatorType::Surfpool) => {
            // For Surfpool, we don't need to stream logs via external commands
            // Surfpool handles its own logging to .surfpool/logs/ directory
            if config
                .surfpool_config
                .as_ref()
                .and_then(|s| {
                    s.log_level
                        .as_ref()
                        .map(|l| l.to_ascii_lowercase().ne("none"))
                })
                .unwrap_or(false)
            {
                println!("Surfpool validator logs: .surfpool/logs/ directory");
            }
            Ok(vec![])
        }
        Some(ValidatorType::Legacy) | None => stream_solana_logs(config, rpc_url),
    }
}

fn stream_solana_logs(config: &WithPath<Config>, rpc_url: &str) -> Result<Vec<LogStreamHandle>> {
    let program_logs_dir = Path::new(".anchor").join("program-logs");
    if program_logs_dir.exists() {
        fs::remove_dir_all(&program_logs_dir)?;
    }
    fs::create_dir_all(&program_logs_dir)?;

    // For solana-test-validator, the WebSocket port is RPC port + WEBSOCKET_PORT_OFFSET
    // Extract port from rpc_url and construct WebSocket URL
    let ws_url = if rpc_url.contains("127.0.0.1") || rpc_url.contains("localhost") {
        // Local validator: increment port by 1 for WebSocket
        let rpc_port = rpc_url
            .rsplit_once(':')
            .and_then(|(_, port)| port.parse::<u16>().ok())
            .unwrap_or(DEFAULT_RPC_PORT);

        let ws_port = rpc_port + WEBSOCKET_PORT_OFFSET;
        let url = format!("ws://127.0.0.1:{}", ws_port);
        url
    } else {
        // Remote cluster: use same URL but replace http(s) with ws(s)
        rpc_url
            .replace("https://", "wss://")
            .replace("http://", "ws://")
    };

    // Give the WebSocket endpoint a moment to be ready (especially for local validators)
    std::thread::sleep(std::time::Duration::from_millis(1500));

    let mut handles = vec![];

    // Subscribe to logs for all workspace programs
    for program in config.read_all_programs()? {
        let idl_path = target_dir()?
            .join("idl")
            .join(&program.lib_name)
            .with_extension("json");
        let idl = fs::read(&idl_path)?;
        let idl = convert_idl(&idl)?;

        let log_file_path =
            program_logs_dir.join(format!("{}.{}.log", idl.address, program.lib_name));
        let program_address = idl.address.clone();

        // Subscribe to logs using PubsubClient
        let (client, receiver) = match PubsubClient::logs_subscribe(
            &ws_url,
            RpcTransactionLogsFilter::Mentions(vec![program_address.clone()]),
            RpcTransactionLogsConfig {
                commitment: Some(CommitmentConfig::confirmed()),
            },
        ) {
            Ok(result) => result,
            Err(e) => {
                eprintln!(
                    "Warning: Failed to subscribe to logs for program {}: {}",
                    program.lib_name, e
                );
                continue;
            }
        };

        // Spawn thread to write logs to file
        spawn_log_receiver_thread(receiver, log_file_path);

        handles.push(LogStreamHandle {
            subscription: client,
        });
    }

    // Also subscribe to logs for genesis programs
    if let Some(test) = config.test_validator.as_ref() {
        if let Some(genesis) = &test.genesis {
            for entry in genesis {
                let log_file_path = program_logs_dir.join(&entry.address).with_extension("log");
                let address = entry.address.clone();

                // Subscribe to logs using PubsubClient
                let (client, receiver) = match PubsubClient::logs_subscribe(
                    &ws_url,
                    RpcTransactionLogsFilter::Mentions(vec![address.clone()]),
                    RpcTransactionLogsConfig {
                        commitment: Some(CommitmentConfig::confirmed()),
                    },
                ) {
                    Ok(result) => result,
                    Err(e) => {
                        eprintln!(
                            "Warning: Failed to subscribe to logs for genesis program {}: {}",
                            &entry.address, e
                        );
                        continue;
                    }
                };

                // Spawn thread to write logs to file
                spawn_log_receiver_thread(receiver, log_file_path);

                handles.push(LogStreamHandle {
                    subscription: client,
                });
            }
        }
    }

    Ok(handles)
}

fn start_surfpool_validator(
    flags: Option<Vec<String>>,
    surfpool_config: &Option<SurfpoolConfig>,
    full_simnet_mode: bool,
) -> Result<Child> {
    let (host, port) = match surfpool_config {
        Some(SurfpoolConfig { host, rpc_port, .. }) => (host.clone(), *rpc_port),
        _ => (SURFPOOL_HOST.to_string(), DEFAULT_RPC_PORT),
    };
    let rpc_url = surfpool_rpc_url(surfpool_config);

    if std::net::TcpStream::connect_timeout(
        &format!("{host}:{port}")
            .parse()
            .map_err(|err| anyhow!("invalid surfpool host:port `{host}:{port}`: {err}"))?,
        std::time::Duration::from_millis(200),
    )
    .is_ok()
    {
        return Err(anyhow!(
            "port {port} on {host} is already in use - another validator is running there. Kill \
             it or set `[surfpool] rpc_port = N` in Anchor.toml to pick a free port."
        ));
    }

    let test_validator_stdout = match full_simnet_mode {
        true => Stdio::inherit(),
        false => Stdio::null(),
    };

    let mut validator_handle = std::process::Command::new("surfpool")
        .arg("start")
        .args(flags.unwrap_or_default())
        .stdout(test_validator_stdout)
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|e| anyhow!("Failed to spawn `surfpool`: {e}"))?;

    let client = create_client(rpc_url.clone());

    let mut count = 0;

    let ms_wait = surfpool_config
        .as_ref()
        .map(|surfpool| surfpool.startup_wait)
        .unwrap_or(STARTUP_WAIT);

    while count < ms_wait {
        if let Ok(Some(status)) = validator_handle.try_wait() {
            return Err(anyhow!(
                "`surfpool` exited during startup with {status} - see the stderr output above. \
                 Common causes: port {port} in use, missing deploy artifacts in `target/deploy/`, \
                 invalid Anchor.toml config."
            ));
        }
        let r = client.get_latest_blockhash();
        if r.is_ok() {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
        count += 100;
    }

    if count >= ms_wait {
        eprintln!(
            "Unable to get latest blockhash. Surfpool validator does not look started. Check \
             .surfpool/logs/ directory for errors. Consider increasing [surfpool.startup_wait] in \
             Anchor.toml."
        );
        validator_handle.kill()?;
        std::process::exit(1);
    }

    loop {
        let resp = client
            .send::<RpcResponse<SurfnetInfoResponse>>(
                RpcRequest::Custom {
                    method: "surfnet_getSurfnetInfo",
                },
                serde_json::Value::Null,
            )?
            .value;

        // break out if all runbooks are completed
        if resp
            .runbook_executions
            .iter()
            .all(|ex| ex.completed_at.is_some())
        {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(500));
    }
    Ok(validator_handle)
}

fn start_solana_test_validator(
    cfg: &Config,
    test_validator: &Option<TestValidator>,
    flags: Option<Vec<String>>,
    test_log_stdout: bool,
) -> Result<Child> {
    let (test_ledger_directory, test_ledger_log_filename) =
        test_validator_file_paths(test_validator)?;

    // Start a validator for testing.
    let (test_validator_stdout, test_validator_stderr) = match test_log_stdout {
        true => {
            let test_validator_stdout_file =
                File::create(&test_ledger_log_filename).with_context(|| {
                    format!(
                        "Failed to create validator log file {}",
                        test_ledger_log_filename.display()
                    )
                })?;
            let test_validator_sterr_file = test_validator_stdout_file.try_clone()?;
            (
                Stdio::from(test_validator_stdout_file),
                Stdio::from(test_validator_sterr_file),
            )
        }
        false => (Stdio::inherit(), Stdio::inherit()),
    };

    let rpc_url = test_validator_rpc_url(test_validator);

    let rpc_port = cfg
        .test_validator
        .as_ref()
        .and_then(|test| test.validator.as_ref().map(|v| v.rpc_port))
        .unwrap_or(DEFAULT_RPC_PORT);
    if !portpicker::is_free(rpc_port) {
        return Err(anyhow!(
            "Your configured rpc port: {rpc_port} is already in use"
        ));
    }
    let faucet_port = cfg
        .test_validator
        .as_ref()
        .and_then(|test| test.validator.as_ref().and_then(|v| v.faucet_port))
        .unwrap_or(DEFAULT_FAUCET_PORT);
    if !portpicker::is_free(faucet_port) {
        return Err(anyhow!(
            "Your configured faucet port: {faucet_port} is already in use"
        ));
    }

    let mut validator_handle = std::process::Command::new("solana-test-validator")
        .arg("--ledger")
        .arg(test_ledger_directory)
        .arg("--mint")
        .arg(cfg.wallet_kp()?.pubkey().to_string())
        .args(flags.unwrap_or_default())
        .stdout(test_validator_stdout)
        .stderr(test_validator_stderr)
        .spawn()
        .map_err(|e| anyhow!("Failed to spawn `solana-test-validator`: {e}"))?;

    // Wait for the validator to be ready.
    let client = create_client(rpc_url);
    let mut count = 0;
    let ms_wait = test_validator
        .as_ref()
        .map(|test| test.startup_wait)
        .unwrap_or(STARTUP_WAIT);
    while count < ms_wait {
        let r = client.get_latest_blockhash();
        if r.is_ok() {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
        count += 100;
    }
    if count >= ms_wait {
        eprintln!(
            "Unable to get latest blockhash. Test validator does not look started. Check \
             {test_ledger_log_filename:?} for errors. Consider increasing [test.startup_wait] in \
             Anchor.toml."
        );
        validator_handle.kill()?;
        std::process::exit(1);
    }
    Ok(validator_handle)
}

// Return the URL that solana-test-validator should be running on given the
// configuration
fn test_validator_rpc_url(test_validator: &Option<TestValidator>) -> String {
    match test_validator {
        Some(TestValidator {
            validator: Some(validator),
            ..
        }) => format!("http://{}:{}", validator.bind_address, validator.rpc_port),
        _ => "http://127.0.0.1:8899".to_string(),
    }
}

// Returns the URL that surfpool should be running for the given configuration
fn surfpool_rpc_url(surfpool_config: &Option<SurfpoolConfig>) -> String {
    match surfpool_config {
        Some(SurfpoolConfig { host, rpc_port, .. }) => format!("http://{}:{}", host, rpc_port),
        _ => format!("http://{}:{}", SURFPOOL_HOST, DEFAULT_RPC_PORT),
    }
}

// Setup and return paths to the solana-test-validator ledger directory and log
// files given the configuration
fn test_validator_file_paths(test_validator: &Option<TestValidator>) -> Result<(PathBuf, PathBuf)> {
    let ledger_path = match test_validator {
        Some(TestValidator {
            validator: Some(validator),
            ..
        }) => PathBuf::from(&validator.ledger),
        _ => get_default_ledger_path(),
    };

    if !ledger_path.is_relative() {
        // Prevent absolute paths to avoid someone using / or similar, as the
        // directory gets removed
        eprintln!("Ledger directory {ledger_path:?} must be relative");
        std::process::exit(1);
    }
    if ledger_path.exists() {
        fs::remove_dir_all(&ledger_path).with_context(|| {
            format!(
                "Failed to remove ledger directory {}",
                ledger_path.display()
            )
        })?;
    }

    fs::create_dir_all(&ledger_path).with_context(|| {
        format!(
            "Failed to create ledger directory {}",
            ledger_path.display()
        )
    })?;

    let log_path = ledger_path.join("test-ledger-log.txt");
    Ok((ledger_path, log_path))
}

pub(crate) fn cluster_url(
    cfg: &Config,
    test_validator: &Option<TestValidator>,
    surfpool_config: &Option<SurfpoolConfig>,
) -> String {
    let is_localnet = cfg.provider.cluster == Cluster::Localnet;
    match is_localnet {
        // Cluster is Localnet, determine which validator to use
        true => match &cfg.validator {
            Some(ValidatorType::Surfpool) => surfpool_rpc_url(surfpool_config),
            Some(ValidatorType::Legacy) | None => test_validator_rpc_url(test_validator),
        },
        false => cfg.provider.cluster.url().to_string(),
    }
}

fn clean(cfg_override: &ConfigOverride) -> Result<()> {
    // Get workspace root - either from Anchor.toml or use current directory
    let workspace_root = if let Ok(Some(cfg)) = Config::discover(cfg_override) {
        cfg.path()
            .parent()
            .expect("Invalid Anchor.toml")
            .to_path_buf()
    } else {
        // No Anchor.toml - use current directory for Cargo workspace
        std::env::current_dir()?
    };

    let dot_anchor_dir = workspace_root.join(".anchor");
    let target_dir = crate::target_dir()?;
    let deploy_dir = target_dir.join("deploy");

    if dot_anchor_dir.exists() {
        fs::remove_dir_all(&dot_anchor_dir)
            .map_err(|e| anyhow!("Could not remove directory {:?}: {}", dot_anchor_dir, e))?;
    }

    if target_dir.exists() {
        for entry in fs::read_dir(target_dir)? {
            let path = entry?.path();
            if path.is_dir() && path != deploy_dir {
                fs::remove_dir_all(&path)
                    .map_err(|e| anyhow!("Could not remove directory {}: {}", path.display(), e))?;
            } else if path.is_file() {
                fs::remove_file(&path)
                    .map_err(|e| anyhow!("Could not remove file {}: {}", path.display(), e))?;
            }
        }
    } else {
        println!("skipping target directory: not found")
    }

    if deploy_dir.exists() {
        for file in fs::read_dir(deploy_dir)? {
            let path = file?.path();
            if path.extension() != Some(&OsString::from("json")) {
                fs::remove_file(&path)
                    .map_err(|e| anyhow!("Could not remove file {}: {}", path.display(), e))?;
            }
        }
    } else {
        println!("skipping deploy directory: not found")
    }

    Ok(())
}

fn deploy(
    cfg_override: &ConfigOverride,
    program_name: Option<String>,
    program_keypair: Option<PathBuf>,
    verifiable: bool,
    no_idl: bool,
    solana_args: Vec<String>,
) -> Result<()> {
    // Execute the code within the workspace
    with_workspace(cfg_override, |cfg| -> Result<()> {
        let url = cluster_url(cfg, &cfg.test_validator, &cfg.surfpool_config);
        let keypair = cfg.provider.wallet.to_string();

        cfg.run_hooks(HookType::PreDeploy)?;
        // Deploy the programs.
        println!("Deploying cluster: {url}");
        println!("Upgrade authority: {keypair}");

        for program in cfg.get_programs(program_name)? {
            let binary_path = program.binary_path(verifiable)?;

            println!("Deploying program {:?}...", program.lib_name);
            println!("Program path: {}...", binary_path.display());

            let program_keypair_filepath = match program_keypair.as_ref() {
                Some(path) => path.clone(),
                None => program.keypair_file()?.path().clone(),
            };

            // Deploy using our native implementation
            program::program_deploy(
                cfg_override,
                Some(strip_workspace_prefix(binary_path)),
                None, // program_name - not needed since we have filepath
                Some(strip_workspace_prefix(program_keypair_filepath)),
                None,  // upgrade_authority - uses wallet from config
                None,  // program_id - derived from program_keypair
                None,  // buffer
                None,  // max_len
                false, // use_rpc
                no_idl,
                false, // make_final
                solana_args.clone(),
            )?;
        }

        println!("Deploy success");
        cfg.run_hooks(HookType::PostDeploy)?;

        Ok(())
    })?
}

fn upgrade(
    cfg_override: &ConfigOverride,
    program_id: Pubkey,
    program_filepath: PathBuf,
    max_retries: u32,
    solana_args: Vec<String>,
) -> Result<()> {
    // Use our native upgrade implementation
    program::program_upgrade(
        cfg_override,
        program_id,
        Some(program_filepath),
        None, // program_name - not needed since we have filepath
        None, // buffer
        None, // upgrade_authority - uses wallet from config
        max_retries,
        false, // use_rpc
        solana_args,
    )
}

fn migrate(cfg_override: &ConfigOverride) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        println!("Running migration deploy script");

        let url = cluster_url(cfg, &cfg.test_validator, &cfg.surfpool_config);
        let cur_dir = std::env::current_dir()?;
        let migrations_dir = cur_dir.join("migrations");
        let deploy_ts = Path::new("deploy.ts");

        let use_ts = Path::new("tsconfig.json").exists() && migrations_dir.join(deploy_ts).exists();

        if !Path::new(".anchor").exists() {
            fs::create_dir(".anchor")?;
        }
        std::env::set_current_dir(".anchor")?;

        let exit = if use_ts {
            let module_path = migrations_dir.join(deploy_ts);
            let deploy_script_host_str =
                template::deploy_ts_script_host(&url, &module_path.display().to_string());
            fs::write(deploy_ts, deploy_script_host_str)?;

            let pkg_manager_cmd =
                resolve_package_manager(cfg.toolchain.package_manager.clone())?.to_string();

            std::process::Command::new(pkg_manager_cmd)
                .args([
                    "run",
                    "ts-node",
                    &fs::canonicalize(deploy_ts)?.to_string_lossy(),
                ])
                .env("ANCHOR_WALLET", cfg.provider.wallet.to_string())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()?
        } else {
            let deploy_js = deploy_ts.with_extension("js");
            let module_path = migrations_dir.join(&deploy_js);
            let deploy_script_host_str =
                template::deploy_js_script_host(&url, &module_path.display().to_string());
            fs::write(&deploy_js, deploy_script_host_str)?;

            std::process::Command::new("node")
                .arg(&deploy_js)
                .env("ANCHOR_WALLET", cfg.provider.wallet.to_string())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .output()?
        };

        if !exit.status.success() {
            eprintln!("Deploy failed.");
            std::process::exit(exit.status.code().unwrap());
        }

        println!("Deploy complete.");
        Ok(())
    })?
}

fn set_workspace_dir_or_exit() {
    // First try to find Anchor workspace
    let d = match Config::discover(&ConfigOverride::default()) {
        Err(err) => {
            println!("Workspace configuration error: {err}");
            std::process::exit(1);
        }
        Ok(d) => d,
    };

    match d {
        None => {
            // No Anchor.toml found - check for Cargo workspace with Solana programs
            let current_dir = match std::env::current_dir() {
                Ok(dir) => dir,
                Err(_) => {
                    println!("Unable to determine current directory");
                    std::process::exit(1);
                }
            };

            let cargo_toml_path = current_dir.join("Cargo.toml");
            if !cargo_toml_path.exists() {
                println!(
                    "Not in a Solana workspace. This command requires either Anchor.toml or a \
                     Cargo workspace with Solana programs."
                );
                std::process::exit(1);
            }

            // Check if this is a workspace and has Solana programs
            match program::discover_solana_programs(None) {
                Ok(programs) if !programs.is_empty() => {
                    // Found Solana programs in Cargo workspace - stay in current directory
                    // (already in the right place)
                }
                _ => {
                    println!(
                        "Not in a Solana workspace. This command requires either Anchor.toml or a \
                         Cargo workspace with Solana programs."
                    );
                    std::process::exit(1);
                }
            }
        }
        Some(cfg) => {
            // Found Anchor.toml - change to workspace root
            match cfg.path().parent() {
                None => {
                    println!("Unable to make new program");
                }
                Some(parent) => {
                    if std::env::set_current_dir(parent).is_err() {
                        println!(
                            "Not in a Solana workspace. This command requires either Anchor.toml \
                             or a Cargo workspace with Solana programs."
                        );
                        std::process::exit(1);
                    }
                }
            };
        }
    }
}

fn airdrop(cfg_override: &ConfigOverride, amount: f64, pubkey: Option<Pubkey>) -> Result<()> {
    // Get cluster URL and wallet path
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;

    // Create RPC client with confirmed commitment
    let client = RpcClient::new_with_commitment(cluster_url, CommitmentConfig::confirmed());

    // Determine recipient
    let recipient_pubkey = if let Some(pubkey) = pubkey {
        pubkey
    } else {
        // Load keypair from wallet path and get pubkey
        let keypair = Keypair::read_from_file(&wallet_path)
            .map_err(|e| anyhow!("Failed to read keypair from {}: {}", wallet_path, e))?;
        keypair.pubkey()
    };

    // Convert SOL to lamports
    let lamports = (amount * 1_000_000_000.0) as u64;
    let starting_balance = client
        .get_balance_with_commitment(&recipient_pubkey, CommitmentConfig::confirmed())?
        .value;

    // Get recent blockhash for airdrop
    let recent_blockhash = client
        .get_latest_blockhash()
        .map_err(|e| anyhow!("Failed to get recent blockhash: {}", e))?;

    // Request airdrop with blockhash
    println!("Requesting airdrop of {} SOL...", amount);
    let signature = client
        .request_airdrop_with_blockhash(&recipient_pubkey, lamports, &recent_blockhash)
        .map_err(|e| anyhow!("Airdrop request failed: {}", e))?;

    println!("Signature: {}", signature);

    // Wait for confirmation with the same blockhash used for the airdrop
    client
        .confirm_transaction_with_spinner(&signature, &recent_blockhash, client.commitment())
        .map_err(|e| anyhow!("Transaction confirmation failed: {}", e))?;

    println!("Airdrop confirmed!");

    // Get and display the new balance
    let balance = wait_for_airdrop_balance(&client, &recipient_pubkey, starting_balance, lamports)?;
    println!("Balance: {}", format_sol(balance));

    Ok(())
}

fn wait_for_airdrop_balance(
    client: &RpcClient,
    recipient_pubkey: &Pubkey,
    starting_balance: u64,
    lamports: u64,
) -> Result<u64> {
    let expected_balance = starting_balance.saturating_add(lamports);
    let mut last_balance = starting_balance;

    for attempt in 0..10 {
        let balance = client
            .get_balance_with_commitment(recipient_pubkey, CommitmentConfig::confirmed())?
            .value;
        if balance >= expected_balance {
            return Ok(balance);
        }
        last_balance = balance;

        if attempt < 9 {
            std::thread::sleep(std::time::Duration::from_millis(500));
        }
    }

    eprintln!(
        "warning: confirmed balance has not reflected the airdrop yet; showing latest confirmed \
         balance"
    );
    Ok(last_balance)
}

fn cluster(_cmd: ClusterCommand) -> Result<()> {
    println!("Cluster Endpoints:\n");
    println!("* Mainnet - https://api.mainnet-beta.solana.com");
    println!("* Devnet  - https://api.devnet.solana.com");
    println!("* Testnet - https://api.testnet.solana.com");
    Ok(())
}

fn config_cmd(cfg_override: &ConfigOverride, cmd: ConfigCommand) -> Result<()> {
    match cmd {
        ConfigCommand::Get => config_get(cfg_override),
        ConfigCommand::Set { url, keypair } => config_set(cfg_override, url, keypair),
    }
}

fn config_get(cfg_override: &ConfigOverride) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        println!("Anchor Configuration:");
        println!();
        println!("Cluster: {}", cfg.provider.cluster.url());
        println!("Wallet:  {}", cfg.provider.wallet);
        Ok(())
    })?
}

fn config_set(
    cfg_override: &ConfigOverride,
    url: Option<String>,
    keypair: Option<PathBuf>,
) -> Result<()> {
    // Find the Anchor.toml file
    let anchor_toml_path = match Config::discover(cfg_override)? {
        Some(cfg) => cfg.path().parent().unwrap().join("Anchor.toml"),
        None => bail!("Not in an Anchor workspace"),
    };

    // Read the current Anchor.toml
    let mut toml_content =
        fs::read_to_string(&anchor_toml_path).context("Failed to read Anchor.toml")?;
    let mut toml_doc: toml::Value =
        toml::from_str(&toml_content).context("Failed to parse Anchor.toml")?;

    let mut updated = false;

    // Update cluster URL if provided
    if let Some(cluster_url) = url {
        let expanded_url = match cluster_url.as_str() {
            "m" => "https://api.mainnet-beta.solana.com".to_string(),
            "d" => "https://api.devnet.solana.com".to_string(),
            "t" => "https://api.testnet.solana.com".to_string(),
            "l" => "http://127.0.0.1:8899".to_string(),
            _ => cluster_url,
        };

        if let Some(provider) = toml_doc.get_mut("provider").and_then(|v| v.as_table_mut()) {
            provider.insert(
                "cluster".to_string(),
                toml::Value::String(expanded_url.clone()),
            );
            println!("Updated cluster to: {}", expanded_url);
            updated = true;
        }
    }

    // Update wallet path if provided
    if let Some(keypair_path) = keypair {
        let expanded_path = shellexpand::tilde(&keypair_path.to_string_lossy()).to_string();

        // Check if the wallet file exists
        if !Path::new(&expanded_path).exists() {
            eprintln!("Warning: Wallet file does not exist: {}", expanded_path);
        }

        if let Some(provider) = toml_doc.get_mut("provider").and_then(|v| v.as_table_mut()) {
            provider.insert(
                "wallet".to_string(),
                toml::Value::String(expanded_path.clone()),
            );
            println!("Updated wallet to: {}", expanded_path);
            updated = true;
        }
    }

    if updated {
        // Write the updated config back to Anchor.toml
        toml_content =
            toml::to_string_pretty(&toml_doc).context("Failed to serialize Anchor.toml")?;
        fs::write(&anchor_toml_path, toml_content).context("Failed to write Anchor.toml")?;
        println!("\nConfiguration updated successfully!");
    } else {
        println!("No changes made. Use --url or --keypair to update settings.");
    }

    Ok(())
}

fn shell(cfg_override: &ConfigOverride) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        let programs = {
            // Create idl map from all workspace programs.
            let mut idls: HashMap<String, Idl> = cfg
                .read_all_programs()?
                .iter()
                .filter(|program| program.idl.is_some())
                .map(|program| {
                    (
                        program.idl.as_ref().unwrap().metadata.name.clone(),
                        program.idl.clone().unwrap(),
                    )
                })
                .collect();
            // Insert all manually specified idls into the idl map.
            if let Some(programs) = cfg.programs.get(&cfg.provider.cluster) {
                let _ = programs
                    .iter()
                    .map(|(name, pd)| {
                        if let Some(idl_fp) = &pd.idl {
                            let file_str =
                                fs::read_to_string(idl_fp).expect("Unable to read IDL file");
                            let idl = serde_json::from_str(&file_str).expect("Idl not readable");
                            idls.insert(name.clone(), idl);
                        }
                    })
                    .collect::<Vec<_>>();
            }

            // Finalize program list with all programs with IDLs.
            match cfg.programs.get(&cfg.provider.cluster) {
                None => Vec::new(),
                Some(programs) => programs
                    .iter()
                    .filter_map(|(name, program_deployment)| {
                        Some(ProgramWorkspace {
                            name: name.to_string(),
                            program_id: program_deployment.address,
                            idl: match idls.get(name) {
                                None => return None,
                                Some(idl) => idl.clone(),
                            },
                        })
                    })
                    .collect::<Vec<ProgramWorkspace>>(),
            }
        };
        let url = cluster_url(cfg, &cfg.test_validator, &cfg.surfpool_config);
        let js_code = template::node_shell(&url, &cfg.provider.wallet.to_string(), programs)?;
        let mut child = std::process::Command::new("node")
            .args(["-e", &js_code, "-i", "--experimental-repl-await"])
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()
            .map_err(|e| anyhow::format_err!("{}", e))?;

        if !child.wait()?.success() {
            println!("Error running node shell");
            return Ok(());
        }
        Ok(())
    })?
}

fn run(cfg_override: &ConfigOverride, script: String, script_args: Vec<String>) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        let url = cluster_url(cfg, &cfg.test_validator, &cfg.surfpool_config);
        let script_cmd = cfg.scripts.get(&script).ok_or_else(|| {
            let mut available_scripts: Vec<String> = cfg.scripts.keys().cloned().collect();
            available_scripts.sort();
            if available_scripts.is_empty() {
                anyhow!("Script '{script}' not found. No scripts defined in Anchor.toml.")
            } else {
                anyhow!(
                    "Script '{script}' not found.\n\nAvailable scripts:\n  {}",
                    available_scripts.join("\n  ")
                )
            }
        })?;
        let script_with_args = format!("{script_cmd} {}", script_args.join(" "));
        let exit = std::process::Command::new("bash")
            .arg("-c")
            .arg(&script_with_args)
            .env("ANCHOR_PROVIDER_URL", url)
            .env("ANCHOR_WALLET", cfg.provider.wallet.to_string())
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .output()
            .unwrap();
        if !exit.status.success() {
            std::process::exit(exit.status.code().unwrap_or(1));
        }
        Ok(())
    })?
}

fn keys(cfg_override: &ConfigOverride, cmd: KeysCommand) -> Result<()> {
    match cmd {
        KeysCommand::List => keys_list(cfg_override),
        KeysCommand::Sync { program_name } => keys_sync(cfg_override, program_name),
    }
}

fn keys_list(cfg_override: &ConfigOverride) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        for program in cfg.read_all_programs()? {
            let pubkey = program.pubkey()?;
            println!("{}: {}", program.lib_name, pubkey);
        }
        Ok(())
    })?
}

/// Sync program `declare_id!` pubkeys with the pubkey from `target/deploy/<KEYPAIR>.json`.
fn keys_sync(cfg_override: &ConfigOverride, program_name: Option<String>) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        let declare_id_regex = RegexBuilder::new(r#"^(([\w]+::)*)declare_id!\("(\w*)"\)"#)
            .multi_line(true)
            .build()
            .unwrap();

        let cfg_cluster = cfg.provider.cluster.to_owned();
        println!("Syncing program ids for the configured cluster ({cfg_cluster})\n");

        let mut changed_src = false;
        for program in cfg.get_programs(program_name)? {
            // Get the pubkey from the keypair file
            let actual_program_id = program.pubkey()?.to_string();

            // Handle declaration in program files
            let src_path = program.path.join("src");
            let files_to_check = vec![src_path.join("lib.rs"), src_path.join("id.rs")];

            for path in files_to_check {
                let mut content = match fs::read_to_string(&path) {
                    Ok(content) => content,
                    Err(_) => continue,
                };

                let incorrect_program_id = declare_id_regex
                    .captures(&content)
                    .and_then(|captures| captures.get(3))
                    .filter(|program_id_match| program_id_match.as_str() != actual_program_id);
                if let Some(program_id_match) = incorrect_program_id {
                    println!("Found incorrect program id declaration in {path:?}");

                    // Update the program id
                    content.replace_range(program_id_match.range(), &actual_program_id);
                    fs::write(&path, content)?;

                    changed_src = true;
                    println!("Updated to {actual_program_id}\n");
                    break;
                }
            }

            // Handle declaration in Anchor.toml
            'outer: for (cluster, programs) in &mut cfg.programs {
                // Only change if the configured cluster matches the program's cluster
                if cluster != &cfg_cluster {
                    continue;
                }

                for (name, deployment) in programs {
                    // Skip other programs
                    if name != &program.lib_name {
                        continue;
                    }

                    if deployment.address.to_string() != actual_program_id {
                        println!(
                            "Found incorrect program id declaration in Anchor.toml for the \
                             program `{name}`"
                        );

                        // Update the program id
                        deployment.address = Pubkey::try_from(actual_program_id.as_str()).unwrap();
                        fs::write(cfg.path(), cfg.to_string())?;

                        println!("Updated to {actual_program_id}\n");
                        break 'outer;
                    }
                }
            }
        }

        println!("All program id declarations are synced.");
        if changed_src {
            println!("Please rebuild the program to update the generated artifacts.")
        }

        Ok(())
    })?
}

enum ProgramIdComparison {
    Same,
    Mismatch {
        lib_name: String,
        actual_id: String,
        declared_id: String,
    },
}

/// Check if there's a mismatch between the program keypair and the `declare_id!` in the source code.
/// Returns `false` if there is a mismatch.
fn check_program_id_mismatch(
    cfg: &WithPath<Config>,
    program_name: Option<String>,
) -> Result<ProgramIdComparison> {
    let declare_id_regex = RegexBuilder::new(r#"^(([\w]+::)*)declare_id!\("(\w*)"\)"#)
        .multi_line(true)
        .build()
        .unwrap();

    for program in cfg.get_programs(program_name)? {
        // Get the pubkey from the keypair file
        let actual_program_id = program.pubkey()?.to_string();

        // Check declaration in program files
        let src_path = program.path.join("src");
        let files_to_check = vec![src_path.join("lib.rs"), src_path.join("id.rs")];

        for path in files_to_check {
            let content = match fs::read_to_string(&path) {
                Ok(content) => content,
                Err(_) => continue,
            };

            let incorrect_program_id = declare_id_regex
                .captures(&content)
                .and_then(|captures| captures.get(3))
                .filter(|program_id_match| program_id_match.as_str() != actual_program_id);

            if let Some(program_id_match) = incorrect_program_id {
                return Ok(ProgramIdComparison::Mismatch {
                    lib_name: program.lib_name,
                    actual_id: actual_program_id,
                    declared_id: program_id_match.as_str().to_string(),
                });
            }
        }
    }

    Ok(ProgramIdComparison::Same)
}

#[allow(clippy::too_many_arguments)]
fn localnet(
    cfg_override: &ConfigOverride,
    skip_build: bool,
    skip_deploy: bool,
    skip_lint: bool,
    ignore_keys: bool,
    validator_type: ValidatorType,
    env_vars: Vec<String>,
    cargo_args: Vec<String>,
) -> Result<()> {
    with_workspace(cfg_override, |cfg| -> Result<()> {
        // Build if needed.
        if !skip_build {
            build(
                cfg_override,
                false,
                None,
                None,
                false,
                skip_lint,
                ignore_keys,
                None,
                None,
                None,
                BootstrapMode::None,
                None,
                None,
                env_vars,
                cargo_args,
                false,
            )?;
        }

        let generated_accounts = generated_validator_accounts(cfg, &cfg.test_validator)?;
        let validator_handle: Option<Child> = match validator_type {
            ValidatorType::Surfpool => {
                let full_simnet_mode = true;
                let flags = Some(surfpool_flags(
                    cfg,
                    &cfg.surfpool_config,
                    full_simnet_mode,
                    skip_deploy,
                    None,
                    &generated_accounts,
                )?);
                Some(start_surfpool_validator(
                    flags,
                    &cfg.surfpool_config,
                    full_simnet_mode,
                )?)
            }
            ValidatorType::Legacy => {
                let flags = Some(validator_flags(
                    cfg,
                    &cfg.test_validator,
                    skip_deploy,
                    &generated_accounts,
                )?);
                Some(start_solana_test_validator(
                    cfg,
                    &cfg.test_validator,
                    flags,
                    false,
                )?)
            }
        };

        // Setup log reader.
        let url = test_validator_rpc_url(&cfg.test_validator);
        let log_streams = match stream_logs(cfg, &url) {
            Ok(streams) => {
                println!(
                    "Log streams set up successfully ({} streams)",
                    streams.len()
                );
                Some(streams)
            }
            Err(e) => {
                eprintln!("Warning: Failed to setup program log streaming: {:#}", e);
                eprintln!("  Program logs will still be visible in the validator output.");
                None
            }
        };

        std::io::stdin().lock().lines().next().unwrap().unwrap();

        // Check all errors and shut down.
        if let Some(mut handle) = validator_handle {
            if let Err(err) = handle.kill() {
                println!("Failed to kill subprocess {}: {}", handle.id(), err);
            }
        }

        // Explicitly shutdown log streams - closes WebSocket subscriptions
        if let Some(log_streams) = log_streams {
            for handle in log_streams {
                handle.shutdown();
            }
        }

        Ok(())
    })?
}

/// Return the cargo build artifacts directory. The successful result is
/// cached.
pub fn target_dir() -> Result<&'static Path> {
    static TARGET_DIR: OnceLock<PathBuf> = OnceLock::new();
    if let Some(path) = TARGET_DIR.get() {
        return Ok(path.as_path());
    }
    let path = target_dir_no_cache()?;
    let _ = TARGET_DIR.set(path);
    Ok(TARGET_DIR.get().expect("just set").as_path())
}

/// Return the cargo build artifacts directory.
fn target_dir_no_cache() -> Result<PathBuf> {
    // `cargo metadata` produces a JSON blob from which we extract the
    // `target_directory` field.
    let output = std::process::Command::new("cargo")
        .args(["metadata", "--no-deps", "--format-version=1"])
        .output()
        .context("Failed to execute 'cargo metadata'")?;

    if !output.status.success() {
        let stderr_msg = String::from_utf8_lossy(&output.stderr);
        bail!("'cargo metadata' failed with: {stderr_msg}");
    }

    #[derive(Deserialize)]
    struct CargoMetadata {
        target_directory: PathBuf,
    }

    let metadata: CargoMetadata = serde_json::from_slice(&output.stdout)
        .context("Failed to parse 'cargo metadata' output")?;

    Ok(metadata.target_directory)
}

// with_workspace ensures the current working directory is always the top level
// workspace directory, i.e., where the `Anchor.toml` file is located, before
// and after the closure invocation.
//
// The closure passed into this function must never change the working directory
// to be outside the workspace. Doing so will have undefined behavior.
pub(crate) fn with_workspace<R>(
    cfg_override: &ConfigOverride,
    f: impl FnOnce(&mut WithPath<Config>) -> R,
) -> Result<R> {
    set_workspace_dir_or_exit();

    let mut cfg = Config::discover(cfg_override)
        .map_err(|e| anyhow!("Workspace configuration error: {}", e))?
        .ok_or_else(|| anyhow!("This command requires an Anchor workspace."))?;

    let r = f(&mut cfg);

    set_workspace_dir_or_exit();

    Ok(r)
}

fn is_hidden(entry: &walkdir::DirEntry) -> bool {
    entry
        .file_name()
        .to_str()
        .map(|s| s == "." || s.starts_with('.') || s == "target")
        .unwrap_or(false)
}

fn logs_websocket_url(cfg_override: &ConfigOverride, cluster_url: &str) -> String {
    let ws_scheme_url = cluster_url
        .replace("https://", "wss://")
        .replace("http://", "ws://");

    let is_local = cluster_url.contains("localhost") || cluster_url.contains("127.0.0.1");
    if !is_local {
        return ws_scheme_url;
    }

    let default_ws_port = extract_url_port(cluster_url)
        .map(|port| port.saturating_add(1))
        .unwrap_or(DEFAULT_RPC_PORT + 1);
    let ws_port = Config::discover(cfg_override)
        .ok()
        .flatten()
        .and_then(|cfg| {
            cfg.surfpool_config
                .as_ref()
                .and_then(|surfpool| surfpool.ws_port)
        })
        .unwrap_or(default_ws_port);

    replace_url_port(&ws_scheme_url, ws_port)
}

fn extract_url_port(url: &str) -> Option<u16> {
    let (_, after_scheme) = url.split_once("://")?;
    let host_port_end = after_scheme.find('/').unwrap_or(after_scheme.len());
    let (_, port_str) = after_scheme[..host_port_end].rsplit_once(':')?;
    port_str.parse().ok()
}

fn replace_url_port(url: &str, new_port: u16) -> String {
    let Some((scheme, rest)) = url.split_once("://") else {
        return url.to_string();
    };
    let (host_port_part, tail) = match rest.find('/') {
        Some(index) => (&rest[..index], &rest[index..]),
        None => (rest, ""),
    };
    let host = host_port_part
        .rsplit_once(':')
        .map(|(host, _)| host)
        .unwrap_or(host_port_part);
    format!("{scheme}://{host}:{new_port}{tail}")
}

fn get_node_version() -> Result<Version> {
    let node_version = std::process::Command::new("node")
        .arg("--version")
        .stderr(Stdio::inherit())
        .output()
        .map_err(|e| anyhow::format_err!("node failed: {}", e))?;
    parse_node_version(std::str::from_utf8(&node_version.stdout)?)
}

fn parse_node_version(output: &str) -> Result<Version> {
    let trimmed = output.trim();
    let without_v = trimmed.strip_prefix('v').unwrap_or(trimmed);
    Version::parse(without_v).map_err(Into::into)
}

/// Default re-sign attempts when blockhashes expire mid-deploy.
/// Matches Agave's `solana program deploy` default (agave/cli/src/program.rs).
/// Each blockhash window is ~60s, so 5 → ~5 minutes of resign budget.
pub const DEFAULT_MAX_SIGN_ATTEMPTS: usize = 5;

fn add_recommended_deployment_solana_args(
    client: &RpcClient,
    args: Vec<String>,
    write_locked_accounts: &[Pubkey],
) -> Result<Vec<String>> {
    let mut augmented_args = args.clone();

    // If no priority fee is provided, calculate a recommended fee based on recent txs.
    if !args.contains(&"--with-compute-unit-price".to_string()) {
        let priority_fee = get_recommended_micro_lamport_fee(client, write_locked_accounts);
        augmented_args.push("--with-compute-unit-price".to_string());
        augmented_args.push(priority_fee.to_string());
    }

    if !args.contains(&"--max-sign-attempts".to_string()) {
        augmented_args.push("--max-sign-attempts".to_string());
        augmented_args.push(DEFAULT_MAX_SIGN_ATTEMPTS.to_string());
    }

    // Note: `--buffer` injection is handled by callers (program_deploy /
    // program_upgrade) so the path can be scoped per program
    // (`target/deploy/{name}-upgrade-buffer.json`). Doing it here would either
    // collide across concurrent program deploys or require threading
    // program_name down into this fee/sign-attempts helper.

    Ok(augmented_args)
}

fn get_node_dns_option() -> &'static str {
    let Ok(version) = get_node_version() else {
        return "";
    };
    let req = VersionReq::parse(">=16.4.0").unwrap();
    if req.matches(&version) {
        "--dns-result-order=ipv4first"
    } else {
        ""
    }
}

// Remove the current workspace directory if it prefixes a string.
// This is used as a workaround for the Solana CLI using the uriparse crate to
// parse args but not handling percent encoding/decoding when using the path as
// a local filesystem path. Removing the workspace prefix handles most/all cases
// of spaces in keypair/binary paths, but this should be fixed in the Solana CLI
// and removed here.
fn strip_workspace_prefix(absolute_path: PathBuf) -> PathBuf {
    let workspace_prefix = std::env::current_dir().unwrap();
    absolute_path
        .strip_prefix(&workspace_prefix)
        .unwrap_or(&absolute_path)
        .into()
}

/// Create a new [`RpcClient`] with `confirmed` commitment level instead of the default(finalized).
pub(crate) fn create_client<U: ToString>(url: U) -> RpcClient {
    RpcClient::new_with_commitment(url, CommitmentConfig::confirmed())
}

fn address(cfg_override: &ConfigOverride) -> Result<()> {
    let (_cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;

    // Load keypair and get pubkey
    let keypair = Keypair::read_from_file(&wallet_path)
        .map_err(|e| anyhow!("Failed to read keypair from {}: {}", wallet_path, e))?;

    // Print the public key
    println!("{}", keypair.pubkey());

    Ok(())
}

fn balance(cfg_override: &ConfigOverride, pubkey: Option<Pubkey>, lamports: bool) -> Result<()> {
    let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;

    // Create RPC client
    let client = RpcClient::new(cluster_url);

    // Determine which account to check
    let account_pubkey = if let Some(pubkey) = pubkey {
        pubkey
    } else {
        // Load keypair from wallet path and get pubkey
        let keypair = Keypair::read_from_file(&wallet_path)
            .map_err(|e| anyhow!("Failed to read keypair from {}: {}", wallet_path, e))?;
        keypair.pubkey()
    };

    // Get balance
    let balance = client.get_balance(&account_pubkey)?;

    // Format and display output
    if lamports {
        println!("{}", balance);
    } else {
        println!("{}", format_sol(balance));
    }

    Ok(())
}

fn epoch(cfg_override: &ConfigOverride) -> Result<()> {
    let (cluster_url, _wallet_path) = get_cluster_and_wallet(cfg_override)?;

    // Create RPC client
    let client = RpcClient::new(cluster_url);

    // Get epoch info
    let epoch_info = client.get_epoch_info()?;

    // Print just the epoch number
    println!("{}", epoch_info.epoch);

    Ok(())
}

fn epoch_info(cfg_override: &ConfigOverride) -> Result<()> {
    let (cluster_url, _wallet_path) = get_cluster_and_wallet(cfg_override)?;

    // Create RPC client
    let client = RpcClient::new(cluster_url);

    // Get epoch info
    let epoch_info = client.get_epoch_info()?;

    // Calculate epoch slot range
    let first_slot_in_epoch = epoch_info.absolute_slot - epoch_info.slot_index;
    let last_slot_in_epoch = first_slot_in_epoch + epoch_info.slots_in_epoch;

    // Calculate completion stats
    let epoch_completed_percent =
        epoch_info.slot_index as f64 / epoch_info.slots_in_epoch as f64 * 100.0;
    let remaining_slots = epoch_info.slots_in_epoch - epoch_info.slot_index;

    // Display epoch information (matching Solana CLI format)
    println!("Block height: {}", epoch_info.block_height);
    println!("Slot: {}", epoch_info.absolute_slot);
    println!("Epoch: {}", epoch_info.epoch);

    if let Some(tx_count) = epoch_info.transaction_count {
        println!("Transaction Count: {}", tx_count);
    }

    println!(
        "Epoch Slot Range: [{}..{})",
        first_slot_in_epoch, last_slot_in_epoch
    );
    println!("Epoch Completed Percent: {:>3.3}%", epoch_completed_percent);
    println!(
        "Epoch Completed Slots: {}/{} ({} remaining)",
        epoch_info.slot_index, epoch_info.slots_in_epoch, remaining_slots
    );

    // Try to calculate epoch completed time
    // Get average slot time from performance samples (aggregate up to 60 samples)
    if let Ok(samples) = client.get_recent_performance_samples(Some(60)) {
        // Aggregate all samples to calculate average slot time
        let (total_slots, total_secs) =
            samples.iter().fold((0u64, 0u64), |(slots, secs), sample| {
                (
                    slots.saturating_add(sample.num_slots),
                    secs.saturating_add(sample.sample_period_secs as u64),
                )
            });

        if total_slots > 0 {
            let avg_slot_time_ms = (total_secs * 1000) / total_slots;

            // Calculate time_remaining using average slot time (always estimated)
            let remaining_secs = (remaining_slots * avg_slot_time_ms) / 1000;

            // Calculate time_elapsed - try actual block times first, then estimate
            // Get the first actual block in the epoch and adjust for slot differences
            let start_block_time = client
                .get_blocks_with_limit(first_slot_in_epoch, 1)
                .ok()
                .and_then(|slots| slots.first().cloned())
                .and_then(|first_actual_block| {
                    client.get_block_time(first_actual_block).ok().map(|time| {
                        // Adjust backwards if first actual block is after expected start
                        let slot_diff = first_actual_block.saturating_sub(first_slot_in_epoch);
                        let time_adjustment = (slot_diff * avg_slot_time_ms / 1000) as i64;
                        time.saturating_sub(time_adjustment)
                    })
                });

            let current_block_time = client.get_block_time(epoch_info.absolute_slot).ok();

            let (elapsed_secs, is_estimated) = if let (Some(start_time), Some(current_time)) =
                (start_block_time, current_block_time)
            {
                // Use actual block times for elapsed
                ((current_time - start_time) as u64, false)
            } else {
                // Estimate elapsed using average slot time
                ((epoch_info.slot_index * avg_slot_time_ms) / 1000, true)
            };

            // Total time = elapsed + remaining
            let total_secs = elapsed_secs + remaining_secs;

            let estimated_marker = if is_estimated { "*" } else { "" };
            println!(
                "Epoch Completed Time: {}{}/{} ({} remaining)",
                format_duration_secs(elapsed_secs),
                estimated_marker,
                format_duration_secs(total_secs),
                format_duration_secs(remaining_secs)
            );
        }
    }

    Ok(())
}

/// Format seconds into human-readable duration (e.g., "1day 5h 49m 8s")
fn format_duration_secs(total_seconds: u64) -> String {
    let seconds = total_seconds % 60;
    let total_minutes = total_seconds / 60;
    let minutes = total_minutes % 60;
    let total_hours = total_minutes / 60;
    let hours = total_hours % 24;
    let days = total_hours / 24;

    let mut parts = Vec::new();
    if days > 0 {
        parts.push(format!("{}day", days));
    }
    if hours > 0 {
        parts.push(format!("{}h", hours));
    }
    if minutes > 0 {
        parts.push(format!("{}m", minutes));
    }
    if seconds > 0 || parts.is_empty() {
        parts.push(format!("{}s", seconds));
    }

    parts.join(" ")
}

fn logs_subscribe(
    cfg_override: &ConfigOverride,
    include_votes: bool,
    address: Option<Vec<Pubkey>>,
) -> Result<()> {
    let (cluster_url, _wallet_path) = get_cluster_and_wallet(cfg_override)?;
    let ws_url = logs_websocket_url(cfg_override, &cluster_url);

    println!("Connecting to {}", ws_url);

    let filter = match (include_votes, address) {
        (true, Some(address)) => {
            RpcTransactionLogsFilter::Mentions(address.iter().map(|p| p.to_string()).collect())
        }
        (true, None) => RpcTransactionLogsFilter::AllWithVotes,
        (false, Some(address)) => {
            RpcTransactionLogsFilter::Mentions(address.iter().map(|p| p.to_string()).collect())
        }
        (false, None) => RpcTransactionLogsFilter::All,
    };

    let (_client, receiver) = PubsubClient::logs_subscribe(
        &ws_url,
        filter,
        RpcTransactionLogsConfig {
            commitment: cfg_override.commitment.map(|c| CommitmentConfig {
                commitment: c.into(),
            }),
        },
    )?;

    loop {
        match receiver.recv() {
            Ok(logs) => {
                println!("Transaction executed in slot {}:", logs.context.slot);
                println!("  Signature: {}", logs.value.signature);
                println!(
                    "  Status: {}",
                    logs.value
                        .err
                        .map(|err| err.to_string())
                        .unwrap_or_else(|| "Ok".to_string())
                );
                println!("  Log Messages:");
                for log in logs.value.logs {
                    println!("    {log}");
                }
            }
            Err(err) => {
                return Err(anyhow!("Disconnected: {err}"));
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        anchor_lang_idl::types::{
            IdlGenericArg, IdlInstructionAccount, IdlInstructionAccountItem, IdlPda, IdlSeed,
            IdlSeedAccount, IdlTypeDef, IdlTypeDefGeneric,
        },
        std::collections::{HashMap, HashSet},
        tempfile::tempdir,
    };

    #[test]
    fn test_init_accepts_anchor_version() {
        let opts =
            Opts::try_parse_from(["anchor", "init", "example", "--anchor-version", "v2"]).unwrap();

        let Command::Init { anchor_version, .. } = opts.command else {
            panic!("expected init command");
        };

        assert_eq!(anchor_version, AnchorVersion::V2);
    }

    #[test]
    fn test_new_accepts_anchor_version() {
        let opts =
            Opts::try_parse_from(["anchor", "new", "example", "--anchor-version", "v2"]).unwrap();

        let Command::New { anchor_version, .. } = opts.command else {
            panic!("expected new command");
        };

        assert_eq!(anchor_version, AnchorVersion::V2);
    }

    #[test]
    #[cfg(not(windows))]
    fn test_debugger_and_coverage_commands_parse() {
        let opts =
            Opts::try_parse_from(["anchor", "debugger", "initialize", "--skip-run"]).unwrap();
        let Command::Debugger {
            test_name,
            skip_run,
            ..
        } = opts.command
        else {
            panic!("expected debugger command");
        };
        assert_eq!(test_name.as_deref(), Some("initialize"));
        assert!(skip_run);

        let opts =
            Opts::try_parse_from(["anchor", "coverage", "--skip-run", "--output", "lcov.info"])
                .unwrap();
        let Command::Coverage {
            skip_run, output, ..
        } = opts.command
        else {
            panic!("expected coverage command");
        };
        assert!(skip_run);
        assert_eq!(output, "lcov.info");
    }

    #[test]
    fn test_validator_defaults_to_surfpool() {
        let opts = Opts::try_parse_from(["anchor", "test"]).unwrap();
        let Command::Test { validator, .. } = opts.command else {
            panic!("expected test command");
        };
        assert_eq!(validator, ValidatorType::Surfpool);

        let opts = Opts::try_parse_from(["anchor", "localnet"]).unwrap();
        let Command::Localnet { validator, .. } = opts.command else {
            panic!("expected localnet command");
        };
        assert_eq!(validator, ValidatorType::Surfpool);
    }

    #[test]
    fn test_codama_command_parses() {
        let opts = Opts::try_parse_from([
            "anchor",
            "codama",
            "generate",
            "-l",
            "rust,go",
            "-p",
            "clients",
            "target/idl/demo.json",
        ])
        .unwrap();
        let Command::Codama { subcmd } = opts.command else {
            panic!("expected codama command");
        };
        let codama::CodamaCommand::Generate {
            language,
            path,
            idl,
        } = subcmd
        else {
            panic!("expected codama generate command");
        };
        assert_eq!(language, vec![codama::Language::Rust, codama::Language::Go]);
        assert_eq!(path, "clients");
        assert_eq!(idl, "target/idl/demo.json");
    }

    #[test]
    #[should_panic(expected = "Anchor workspace name must be a valid Rust identifier.")]
    fn test_init_reserved_word() {
        init(
            &ConfigOverride {
                cluster: None,
                wallet: None,
                commitment: None,
            },
            "await".to_string(),
            true,
            true,
            None,
            false,
            ProgramTemplate::default(),
            AnchorVersion::default(),
            TestTemplate::default(),
            true,
            true,
        )
        .unwrap();
    }

    #[test]
    #[should_panic(expected = "Anchor workspace name must be a valid Rust identifier.")]
    fn test_init_reserved_word_from_syn() {
        init(
            &ConfigOverride {
                cluster: None,
                wallet: None,
                commitment: None,
            },
            "fn".to_string(),
            true,
            true,
            None,
            false,
            ProgramTemplate::default(),
            AnchorVersion::default(),
            TestTemplate::default(),
            true,
            true,
        )
        .unwrap();
    }

    #[test]
    #[should_panic(expected = "Anchor workspace name must be a valid Rust identifier.")]
    fn test_init_starting_with_digit() {
        init(
            &ConfigOverride {
                cluster: None,
                wallet: None,
                commitment: None,
            },
            "1project".to_string(),
            true,
            true,
            None,
            false,
            ProgramTemplate::default(),
            AnchorVersion::default(),
            TestTemplate::default(),
            true,
            true,
        )
        .unwrap();
    }

    fn index_set(indices: &[usize]) -> HashSet<usize> {
        indices.iter().copied().collect()
    }

    #[test]
    fn program_order_prefers_programs_with_more_anchor_program_deps() {
        let program_indices = index_set(&[0, 1]);
        let program_closures = HashMap::from([(0, index_set(&[1])), (1, index_set(&[]))]);
        let original_order = HashMap::from([(0, 0), (1, 1)]);

        let ordered = order_program_indices_by_dependency_cache_heuristic(
            &program_indices,
            &program_closures,
            &original_order,
        );

        assert_eq!(ordered, vec![0, 1]);
    }

    #[test]
    fn program_order_uses_total_dependency_closure_as_tiebreaker() {
        let program_indices = index_set(&[0, 1, 2, 3]);
        let program_closures = HashMap::from([
            (0, index_set(&[2, 4])),
            (1, index_set(&[3])),
            (2, index_set(&[])),
            (3, index_set(&[])),
        ]);
        let original_order = HashMap::from([(0, 1), (1, 0), (2, 2), (3, 3)]);

        let ordered = order_program_indices_by_dependency_cache_heuristic(
            &program_indices,
            &program_closures,
            &original_order,
        );

        assert_eq!(ordered, vec![0, 1, 2, 3]);
    }

    #[test]
    fn program_order_places_isolated_programs_first() {
        let program_indices = index_set(&[0, 1, 2]);
        let program_closures = HashMap::from([
            (0, index_set(&[])),
            (1, index_set(&[2])),
            (2, index_set(&[])),
        ]);
        let original_order = HashMap::from([(0, 0), (1, 1), (2, 2)]);

        let ordered = order_program_indices_by_dependency_cache_heuristic(
            &program_indices,
            &program_closures,
            &original_order,
        );

        assert_eq!(ordered, vec![0, 1, 2]);
    }

    #[test]
    fn program_order_preserves_original_order_for_unrelated_programs() {
        let program_indices = index_set(&[0, 1, 2]);
        let program_closures = HashMap::from([
            (0, index_set(&[3])),
            (1, index_set(&[])),
            (2, index_set(&[])),
        ]);
        let original_order = HashMap::from([(0, 0), (1, 1), (2, 2)]);

        let ordered = order_program_indices_by_dependency_cache_heuristic(
            &program_indices,
            &program_closures,
            &original_order,
        );

        assert_eq!(ordered, vec![0, 1, 2]);
    }

    #[test]
    fn test_predeploy_preserves_explicit_external_validator() {
        assert!(should_predeploy_before_test(false, false, false));
        assert!(should_predeploy_before_test(false, true, true));
        assert!(!should_predeploy_before_test(false, true, false));
        assert!(!should_predeploy_before_test(true, true, true));
    }

    #[test]
    fn test_validator_plan_handles_in_process_template_skip() {
        assert_eq!(
            test_validator_plan(false, true, false, true),
            TestValidatorPlan {
                skip_local_validator: true,
                predeploy: false,
                stream_program_logs: true,
            }
        );
    }

    #[test]
    fn test_validator_plan_preserves_explicit_external_validator() {
        assert_eq!(
            test_validator_plan(false, true, true, false),
            TestValidatorPlan {
                skip_local_validator: true,
                predeploy: true,
                stream_program_logs: true,
            }
        );
    }

    #[test]
    fn surfpool_flags_do_not_force_runtime_features() {
        let dir = tempdir().unwrap();
        let cfg = WithPath::new(Config::default(), dir.path().join("Anchor.toml"));
        let flags = surfpool_flags(&cfg, &None, false, false, None, &[]).unwrap();

        assert!(!flags.iter().any(|flag| flag == "--feature"));
    }

    #[test]
    fn surfpool_flags_include_snapshot_for_generated_accounts() {
        let workspace = tempdir().unwrap();
        let cfg = WithPath::new(Config::default(), workspace.path().join("Anchor.toml"));
        let test_validator = Some(TestValidator {
            validator: Some(crate::config::Validator {
                bind_address: "127.0.0.1".to_string(),
                ledger: ".anchor/test-ledger".to_string(),
                rpc_port: 18999,
                fund_accounts: Some(vec![crate::config::FundedAccount {
                    address: "new".to_string(),
                    lamports: Some(2_000_000_000),
                }]),
                ..Default::default()
            }),
            ..Default::default()
        });

        let generated_accounts = generated_validator_accounts(&cfg, &test_validator).unwrap();
        let flags = surfpool_flags(&cfg, &None, false, false, None, &generated_accounts).unwrap();

        let snapshot_index = flags.iter().position(|flag| flag == "--snapshot").unwrap();
        let snapshot_path = PathBuf::from(&flags[snapshot_index + 1]);
        let snapshot: JsonValue =
            serde_json::from_reader(File::open(&snapshot_path).unwrap()).unwrap();

        assert!(snapshot_path.exists());
        assert!(snapshot
            .get(generated_accounts[0].pubkey.to_string())
            .is_some());
    }

    #[test]
    fn test_jest_package_json_pins_uuid_for_commonjs() {
        for package_json in [
            template::package_json(true, "ISC".to_owned(), AnchorVersion::V1),
            template::ts_package_json(true, "ISC".to_owned(), AnchorVersion::V1),
        ] {
            let package: JsonValue = serde_json::from_str(&package_json).unwrap();

            assert_eq!(package["overrides"]["uuid"], "^9.0.1");
            assert_eq!(package["resolutions"]["uuid"], "^9.0.1");
            assert_eq!(package["pnpm"]["overrides"]["uuid"], "^9.0.1");
        }
    }

    #[test]
    fn parse_node_version_with_v_prefix() {
        let version = parse_node_version("v20.10.0\n").unwrap();
        assert_eq!(version.major, 20);
        assert_eq!(version.minor, 10);
        assert_eq!(version.patch, 0);
    }

    #[test]
    fn parse_node_version_without_v_prefix() {
        let version = parse_node_version("20.10.0").unwrap();
        assert_eq!(version.major, 20);
    }

    #[test]
    fn parse_node_version_ignores_surrounding_whitespace() {
        let version = parse_node_version("  v18.17.1  \n").unwrap();
        assert_eq!(version.major, 18);
        assert_eq!(version.minor, 17);
    }

    #[test]
    fn parse_node_version_errors_on_garbage() {
        assert!(parse_node_version("not a version").is_err());
        assert!(parse_node_version("").is_err());
    }

    #[test]
    fn extract_url_port_common_shapes() {
        assert_eq!(extract_url_port("http://127.0.0.1:8899"), Some(8899));
        assert_eq!(extract_url_port("http://127.0.0.1:8899/"), Some(8899));
        assert_eq!(extract_url_port("ws://localhost:8900/path?q=1"), Some(8900));
        assert_eq!(
            extract_url_port("https://api.mainnet-beta.solana.com"),
            None
        );
        assert_eq!(extract_url_port("http://127.0.0.1"), None);
        assert_eq!(extract_url_port("not a url"), None);
    }

    #[test]
    fn replace_url_port_preserves_structure() {
        assert_eq!(
            replace_url_port("http://127.0.0.1:8899", 9001),
            "http://127.0.0.1:9001"
        );
        assert_eq!(
            replace_url_port("ws://127.0.0.1:8899/path?q=1", 9050),
            "ws://127.0.0.1:9050/path?q=1"
        );
        assert_eq!(
            replace_url_port("http://127.0.0.1", 8900),
            "http://127.0.0.1:8900"
        );
    }

    #[test]
    fn idl_ts_preserves_literal_values() {
        let idl = Idl {
            address: "11111111111111111111111111111111".to_string(),
            metadata: anchor_lang_idl::types::IdlMetadata {
                name: "test_program".to_string(),
                version: "0.1.0".to_string(),
                spec: "0.1.0".to_string(),
                description: None,
                repository: None,
                dependencies: Vec::new(),
                contact: None,
                deployments: None,
            },
            docs: Vec::new(),
            instructions: vec![anchor_lang_idl::types::IdlInstruction {
                name: "do_thing".to_string(),
                docs: Vec::new(),
                discriminator: vec![0, 1, 2, 3, 4, 5, 6, 7],
                accounts: vec![IdlInstructionAccountItem::Single(IdlInstructionAccount {
                    name: "target_account".to_string(),
                    docs: Vec::new(),
                    writable: false,
                    signer: false,
                    optional: false,
                    address: None,
                    pda: Some(IdlPda {
                        seeds: vec![IdlSeed::Account(IdlSeedAccount {
                            path: "source_account.authority".to_string(),
                            account: Some("source_account".to_string()),
                        })],
                        program: None,
                    }),
                    relations: vec!["source_account".to_string()],
                })],
                args: vec![anchor_lang_idl::types::IdlField {
                    name: "some_arg".to_string(),
                    docs: Vec::new(),
                    ty: IdlType::U8,
                }],
                returns: None,
            }],
            accounts: vec![anchor_lang_idl::types::IdlAccount {
                name: "source_account".to_string(),
                discriminator: vec![8, 7, 6, 5, 4, 3, 2, 1],
            }],
            events: Vec::new(),
            errors: vec![anchor_lang_idl::types::IdlErrorCode {
                code: 6000,
                name: "Unauthorized".to_string(),
                msg: Some("Unauthorized".to_string()),
            }],
            types: vec![IdlTypeDef {
                name: "wrapper_type".to_string(),
                docs: Vec::new(),
                serialization: Default::default(),
                repr: None,
                generics: vec![IdlTypeDefGeneric::Type {
                    name: "item_type".to_string(),
                }],
                ty: IdlTypeDefTy::Type {
                    alias: IdlType::Defined {
                        name: "generic_holder".to_string(),
                        generics: vec![
                            IdlGenericArg::Type {
                                ty: IdlType::Generic("item_type".to_string()),
                            },
                            IdlGenericArg::Const {
                                value: "SEED_PREFIX".to_string(),
                            },
                        ],
                    },
                },
            }],
            constants: vec![anchor_lang_idl::types::IdlConst {
                name: "seed_prefix".to_string(),
                docs: Vec::new(),
                ty: IdlType::String,
                value: "SEED_PREFIX".to_string(),
            }],
        };

        let ts = idl_ts(&idl).unwrap();

        assert!(ts.contains(r#""name": "doThing""#));
        assert!(ts.contains(r#""name": "targetAccount""#));
        assert!(ts.contains(r#""path": "sourceAccount.authority""#));
        assert!(ts.contains(r#""account": "sourceAccount""#));
        assert!(ts.contains(r#""sourceAccount""#));
        assert!(ts.contains(r#""name": "someArg""#));
        assert!(ts.contains(r#""name": "sourceAccount""#));
        assert!(ts.contains(r#""name": "unauthorized""#));
        assert!(ts.contains(r#""msg": "Unauthorized""#));
        assert!(ts.contains(r#""name": "wrapperType""#));
        assert!(ts.contains(r#""name": "itemType""#));
        assert!(ts.contains(r#""name": "genericHolder""#));
        assert!(ts.contains(r#""generic": "itemType""#));
        assert!(ts.contains(r#""name": "seedPrefix""#));
        assert!(ts.contains(r#""value": "SEED_PREFIX""#));
    }

    // ---------------------------------------------------------------------
    // `anchor idl convert` regression tests.
    // ---------------------------------------------------------------------

    const TEST_PROGRAM_ID: Pubkey =
        solana_pubkey::pubkey!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

    #[test]
    fn apply_program_id_override_current_spec_sets_top_level_address() {
        // Current-spec input. Must patch top-level `address` and keep the
        // `metadata.spec` field so the downstream parser still detects
        // the current spec.
        let idl = serde_json::json!({
            "address": "11111111111111111111111111111111",
            "metadata": { "name": "demo", "version": "0.1.0", "spec": "0.1.0" },
            "instructions": [],
        })
        .to_string();
        let out = apply_program_id_override(idl.as_bytes(), TEST_PROGRAM_ID).unwrap();
        let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(v["address"], TEST_PROGRAM_ID.to_string());
        // Sibling metadata fields must survive.
        assert_eq!(v["metadata"]["spec"], "0.1.0");
        assert_eq!(v["metadata"]["name"], "demo");
        assert_eq!(v["metadata"]["version"], "0.1.0");
    }

    #[test]
    fn apply_program_id_override_legacy_merges_into_existing_metadata() {
        // Legacy input with sibling metadata fields. Override must merge
        // into the existing `metadata` object, not replace it.
        let idl = serde_json::json!({
            "version": "0.1.0",
            "name": "demo",
            "instructions": [],
            "metadata": { "origin": "anchor", "address": "11111111111111111111111111111111" },
        })
        .to_string();
        let out = apply_program_id_override(idl.as_bytes(), TEST_PROGRAM_ID).unwrap();
        let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(v["metadata"]["address"], TEST_PROGRAM_ID.to_string());
        assert_eq!(v["metadata"]["origin"], "anchor");
    }

    #[test]
    fn apply_program_id_override_legacy_no_metadata_creates_object() {
        // Legacy input without any metadata block. Override should create
        // a fresh `metadata.address` entry.
        let idl = serde_json::json!({
            "version": "0.1.0",
            "name": "demo",
            "instructions": [],
        })
        .to_string();
        let out = apply_program_id_override(idl.as_bytes(), TEST_PROGRAM_ID).unwrap();
        let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(v["metadata"]["address"], TEST_PROGRAM_ID.to_string());
    }

    #[test]
    fn skip_deploy_preserves_legacy_validator_config_flags() {
        let cfg = WithPath::new(Config::default(), PathBuf::from("Anchor.toml"));
        let test_validator = Some(TestValidator {
            validator: Some(crate::config::Validator {
                bind_address: "127.0.0.1".to_string(),
                ledger: ".anchor/test-ledger".to_string(),
                rpc_port: 18999,
                warp_slot: Some(42),
                ..Default::default()
            }),
            ..Default::default()
        });

        let flags = validator_flags(&cfg, &test_validator, true, &[]).unwrap();

        assert!(flags
            .windows(2)
            .any(|args| args[0] == "--rpc-port" && args[1] == "18999"));
        assert!(flags
            .windows(2)
            .any(|args| args[0] == "--warp-slot" && args[1] == "42"));
        assert!(!flags.iter().any(|arg| arg == "--bpf-program"));
        assert!(!flags.iter().any(|arg| arg == "--upgradeable-program"));
    }

    #[test]
    fn validator_flags_emits_extra_args() {
        let workspace = tempdir().unwrap();
        let cfg = WithPath::new(Config::default(), workspace.path().join("Anchor.toml"));
        let expected = vec![
            "--rpc-pubsub-enable-block-subscription".to_string(),
            "--geyser-plugin-config".to_string(),
            "geyser.json".to_string(),
        ];
        let test_validator = Some(TestValidator {
            validator: Some(crate::config::Validator {
                bind_address: "127.0.0.1".to_string(),
                ledger: ".anchor/test-ledger".to_string(),
                rpc_port: 18999,
                extra_args: Some(expected.clone()),
                ..Default::default()
            }),
            ..Default::default()
        });

        let flags = validator_flags(&cfg, &test_validator, true, &[]).unwrap();

        assert!(flags
            .windows(expected.len())
            .any(|args| args == expected.as_slice()));
    }

    #[test]
    fn skip_deploy_keeps_generated_account_flags() {
        let workspace = tempdir().unwrap();
        let cfg = WithPath::new(Config::default(), workspace.path().join("Anchor.toml"));
        let funded_pubkey = Pubkey::new_unique();
        let test_validator = Some(TestValidator {
            validator: Some(crate::config::Validator {
                bind_address: "127.0.0.1".to_string(),
                ledger: ".anchor/test-ledger".to_string(),
                rpc_port: 18999,
                fund_accounts: Some(vec![crate::config::FundedAccount {
                    address: funded_pubkey.to_string(),
                    lamports: Some(2_000_000_000),
                }]),
                ..Default::default()
            }),
            ..Default::default()
        });

        let generated_accounts = generated_validator_accounts(&cfg, &test_validator).unwrap();
        let flags = validator_flags(&cfg, &test_validator, true, &generated_accounts).unwrap();
        let expected_path = generated_accounts[0].file_path.display().to_string();

        assert_eq!(generated_accounts.len(), 1);
        assert!(flags.windows(3).any(|args| {
            args[0] == "--account"
                && args[1] == funded_pubkey.to_string()
                && args[2] == expected_path
        }));
    }

    #[test]
    fn token_account_requires_loaded_explicit_mint() {
        let workspace = tempdir().unwrap();
        let cfg = WithPath::new(Config::default(), workspace.path().join("Anchor.toml"));
        let missing_mint = Pubkey::new_unique();
        let owner = Pubkey::new_unique();
        let test_validator = Some(TestValidator {
            validator: Some(crate::config::Validator {
                bind_address: "127.0.0.1".to_string(),
                ledger: ".anchor/test-ledger".to_string(),
                rpc_port: 18999,
                token_accounts: Some(vec![crate::config::TokenAccount {
                    mint: missing_mint.to_string(),
                    owner: owner.to_string(),
                    amount: 1,
                    address: None,
                }]),
                ..Default::default()
            }),
            ..Default::default()
        });

        let err = generated_validator_accounts(&cfg, &test_validator).unwrap_err();

        assert!(err.to_string().contains("token_account mint"));
    }

    #[test]
    fn token_account_accepts_cloned_explicit_mint() {
        let workspace = tempdir().unwrap();
        let cfg = WithPath::new(Config::default(), workspace.path().join("Anchor.toml"));
        let cloned_mint = Pubkey::new_unique();
        let owner = Pubkey::new_unique();
        let test_validator = Some(TestValidator {
            validator: Some(crate::config::Validator {
                bind_address: "127.0.0.1".to_string(),
                ledger: ".anchor/test-ledger".to_string(),
                rpc_port: 18999,
                clone: Some(vec![crate::config::CloneEntry {
                    address: cloned_mint.to_string(),
                }]),
                token_accounts: Some(vec![crate::config::TokenAccount {
                    mint: cloned_mint.to_string(),
                    owner: owner.to_string(),
                    amount: 1,
                    address: None,
                }]),
                ..Default::default()
            }),
            ..Default::default()
        });

        let generated_accounts = generated_validator_accounts(&cfg, &test_validator).unwrap();

        assert_eq!(generated_accounts.len(), 1);
        assert!(generated_accounts[0]
            .file_path
            .file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|name| name.ends_with(".json")));
    }

    #[test]
    fn multiple_new_funded_accounts_get_distinct_pubkeys() {
        let workspace = tempdir().unwrap();
        let cfg = WithPath::new(Config::default(), workspace.path().join("Anchor.toml"));
        let test_validator = Some(TestValidator {
            validator: Some(crate::config::Validator {
                bind_address: "127.0.0.1".to_string(),
                ledger: ".anchor/test-ledger".to_string(),
                rpc_port: 18999,
                fund_accounts: Some(vec![
                    crate::config::FundedAccount {
                        address: "new".to_string(),
                        lamports: Some(15_000_000_000_000),
                    },
                    crate::config::FundedAccount {
                        address: "new".to_string(),
                        lamports: Some(20_000_000_000_000),
                    },
                ]),
                ..Default::default()
            }),
            ..Default::default()
        });

        let generated_accounts = generated_validator_accounts(&cfg, &test_validator).unwrap();

        assert_eq!(generated_accounts.len(), 2);
        assert_ne!(generated_accounts[0].pubkey, generated_accounts[1].pubkey);
    }
}