ccxt-base 4.5.78

CCXT – CryptoCurrency eXchange Trading Library (Rust) – untyped engine (runtime, Value, REST/prediction Cores, WS infra)
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
// Hand-written methods that are ABOVE the
// `METHODS BELOW THIS LINE ARE TRANSPILED FROM TYPESCRIPT` marker in
// `ts/src/base/Exchange.ts`. These are TS class-field aliases that bind
// imported free functions (`safeValue = safeValue;` etc.) — they aren't
// transpiled, so we implement them by hand here.
//
// Signature convention to match the transpiler's call shape:
//
//   pub fn name(&self, arg1: Value, arg2: Value, optional_args: &[Value]) -> Value
//
// The call-site post-processor in `build/rustTranspiler.ts` wraps trailing
// args (defaults) into the `&[Value]` slice so `self.safe_value(obj, key)`
// becomes `self.safe_value(obj, key, &[])` and `self.safe_value(obj, key, d)`
// becomes `self.safe_value(obj, key, &[d])`.

#![allow(non_snake_case, dead_code, unused_variables)]

use crate::exchange::Exchange;
use crate::runtime::stringify_param;
use crate::{ExchangeError, Value};
use indexmap::IndexMap as HashMap;
use std::sync::Arc;

/// Coerces `&Exchange` to `&mut Exchange`. The lint that warns about
/// this is suppressed locally — see the safety note on the call site.
#[allow(clippy::mut_from_ref, invalid_reference_casting)]
unsafe fn coerce_to_mut_unsafe(e: &Exchange) -> &mut Exchange {
    let ptr = e as *const Exchange as *mut Exchange;
    &mut *ptr
}

/// Non-cryptographic 64-bit PRNG (splitmix64) seeded from a global
/// counter mixed with the high-resolution clock. Good enough for the
/// `uuid*` helpers — uniqueness across calls, not security.
fn random_u64() -> u64 {
    use std::sync::atomic::{AtomicU64, Ordering};
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0);
    let n = COUNTER.fetch_add(1, Ordering::Relaxed);
    let mut z = now ^ n.wrapping_mul(0x9E37_79B9_7F4A_7C15);
    z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    z ^ (z >> 31)
}

/// Returns `n` random lowercase hex characters.
fn random_hex(n: usize) -> String {
    let mut s = String::with_capacity(n);
    while s.len() < n {
        s.push_str(&format!("{:016x}", random_u64()));
    }
    s.truncate(n);
    s
}

/// Wraps a byte slice into the port's binary representation —
/// a `Value::Array` of `Value::Int` byte values.
pub(crate) fn bytes_to_value(bytes: &[u8]) -> Value {
    Value::Array(bytes.iter().map(|b| Value::Int(*b as i64)).collect())
}

/// True when `v` is a `Value::Array` whose elements are all integer
/// byte values (0..=255) — the port's stand-in for a binary buffer.
fn is_byte_array(v: &Value) -> bool {
    match v {
        Value::Arr(a) => a
            .iter()
            .all(|x| matches!(x, Value::Int(n) if (0..=255).contains(n))),
        _ => false,
    }
}

/// Recursive helper for `urlencodeNested` — flattens nested maps and
/// arrays into bracketed keys (`b[c]`, `d[0]`), encoding only values.
fn urlencode_nested_walk(prefix: &str, v: &Value, out: &mut Vec<String>) {
    match v {
        Value::Dict(m) => {
            for (k, val) in m.iter() {
                urlencode_nested_walk(&format!("{prefix}[{k}]"), val, out);
            }
        }
        Value::Arr(a) => {
            for (i, val) in a.iter().enumerate() {
                urlencode_nested_walk(&format!("{prefix}[{i}]"), val, out);
            }
        }
        scalar => {
            out.push(format!(
                "{}={}",
                prefix,
                crate::exchange::url_pct(&stringify_param(scalar))
            ));
        }
    }
}

/// f64 → plain decimal string (Rust's `{}` never uses scientific
/// notation for f64, matching TS `numberToString`).
fn dtp_num_to_string(x: f64) -> String {
    format!("{x}")
}

/// Free-function form of `precisionFromString` (returns i64).
fn precision_from_string_i64(s: &str) -> i64 {
    if let Some(idx) = s.find(['e', 'E']) {
        let exp: i64 = s[idx + 1..].parse().unwrap_or(0);
        return -exp;
    }
    let trimmed = s.trim_end_matches('0');
    match trimmed.split_once('.') {
        Some((_, frac)) => frac.len() as i64,
        None => 0,
    }
}

/// Port of `truncate_to_string` from `functions/number.ts`.
fn truncate_to_string(num: &str, precision: i64) -> String {
    if precision > 0 {
        if let Some(dot) = num.find('.') {
            let frac = &num[dot + 1..];
            if frac.len() as i64 > precision {
                return format!("{}.{}", &num[..dot], &frac[..precision as usize]);
            }
        }
        return num.to_string();
    }
    let int_part = num.split('.').next().unwrap_or("0");
    int_part
        .parse::<i64>()
        .map(|n| n.to_string())
        .unwrap_or_else(|_| "0".to_string())
}

/// Faithful port of `_decimalToPrecision` from `functions/number.ts`.
fn decimal_to_precision_impl(
    x: &str,
    rounding_mode: i64,
    num_precision_digits: f64,
    counting_mode: i64,
    padding_mode: i64,
) -> String {
    use crate::runtime::{
        DECIMAL_PLACES, NO_PADDING, ROUND, SIGNIFICANT_DIGITS, TICK_SIZE, TRUNCATE,
    };
    const ZERO: i64 = 48;
    const ONE: i64 = 49;
    const FIVE: i64 = 53;
    const NINE: i64 = 57;

    // ── negative precision ───────────────────────────────────────────
    if num_precision_digits < 0.0 {
        let to_nearest = 10f64.powf(-num_precision_digits);
        let xnum: f64 = x.parse().unwrap_or(0.0);
        if rounding_mode == ROUND {
            let inner = decimal_to_precision_impl(
                &dtp_num_to_string(xnum / to_nearest),
                rounding_mode,
                0.0,
                counting_mode,
                padding_mode,
            );
            let inner_num: f64 = inner.parse().unwrap_or(0.0);
            return dtp_num_to_string(to_nearest * inner_num);
        }
        if rounding_mode == TRUNCATE {
            return dtp_num_to_string(xnum - (xnum % to_nearest));
        }
    }

    // ── tick size ────────────────────────────────────────────────────
    if counting_mode == TICK_SIZE {
        let precision_digits_string = decimal_to_precision_impl(
            &dtp_num_to_string(num_precision_digits),
            ROUND,
            22.0,
            DECIMAL_PLACES,
            NO_PADDING,
        );
        let new_npd = precision_from_string_i64(&precision_digits_string);
        if rounding_mode == TRUNCATE {
            let truncated_x = truncate_to_string(x, new_npd.max(0));
            let x_num: f64 = truncated_x.parse().unwrap_or(0.0);
            let scale = 10f64.powi(new_npd as i32);
            let x_scaled = (x_num * scale).round();
            let tick_scaled = (num_precision_digits * scale).round();
            let ticks = (x_scaled / tick_scaled).trunc();
            let x_new = (ticks * tick_scaled) / scale;
            if padding_mode == NO_PADDING {
                let fixed = format!("{:.*}", new_npd.max(0) as usize, x_new);
                let renum: f64 = fixed.parse().unwrap_or(0.0);
                return dtp_num_to_string(renum);
            }
            return decimal_to_precision_impl(
                &dtp_num_to_string(x_new),
                ROUND,
                new_npd as f64,
                DECIMAL_PLACES,
                padding_mode,
            );
        }
        let mut x_num: f64 = x.parse().unwrap_or(0.0);
        let mut missing = x_num % num_precision_digits;
        missing = decimal_to_precision_impl(
            &dtp_num_to_string(missing),
            ROUND,
            8.0,
            DECIMAL_PLACES,
            NO_PADDING,
        )
        .parse()
        .unwrap_or(0.0);
        let fp_error = decimal_to_precision_impl(
            &dtp_num_to_string(missing / num_precision_digits),
            ROUND,
            new_npd.max(8) as f64,
            DECIMAL_PLACES,
            NO_PADDING,
        );
        if precision_from_string_i64(&fp_error) != 0 {
            if x_num > 0.0 {
                if missing >= num_precision_digits / 2.0 {
                    x_num = x_num - missing + num_precision_digits;
                } else {
                    x_num -= missing;
                }
            } else if missing >= num_precision_digits / 2.0 {
                x_num -= missing;
            } else {
                x_num = x_num - missing - num_precision_digits;
            }
        }
        return decimal_to_precision_impl(
            &dtp_num_to_string(x_num),
            ROUND,
            new_npd as f64,
            DECIMAL_PLACES,
            padding_mode,
        );
    }

    // ── main char-buffer path ────────────────────────────────────────
    let npd = num_precision_digits as i64;
    let bytes: Vec<u8> = x.as_bytes().to_vec();
    let str_end = bytes.len() as i64;
    if str_end == 0 {
        return String::new();
    }
    let is_negative = bytes[0] == b'-';
    let str_start: i64 = if is_negative { 1 } else { 0 };
    let mut str_dot = str_end;
    for k in 0..str_end {
        if bytes[k as usize] == b'.' {
            str_dot = k;
            break;
        }
    }
    let has_dot = str_dot < str_end;
    let chars_len = ((str_end - str_start) + if has_dot { 0 } else { 1 }) as usize;
    let mut chars = vec![ZERO as u8; chars_len];

    let mut after_dot = chars_len as i64;
    let mut digits_start: i64 = -1;
    let mut digits_end: i64;
    {
        let mut i: i64 = 1;
        let mut j: i64 = str_start;
        while j < str_end {
            let c = bytes[j as usize];
            if c == b'.' {
                after_dot = i;
                i -= 1;
            } else if !c.is_ascii_digit() {
                return x.to_string();
            } else {
                if (i as usize) < chars.len() {
                    chars[i as usize] = c;
                }
                if c != b'0' && digits_start < 0 {
                    digits_start = i;
                }
            }
            j += 1;
            i += 1;
        }
    }
    if digits_start < 0 {
        digits_start = 1;
    }

    let mut precision_start = if counting_mode == DECIMAL_PLACES {
        after_dot
    } else {
        digits_start
    };

    digits_end = -1;
    let mut all_zeros = true;
    let mut sign_needed = is_negative;
    {
        let mut memo: i64 = 0;
        let mut i = chars.len() as i64 - 1;
        while i >= 0 {
            let mut c = chars[i as usize] as i64;
            if i != 0 {
                c += memo;
                if i >= precision_start + npd {
                    let ceil =
                        (rounding_mode == ROUND) && (c >= FIVE) && !((c == FIVE) && memo != 0);
                    c = if ceil { NINE + 1 } else { ZERO };
                }
                if c > NINE {
                    c = ZERO;
                    memo = 1;
                } else {
                    memo = 0;
                }
            } else if memo != 0 {
                c = ONE;
            }
            chars[i as usize] = c as u8;
            if c != ZERO {
                all_zeros = false;
                digits_start = i;
                digits_end = if digits_end < 0 { i + 1 } else { digits_end };
            }
            i -= 1;
        }
    }

    if counting_mode == SIGNIFICANT_DIGITS {
        precision_start = digits_start;
    }
    let precision_end = precision_start + npd;
    if all_zeros {
        sign_needed = false;
    }

    let read_start = if digits_start >= after_dot || all_zeros {
        after_dot - 1
    } else {
        digits_start
    };
    let read_end = if digits_end < after_dot {
        after_dot
    } else {
        digits_end
    };

    let n_sign: i64 = if sign_needed { 1 } else { 0 };
    let n_before_dot = n_sign + (after_dot - read_start);
    let n_after_dot = (read_end - after_dot).max(0);
    let actual_length = read_end - read_start;
    let desired_length = if padding_mode == NO_PADDING {
        actual_length
    } else {
        precision_end - read_start
    };
    let pad = (desired_length - actual_length).max(0);
    let pad_start = n_before_dot + 1 + n_after_dot;
    let pad_end = pad_start + pad;
    let is_integer = (n_after_dot + pad) == 0;

    let out_len =
        (n_before_dot + if is_integer { 0 } else { 1 } + n_after_dot + pad).max(0) as usize;
    let mut out = vec![ZERO as u8; out_len];
    let get_char = |idx: i64| -> u8 {
        if idx >= 0 && (idx as usize) < chars.len() {
            chars[idx as usize]
        } else {
            ZERO as u8
        }
    };
    if sign_needed && !out.is_empty() {
        out[0] = b'-';
    }
    {
        let mut i = n_sign;
        let mut j = read_start;
        while i < n_before_dot {
            if (i as usize) < out.len() {
                out[i as usize] = get_char(j);
            }
            i += 1;
            j += 1;
        }
    }
    if !is_integer && (n_before_dot as usize) < out.len() {
        out[n_before_dot as usize] = b'.';
    }
    {
        let mut i = n_before_dot + 1;
        let mut j = after_dot;
        while i < pad_start {
            if (i as usize) < out.len() {
                out[i as usize] = get_char(j);
            }
            i += 1;
            j += 1;
        }
    }
    {
        let mut i = pad_start;
        while i < pad_end {
            if (i as usize) < out.len() {
                out[i as usize] = ZERO as u8;
            }
            i += 1;
        }
    }
    String::from_utf8(out).unwrap_or_default()
}

/// Coerces a `Value` (or a missing one) to `f64` — used by `aggregate`.
fn value_as_f64(v: Option<&Value>) -> f64 {
    match v {
        Some(Value::Int(n)) => *n as f64,
        Some(Value::Float(f)) => *f,
        Some(Value::Str(s)) => s.parse().unwrap_or(0.0),
        _ => 0.0,
    }
}

/// Generic ordering for `Value`s — numeric when both sides are
/// numbers, lexical for strings, used by `sortBy`/`sortBy2`.
fn value_cmp(a: &Value, b: &Value) -> std::cmp::Ordering {
    use std::cmp::Ordering;
    let num = |v: &Value| -> Option<f64> {
        match v {
            Value::Int(n) => Some(*n as f64),
            Value::Float(f) => Some(*f),
            _ => None,
        }
    };
    match (num(a), num(b)) {
        (Some(x), Some(y)) => x.partial_cmp(&y).unwrap_or(Ordering::Equal),
        _ => stringify_param(a).cmp(&stringify_param(b)),
    }
}

const BASE58_ALPHABET: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

/// Base58 encode (Bitcoin alphabet).
fn base58_encode(input: &[u8]) -> String {
    if input.is_empty() {
        return String::new();
    }
    let zeros = input.iter().take_while(|&&b| b == 0).count();
    let mut digits: Vec<u8> = Vec::new();
    for &byte in input {
        let mut carry = byte as u32;
        for d in digits.iter_mut() {
            carry += (*d as u32) << 8;
            *d = (carry % 58) as u8;
            carry /= 58;
        }
        while carry > 0 {
            digits.push((carry % 58) as u8);
            carry /= 58;
        }
    }
    let mut out = String::with_capacity(zeros + digits.len());
    for _ in 0..zeros {
        out.push('1');
    }
    for &d in digits.iter().rev() {
        out.push(BASE58_ALPHABET[d as usize] as char);
    }
    out
}

/// Base58 decode (Bitcoin alphabet). Returns `None` on invalid input.
fn base58_decode(input: &str) -> Option<Vec<u8>> {
    if input.is_empty() {
        return Some(Vec::new());
    }
    let zeros = input.bytes().take_while(|&b| b == b'1').count();
    let mut bytes: Vec<u8> = Vec::new();
    for ch in input.bytes() {
        let mut carry = BASE58_ALPHABET.iter().position(|&a| a == ch)? as u32;
        for b in bytes.iter_mut() {
            carry += (*b as u32) * 58;
            *b = (carry & 0xff) as u8;
            carry >>= 8;
        }
        while carry > 0 {
            bytes.push((carry & 0xff) as u8);
            carry >>= 8;
        }
    }
    let mut out = vec![0u8; zeros];
    out.extend(bytes.iter().rev());
    Some(out)
}

fn arg_default(opt: &[Value]) -> Value {
    opt.get(0).cloned().unwrap_or(Value::Null)
}


fn key_str(key: &Value) -> String {
    stringify_param(key)
}

// ── deferred coroutine (`spawn` / `delay`) queue ─────────────────────────────
// JS `this.spawn(this.method, ...args)` backgrounds a coroutine. We can't truly
// background it (it needs `&mut self`, held by the WS drive loop), so `spawn`
// records the (method-name, args) and the drive loop (`ws_run`) drains and runs
// them inline. The transpiler already lowers the method reference to a
// dispatchable name string.
/// A queued coroutine. `due_at` is `None` for `spawn` (run at the next drain)
/// and `Some(instant)` for `delay` (run at the first drain at or after it —
/// the drive loop bounds its socket read by the earliest deadline so a timer
/// fires even on a silent stream).
///
/// The queue is thread-local, so a coroutine is only ever run by the drive loop
/// that queued it. That holds because a `watch` drive loop and the venue code
/// feeding it are one task, and tokio only migrates a task when another worker
/// steals it — measured as not happening for this loop. A long-deadline item
/// (a 20-minute `keepAliveListenKey`) now sits here across many iterations
/// rather than being drained immediately, so if a future change contends the
/// runtime hard enough to provoke stealing, this queue is the thing to move to
/// a task-local.
struct QueuedSpawn {
    method: String,
    args: Vec<Value>,
    due_at: Option<std::time::Instant>,
}

thread_local! {
    static SPAWN_QUEUE: std::cell::RefCell<Vec<QueuedSpawn>> =
        std::cell::RefCell::new(Vec::new());
}

/// Take everything queued by `spawn`, plus every `delay` whose deadline has
/// passed. Items still waiting on their deadline stay queued.
pub(crate) fn drain_spawn_queue() -> Vec<(String, Vec<Value>)> {
    let now = std::time::Instant::now();
    SPAWN_QUEUE.with(|q| {
        let mut q = q.borrow_mut();
        let mut due = Vec::new();
        let mut pending = Vec::new();
        for item in std::mem::take(&mut *q) {
            if item.due_at.map(|t| t <= now).unwrap_or(true) {
                due.push((item.method, item.args));
            } else {
                pending.push(item);
            }
        }
        *q = pending;
        due
    })
}

/// How long until the earliest not-yet-due `delay` fires, or `None` when
/// nothing is waiting on a deadline. The WS drive loop uses this to bound its
/// read, so a timer still fires on a stream that has gone quiet.
pub(crate) fn next_spawn_due() -> Option<std::time::Duration> {
    let now = std::time::Instant::now();
    SPAWN_QUEUE.with(|q| {
        q.borrow()
            .iter()
            .filter_map(|i| i.due_at)
            .min()
            .map(|t| t.saturating_duration_since(now))
    })
}

/// Await a WS single-flight handle (what `client.future ()` /
/// `client.reusable_future ()` return). Re-exported here because the generated
/// venue files reach the base through `crate::exchange_stubs::…`, a path that
/// resolves in both `ccxt-base` and `ccxt-pro`.
pub async fn ws_await_flight(handle: &Value) -> Value {
    crate::pro::ws_client::ws_await_flight(handle).await
}

/// Queue an async WS handler for the drive loop to run. The sync
/// `dispatch_ws_handler` can't `.await` an async handler (e.g. htx's
/// `handle_order_book_snapshot`), so it enqueues it here; `ws_run` drains and
/// dispatches it asynchronously right after the current `handle_message`.
pub fn enqueue_spawn(name: &str, args: Vec<Value>) {
    SPAWN_QUEUE.with(|q| q.borrow_mut().push(QueuedSpawn {
        method: name.to_string(),
        args,
        due_at: None,
    }));
}

/// Queue a coroutine to run no earlier than `ms` from now — the scheduling half
/// of `delay`. Used for the venues' periodic self-rescheduling loops
/// (`keepAliveListenKey`, token refresh, …), which must fire on their own
/// cadence rather than on the next inbound frame.
pub fn enqueue_spawn_after(name: &str, args: Vec<Value>, ms: i64) {
    let due_at = std::time::Instant::now()
        + std::time::Duration::from_millis(ms.max(0) as u64);
    SPAWN_QUEUE.with(|q| q.borrow_mut().push(QueuedSpawn {
        method: name.to_string(),
        args,
        due_at: Some(due_at),
    }));
}

impl Exchange {
    // ── safe_* aliases (above-marker in TS) ─────────────────────────────────

    /// Coerce a caught panic payload into the `[ClassName] message`
    /// shape TS uses (see `Exchange.exceptionMessage`). Truncates to
    /// 100k chars to match. Rust panics carry just a string in `Value`,
    /// so we trust it as the formatted message and trim it. Used by
    /// exchanges that re-throw with a contextual prefix in their
    /// catch arms (grvt.transfer is the canonical case).
    pub fn exception_message(&self, exc: Value, _optional_args: &[Value]) -> Value {
        let s = match exc {
            Value::Str(s) => s,
            other => crate::runtime::stringify_param(&other),
        };
        let n = s.chars().count().min(100_000);
        Value::Str(s.chars().take(n).collect())
    }

    /// Port of `Exchange.getCcxtVersion()`. The TS reads the static
    /// `Exchange.ccxtVersion` (a class member the release process bumps); the
    /// transpiler can't lower that `(Exchange as any).ccxtVersion` static access,
    /// so `getCcxtVersion` is dropped from `exchange_generated.rs` and lives here
    /// as a hand-written stub instead. Value mirrors `ts/src/base/Exchange.ts`'s
    /// `static ccxtVersion` (== package.json "version"); only foxbit consumes it
    /// (the `X-FB-CLIENT-VERSION` request header), and no static fixture asserts
    /// the header, so a version bump can't break the suites.
    pub fn get_ccxt_version(&self) -> Value {
        Value::Str("4.5.71".to_string())
    }

    // ── Hot-path `safe_*_k` variants — take `key: &str` directly so the
    // transpiler can skip wrapping every literal key in a `Value::Str`.
    // Each `_k` saves two String allocations per call (the key Value +
    // the key_str re-extraction). On a 500-trade fetchTrades, cumulative
    // savings are several thousand allocations.

    #[inline]
    pub fn safe_value_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        let v = crate::value::get_value_k(&obj, key);
        let missing = matches!(&v, Value::Null) || matches!(&v, Value::Str(s) if s.is_empty());
        if missing {
            arg_default(optional_args)
        } else {
            v
        }
    }

    #[inline]
    pub fn safe_string_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        let v = self.safe_value_k(obj, key, &[]);
        match v {
            Value::Str(_) => v,
            Value::Int(n) => Value::Str(n.to_string()),
            Value::Float(f) => Value::Str(f.to_string()),
            // Booleans/lists/dicts are not strings or finite numbers → return
            // the default, matching TS `safeString` (base/functions/type.ts).
            _ => arg_default(optional_args),
        }
    }

    #[inline]
    pub fn safe_integer_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        let v = self.safe_value_k(obj, key, &[]);
        match v {
            Value::Int(_) => v,
            Value::Float(f) => Value::Int(f as i64),
            Value::Str(s) => match s.parse::<i64>() {
                Ok(n) => Value::Int(n),
                Err(_) => match s.parse::<f64>() {
                    Ok(f) if f.is_finite() => Value::Int(f as i64),
                    _ => arg_default(optional_args),
                },
            },
            _ => arg_default(optional_args),
        }
    }

    #[inline]
    pub fn safe_float_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        let v = self.safe_value_k(obj, key, &[]);
        match v {
            Value::Float(_) => v,
            Value::Int(n) => Value::Float(n as f64),
            Value::Str(s) => match s.parse::<f64>() {
                Ok(n) => Value::Float(n),
                Err(_) => arg_default(optional_args),
            },
            _ => arg_default(optional_args),
        }
    }

    #[inline]
    pub fn safe_number_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        self.safe_float_k(obj, key, optional_args)
    }

    #[inline]
    pub fn safe_bool_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        // Mirror CCXT `safeBool`: the value is returned ONLY when it is an
        // actual boolean (`typeof value === 'boolean'`). A string like
        // "true" or an integer is NOT a boolean, so it yields the default
        // — coercing them (as a previous version did) diverges from every
        // other language and broke e.g. zebpay `fetchCurrencies`, where
        // `isDepositEnabled: "true"` (a string) must read as the default.
        let v = self.safe_value_k(obj, key, &[]);
        match v {
            Value::Bool(_) => v,
            _ => arg_default(optional_args),
        }
    }

    #[inline]
    pub fn safe_dict_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        let v = self.safe_value_k(obj, key, &[]);
        if matches!(v, Value::Dict(_)) {
            v
        } else {
            arg_default(optional_args)
        }
    }

    #[inline]
    pub fn safe_list_k(&self, obj: Value, key: &str, optional_args: &[Value]) -> Value {
        let v = self.safe_value_k(obj, key, &[]);
        if matches!(v, Value::Arr(_)) {
            v
        } else {
            arg_default(optional_args)
        }
    }

    pub fn safe_value(&self, obj: Value, key: Value, optional_args: &[Value]) -> Value {
        // Match TS `prop` semantics: an empty string is treated as
        // "missing" — same as null/undefined. Without this, exchanges
        // that signal end-of-pagination with `cursor: ""` loop forever
        // because `is_equal(&Value::Str(""), &Value::Null)` is false.
        let v = crate::get_value(&obj, &Value::Str(key_str(&key)));
        let missing = matches!(&v, Value::Null) || matches!(&v, Value::Str(s) if s.is_empty());
        if missing {
            arg_default(optional_args)
        } else {
            v
        }
    }

    pub fn safe_value2(&self, obj: Value, k1: Value, k2: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_value(obj.clone(), k1, &[]);
        if !v.is_null() {
            return v;
        }
        let v = self.safe_value(obj, k2, &[]);
        if !v.is_null() {
            v
        } else {
            arg_default(optional_args)
        }
    }

    pub fn safe_value_n(&self, obj: Value, keys: Value, optional_args: &[Value]) -> Value {
        if let Value::Arr(ks) = keys {
            for k in ks.iter().cloned() {
                let v = self.safe_value(obj.clone(), k, &[]);
                if !v.is_null() {
                    return v;
                }
            }
        }
        arg_default(optional_args)
    }

    pub fn safe_string(&self, obj: Value, key: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_value(obj, key, &[]);
        match v {
            Value::Str(_) => v,
            Value::Int(n) => Value::Str(n.to_string()),
            Value::Float(f) => Value::Str(f.to_string()),
            // A boolean is NOT a string or finite number — TS `safeString`
            // (base/functions/type.ts) returns the default for it, not "true"/
            // "false". Same for lists/dicts (the `_` arm).
            Value::Null => arg_default(optional_args),
            _ => arg_default(optional_args),
        }
    }

    pub fn safe_string2(&self, obj: Value, k1: Value, k2: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_string(obj.clone(), k1, &[]);
        if !v.is_null() {
            return v;
        }
        let v = self.safe_string(obj, k2, &[]);
        if !v.is_null() {
            v
        } else {
            arg_default(optional_args)
        }
    }

    pub fn safe_string_n(&self, obj: Value, keys: Value, optional_args: &[Value]) -> Value {
        if let Value::Arr(ks) = keys {
            for k in ks.iter().cloned() {
                let v = self.safe_string(obj.clone(), k, &[]);
                if !v.is_null() {
                    return v;
                }
            }
        }
        arg_default(optional_args)
    }

    // NB: case-transform the *found* value only, and return the default
    // UNCHANGED — matching TS `safeStringLower/Upper` (type.ts), which lower/
    // upper-cases `String(x)` when the key resolves and otherwise returns
    // `$default` verbatim. Passing the default through `safe_string` first
    // (and then casing the result) wrongly lower-cased mixed-case defaults
    // such as hyperliquid's checksummed builder address.
    pub fn safe_string_upper(&self, obj: Value, key: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_string(obj, key, &[]);
        if !v.is_null() {
            if let Value::Str(s) = v {
                return Value::Str(s.to_uppercase());
            }
            return v;
        }
        arg_default(optional_args)
    }

    pub fn safe_string_lower_n(&self, obj: Value, keys: Value, optional_args: &[Value]) -> Value {
        if let Value::Arr(ks) = keys {
            for k in ks.iter().cloned() {
                let v = self.safe_string(obj.clone(), k, &[]);
                if !v.is_null() {
                    if let Value::Str(s) = v {
                        return Value::Str(s.to_lowercase());
                    }
                    return v;
                }
            }
        }
        arg_default(optional_args)
    }

    pub fn safe_string_upper_n(&self, obj: Value, keys: Value, optional_args: &[Value]) -> Value {
        if let Value::Arr(ks) = keys {
            for k in ks.iter().cloned() {
                let v = self.safe_string(obj.clone(), k, &[]);
                if !v.is_null() {
                    if let Value::Str(s) = v {
                        return Value::Str(s.to_uppercase());
                    }
                    return v;
                }
            }
        }
        arg_default(optional_args)
    }

    pub fn safe_string_lower(&self, obj: Value, key: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_string(obj, key, &[]);
        if !v.is_null() {
            if let Value::Str(s) = v {
                return Value::Str(s.to_lowercase());
            }
            return v;
        }
        arg_default(optional_args)
    }

    pub fn safe_string_lower2(
        &self,
        obj: Value,
        k1: Value,
        k2: Value,
        optional_args: &[Value],
    ) -> Value {
        let v = self.safe_string2(obj, k1, k2, &[]);
        if !v.is_null() {
            if let Value::Str(s) = v {
                return Value::Str(s.to_lowercase());
            }
            return v;
        }
        arg_default(optional_args)
    }

    pub fn safe_string_upper2(
        &self,
        obj: Value,
        k1: Value,
        k2: Value,
        optional_args: &[Value],
    ) -> Value {
        let v = self.safe_string2(obj, k1, k2, &[]);
        if !v.is_null() {
            if let Value::Str(s) = v {
                return Value::Str(s.to_uppercase());
            }
            return v;
        }
        arg_default(optional_args)
    }

    /// `super.X(...)` from derived exchanges — routes back to the base
    /// Exchange impl. These stubs return Value::Null; they exist so
    /// transpiled `self.super_X()` calls type-check.
    pub fn super_currency(&self, _code: Value) -> Value {
        Value::Null
    }
    pub fn super_handle_errors(
        &self,
        _code: Value,
        _reason: Value,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
        _response: Value,
        _request_headers: Value,
        _request_body: Value,
    ) -> Value {
        Value::Null
    }

    // ── request bookkeeping (hand-written, above the transpile marker) ──
    // These live above the `METHODS BELOW THIS LINE ...` marker in
    // `ts/src/base/Exchange.ts` (plain assignments in most languages), so
    // they are implemented by hand rather than transpiled. They mutate
    // instance fields; the `&self` receiver matches the transpiler's call
    // shape and we reborrow `&mut` via the same helper as `super_set_markets`.
    pub fn set_last_rest_request_timestamp(&self) {
        let me = unsafe { coerce_to_mut_unsafe(self) };
        me.lastRestRequestTimestamp = self.milliseconds();
    }

    pub fn set_last_request(&self, request: Value) {
        let me = unsafe { coerce_to_mut_unsafe(self) };
        me.last_request_headers =
            crate::runtime::get_value(&request, &Value::Str("headers".to_string()));
        me.last_request_body = crate::runtime::get_value(&request, &Value::Str("body".to_string()));
        me.last_request_url = crate::runtime::get_value(&request, &Value::Str("url".to_string()));
    }

    pub fn get_fetch_cache(&self) -> Value {
        self.fetchHistoryCache.clone()
    }

    pub fn add_fetch_cache(&self, data: Value) {
        let me = unsafe { coerce_to_mut_unsafe(self) };
        if crate::runtime::is_less_than_or_equal(&me.fetchHistoryCacheSize, &Value::Int(0)) {
            return;
        }
        while crate::runtime::is_greater_than_or_equal(
            &crate::runtime::get_array_length(&me.fetchHistoryCache),
            &me.fetchHistoryCacheSize,
        ) {
            crate::runtime::remove(&mut me.fetchHistoryCache, &Value::Int(0));
        }
        crate::runtime::append_to_array(&mut me.fetchHistoryCache, data);
    }

    // ── WS (pro) stubs ─────────────────────────────────────────────────────
    // The transpiled pro/<id>.rs files reference `self.watch()`, `self.client()`,
    // `self.spawn()` etc. from the WS Client infrastructure that hasn't been
    // ported yet. These stubs make the WS-extends-REST inheritance compile —
    // calls at runtime panic with a clear "WS not yet ported" error so we
    // surface the gap loudly instead of silently no-op'ing.
    //
    // `watch()` / `watch_multiple()` are the WS drive loop and live in the
    // `ExchangeRuntime` trait (generic `self`) so they can dispatch to the
    // concrete Core's `handle_message` override — see exchange.rs.
    //
    // `client(url)` returns the client-handle `Value` (`Map{url, subscriptions,
    // futures}`) the transpiled `handle_message` reads; the live connection is
    // established lazily by `watch()`.
    pub fn client(&mut self, args: &[Value]) -> Value {
        match args.get(0) {
            Some(Value::Str(url)) => crate::pro::ws_client::client_value(url),
            _ => Value::Null,
        }
    }
    // `spawn(fn, ...)` / `delay(ms, fn, ...)`: the transpiler lowers the JS
    // function reference to `Value::Null` (it has no Rust callable to pass), so
    // these can't invoke the coroutine and are necessarily no-ops. Background
    // keep-alive/auth-refresh loops that rely on them are a known gap.
    pub fn spawn(&self, args: &[Value]) -> Value {
        // args = [ method_name, ...call_args ]; queue it for the drive loop.
        if let Some(Value::Str(name)) = args.get(0) {
            let call_args: Vec<Value> = args.get(1..).map(|s| s.to_vec()).unwrap_or_default();
            enqueue_spawn(name, call_args);
        }
        Value::Null
    }
    pub async fn delay(&mut self, ms: Value, args: &[Value]) -> Value {
        // `delay(ms, method, ...args)` schedules a coroutine after a warm-up
        // delay (bitvavo's snapshot fetch, htx's resync retry) or on a long
        // period (binance's `keepAliveListenKey`, every 20 min). We can't sleep
        // and then call it — that needs &mut self — so queue it with its
        // deadline; the WS drive loop bounds its socket read by the earliest
        // deadline and runs the coroutine when it comes due.
        if let Some(Value::Str(name)) = args.get(0) {
            let call_args: Vec<Value> = args.get(1..).map(|s| s.to_vec()).unwrap_or_default();
            enqueue_spawn_after(name, call_args, ms.as_f64().unwrap_or(0.0) as i64);
        }
        Value::Null
    }
    pub fn order_book(&self, args: &[Value]) -> Value {
        crate::pro::OrderBook::new(crate::runtime::get_arg(args, 0, Value::Null), crate::runtime::get_arg(args, 1, Value::Null))
    }
    pub fn indexed_order_book(&self, args: &[Value]) -> Value {
        crate::pro::IndexedOrderBook::new(crate::runtime::get_arg(args, 0, Value::Null), crate::runtime::get_arg(args, 1, Value::Null))
    }
    pub fn counted_order_book(&self, args: &[Value]) -> Value {
        crate::pro::CountedOrderBook::new(crate::runtime::get_arg(args, 0, Value::Null), crate::runtime::get_arg(args, 1, Value::Null))
    }
    pub fn safe_order_tracker(&self, _args: &[Value]) -> Value {
        Value::Null
    }
    pub async fn un_watch(&mut self, _topic: Value, _args: &[Value]) -> Value {
        Value::Null
    }
    pub fn send(&self, _payload: Value) -> Value {
        Value::Null
    }
    pub fn lock_id(&self, _args: &[Value]) -> Value {
        Value::Null
    }
    pub fn unlock_id(&self, _args: &[Value]) -> Value {
        Value::Null
    }
    pub fn extend_exchange_options(&self, _args: &[Value]) -> Value {
        Value::Null
    }
    pub fn on_error(&self, _args: &[Value]) -> Value {
        Value::Null
    }
    pub fn on_close(&self, _args: &[Value]) -> Value {
        Value::Null
    }
    pub fn on_pong(&self, _args: &[Value]) -> Value {
        Value::Null
    }
    /// CRC-32 checksum — used by a few WS order-book reconcilers
    /// (bitget, bitfinex, independentreserve). Stub returns 0; will be
    /// replaced with the real algorithm when those order books need it.
    pub fn crc32(&self, args: &[Value]) -> Value {
        let payload = args.get(0).cloned().unwrap_or(Value::Str(String::new()));
        let signed = args.get(1).cloned().unwrap_or(Value::Bool(false));
        crate::runtime::crc32(payload, signed)
    }
    /// `decode_proto_msg(message)` — exchange-specific protobuf decoder
    /// (mexc). `message` is the binary frame carried as a byte array; decode
    /// it into mexc's `PushDataV3ApiWrapper` shape (matching protobufjs
    /// `.toJSON()`) so the ported handlers consume it unchanged.
    pub fn decode_proto_msg(&self, args: &[Value]) -> Value {
        match args.get(0) {
            Some(v) if is_byte_array(v) => {
                let bytes = crate::exchange::value_to_bytes(v);
                crate::runtime::decode_mexc_push_data(&bytes)
            }
            Some(v) => v.clone(),
            None => Value::Null,
        }
    }
    /// `loadOrderBook` — WS helper used by some bookkeeping methods.
    /// Stubbed identically to the unwatch flow.
    pub async fn load_order_book(&mut self, _args: &[Value]) -> Value {
        Value::Null
    }

    // ── hand-written base helpers ──────────────────────────────────────────
    // These live above the `METHODS BELOW THIS LINE ARE TRANSPILED` marker in
    // ts/src/base/Exchange.ts, so they are language-specific hand-written code
    // rather than transpiled into exchange_generated.rs.

    /// `remove0xPrefix(hexData)` — strips a leading `0x`/`0X` if present.
    pub fn remove0x_prefix(&self, hex_data: Value) -> Value {
        match &hex_data {
            Value::Str(s) if s.len() >= 2 && (&s[0..2] == "0x" || &s[0..2] == "0X") => {
                Value::Str(s[2..].to_string())
            }
            _ => hex_data,
        }
    }

    /// `checkRequiredDependencies()` — a no-op in Rust; all crypto deps are
    /// compiled in.
    pub fn check_required_dependencies(&self) -> Value {
        Value::Null
    }

    /// `randNumber(size)` — a random integer with `size` decimal digits.
    pub fn rand_number(&self, size: Value) -> Value {
        use rand::Rng;
        let n = size.as_i64().unwrap_or(0).max(0) as usize;
        let mut rng = rand::thread_rng();
        let mut s = String::new();
        for _ in 0..n {
            s.push(char::from(b'0' + rng.gen_range(0..10u8)));
        }
        Value::Int(s.parse::<i64>().unwrap_or(0))
    }

    /// `randomBytes(length)` — `length` random bytes as a hex string.
    pub fn random_bytes(&self, length: Value) -> Value {
        use rand::Rng;
        let n = length.as_i64().unwrap_or(0).max(0) as usize;
        let mut rng = rand::thread_rng();
        let bytes: Vec<u8> = (0..n).map(|_| rng.gen()).collect();
        Value::Str(hex::encode(bytes))
    }

    /// `convertToBigInt(value)` — the Value-bag has no distinct bigint type,
    /// so coerce to a 64-bit integer.
    pub fn convert_to_big_int(&self, value: Value) -> Value {
        match &value {
            Value::Int(_) => value,
            Value::Float(f) => Value::Int(*f as i64),
            Value::Str(s) => {
                let t = s.trim();
                // A uint256 (e.g. polymarket tokenId) overflows i64; keep the
                // decimal/0x-hex string so downstream big-int consumers
                // (eth_abi_encode) encode the full value instead of truncating.
                match t.parse::<i64>() {
                    Ok(n) => Value::Int(n),
                    Err(_) => Value::Str(t.to_string()),
                }
            }
            _ => Value::Int(0),
        }
    }

    /// `fixStringifiedJsonMembers(content)` — un-escapes a JSON string whose
    /// members are themselves stringified JSON (used by bingx).
    pub fn fix_stringified_json_members(&self, content: Value) -> Value {
        match &content {
            Value::Str(s) => Value::Str(
                s.replace('\\', "")
                    .replace("\"{", "{")
                    .replace("}\"", "}")
                    .replace("\"[", "[")
                    .replace("]\"", "]"),
            ),
            _ => content,
        }
    }

    /// `binaryConcatArray(arrayOfChunks)` — concatenates hex-string chunks.
    pub fn binary_concat_array(&self, arr: Value) -> Value {
        let mut out = String::new();
        if let Value::Arr(items) = &arr {
            for it in items.iter() {
                if let Value::Str(s) = it {
                    out.push_str(s);
                }
            }
        }
        Value::Str(out)
    }

    /// `uuid5(namespace, name)` — RFC-4122 v5 (SHA-1 based) UUID.
    pub fn uuid5(&self, namespace: Value, name: Value) -> Value {
        use sha1::{Digest, Sha1};
        let ns = namespace.as_str().unwrap_or("").replace('-', "");
        let mut data: Vec<u8> = Vec::new();
        let mut i = 0;
        while i + 1 < ns.len() {
            if let Ok(b) = u8::from_str_radix(&ns[i..i + 2], 16) {
                data.push(b);
            }
            i += 2;
        }
        data.extend_from_slice(name.as_str().unwrap_or("").as_bytes());
        let mut hasher = Sha1::new();
        hasher.update(&data);
        let mut hash = hasher.finalize().to_vec();
        hash[6] = (hash[6] & 0x0f) | 0x50;
        hash[8] = (hash[8] & 0x3f) | 0x80;
        let h = hex::encode(&hash[0..16]);
        Value::Str(format!(
            "{}-{}-{}-{}-{}",
            &h[0..8],
            &h[8..12],
            &h[12..16],
            &h[16..20],
            &h[20..32]
        ))
    }

    /// `ethAbiEncode(types, values)` — Solidity `abi.encode` of a head-only
    /// (all-static) tuple: every element becomes one 32-byte word. Supports the
    /// static types the callers use (address, bytesN, uint*/int*, bool). Dynamic
    /// types (`bytes`, `string`, arrays) are not needed (polymarket ERC-7739) and
    /// return Null. Values arrive as: bytesN → binary `Value::Arr` (or 0x-hex
    /// string); uint/int → `Value::Int` or a decimal/0x-hex string (big uint256
    /// like tokenId exceed i64, so string form is preserved by convert_to_big_int);
    /// address → 0x-hex string. Result is a binary `Value::Arr` that `hash(...)`
    /// consumes via value_to_bytes.
    pub fn eth_abi_encode(&self, types: Value, values: Value) -> Value {
        use num_bigint::BigInt;
        let ts: Vec<Value> = match &types  { Value::Arr(a) => (**a).clone(), _ => return Value::Null };
        let vs: Vec<Value> = match &values { Value::Arr(a) => (**a).clone(), _ => return Value::Null };
        let to_bytes = |v: &Value| -> Vec<u8> {
            match v {
                Value::Arr(a) => a.iter().filter_map(|x| if let Value::Int(n) = x { Some(*n as u8) } else { None }).collect(),
                Value::Str(s) => hex::decode(s.trim_start_matches("0x")).unwrap_or_default(),
                _ => Vec::new(),
            }
        };
        let to_bigint = |v: &Value| -> BigInt {
            match v {
                Value::Int(n)   => BigInt::from(*n),
                Value::Float(f) => BigInt::from(*f as i64),
                Value::Str(s)   => {
                    let t = s.trim();
                    if let Some(h) = t.strip_prefix("0x") {
                        BigInt::parse_bytes(h.as_bytes(), 16).unwrap_or_default()
                    } else {
                        BigInt::parse_bytes(t.as_bytes(), 10).unwrap_or_default()
                    }
                }
                _ => BigInt::from(0),
            }
        };
        let mut out: Vec<u8> = Vec::new();
        for (t, v) in ts.iter().zip(vs.iter()) {
            let ty = match t { Value::Str(s) => s.as_str(), _ => return Value::Null };
            let mut word = [0u8; 32];
            if ty == "address" {
                let b = to_bytes(v);
                let n = b.len().min(20);
                word[32 - n..].copy_from_slice(&b[b.len() - n..]);
            } else if ty.starts_with("bytes") && ty != "bytes" {
                // fixed bytesN — left-aligned
                let b = to_bytes(v);
                let n = b.len().min(32);
                word[..n].copy_from_slice(&b[..n]);
            } else if ty.starts_with("uint") || ty.starts_with("int") {
                // uintN/intN — validate the declared bit width, reject
                // negative-for-unsigned / overflow, then encode (review #15).
                word = crate::exchange::eip712_int_word(ty, &to_bigint(v));
            } else if ty == "bool" {
                if matches!(v, Value::Bool(true)) { word[31] = 1; }
            } else {
                return Value::Null; // unsupported (dynamic) type
            }
            out.extend_from_slice(&word);
        }
        Value::Array(out.into_iter().map(|b| Value::Int(b as i64)).collect())
    }

    /// `setProperty(this, key, value)` — dynamic field setter. The typed
    /// `Exchange` struct has no Value-shaped dynamic field store, so this
    /// is currently a no-op (the receiver arg is dropped by the transpiler).
    pub fn set_property(&self, _property: Value, _optional_args: &[Value]) -> Value {
        Value::Null
    }

    // ── exchange-specific signing helpers (above-the-marker base methods) ──
    // These wrap heavy crypto (curve25519, StarkNet, dydx protobuf, lighter
    // zk-proofs) that has not yet been ported to Rust. The terminal
    // signature-producing methods fail loudly with NotSupported rather than
    // returning a null/empty signature — an unsigned request silently sent to an
    // exchange is worse than an explicit error (review #4). The static request
    // fixtures mark the affected private cases `disabledRS`, as Go/Java do.

    /// Fail loudly for an unported crypto/signing primitive. Diverges (`-> !`),
    /// so it satisfies any `-> Value` stub body.
    fn crypto_not_supported(&self, what: &str) -> ! {
        let id = match &self.id { Value::Str(s) => s.clone(), _ => String::new() };
        panic!("{}", crate::exchange_errors::not_supported(Value::Str(
            format!("{id} {what}() signing is not implemented in the Rust port yet"),
        )));
    }

    /// `axolotl(payload, hexKey, ed25519)` — curve25519 signing (waves).
    pub fn axolotl(&self, _payload: Value, _hex_key: Value, _ed25519: Value) -> Value {
        self.crypto_not_supported("axolotl")
    }

    /// `retrieveStarkAccount(signature, accountClassHash, accountProxyClassHash)`.
    pub fn retrieve_stark_account(
        &self,
        _signature: Value,
        _class_hash: Value,
        _proxy_hash: Value,
    ) -> Value {
        Value::Null
    }

    /// `starknetEncodeStructuredData(domain, messageTypes, messageData, address)`.
    pub fn starknet_encode_structured_data(
        &self,
        _domain: Value,
        _types: Value,
        _data: Value,
        _address: Value,
    ) -> Value {
        Value::Null
    }

    /// `starknetSign(msgHash, pri)`.
    pub fn starknet_sign(&self, _msg_hash: Value, _pri: Value) -> Value {
        self.crypto_not_supported("starknetSign")
    }

    /// `extendedStarknetSign(msgHash, pri)` — starknet-curve signing for the
    /// `extended` exchange. Stub: starknet crypto (poseidon/stark-curve) is
    /// not ported to Rust yet, matching `starknet_sign` above.
    pub fn extended_starknet_sign(&self, _msg_hash: Value, _pri: Value) -> Value {
        self.crypto_not_supported("extendedStarknetSign")
    }

    /// `extendedStarknetGetSelectorFromName(name)`.
    pub fn extended_starknet_get_selector_from_name(&self, _name: Value) -> Value {
        Value::Null
    }

    /// `extendedStarknetComputePoseidonHashOnElements(data)`.
    pub fn extended_starknet_compute_poseidon_hash_on_elements(&self, _data: Value) -> Value {
        Value::Null
    }

    /// `toDydxLong(numStr)`.
    pub fn to_dydx_long(&self, _num_str: Value) -> Value {
        Value::Null
    }

    /// `retrieveDydxCredentials(entropy)`.
    pub fn retrieve_dydx_credentials(&self, _entropy: Value) -> Value {
        Value::Null
    }

    /// `loadDydxProtos()`.
    pub async fn load_dydx_protos(&self) -> Value {
        Value::Null
    }

    /// `encodeDydxTxRaw(signDoc, signature)`.
    pub fn encode_dydx_tx_raw(&self, _sign_doc: Value, _signature: Value) -> Value {
        self.crypto_not_supported("encodeDydxTxRaw")
    }

    /// `encodeDydxTxForSimulation(message, memo, sequence, publicKey)`.
    pub fn encode_dydx_tx_for_simulation(
        &self,
        _message: Value,
        _memo: Value,
        _sequence: Value,
        _public_key: Value,
    ) -> Value {
        Value::Null
    }

    /// `encodeDydxTxForSigning(message, memo, chainId, account, authenticators, fee?)`.
    pub fn encode_dydx_tx_for_signing(
        &self,
        _message: Value,
        _memo: Value,
        _chain_id: Value,
        _account: Value,
        _authenticators: Value,
        _optional_args: &[Value],
    ) -> Value {
        Value::Null
    }

    /// `loadLighterLibrary(libraryPath, chainId, privateKey, apiKeyIndex, accountIndex, createClient?)`.
    pub async fn load_lighter_library(
        &self,
        _library_path: Value,
        _chain_id: Value,
        _private_key: Value,
        _api_key_index: Value,
        _account_index: Value,
        _optional_args: &[Value],
    ) -> Value {
        Value::Null
    }

    /// `lighterCreateClient(signer, chainId, privateKey, apiKeyIndex, accountIndex)`.
    pub fn lighter_create_client(
        &self,
        _signer: Value,
        _chain_id: Value,
        _private_key: Value,
        _api_key_index: Value,
        _account_index: Value,
    ) -> Value {
        Value::Null
    }

    /// `lighterGenerateApiKey(signer)`.
    pub fn lighter_generate_api_key(&self, _signer: Value) -> Value {
        self.crypto_not_supported("lighterGenerateApiKey")
    }

    /// `getZKTransferSignatureObj(seeds, order)` — apex StarkEx signing.
    pub async fn get_zk_transfer_signature_obj(
        &self,
        _seeds: Value,
        _optional_args: &[Value],
    ) -> Value {
        self.crypto_not_supported("getZKTransferSignatureObj")
    }

    /// `getZKContractSignatureObj(seeds, order)` — apex StarkEx signing.
    pub async fn get_zk_contract_signature_obj(
        &self,
        _seeds: Value,
        _optional_args: &[Value],
    ) -> Value {
        self.crypto_not_supported("getZKContractSignatureObj")
    }

    /// `futuresTransfer(code, amount, type, params?)` — binance futures
    /// wallet transfer; inherited by binancecoinm / binanceusdm.
    pub async fn futures_transfer(
        &self,
        _code: Value,
        _amount: Value,
        _transfer_type: Value,
        _optional_args: &[Value],
    ) -> Value {
        Value::Null
    }

    /// `lighterSign*(signer, request)` — lighter zk-proof signing helpers.
    pub fn lighter_sign_create_order(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignCreateOrder")
    }
    pub fn lighter_sign_create_grouped_orders(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignCreateGroupedOrders")
    }
    pub fn lighter_sign_cancel_order(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignCancelOrder")
    }
    pub fn lighter_sign_cancel_all_orders(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignCancelAllOrders")
    }
    pub fn lighter_sign_withdraw(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignWithdraw")
    }
    pub fn lighter_sign_create_sub_account(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignCreateSubAccount")
    }
    pub fn lighter_sign_modify_order(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignModifyOrder")
    }
    pub fn lighter_sign_transfer(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignTransfer")
    }
    pub fn lighter_sign_update_leverage(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignUpdateLeverage")
    }
    pub fn lighter_sign_update_margin(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignUpdateMargin")
    }
    pub fn lighter_sign_approve_integrator(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignApproveIntegrator")
    }
    pub fn lighter_sign_change_pubkey(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterSignChangePubkey")
    }
    pub fn lighter_create_auth_token(&self, _signer: Value, _request: Value) -> Value {
        self.crypto_not_supported("lighterCreateAuthToken")
    }

    /// `urlencodeWithArrayRepeat(params)` — `qs.stringify(object, { arrayFormat: 'repeat' })`:
    /// array values repeat the key (`a=1&a=2`); scalars encode normally.
    pub fn urlencode_with_array_repeat(&self, params: Value) -> Value {
        let m = match &params {
            Value::Dict(m) => m,
            _ => return Value::Str(String::new()),
        };
        let mut pairs: Vec<String> = Vec::new();
        for (k, v) in m.iter() {
            let key = crate::exchange::url_pct(k);
            match v {
                Value::Arr(items) => {
                    for item in items.iter() {
                        pairs.push(format!(
                            "{}={}",
                            key,
                            crate::exchange::url_pct(&stringify_param(item))
                        ));
                    }
                }
                other => {
                    pairs.push(format!(
                        "{}={}",
                        key,
                        crate::exchange::url_pct(&stringify_param(other))
                    ));
                }
            }
        }
        Value::Str(pairs.join("&"))
    }

    pub fn safe_integer(&self, obj: Value, key: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_value(obj, key, &[]);
        match v {
            Value::Int(_) => v,
            Value::Float(f) => Value::Int(f as i64),
            // CCXT `safeInteger` is parseInt-style: a decimal string like
            // "3.33" truncates to 3 (arkham leverage tiers), not null. Try
            // an exact i64 first, then fall back to f64 → truncate.
            Value::Str(s) => match s.parse::<i64>() {
                Ok(n) => Value::Int(n),
                Err(_) => match s.parse::<f64>() {
                    Ok(f) if f.is_finite() => Value::Int(f as i64),
                    _ => arg_default(optional_args),
                },
            },
            _ => arg_default(optional_args),
        }
    }

    pub fn safe_integer2(
        &self,
        obj: Value,
        k1: Value,
        k2: Value,
        optional_args: &[Value],
    ) -> Value {
        let v = self.safe_integer(obj.clone(), k1, &[]);
        if !v.is_null() {
            return v;
        }
        let v = self.safe_integer(obj, k2, &[]);
        if !v.is_null() {
            v
        } else {
            arg_default(optional_args)
        }
    }

    pub fn safe_integer_n(&self, obj: Value, keys: Value, optional_args: &[Value]) -> Value {
        if let Value::Arr(ks) = keys {
            for k in ks.iter().cloned() {
                let v = self.safe_integer(obj.clone(), k, &[]);
                if !v.is_null() {
                    return v;
                }
            }
        }
        arg_default(optional_args)
    }

    /// Reads `obj[key]` as a number, multiplies by `factor`, and casts
    /// to i64. Used to convert seconds → milliseconds for timestamps.
    pub fn safe_integer_product(
        &self,
        obj: Value,
        key: Value,
        factor: Value,
        optional_args: &[Value],
    ) -> Value {
        let v = self.safe_value(obj, key, &[]);
        let n = match v {
            Value::Int(n) => Some(n as f64),
            Value::Float(f) => Some(f),
            Value::Str(s) => s.parse::<f64>().ok(),
            _ => None,
        };
        let f = match factor {
            Value::Int(n) => n as f64,
            Value::Float(f) => f,
            _ => return arg_default(optional_args),
        };
        match n {
            Some(x) => Value::Int((x * f) as i64),
            None => arg_default(optional_args),
        }
    }

    pub fn safe_integer_product2(
        &self,
        obj: Value,
        k1: Value,
        k2: Value,
        factor: Value,
        optional_args: &[Value],
    ) -> Value {
        let v = self.safe_integer_product(obj.clone(), k1, factor.clone(), &[]);
        if !v.is_null() {
            return v;
        }
        let v = self.safe_integer_product(obj, k2, factor, &[]);
        if !v.is_null() {
            v
        } else {
            arg_default(optional_args)
        }
    }

    pub fn safe_integer_product_n(
        &self,
        obj: Value,
        keys: Value,
        factor: Value,
        optional_args: &[Value],
    ) -> Value {
        if let Value::Arr(ks) = keys {
            for k in ks.iter().cloned() {
                let v = self.safe_integer_product(obj.clone(), k, factor.clone(), &[]);
                if !v.is_null() {
                    return v;
                }
            }
        }
        arg_default(optional_args)
    }

    pub fn safe_timestamp2(
        &self,
        obj: Value,
        k1: Value,
        k2: Value,
        optional_args: &[Value],
    ) -> Value {
        self.safe_integer_product2(obj, k1, k2, Value::Int(1000), optional_args)
    }

    pub fn safe_timestamp_n(&self, obj: Value, keys: Value, optional_args: &[Value]) -> Value {
        self.safe_integer_product_n(obj, keys, Value::Int(1000), optional_args)
    }

    pub fn safe_float(&self, obj: Value, key: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_value(obj, key, &[]);
        match v {
            Value::Float(_) => v,
            Value::Int(n) => Value::Float(n as f64),
            Value::Str(s) => match s.parse::<f64>() {
                Ok(n) => Value::Float(n),
                Err(_) => arg_default(optional_args),
            },
            _ => arg_default(optional_args),
        }
    }

    /// `safeTimestamp(o, k)` — `parseInt(asFloat(value) * 1000)`. The
    /// value is read as a float (seconds, possibly fractional) and
    /// converted to integer milliseconds.
    pub fn safe_timestamp(&self, obj: Value, key: Value, optional_args: &[Value]) -> Value {
        match self.safe_float(obj, key, &[]) {
            Value::Float(f) => Value::Int((f * 1000.0) as i64),
            Value::Int(n) => Value::Int(n * 1000),
            _ => arg_default(optional_args),
        }
    }

    /// `isEmpty(value)` — true when an array/dict is empty, or the
    /// value is null/undefined. Scalars (strings, numbers, bools) are
    /// never considered empty — mirrors `functions/generic.ts`.
    pub fn is_empty(&self, v: Value) -> Value {
        let empty = match &v {
            Value::Null => true,
            Value::Arr(a) => a.is_empty(),
            Value::Dict(m) => m.is_empty(),
            _ => false,
        };
        Value::Bool(empty)
    }

    pub fn value_is_defined(&self, v: Value) -> Value {
        Value::Bool(!v.is_null())
    }

    // ── extend / deep_extend / omit ─────────────────────────────────────────

    pub fn extend(&self, a: Value, optional_args: &[Value]) -> Value {
        let mut out = match a {
            Value::Dict(m) => Arc::try_unwrap(m).unwrap_or_else(|x| (*x).clone()),
            _ => HashMap::new(),
        };
        let merge = |out: &mut HashMap<String, Value>, src: Value| {
            if let Value::Dict(m) = src {
                let m = Arc::try_unwrap(m).unwrap_or_else(|x| (*x).clone());
                for (k, v) in m {
                    out.insert(k, v);
                }
            }
        };
        for extra in optional_args {
            merge(&mut out, extra.clone());
        }
        Value::Map(out)
    }

    pub fn deep_extend(&self, a: Value, optional_args: &[Value]) -> Value {
        let mut current = a;
        for extra in optional_args {
            current = crate::value::deep_extend(current, extra.clone());
        }
        current
    }

    pub fn omit(&self, obj: Value, keys: Value, _optional_args: &[Value]) -> Value {
        match obj {
            Value::Dict(mut m) => {
                let mm = Arc::make_mut(&mut m);
                match keys {
                    Value::Arr(ks) => {
                        for k in ks.iter() {
                            if let Value::Str(s) = k {
                                mm.shift_remove(s);
                            }
                        }
                    }
                    Value::Str(s) => {
                        mm.shift_remove(&s);
                    }
                    _ => {}
                }
                Value::Dict(m)
            }
            other => other,
        }
    }

    pub fn omit_zero(&self, v: Value) -> Value {
        // Mirrors TS `omitZero`: undefined/'' or any value that parses to
        // numeric 0 → undefined. The old `s == "0"` check missed "0.0",
        // "0.00", "0e0" etc. — e.g. gate echoes `gt_fee:"0.0"`, and
        // keeping it as a non-null fee left `safeTrade` with 3 fee
        // entries (so it couldn't collapse to a single `fee`).
        match &v {
            Value::Int(0) => Value::Null,
            Value::Float(f) if *f == 0.0 => Value::Null,
            Value::Str(s) => {
                if s.is_empty() {
                    return Value::Null;
                }
                match s.parse::<f64>() {
                    Ok(n) if n == 0.0 => Value::Null,
                    _ => v,
                }
            }
            _ => v,
        }
    }

    // ── array / object utilities ────────────────────────────────────────────

    pub fn to_array(&self, v: Value) -> Value {
        // A WS cache (ArrayCache / order-book cache) keeps its entries in `_data`
        // or the shared cache registry, not the dict's meta fields — surface
        // those, else `to_array` would leak `__cacheKind`/counters/flags as rows.
        if let Some(entries) = crate::value::cache_entries_as_value(&v) {
            return entries;
        }
        match v {
            Value::Arr(_) => v,
            Value::Dict(m) => Value::Array(m.values().cloned().collect()),
            Value::Null => Value::Array(vec![]),
            other => Value::Array(vec![other]),
        }
    }

    pub fn array_concat(&self, a: Value, b: Value) -> Value {
        crate::runtime::concat_arrays(&a, &b)
    }

    pub fn array_slice(&self, v: Value, start: Value, optional_args: &[Value]) -> Value {
        let Value::Arr(a) = v else {
            return Value::Array(vec![]);
        };
        let len = a.len() as i64;
        let s = match start {
            Value::Int(n) => n,
            _ => 0,
        };
        let s = if s < 0 {
            (len + s).max(0) as usize
        } else {
            (s as usize).min(a.len())
        };
        let e = match optional_args.get(0) {
            None | Some(Value::Null) => a.len(),
            Some(Value::Int(n)) => {
                if *n < 0 {
                    ((len) + n).max(0) as usize
                } else {
                    (*n as usize).min(a.len())
                }
            }
            _ => a.len(),
        };
        if s <= e {
            Value::Array(a[s..e].to_vec())
        } else {
            Value::Array(vec![])
        }
    }

    pub fn in_array(&self, needle: Value, haystack: Value) -> Value {
        let hit = if let Value::Arr(a) = haystack {
            a.iter().any(|x| crate::runtime::is_equal(x, &needle))
        } else {
            false
        };
        Value::Bool(hit)
    }

    pub fn unique(&self, v: Value) -> Value {
        if let Value::Arr(a) = v {
            let mut out: Vec<Value> = vec![];
            for item in a.iter().cloned() {
                if !out.iter().any(|x| crate::runtime::is_equal(x, &item)) {
                    out.push(item);
                }
            }
            Value::Array(out)
        } else {
            Value::Array(vec![])
        }
    }

    pub fn index_by(&self, arr: Value, key: Value) -> Value {
        let k = key_str(&key);
        let mut out: HashMap<String, Value> = HashMap::new();
        // TS `indexBy` accepts an array OR an object — for an object it
        // iterates `Object.values`. okx's parseDepositAddress relies on
        // this: `indexBy(currency['networks'], 'id')` where `networks` is
        // a dict keyed by network code. Array-only would return {} and
        // collapse every parsed address to a null network.
        let items: Vec<Value> = match arr {
            Value::Arr(items) => Arc::try_unwrap(items).unwrap_or_else(|a| (*a).clone()),
            Value::Dict(m) => m.values().cloned().collect(),
            _ => return Value::Map(out),
        };
        for item in items {
            if let Some(kv) = crate::value::safe_string(&item, &k, None) {
                out.insert(kv, item);
            }
        }
        Value::Map(out)
    }

    pub fn index_by_safe(&self, arr: Value, key: Value) -> Value {
        self.index_by(arr, key)
    }

    pub fn group_by(&self, arr: Value, key: Value, _optional_args: &[Value]) -> Value {
        let k = key_str(&key);
        let mut out: HashMap<String, Vec<Value>> = HashMap::new();
        if let Value::Arr(items) = arr {
            for item in items.iter().cloned() {
                if let Some(kv) = crate::value::safe_string(&item, &k, None) {
                    out.entry(kv).or_default().push(item);
                }
            }
        }
        Value::Map(out.into_iter().map(|(k, v)| (k, Value::Array(v))).collect())
    }

    pub fn filter_by(
        &self,
        arr: Value,
        key: Value,
        value: Value,
        _optional_args: &[Value],
    ) -> Value {
        let k = key_str(&key);
        if let Value::Arr(items) = arr {
            let items = Arc::try_unwrap(items).unwrap_or_else(|a| (*a).clone());
            let out: Vec<Value> = items
                .into_iter()
                .filter(|it| {
                    let v = crate::get_value(it, &Value::Str(k.clone()));
                    crate::runtime::is_equal(&v, &value)
                })
                .collect();
            Value::Array(out)
        } else {
            Value::Array(vec![])
        }
    }

    /// `sortBy(array, key, descending=false)` — stable sort of an array
    /// of objects by `obj[key]`. Mirrors `functions/generic.ts`.
    pub fn sort_by(&self, arr: Value, key: Value, optional_args: &[Value]) -> Value {
        let mut items = match arr {
            Value::Arr(a) => Arc::try_unwrap(a).unwrap_or_else(|x| (*x).clone()),
            other => return other,
        };
        let descending = matches!(optional_args.get(0), Some(Value::Bool(true)));
        let key = Value::Str(stringify_param(&key));
        items.sort_by(|a, b| {
            let av = crate::get_value(a, &key);
            let bv = crate::get_value(b, &key);
            let ord = value_cmp(&av, &bv);
            if descending {
                ord.reverse()
            } else {
                ord
            }
        });
        Value::Array(items)
    }

    /// `sortBy2(array, key1, key2, descending=false)` — sort by `key1`,
    /// then `key2` as a tiebreaker. Mirrors `functions/generic.ts`.
    pub fn sort_by2(&self, arr: Value, k1: Value, k2: Value, optional_args: &[Value]) -> Value {
        let mut items = match arr {
            Value::Arr(a) => Arc::try_unwrap(a).unwrap_or_else(|x| (*x).clone()),
            other => return other,
        };
        let descending = matches!(optional_args.get(0), Some(Value::Bool(true)));
        let k1 = Value::Str(stringify_param(&k1));
        let k2 = Value::Str(stringify_param(&k2));
        items.sort_by(|a, b| {
            let ord = value_cmp(&crate::get_value(a, &k1), &crate::get_value(b, &k1)).then(
                value_cmp(&crate::get_value(a, &k2), &crate::get_value(b, &k2)),
            );
            if descending {
                ord.reverse()
            } else {
                ord
            }
        });
        Value::Array(items)
    }

    pub fn keysort(&self, obj: Value, _optional_args: &[Value]) -> Value {
        if let Value::Dict(m) = obj {
            let mut keys: Vec<&String> = m.keys().collect();
            keys.sort();
            let mut out = HashMap::new();
            for k in &keys {
                out.insert((*k).clone(), m[*k].clone());
            }
            Value::Map(out)
        } else {
            obj
        }
    }

    /// `aggregate(bidasks)` — groups `[price, volume]` rows by price,
    /// summing volume and dropping zero-volume rows. Preserves the
    /// first-occurrence order of prices. Mirrors `functions/misc.ts`.
    pub fn aggregate(&self, arr: Value) -> Value {
        // An order-book side is a marker Dict backed by the shared side store,
        // not a plain array (in JS `OrderBookSide` extends Array). Snapshot its
        // `[[price, size], …]` rows so aggregate iterates the levels.
        let arr = crate::value::side_entries_as_value(&arr).unwrap_or(arr);
        let items = match arr {
            Value::Arr(a) => Arc::try_unwrap(a).unwrap_or_else(|x| (*x).clone()),
            _ => return Value::Array(vec![]),
        };
        let mut order: Vec<u64> = Vec::new();
        let mut buckets: HashMap<u64, (f64, f64)> = HashMap::new();
        for item in items {
            let row = match item {
                Value::Arr(r) => r,
                _ => continue,
            };
            let price = value_as_f64(row.get(0));
            let volume = value_as_f64(row.get(1));
            if volume > 0.0 {
                let bits = price.to_bits();
                let entry = buckets.entry(bits).or_insert_with(|| {
                    order.push(bits);
                    (price, 0.0)
                });
                entry.1 += volume;
            }
        }
        let out: Vec<Value> = order
            .iter()
            .map(|b| {
                let (p, v) = buckets[b];
                Value::Array(vec![Value::Float(p), Value::Float(v)])
            })
            .collect();
        Value::Array(out)
    }
    pub fn map_to_safe_map(&self, v: Value) -> Value {
        v
    }
    pub fn create_safe_dictionary(&self, _optional_args: &[Value]) -> Value {
        Value::Map(HashMap::new())
    }

    /// `clone(value)` — CCXT's deep clone, distinct from Rust's `Clone` trait.
    /// We disambiguate by name; the transpiler emits `self.clone(x)`.
    pub fn clone_value(&self, v: Value) -> Value {
        v
    }

    /// `sum(...)` — variadic numeric sum. Pure variadic (0 fixed
    /// args) so single-arg `sum(x)` (e.g. from test.sum) also works.
    pub fn sum(&self, optional_args: &[Value]) -> Value {
        let mut acc: Option<Value> = None;
        for x in optional_args {
            acc = Some(match acc {
                None => x.clone(),
                Some(a) => crate::runtime::add(&a, x),
            });
        }
        acc.unwrap_or(Value::Null)
    }

    /// `parse_json(text)` — JSON parse. Mirrors CCXT `parseJson`, which
    /// runs `onJsonResponse` first: when `quoteJsonNumbers` is on
    /// (default), every object number VALUE is wrapped in quotes before
    /// parsing so big integer ids (e.g. bingx's 18-digit `orderId`) and
    /// precise decimals survive as strings instead of lossy f64/i64.
    pub fn parse_json_value(&self, v: Value) -> Value {
        match v {
            Value::Str(s) => {
                let prepared = crate::runtime::quote_json_numbers(&s);
                crate::runtime::json_parse(&Value::Str(prepared))
            }
            // Already a parsed object/array (some fixtures store the
            // response pre-parsed) — pass it through untouched.
            other => other,
        }
    }

    // `yymmdd()` / `yyyymmdd()` — date formatters.
    // ── methods called by transpiled base tests ─────────────────────────────

    /// Test helper: tests sometimes call `exchange.clone()` to deep-copy
    /// state. We don't implement structural clone (the trait pointer
    /// can't be cloned safely) — return a fresh empty Exchange so the
    /// call compiles and tests can move on.
    /// No-arg `exchange.clone()` form — a structural copy of every
    /// `Value` field. The copy gets a fresh `Internals` (no shared http
    /// client / derived pointers), which is all the transpiled tests need.
    pub fn clone_self(&self) -> Exchange {
        Clone::clone(self)
    }

    /// `clone(v)` — deep-clone a Value. Matches TS `exchange.clone(x)`.
    /// Note: keep this name even though it shadows the (manual) `clone_self`,
    /// because the transpiled tests call `exchange.clone(value)`. Empty-arg
    /// calls `exchange.clone()` get post-processed to `clone_self()`.
    pub fn clone(&self, v: Value) -> Value {
        v
    }

    /// Dynamic-property lookup for transpiled base tests. The TS code
    /// does `exchange['options']`, `exchange['markets']`, etc. — this
    /// reads from the unified `to_value()` view so both access paths
    /// (`exchange.prop(k)` and `testSharedMethods.exchangeProp`) agree.
    pub fn prop(&self, key: &Value) -> Value {
        crate::get_value(&self.to_value(), key)
    }

    /// Full `Value::Map` snapshot of every transpiler-visible field —
    /// the Rust analogue of indexing a JS object by property name.
    /// Used by `prop()` and by the base-test `exchangeProp` shim.
    pub fn to_value(&self) -> Value {
        let mut m = indexmap::IndexMap::new();
        let mut put = |k: &str, v: &Value| {
            m.insert(k.to_string(), v.clone());
        };
        put("id", &self.id);
        put("name", &self.name);
        put("countries", &self.countries);
        put("version", &self.version);
        put("alias", &self.alias);
        put("certified", &self.certified);
        put("pro", &self.pro);
        put("hostname", &self.hostname);
        put("apiKey", &self.apiKey);
        put("secret", &self.secret);
        put("password", &self.password);
        put("uid", &self.uid);
        put("walletAddress", &self.walletAddress);
        put("privateKey", &self.privateKey);
        put("twofa", &self.twofa);
        put("token", &self.token);
        put("login", &self.login);
        put("accountId", &self.accountId);
        put("requiredCredentials", &self.requiredCredentials);
        put("timeout", &self.timeout);
        put("rateLimit", &self.rateLimit);
        put("enableRateLimit", &self.enableRateLimit);
        put("fetchHistoryCacheSize", &self.fetchHistoryCacheSize);
        put("rateLimiterAlgorithm", &self.rateLimiterAlgorithm);
        put("rollingWindowSize", &self.rollingWindowSize);
        put("tokenBucket", &self.tokenBucket);
        put("verbose", &self.verbose);
        put("isSandboxModeEnabled", &self.isSandboxModeEnabled);
        put("proxy", &self.proxy);
        put("proxyUrl", &self.proxyUrl);
        put("proxy_url", &self.proxy_url);
        put("proxyUrlCallback", &self.proxyUrlCallback);
        put("proxy_url_callback", &self.proxy_url_callback);
        put("httpProxy", &self.httpProxy);
        put("http_proxy", &self.http_proxy);
        put("httpProxyCallback", &self.httpProxyCallback);
        put("http_proxy_callback", &self.http_proxy_callback);
        put("httpsProxy", &self.httpsProxy);
        put("https_proxy", &self.https_proxy);
        put("httpsProxyCallback", &self.httpsProxyCallback);
        put("https_proxy_callback", &self.https_proxy_callback);
        put("socksProxy", &self.socksProxy);
        put("socks_proxy", &self.socks_proxy);
        put("socksProxyCallback", &self.socksProxyCallback);
        put("socks_proxy_callback", &self.socks_proxy_callback);
        put("wsProxy", &self.wsProxy);
        put("ws_proxy", &self.ws_proxy);
        put("wssProxy", &self.wssProxy);
        put("wss_proxy", &self.wss_proxy);
        put("wsSocksProxy", &self.wsSocksProxy);
        put("ws_socks_proxy", &self.ws_socks_proxy);
        put("markets", &self.markets);
        put("markets_by_id", &self.markets_by_id);
        put("currencies", &self.currencies);
        put("currencies_by_id", &self.currencies_by_id);
        put("commonCurrencies", &self.commonCurrencies);
        put("baseCurrencies", &self.baseCurrencies);
        put("quoteCurrencies", &self.quoteCurrencies);
        put("symbols", &self.symbols);
        put("ids", &self.ids);
        put("codes", &self.codes);
        put("timeframes", &self.timeframes);
        put("precision", &self.precision);
        put("limits", &self.limits);
        put("fees", &self.fees);
        put("features", &self.features);
        put("has", &self.has);
        put("exceptions", &self.exceptions);
        put("urls", &self.urls);
        put("api", &self.api);
        put("options", &self.options);
        put("headers", &self.headers);
        put("accounts", &self.accounts);
        put("accountsById", &self.accountsById);
        put("orderbooks", &self.orderbooks);
        put("orders", &self.orders);
        put("trades", &self.trades);
        put("myTrades", &self.myTrades);
        put("positions", &self.positions);
        put("tickers", &self.tickers);
        put("bidsasks", &self.bidsasks);
        put("ohlcvs", &self.ohlcvs);
        put("clients", &self.clients);
        put("balance", &self.balance);
        put("liquidations", &self.liquidations);
        put("myLiquidations", &self.myLiquidations);
        put("transactions", &self.transactions);
        put("reloadingMarkets", &self.reloadingMarkets);
        put("marketsLoading", &self.marketsLoading);
        put("last_request_url", &self.last_request_url);
        put("last_request_headers", &self.last_request_headers);
        put("last_request_body", &self.last_request_body);
        put("last_http_response", &self.last_http_response);
        put("last_response_headers", &self.last_response_headers);
        put("last_json_response", &self.last_json_response);
        put("lastRestRequestTimestamp", &self.lastRestRequestTimestamp);
        put("paddingMode", &self.paddingMode);
        put("precisionMode", &self.precisionMode);
        put(
            "substituteCommonCurrencyCodes",
            &self.substituteCommonCurrencyCodes,
        );
        put("reduceFees", &self.reduceFees);
        put("minFundingAddressLength", &self.minFundingAddressLength);
        put("MAX_VALUE", &self.MAX_VALUE);
        put("returnResponseHeaders", &self.returnResponseHeaders);
        put("httpExceptions", &self.httpExceptions);
        put("status", &self.status);
        put("userAgent", &self.userAgent);
        put("user_agent", &self.user_agent);
        put("userAgents", &self.userAgents);
        Value::Map(m)
    }

    pub fn base16ToBinary(&self, hex: Value, optional_args: &[Value]) -> Value {
        self.base16_to_binary(hex, optional_args)
    }
    pub fn base58_to_binary(&self, s: Value, _optional_args: &[Value]) -> Value {
        match &s {
            Value::Str(s) => match base58_decode(s) {
                Some(b) => bytes_to_value(&b),
                None => Value::Null,
            },
            _ => Value::Null,
        }
    }
    pub fn binary_to_base58(&self, b: Value, _optional_args: &[Value]) -> Value {
        Value::Str(base58_encode(&crate::exchange::value_to_bytes(&b)))
    }
    pub fn binary_length(&self, b: Value, _optional_args: &[Value]) -> Value {
        match b {
            Value::Arr(a) => Value::Int(a.len() as i64),
            _ => Value::Int(0),
        }
    }
    /// `isBinaryMessage` — in this port a binary buffer is a
    /// `Value::Array` of byte-valued ints (see `base16_to_binary`,
    /// `string_to_binary`). `instanceof Uint8Array` can't transpile.
    pub fn is_binary_message(&self, v: Value, _optional_args: &[Value]) -> Value {
        Value::Bool(is_byte_array(&v))
    }
    /// `numberToBE(n, padding)` — big-endian byte array of width `padding`.
    pub fn number_to_be(&self, n: Value, optional_args: &[Value]) -> Value {
        let num: u64 = match &n {
            Value::Int(i) => (*i).max(0) as u64,
            Value::Float(f) => (*f).max(0.0) as u64,
            Value::Str(s) => s.parse().unwrap_or(0),
            _ => 0,
        };
        let padding: usize = match optional_args.get(0) {
            Some(Value::Int(i)) => (*i).max(0) as usize,
            Some(Value::Float(f)) => (*f).max(0.0) as usize,
            _ => 8,
        };
        let be = num.to_be_bytes(); // 8 bytes
        let mut out: Vec<u8> = Vec::with_capacity(padding);
        if padding >= be.len() {
            out.extend(std::iter::repeat(0u8).take(padding - be.len()));
            out.extend_from_slice(&be);
        } else {
            out.extend_from_slice(&be[be.len() - padding..]);
        }
        bytes_to_value(&out)
    }
    /// `parseDate(x)` — ports `functions/time.ts`. Parses an ISO-8601
    /// timestamp (with `T`/`Z` or a plain `YYYY-MM-DD HH:MM:SS` form,
    /// treated as UTC) into epoch milliseconds. Invalid → Null.
    pub fn parse_date(&self, s: Value, _optional_args: &[Value]) -> Value {
        let text = match &s {
            Value::Str(s) if !s.is_empty() => s.clone(),
            _ => return Value::Null,
        };
        // rfc3339 (handles the `T...Z` and offset forms)
        if let Ok(t) = chrono::DateTime::parse_from_rfc3339(&text) {
            return Value::Int(t.timestamp_millis());
        }
        // naive `YYYY-MM-DD HH:MM:SS[.fff]` — interpreted as UTC
        for fmt in [
            "%Y-%m-%d %H:%M:%S%.f",
            "%Y-%m-%d %H:%M:%S",
            "%Y-%m-%dT%H:%M:%S%.f",
            "%Y-%m-%dT%H:%M:%S",
        ] {
            if let Ok(ndt) = chrono::NaiveDateTime::parse_from_str(&text, fmt) {
                return Value::Int(ndt.and_utc().timestamp_millis());
            }
        }
        Value::Null
    }
    pub fn is_binary_message_var(&self, v: Value, _optional_args: &[Value]) -> Value {
        Value::Bool(is_byte_array(&v))
    }
    /// `ymd(timestamp, infix='')` — like `yyyymmdd` but the infix
    /// defaults to empty (TS `functions/time.ts`).
    pub fn ymd(&self, ts: Value, optional_args: &[Value]) -> Value {
        if optional_args.is_empty() {
            self.yyyymmdd(ts, &[Value::Str(String::new())])
        } else {
            self.yyyymmdd(ts, optional_args)
        }
    }
    pub fn safe_float_n(&self, obj: Value, keys: Value, optional_args: &[Value]) -> Value {
        if let Value::Arr(ks) = keys {
            for k in ks.iter().cloned() {
                let v = self.safe_float(obj.clone(), k, &[]);
                if !v.is_null() {
                    return v;
                }
            }
        }
        arg_default(optional_args)
    }
    pub fn safe_float2(&self, obj: Value, k1: Value, k2: Value, optional_args: &[Value]) -> Value {
        let v = self.safe_float(obj.clone(), k1, &[]);
        if !v.is_null() {
            return v;
        }
        let v = self.safe_float(obj, k2, &[]);
        if !v.is_null() {
            v
        } else {
            arg_default(optional_args)
        }
    }
    // The transpiler may emit camelCase callers (`safeString2`) directly
    // because the rust-port preserved the TS name. Alias to the
    // snake-cased one we already have.
    pub fn safeString2(&self, obj: Value, k1: Value, k2: Value, optional_args: &[Value]) -> Value {
        self.safe_string2(obj, k1, k2, optional_args)
    }
    /// `binaryConcat(a, b, ...)` — concatenates byte buffers. The
    /// variadic call-site wrap folds the tail args into `optional_args`.
    pub fn binary_concat(&self, first: Value, optional_args: &[Value]) -> Value {
        let mut out = crate::exchange::value_to_bytes(&first);
        for v in optional_args {
            out.extend(crate::exchange::value_to_bytes(v));
        }
        bytes_to_value(&out)
    }
    pub fn stringToBase64(&self, s: Value, optional_args: &[Value]) -> Value {
        self.string_to_base64(s, optional_args)
    }
    /// `binaryToString(buffer)` — decodes a byte buffer as UTF-8.
    pub fn binary_to_string(&self, b: Value, _optional_args: &[Value]) -> Value {
        let bytes = crate::exchange::value_to_bytes(&b);
        Value::Str(String::from_utf8_lossy(&bytes).into_owned())
    }
    pub fn string_to_binary(&self, s: Value, _optional_args: &[Value]) -> Value {
        match s {
            Value::Str(s) => Value::Array(s.bytes().map(|b| Value::Int(b as i64)).collect()),
            _ => Value::Array(vec![]),
        }
    }
    /// `urlencodeBase64(payload)` — url-safe base64 with `+/` → `-_`
    /// and trailing `=` padding stripped. Accepts a string or a byte
    /// buffer (`Value::Array`).
    pub fn urlencode_base64(&self, v: Value, _optional_args: &[Value]) -> Value {
        let bytes = match &v {
            Value::Str(s) => s.as_bytes().to_vec(),
            _ => crate::exchange::value_to_bytes(&v),
        };
        let encoded = match self.binary_to_base64(bytes_to_value(&bytes), &[]) {
            Value::Str(s) => s,
            _ => String::new(),
        };
        let trimmed = encoded.trim_end_matches('=');
        Value::Str(trimmed.replace('+', "-").replace('/', "_"))
    }
    /// `urlencodeNested(object)` — `qs.stringify(object, { encodeValuesOnly: true })`:
    /// nested maps → `parent[child]`, arrays → `parent[index]`, only
    /// values are percent-encoded.
    pub fn urlencode_nested(&self, v: Value, _optional_args: &[Value]) -> Value {
        let m = match &v {
            Value::Dict(m) => m,
            _ => return Value::Str(String::new()),
        };
        let mut pairs: Vec<String> = Vec::new();
        for (k, val) in m.iter() {
            urlencode_nested_walk(k, val, &mut pairs);
        }
        Value::Str(pairs.join("&"))
    }
    pub fn is_json_encoded_object(&self, optional_args: &[Value]) -> Value {
        match optional_args.get(0) {
            Some(Value::Str(s)) => Value::Bool(s.starts_with('{') || s.starts_with('[')),
            _ => Value::Bool(false),
        }
    }
    pub fn strip(&self, s: Value, _optional_args: &[Value]) -> Value {
        match s {
            Value::Str(s) => Value::Str(s.trim().to_string()),
            other => other,
        }
    }
    pub fn sort(&self, v: Value, _optional_args: &[Value]) -> Value {
        if let Value::Arr(a) = v {
            let mut a = Arc::try_unwrap(a).unwrap_or_else(|x| (*x).clone());
            a.sort_by(|x, y| {
                let xs = crate::runtime::stringify_param(x);
                let ys = crate::runtime::stringify_param(y);
                xs.cmp(&ys)
            });
            Value::Array(a)
        } else {
            v
        }
    }
    /// `roundTimeframe(timeframe, timestamp, direction)` — snap a
    /// timestamp to the timeframe boundary (down by default, up for
    /// `ROUND_UP`).
    pub fn round_timeframe(&self, tf: Value, ts: Value, direction: Value) -> Value {
        let secs = match self.parse_timeframe(tf) {
            Value::Int(s) => s,
            Value::Float(s) => s as i64,
            _ => return ts,
        };
        let ms = secs * 1000;
        let t = match &ts {
            Value::Int(i) => *i,
            Value::Float(f) => *f as i64,
            _ => return ts,
        };
        if ms == 0 {
            return ts;
        }
        let offset = t % ms;
        let is_up = matches!(&direction, Value::Int(d) if *d == crate::runtime::ROUND_UP);
        Value::Int(t - offset + if is_up { ms } else { 0 })
    }
    pub async fn sleep(&self, ms: Value) -> Value {
        if let Some(n) = match ms {
            Value::Int(n) => Some(n),
            Value::Float(f) => Some(f as i64),
            _ => None,
        } {
            if n > 0 {
                tokio::time::sleep(std::time::Duration::from_millis(n as u64)).await;
            }
        }
        Value::Null
    }
    /// `ethGetAddressFromPrivateKey(pk)` — derive the Ethereum address
    /// (keccak256 of the uncompressed secp256k1 public key, last 20 bytes).
    pub fn eth_get_address_from_private_key(&self, pk: Value, _optional_args: &[Value]) -> Value {
        use k256::ecdsa::SigningKey;
        let pk_s = match &pk {
            Value::Str(s) => s.trim_start_matches("0x").to_string(),
            _ => return Value::Str(String::new()),
        };
        let pk_bytes = match hex::decode(&pk_s) {
            Ok(b) => b,
            Err(_) => return Value::Str(String::new()),
        };
        let sk = match SigningKey::from_slice(&pk_bytes) {
            Ok(k) => k,
            Err(_) => return Value::Str(String::new()),
        };
        let point = sk.verifying_key().to_encoded_point(false); // 0x04 || X || Y
        let pubkey = &point.as_bytes()[1..]; // 64 bytes
        let hash = crate::exchange::hash_raw(pubkey, "keccak");
        Value::Str(format!("0x{}", hex::encode(&hash[12..32])))
    }
    pub fn exists_file(&self, p: Value) -> Value {
        match &p {
            Value::Str(path) => Value::Bool(std::path::Path::new(path).is_file()),
            _ => Value::Bool(false),
        }
    }
    pub fn read_file(&self, p: Value) -> Value {
        match &p {
            Value::Str(path) => match std::fs::read_to_string(path) {
                Ok(s) => Value::Str(s),
                Err(_) => Value::Null,
            },
            _ => Value::Null,
        }
    }
    pub fn write_file(&self, p: Value, content: Value) -> Value {
        let path = match &p {
            Value::Str(s) => s.clone(),
            _ => return Value::Bool(false),
        };
        let body = match &content {
            Value::Str(s) => s.clone(),
            other => stringify_param(other),
        };
        Value::Bool(std::fs::write(&path, body).is_ok())
    }
    pub fn get_temp_dir(&self) -> Value {
        Value::Str(format!(
            "{}/",
            std::env::temp_dir().to_string_lossy().trim_end_matches('/')
        ))
    }
    pub fn get_property(&self, obj: Value, key: Value) -> Value {
        crate::get_value(&obj, &Value::Str(stringify_param(&key)))
    }

    pub fn yymmdd(&self, ts: Value, optional_args: &[Value]) -> Value {
        let infix = match optional_args.get(0) {
            Some(Value::Str(s)) => s.clone(),
            _ => String::new(),
        };
        let n = match ts {
            Value::Int(n) => n,
            _ => 0,
        };
        let dt = chrono::DateTime::<chrono::Utc>::from_timestamp_millis(n);
        match dt {
            Some(t) => Value::Str(t.format(&format!("%y{i}%m{i}%d", i = infix)).to_string()),
            None => Value::Null,
        }
    }
    pub fn yyyymmdd(&self, ts: Value, optional_args: &[Value]) -> Value {
        // TS default infix for yyyymmdd is '-'.
        let infix = match optional_args.get(0) {
            Some(Value::Str(s)) => s.clone(),
            _ => "-".to_string(),
        };
        let n = match ts {
            Value::Int(n) => n,
            _ => 0,
        };
        let dt = chrono::DateTime::<chrono::Utc>::from_timestamp_millis(n);
        match dt {
            Some(t) => Value::Str(t.format(&format!("%Y{i}%m{i}%d", i = infix)).to_string()),
            None => Value::Null,
        }
    }
    /// `ymdhms(timestamp, infix=' ')` — UTC `YYYY-MM-DD<infix>HH:MM:SS`.
    pub fn ymdhms(&self, ts: Value, optional_args: &[Value]) -> Value {
        let infix = match optional_args.get(0) {
            Some(Value::Str(s)) => s.clone(),
            _ => " ".to_string(),
        };
        let n = match ts {
            Value::Int(n) => n,
            Value::Float(f) => f as i64,
            _ => 0,
        };
        let dt = chrono::DateTime::<chrono::Utc>::from_timestamp_millis(n);
        match dt {
            Some(t) => Value::Str(t.format(&format!("%Y-%m-%d{infix}%H:%M:%S")).to_string()),
            None => Value::Null,
        }
    }

    /// `rawencode(params, [doSeq])` — URL-encode without sorting.
    /// `rawencode(object)` — `qs.stringify(object, { encode: false })`:
    /// flat `key=value` pairs with no percent-encoding.
    pub fn rawencode(&self, params: Value, _optional_args: &[Value]) -> Value {
        let m = match &params {
            Value::Dict(m) => m,
            _ => return Value::Str(String::new()),
        };
        let pairs: Vec<String> = m
            .iter()
            .map(|(k, v)| format!("{}={}", k, stringify_param(v)))
            .collect();
        Value::Str(pairs.join("&"))
    }

    /// `intToBase16(n)` — int → lowercase hex string.
    pub fn int_to_base16(&self, n: Value, _optional_args: &[Value]) -> Value {
        let v = match n {
            Value::Int(n) => n as u64,
            Value::Float(f) => f as u64,
            Value::Str(s) => s.parse::<u64>().unwrap_or(0),
            _ => 0,
        };
        Value::Str(format!("{:x}", v))
    }

    /// `packb(action)` — MessagePack pack stub. Hyperliquid signs the
    /// packed bytes of an action. Returning empty bytes here is enough
    /// to make the call compile; live signing won't be correct.
    /// `packb(value)` — MessagePack-encode a Value into a byte array.
    pub fn packb(&self, action: Value, _optional_args: &[Value]) -> Value {
        fn to_rmpv(v: &Value) -> rmpv::Value {
            match v {
                Value::Null => rmpv::Value::Nil,
                Value::Bool(b) => rmpv::Value::Boolean(*b),
                Value::Int(i) => rmpv::Value::Integer((*i).into()),
                Value::Float(f) => rmpv::Value::F64(*f),
                Value::Str(s) => rmpv::Value::String(s.clone().into()),
                Value::Arr(a) => rmpv::Value::Array(a.iter().map(to_rmpv).collect()),
                Value::Dict(m) => rmpv::Value::Map(
                    m.iter()
                        .map(|(k, v)| (rmpv::Value::String(k.clone().into()), to_rmpv(v)))
                        .collect(),
                ),
            }
        }
        let mut buf: Vec<u8> = Vec::new();
        match rmpv::encode::write_value(&mut buf, &to_rmpv(&action)) {
            Ok(_) => Value::Array(buf.iter().map(|b| Value::Int(*b as i64)).collect()),
            Err(_) => Value::Null,
        }
    }

    /// `ethEncodeStructuredData(domain, types, message)` — EIP-712
    /// encoding: returns the `\x19\x01 || domainSeparator || structHash`
    /// preimage that the caller keccak-hashes before signing.
    pub fn eth_encode_structured_data(&self, domain: Value, types: Value, message: Value) -> Value {
        let bytes = crate::exchange::eip712_encode(&domain, &types, &message);
        Value::Array(bytes.iter().map(|b| Value::Int(*b as i64)).collect())
    }

    /// `uuid22()` — pseudo-random 22-char hex id.
    pub fn uuid22(&self, _optional_args: &[Value]) -> Value {
        Value::Str(random_hex(22))
    }
    /// `uuid16()` — pseudo-random 16-char hex id.
    pub fn uuid16(&self, _optional_args: &[Value]) -> Value {
        Value::Str(random_hex(16))
    }
    /// `uuid()` — pseudo-random UUID v4: `xxxxxxxx-xxxx-4xxx-[89ab]xxx-xxxxxxxxxxxx`.
    pub fn uuid(&self, _optional_args: &[Value]) -> Value {
        let h = random_hex(32);
        let variant = ['8', '9', 'a', 'b'][(random_u64() % 4) as usize];
        let s = format!(
            "{}-{}-4{}-{}{}-{}",
            &h[0..8],
            &h[8..12],
            &h[13..16],
            variant,
            &h[17..20],
            &h[20..32],
        );
        Value::Str(s)
    }

    /// `urlencode(params)` — wraps the typed `urlencode_kv` for Value args.
    pub fn urlencode(&self, params: Value, _optional_args: &[Value]) -> Value {
        Value::Str(self.urlencode_kv(&params))
    }

    /// `json(value)` — JSON-stringify.
    pub fn json(&self, v: Value) -> Value {
        Value::Str(self.json_str(&v))
    }

    /// `encode(s)` — UTF-8 encode string to bytes (as Value::Array of ints).
    pub fn encode(&self, s: Value) -> Value {
        match s {
            Value::Str(s) => Value::Array(s.bytes().map(|b| Value::Int(b as i64)).collect()),
            _ => Value::Array(vec![]),
        }
    }

    /// `decode(bytes)` — decode bytes to a UTF-8 string.
    pub fn decode(&self, b: Value) -> Value {
        match b {
            Value::Str(s) => Value::Str(s),
            Value::Arr(a) => {
                let bytes: Vec<u8> = a
                    .iter()
                    .filter_map(|v| match v {
                        Value::Int(n) => Some(*n as u8),
                        _ => None,
                    })
                    .collect();
                Value::Str(String::from_utf8_lossy(&bytes).to_string())
            }
            _ => Value::Str(String::new()),
        }
    }

    /// `log(msg)` — debug print.
    pub fn log(&self, msg: Value) {
        if matches!(self.verbose, Value::Bool(true)) {
            println!("[ccxt] {}", crate::runtime::stringify_param(&msg));
        }
    }

    // Dynamic dispatch for the implicit API (`public_get_exchange_info`, …)
    // now lives in the `ExchangeRuntime::call_method` trait method (review #1),
    // routing through `implicit_api_call` → `request_typed` → `fetch_typed`.

    // ── string / number formatting ──────────────────────────────────────────

    pub fn number_to_string(&self, n: Value) -> Value {
        // TS `numberToString(undefined)` returns undefined — don't
        // stringify a missing value into the literal "null".
        if matches!(n, Value::Null) {
            return Value::Null;
        }
        Value::Str(stringify_param(&n))
    }

    pub fn parse_number(&self, v: Value, optional_args: &[Value]) -> Value {
        match v {
            Value::Float(_) | Value::Int(_) => v,
            Value::Str(ref s) => match s.parse::<f64>() {
                Ok(n) => Value::Float(n),
                Err(_) => arg_default(optional_args),
            },
            _ => arg_default(optional_args),
        }
    }

    pub fn parse_timeframe(&self, tf: Value) -> Value {
        let s = stringify_param(&tf);
        let last = s.chars().last().unwrap_or('m');
        let num: i64 = s[..s.len().saturating_sub(1)].parse().unwrap_or(0);
        Value::Int(match last {
            's' => num,
            'm' => num * 60,
            'h' => num * 3600,
            'd' => num * 86400,
            'w' => num * 604800,
            'M' => num * 2592000,
            'y' => num * 31536000,
            _ => 0,
        })
    }

    /// `precisionFromString(str)` — ports `functions/number.ts`.
    /// Scientific notation (`1e-4` → 4, `1e4` → -4); otherwise the
    /// number of fractional digits after stripping trailing zeros.
    pub fn precision_from_string(&self, s: Value) -> Value {
        let s = stringify_param(&s);
        if let Some(idx) = s.find(['e', 'E']) {
            let exp: i64 = s[idx + 1..].parse().unwrap_or(0);
            return Value::Int(-exp);
        }
        let trimmed = s.trim_end_matches('0');
        match trimmed.split_once('.') {
            Some((_, frac)) => Value::Int(frac.len() as i64),
            None => Value::Int(0),
        }
    }

    /// `decimalToPrecision(n, roundingMode, numPrecisionDigits, [countingMode], [paddingMode])`
    /// — faithful port of `_decimalToPrecision` from `functions/number.ts`.
    pub fn decimal_to_precision(
        &self,
        n: Value,
        rounding: Value,
        precision: Value,
        optional_args: &[Value],
    ) -> Value {
        let n_s = stringify_param(&n);
        let prec_f: f64 = match &precision {
            Value::Int(i) => *i as f64,
            Value::Float(f) => *f,
            Value::Str(s) => s.parse().unwrap_or(f64::NAN),
            _ => return Value::Str(n_s),
        };
        let counting: i64 = match optional_args.get(0) {
            Some(Value::Int(c)) => *c,
            _ => crate::runtime::DECIMAL_PLACES,
        };
        let padding: i64 = match optional_args.get(1) {
            Some(Value::Int(p)) => *p,
            _ => crate::runtime::NO_PADDING,
        };
        let rounding_mode: i64 = match &rounding {
            Value::Int(r) => *r,
            _ => crate::runtime::TRUNCATE,
        };
        Value::Str(decimal_to_precision_impl(
            &n_s,
            rounding_mode,
            prec_f,
            counting,
            padding,
        ))
    }

    pub fn capitalize(&self, s: Value) -> Value {
        let s = stringify_param(&s);
        let mut c = s.chars();
        Value::Str(match c.next() {
            None => String::new(),
            Some(f) => f.to_uppercase().chain(c).collect(),
        })
    }

    pub fn encode_uri_component(&self, s: Value) -> Value {
        let s = stringify_param(&s);
        Value::Str(
            s.bytes()
                .map(|b| match b {
                    b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                        (b as char).to_string()
                    }
                    _ => format!("%{b:02X}"),
                })
                .collect(),
        )
    }

    pub fn string_to_chars_array(&self, s: Value) -> Value {
        let s = stringify_param(&s);
        Value::Array(s.chars().map(|c| Value::Str(c.to_string())).collect())
    }

    pub fn string_to_base64(&self, s: Value, _optional_args: &[Value]) -> Value {
        self.binary_to_base64(s, &[])
    }

    /// `extract_params(path)` — pulls `{name}` placeholders from a URL
    /// template. Returns a Value::Array of names.
    pub fn extract_params(&self, path: Value) -> Value {
        let p = stringify_param(&path);
        let mut out: Vec<Value> = vec![];
        let mut chars = p.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '{' {
                let mut name = String::new();
                while let Some(&nc) = chars.peek() {
                    if nc == '}' {
                        chars.next();
                        break;
                    }
                    name.push(nc);
                    chars.next();
                }
                if !name.is_empty() {
                    out.push(Value::Str(name));
                }
            }
        }
        Value::Array(out)
    }

    // ── async lifecycle stubs ───────────────────────────────────────────────

    // Minimal port of TS `loadMarketsHelper`:
    //   1. Return the cached `markets` if already populated and not
    //      forcing a reload.
    //   2. If `has.fetchCurrencies === true`, dispatch `fetch_currencies`
    //      first — passing the result into `set_markets` avoids the
    //      O(N²) "synthesize from markets" branch (cloning the
    //      grouped-by-code Map inside an outer loop).
    //   3. Dispatch `fetch_markets` to the derived exchange.
    //   4. `set_markets(markets, currencies)` populates `markets`,
    //      `markets_by_id`, `symbols`, `ids`, and merges currencies.
    // Returns cached markets on hit; otherwise fetches markets (and
    // currencies when supported) and pushes everything through
    // `set_markets`. The built-in synthesize branch of `set_markets` is
    // O(N) per code with full Map clones —
    // for binance (~4k markets, ~1k unique codes) that grew to minutes
    // and looked like a hang. `load_markets_fast_synthesize_currencies`
    // below short-circuits when currencies are empty by synthesizing
    // them here in idiomatic Rust (HashMap-direct, no Value cloning of
    // the whole grouped map per code).

    // Proxy callback "methods" — the post-processor rewrites
    // `self.proxy_url_callback(args)` → `self.proxy_url_callback_fn(args)`
    // so the field `proxy_url_callback: Value` can coexist with this
    // method-form for the call sites.
    pub fn proxy_fn(&self, _url: Value, _method: Value, _headers: Value, _body: Value) -> Value {
        self.proxy.clone()
    }
    pub fn proxy_url_callback_fn(
        &self,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
    ) -> Value {
        self.proxy_url_callback.clone()
    }
    pub fn http_proxy_callback_fn(
        &self,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
    ) -> Value {
        self.http_proxy_callback.clone()
    }
    pub fn https_proxy_callback_fn(
        &self,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
    ) -> Value {
        self.https_proxy_callback.clone()
    }
    pub fn socks_proxy_callback_fn(
        &self,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
    ) -> Value {
        self.socks_proxy_callback.clone()
    }
    pub fn ws_proxy_callback_fn(
        &self,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
    ) -> Value {
        Value::Null
    }
    pub fn wss_proxy_callback_fn(
        &self,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
    ) -> Value {
        Value::Null
    }
    pub fn ws_socks_proxy_callback_fn(
        &self,
        _url: Value,
        _method: Value,
        _headers: Value,
        _body: Value,
    ) -> Value {
        Value::Null
    }

    pub fn init_throttler(&mut self) { /* stub */
    }
    /// Leaky-bucket rate limiter, mirroring `ts/src/base/functions/throttle.ts`.
    /// `rateLimit` is milliseconds-per-token, so `refillRate = 1/rateLimit`
    /// tokens/ms; `tokens` may go negative (a request proceeds when `tokens >= 0`
    /// and then subtracts its cost), and the next request waits until the bucket
    /// refills back to zero. State lives in `internals.throttle` behind an async
    /// mutex, so concurrent calls on one instance serialize (the TS single queue).
    /// A no-op when `enableRateLimit` is false or the rate is effectively
    /// unlimited.
    pub async fn throttle(&self, cost: &[Value]) -> Value {
        if !matches!(self.enableRateLimit, Value::Bool(true)) {
            return Value::Null;
        }
        let tb = |key: &str| -> Option<f64> {
            crate::get_value(&self.tokenBucket, &Value::Str(key.to_string())).as_f64()
        };
        let rate_limit = self.rateLimit.as_f64().unwrap_or(0.0);
        let refill_rate = tb("refillRate")
            .unwrap_or_else(|| if rate_limit > 0.0 { 1.0 / rate_limit } else { f64::MAX });
        if !(refill_rate.is_finite() && refill_rate > 0.0) {
            return Value::Null; // effectively unlimited
        }
        let capacity = tb("capacity").unwrap_or(1.0);
        let delay_ms = tb("delay").unwrap_or(0.001) * 1000.0;
        let this_cost = cost.first().and_then(|v| v.as_f64())
            .unwrap_or_else(|| tb("cost").unwrap_or(1.0));

        let now_ms = || std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as i64)
            .unwrap_or(0);

        let mut guard = self.internals.throttle.lock().await;
        let (tokens, last_ms) = &mut *guard;
        if *last_ms == 0 {
            *last_ms = now_ms();
        }
        // Refill for the elapsed time (capped at capacity).
        let n = now_ms();
        *tokens = (*tokens + refill_rate * (n - *last_ms).max(0) as f64).min(capacity);
        *last_ms = n;
        // In deficit → wait until the bucket refills to zero, holding the lock so
        // requests stay serialized.
        if *tokens < 0.0 {
            let wait = ((-*tokens) / refill_rate).max(delay_ms);
            tokio::time::sleep(std::time::Duration::from_millis(wait.ceil() as u64)).await;
            let n2 = now_ms();
            *tokens = (*tokens + refill_rate * (n2 - *last_ms).max(0) as f64).min(capacity);
            *last_ms = n2;
        }
        *tokens -= this_cost;
        Value::Null
    }

    pub fn resolve<T>(&self, v: T) -> T {
        v
    }
    pub fn reject(&self, err: ExchangeError) -> ExchangeError {
        err
    }
}

/// Synthesizes a `code → currency` map from a list of markets, mirroring
/// what `Exchange::set_markets`' else-branch does in TS — but in straight
/// Rust without re-cloning the grouped Map per outer iteration. For
/// binance (~4k markets, ~1k codes) the transpiled path took minutes;
/// this version completes in milliseconds.
pub(crate) fn synthesize_currencies_from_markets(markets: &Value, precision_mode: &Value) -> Value {
    let market_list: Vec<&Value> = match markets {
        Value::Arr(a) => a.iter().collect(),
        Value::Dict(m) => m.values().collect(),
        _ => return Value::Map(HashMap::new()),
    };
    let is_tick_size = matches!(precision_mode, Value::Int(n) if *n == crate::runtime::TICK_SIZE);
    let default_precision = if is_tick_size {
        Value::Float(1e-8)
    } else {
        Value::Int(8)
    };

    // Helper to compare precision: tick size → smaller is more precise;
    // decimal places → larger is more precise.
    let more_precise = |new: &Value, cur: &Value| -> bool {
        let to_f = |v: &Value| -> f64 {
            match v {
                Value::Int(n) => *n as f64,
                Value::Float(f) => *f,
                Value::Str(s) => s.parse().unwrap_or(f64::NAN),
                _ => f64::NAN,
            }
        };
        let n = to_f(new);
        let c = to_f(cur);
        if n.is_nan() {
            return false;
        }
        if c.is_nan() {
            return true;
        }
        if is_tick_size {
            n < c
        } else {
            n > c
        }
    };

    // Pull out the "code", "id", "precision" triple for each side. We
    // dedupe by code, keeping the most-precise instance.
    let mut by_code: HashMap<String, HashMap<String, Value>> = HashMap::new();
    for market in market_list {
        let market_precision = crate::get_value(market, &Value::Str("precision".to_string()));
        // base side
        for (id_key, code_key, prec_keys) in [
            ("baseId", "base", ["base", "amount"]),
            ("quoteId", "quote", ["quote", "price"]),
        ] {
            let code = crate::get_value(market, &Value::Str(code_key.to_string()));
            let code_s = match &code {
                Value::Str(s) if !s.is_empty() => s.clone(),
                _ => continue,
            };
            let id = crate::get_value(market, &Value::Str(id_key.to_string()));
            let id_v = match &id {
                Value::Null => Value::Str(code_s.clone()),
                other => other.clone(),
            };
            let mut precision =
                crate::get_value(&market_precision, &Value::Str(prec_keys[0].to_string()));
            if matches!(precision, Value::Null) {
                precision =
                    crate::get_value(&market_precision, &Value::Str(prec_keys[1].to_string()));
            }
            if matches!(precision, Value::Null) {
                precision = default_precision.clone();
            }
            let entry = by_code.entry(code_s.clone()).or_insert_with(|| {
                let mut m = HashMap::new();
                m.insert("id".to_string(), id_v.clone());
                m.insert("numericId".to_string(), Value::Null);
                m.insert("code".to_string(), Value::Str(code_s.clone()));
                m.insert("precision".to_string(), precision.clone());
                m
            });
            let existing_precision = entry.get("precision").cloned().unwrap_or(Value::Null);
            if more_precise(&precision, &existing_precision) {
                entry.insert("precision".to_string(), precision.clone());
            }
        }
    }
    let result: HashMap<String, Value> = by_code
        .into_iter()
        .map(|(k, v)| (k, Value::Map(v)))
        .collect();
    Value::Map(result)
}

#[cfg(test)]
mod spawn_queue_tests {
    use super::*;

    // `delay(ms, method, …)` schedules a coroutine on a real deadline —
    // binance's `keepAliveListenKey` re-arms itself every 20 min this way.
    // Regression cover for the original stub, which elided `ms` entirely and
    // ran the coroutine on the next inbound frame instead.
    #[test]
    fn delayed_spawns_wait_for_their_deadline() {
        // Not yet due: held back, and reported as the next deadline.
        enqueue_spawn_after("keep_alive_listen_key", vec![], 10_000);
        assert!(drain_spawn_queue().is_empty(), "not due yet");
        let due_in = next_spawn_due().expect("a deadline is pending");
        assert!(due_in > std::time::Duration::from_secs(9));

        // An undelayed spawn still runs on the next drain, and does not
        // disturb the queued deadline.
        enqueue_spawn("handle_order_book_snapshot", vec![]);
        let batch = drain_spawn_queue();
        assert_eq!(batch.len(), 1);
        assert_eq!(batch[0].0, "handle_order_book_snapshot");
        assert!(next_spawn_due().is_some(), "the delayed item is still queued");
    }

    #[test]
    fn spawns_run_once_their_deadline_passes() {
        enqueue_spawn_after("keep_alive_listen_key", vec![], 1);
        std::thread::sleep(std::time::Duration::from_millis(5));
        assert_eq!(next_spawn_due(), Some(std::time::Duration::ZERO));
        let batch = drain_spawn_queue();
        assert_eq!(batch.len(), 1);
        assert_eq!(batch[0].0, "keep_alive_listen_key");
        assert!(next_spawn_due().is_none(), "queue drained");
    }
}