hypercall-client 0.5.0

Client library for interacting with Hypercall trading platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
//! TEE perp trading: Hyperliquid perp orders signed by the account's app key
//! (API wallet) and executed through Hypercall's TEE-held Hyperliquid API
//! wallet.
//!
//! # Flow
//!
//! 0. **Register the app key once** (manager wallet):
//!    [`HypercallClient::update_api_wallet`] adds it as an `Account.sol` API
//!    wallet. An options agent key (`ApproveAgent`, including keys made with
//!    "Create agent key" in account settings) is not enough on its own: TEE
//!    perp trades must be signed by an active on-chain API wallet, and
//!    anything else (including the manager itself) is rejected with
//!    `invalid_signature`. Wait for the registration directive to finalize
//!    before the app key signs.
//! 1. **Enroll the TEE API wallet once** (manager wallet):
//!    [`HypercallClient::enroll_tee_api_wallet`] requests the account's TEE
//!    wallet, signs the `HLAddApiWallet` approval in the `HypercallManagerSign`
//!    domain, submits it, and waits until Hyperliquid binds the wallet.
//! 2. **Approve the Hypercall builder once** (manager wallet, HyperEVM
//!    transaction): [`HypercallClient::approve_hypercall_builder`] calls
//!    `Account.approveZeroFeeBuilder(builder)` (Account 0.2.4 or newer). Orders
//!    are refused with [`TeePerpErrorCode::BuilderNotApproved`](crate::TeePerpErrorCode::BuilderNotApproved) until then.
//! 3. **Trade** with the app key (an active Account API wallet). Every action
//!    is EIP-712 signed in the `HypercallApiSign` domain.
//!
//! # Nonces
//!
//! The nonce is a Unix millisecond on the API server's clock. The server
//! derives the venue expiry as `nonce + 9000` and accepts it only when it falls
//! in `(server_now, server_now + 10000]`, so a nonce must lie in
//! `(server_now - 9000, server_now + 1000]`. Methods that take
//! `nonce: Option<u64>` stamp `None` from [`HypercallClient::next_tee_perp_nonce`],
//! which reads the server clock from the `/health` `Date` header (cached for a
//! minute) and keeps nonces strictly increasing per signer.
//!
//! # Outcomes
//!
//! A `200` response carries a [`TeePerpOrderStage`](crate::TeePerpOrderStage). `unknown` means the venue
//! may have received the action: reconcile before acting again, and never
//! resubmit the same intent through another route.
//!
//! A signed submission that fails in transit (transport error, timeout,
//! `5xx`, or an undecodable `2xx`) returns
//! [`ClientError::TeeSubmissionAmbiguous`] with the exact signed body. Resend
//! those bytes with [`HypercallClient::resend_tee_submission`] or reconcile;
//! calling the placing method again would sign a new nonce (and, for batches,
//! new nonce-derived cloids), which is a second order.
//!
//! # Example
//!
//! ```rust,no_run
//! use hypercall_client::{
//!     AccountAddress, ApproveHypercallBuilderParams, DirectiveDeliveryStatus, HypercallClient,
//!     HypercallWallet,
//!     HyperliquidInfoClient, PerpCancelByCloidParams, PerpLimitOrderParams, PerpPreviewAction,
//!     PerpPreviewRequest, PerpTimeInForce, Side, TeeApiWalletEnrollmentPolling, TeePerpBracketEntry,
//!     TeePerpBracketOrderParams, TeePerpExit, TeePerpLeverageParams, TeePerpMarketOrderParams,
//!     TeePerpTpsl, TeePerpTriggerExecution, TeePerpTriggerOrderParams, UpdateApiWalletParams,
//!     PERP_MARKET_SLIPPAGE_BPS,
//! };
//! use alloy::primitives::keccak256;
//! use rust_decimal::Decimal;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = HypercallClient::new("https://api.hypercall.xyz");
//!     let manager = HypercallWallet::from_private_key("0xYOUR_MANAGER_KEY", 999)?;
//!     let app_key = HypercallWallet::from_private_key("0xYOUR_API_WALLET_KEY", 999)?;
//!     let account: AccountAddress = "0xYOUR_ACCOUNT_SOL_ADDRESS".parse()?;
//!     let hyperliquid = HyperliquidInfoClient::mainnet();
//!
//!     // One-time setup with the manager wallet. An options agent key is not
//!     // enough: register the app key as an on-chain API wallet first.
//!     let registration = client
//!         .update_api_wallet(
//!             &manager,
//!             UpdateApiWalletParams {
//!                 account,
//!                 name: keccak256("tee-perps-app-key"),
//!                 api_wallet: app_key.address,
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     // Submission only enqueues the directive. Wait until it is final on
//!     // chain before the app key signs anything.
//!     loop {
//!         let status = client
//!             .get_directive_status(&registration.directive_id)
//!             .await?;
//!         match status.delivery_status {
//!             DirectiveDeliveryStatus::Finalized => break,
//!             DirectiveDeliveryStatus::CoreRejected
//!             | DirectiveDeliveryStatus::Reverted
//!             | DirectiveDeliveryStatus::Expired
//!             | DirectiveDeliveryStatus::DeadLettered => {
//!                 return Err(format!("API wallet registration failed: {status:?}").into())
//!             }
//!             _ => tokio::time::sleep(std::time::Duration::from_secs(1)).await,
//!         }
//!     }
//!     let enrollment = client
//!         .enroll_tee_api_wallet(
//!             &manager,
//!             account,
//!             &hyperliquid,
//!             TeeApiWalletEnrollmentPolling::default(),
//!         )
//!         .await?;
//!     println!("TEE API wallet: {}", enrollment.api_wallet);
//!     client
//!         .approve_hypercall_builder(
//!             &manager,
//!             ApproveHypercallBuilderParams {
//!                 rpc_url: "https://rpc.hyperliquid.xyz/evm".into(),
//!                 account,
//!                 builder: None, // read from /exchange-info hl_builder_address
//!             },
//!         )
//!         .await?;
//!
//!     // Market metadata and a read-only preview.
//!     let markets = client.get_tee_perp_markets().await?;
//!     let btc = markets
//!         .by_hyperliquid_coin("BTC")
//!         .ok_or("BTC is not configured")?;
//!     let preview = client
//!         .preview_tee_perp_action(&PerpPreviewRequest {
//!             account: account.into_wallet_address(),
//!             action: PerpPreviewAction::Leverage {
//!                 asset: btc.asset_id,
//!                 is_cross: true,
//!                 leverage: 5,
//!             },
//!         })
//!         .await?;
//!     println!(
//!         "{:?} {:?}, liquidation after {:?}",
//!         preview.decision, preview.reason_code, preview.liquidation_px_after
//!     );
//!
//!     // Leverage, then read the venue's setting back.
//!     client
//!         .update_tee_perp_leverage(
//!             &app_key,
//!             TeePerpLeverageParams {
//!                 account,
//!                 asset: btc.asset_id,
//!                 is_cross: true,
//!                 leverage: 5,
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     let venue = hyperliquid
//!         .active_asset_leverage(account, &btc.hyperliquid_coin)
//!         .await?;
//!     println!("venue leverage: {venue:?}");
//!
//!     // Limit (GTC, IOC, or ALO) and market (bounded IOC) orders.
//!     let limit = client
//!         .place_tee_perp_limit_order(
//!             &app_key,
//!             PerpLimitOrderParams {
//!                 account,
//!                 asset: btc.asset_id,
//!                 side: Side::Buy,
//!                 price: Decimal::new(95_000, 0),
//!                 size: Decimal::new(1, 3),
//!                 tif: PerpTimeInForce::Alo,
//!                 reduce_only: false,
//!                 client_order_id: Some(42),
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     if limit.requires_builder_approval() {
//!         return Err("approve the Hypercall builder first".into());
//!     }
//!     client
//!         .place_tee_perp_market_order(
//!             &app_key,
//!             TeePerpMarketOrderParams {
//!                 account,
//!                 asset: btc.asset_id,
//!                 side: Side::Buy,
//!                 size: Decimal::new(1, 3),
//!                 reference_price: Decimal::new(100_000, 0), // best ask
//!                 size_decimals: 5,
//!                 slippage_bps: Some(50),
//!                 reduce_only: false,
//!                 client_order_id: None,
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!
//!     // A stop-market order, and an entry with attached TP/SL.
//!     client
//!         .place_tee_perp_trigger_order(
//!             &app_key,
//!             TeePerpTriggerOrderParams {
//!                 account,
//!                 asset: btc.asset_id,
//!                 side: Side::Sell,
//!                 size: Decimal::new(1, 3),
//!                 trigger_price: Decimal::new(90_000, 0),
//!                 tpsl: TeePerpTpsl::Sl,
//!                 execution: TeePerpTriggerExecution::Market {
//!                     size_decimals: 5,
//!                     slippage_bps: None,
//!                 },
//!                 reduce_only: true,
//!                 client_order_id: None,
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     let exit = |trigger: i64| {
//!         TeePerpExit::market(
//!             Decimal::new(trigger, 0),
//!             Side::Sell,
//!             5,
//!             PERP_MARKET_SLIPPAGE_BPS,
//!         )
//!     };
//!     let bracket = client
//!         .place_tee_perp_bracket_order(
//!             &app_key,
//!             TeePerpBracketOrderParams {
//!                 account,
//!                 entry: TeePerpBracketEntry {
//!                     asset: btc.asset_id,
//!                     side: Side::Buy,
//!                     price: Decimal::new(99_000, 0),
//!                     size: Decimal::new(1, 3),
//!                     tif: PerpTimeInForce::Gtc,
//!                     reduce_only: false,
//!                 },
//!                 take_profit: Some(exit(110_000)?),
//!                 stop_loss: Some(exit(92_000)?),
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     println!("bracket {:?}: {} children", bracket.stage, bracket.children.len());
//!
//!     client
//!         .cancel_tee_perp_order_by_cloid(
//!             &app_key,
//!             PerpCancelByCloidParams {
//!                 account,
//!                 asset: btc.asset_id,
//!                 client_order_id: 42,
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     Ok(())
//! }
//! ```
//!
//! # Modify, batch modify and TWAP
//!
//! [`HypercallClient::modify_tee_perp_order`] and
//! [`HypercallClient::batch_modify_tee_perp_orders`] replace resting limit
//! orders; each batch child reports its own [`TeePerpOrderStage`](crate::TeePerpOrderStage) in
//! [`TeePerpBatchModifyResponse::children`].
//! [`HypercallClient::place_tee_perp_twap_order`] and
//! [`HypercallClient::cancel_tee_perp_twap`] drive native TWAPs. TWAP and
//! batch modify are enabled per enclave release; a deployment without them
//! answers `400 unsupported_action` ([`ClientError::is_unsupported_action`]).
//!
//! Hyperliquid parity. TWAP submit and cancel carry every `twapOrder` and
//! `twapCancel` field (`a`, `b`, `s`, `r`, `m`, `t`; `a`, `t`). Modify and batch
//! modify have these scope cuts:
//!
//! - Modify replaces limit orders only (GTC, IOC, ALO). Hyperliquid's
//!   `modify` can also turn an order into a trigger order; the TEE route does not
//!   accept that, so a resting trigger order must be cancelled and re-placed.
//! - The order is addressed by its cloid plus its engine `directiveId`, never by
//!   `oid`, and the modify keeps the cloid. Hyperliquid also accepts an `oid`
//!   target and a new cloid.
//! - A batch modify carries at most the instance cap (50 by default, 100 at
//!   most), where Hyperliquid documents no per-call limit.
//! - No builder field: Hyperliquid documents none on `modify`, `batchModify`,
//!   `twapOrder` or `twapCancel`.
//!
//! ```rust,no_run
//! use hypercall_client::{
//!     AccountAddress, HypercallClient, HypercallWallet, PerpTimeInForce, Side,
//!     TeePerpBatchModifyParams, TeePerpModifyParams, TeePerpOrderReplacement, TeePerpOrderStage,
//!     TeePerpTwapCancelParams, TeePerpTwapParams,
//! };
//! use rust_decimal::Decimal;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = HypercallClient::new("https://api.hypercall.xyz");
//!     let app_key = HypercallWallet::from_private_key("0xYOUR_API_WALLET_KEY", 999)?;
//!     let account: AccountAddress = "0xYOUR_ACCOUNT_SOL_ADDRESS".parse()?;
//!
//!     // `directiveId` and cloid returned when each order was placed.
//!     let bid = TeePerpOrderReplacement {
//!         asset: 0,
//!         target_directive_id: "01999b5a-7c1e-7000-8000-000000000042".parse()?,
//!         cloid: 42,
//!         side: Side::Buy,
//!         price: Decimal::new(99_500, 0),
//!         size: Decimal::new(2, 3),
//!         tif: PerpTimeInForce::Alo,
//!         reduce_only: false,
//!     };
//!     let ask = TeePerpOrderReplacement {
//!         target_directive_id: "01999b5a-7c1e-7000-8000-000000000043".parse()?,
//!         cloid: 43,
//!         side: Side::Sell,
//!         price: Decimal::new(100_500, 0),
//!         ..bid
//!     };
//!
//!     // One order.
//!     let single = client
//!         .modify_tee_perp_order(
//!             &app_key,
//!             TeePerpModifyParams {
//!                 account,
//!                 replacement: bid,
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     println!("modify {:?}", single.stage);
//!
//!     // Both quotes in one signed batchModify; each child settles on its own.
//!     let batch = client
//!         .batch_modify_tee_perp_orders(
//!             &app_key,
//!             TeePerpBatchModifyParams {
//!                 account,
//!                 replacements: vec![bid, ask],
//!                 max_children: None, // the server default, 50
//!                 nonce: None,
//!             },
//!         )
//!         .await?;
//!     for child in &batch.children {
//!         match child.stage {
//!             TeePerpOrderStage::Accepted => println!("{} moved", child.cloid),
//!             // Hyperliquid cancelled the original before the replacement failed.
//!             _ if child.venue_cancelled_original() => {
//!                 println!("{} cancelled: nothing rests any more", child.cloid)
//!             }
//!             TeePerpOrderStage::Rejected | TeePerpOrderStage::NotSubmitted => {
//!                 println!("{} unchanged: the old order still rests", child.cloid)
//!             }
//!             _ => println!("{} unknown: reconcile before touching it", child.cloid),
//!         }
//!     }
//!
//!     // A native TWAP. Deployments that have not enabled it answer
//!     // `400 unsupported_action`.
//!     match client
//!         .place_tee_perp_twap_order(
//!             &app_key,
//!             TeePerpTwapParams {
//!                 account,
//!                 asset: 0,
//!                 side: Side::Sell,
//!                 size: Decimal::new(5, 2),
//!                 reduce_only: true,
//!                 minutes: 30,
//!                 randomize: true,
//!                 nonce: None,
//!             },
//!         )
//!         .await
//!     {
//!         Ok(twap) => {
//!             if let Some(twap_id) = twap.twap_id {
//!                 client
//!                     .cancel_tee_perp_twap(
//!                         &app_key,
//!                         TeePerpTwapCancelParams {
//!                             account,
//!                             asset: 0,
//!                             twap_id,
//!                             nonce: None,
//!                         },
//!                     )
//!                     .await?;
//!             }
//!         }
//!         Err(error) if error.is_unsupported_action() => println!("TWAP is not enabled here"),
//!         Err(error) => return Err(error.into()),
//!     }
//!     Ok(())
//! }
//! ```

#![allow(
    clippy::result_large_err,
    reason = "these helpers preserve the established unboxed public ClientError contract"
)]

use std::collections::HashMap;
use std::sync::{Arc, Mutex as StdMutex, OnceLock};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use alloy::primitives::{Address, B256, U256};
use alloy::providers::{Provider, ProviderBuilder};
use alloy::sol;
use alloy::sol_types::{Eip712Domain, SolStruct};
use hypercall_sdk_types::tee_perp::{
    TEE_API_WALLET_APPROVAL_REQUEST_ROUTE, TEE_API_WALLET_APPROVAL_ROUTE,
    TEE_PERP_CANCEL_BY_OID_SUBMIT_ROUTE, TEE_PERP_CANCEL_SUBMIT_ROUTE,
    TEE_PERP_LEVERAGE_SUBMIT_ROUTE, TEE_PERP_MARKETS_ROUTE, TEE_PERP_ORDER_BATCH_MODIFY_ROUTE,
    TEE_PERP_ORDER_BATCH_SUBMIT_ROUTE, TEE_PERP_ORDER_MODIFY_ROUTE, TEE_PERP_ORDER_SUBMIT_ROUTE,
    TEE_PERP_PREVIEW_ROUTE, TEE_PERP_TRIGGER_ORDER_SUBMIT_ROUTE, TEE_PERP_TWAP_CANCEL_ROUTE,
    TEE_PERP_TWAP_SUBMIT_ROUTE,
};
use hypercall_sdk_types::{
    ApiResponse, DirectiveDeliveryStatus, DirectiveDomainStatus, DirectiveStatusResponse,
    DirectiveSubmitRequest, DirectiveSubmitResponse, ExchangeInfoResponse, HlCancelByCloidAction,
    HlCancelByOidAction, HlLimitOrderAction, PerpPreviewRequest, PerpPreviewResponse,
    PerpTimeInForce, Side, TeeApiWalletApprovalPackage, TeeApiWalletApprovalRequest,
    TeeApiWalletApprovalSubmitRequest, TeePerpCancelResponse, TeePerpLeverageAction,
    TeePerpLeverageResponse, TeePerpLimitOrderType, TeePerpMarketsResponse,
    TeePerpOrderBatchAction, TeePerpOrderBatchResponse, TeePerpOrderGrouping, TeePerpOrderResponse,
    TeePerpOrderType, TeePerpOrderWire, TeePerpTpsl, TeePerpTriggerOrderType,
    TeePerpVenueActionResponse, WalletAddress, MAX_TEE_PERP_ORDER_BATCH_CHILDREN,
    TEE_PERP_VENUE_TTL_MS,
};
use hypercall_sdk_types::{
    TeePerpBatchModifyAction, TeePerpBatchModifyResponse, TeePerpModifyAction,
    TeePerpModifyResponse, TeePerpTwapAction, TeePerpTwapCancelAction, TeePerpTwapResponse,
    DEFAULT_TEE_PERP_BATCH_MODIFY_MAX_CHILDREN, MAX_TEE_PERP_BATCH_MODIFY_CHILDREN,
    TEE_PERP_TWAP_MAX_MINUTES, TEE_PERP_TWAP_MIN_MINUTES,
};
use reqwest::Method;
use rust_decimal::Decimal;
use serde::Deserialize;

use crate::api::HypercallClient;
use crate::error::{ClientError, Result};
use crate::perp::{
    to_perp_units, PerpCancelByCloidParams, PerpCancelByOidParams, PerpLimitOrderParams,
};
use crate::wallet::{
    evm_submission_lock, AccountAddress, HypercallSigner, HypercallWallet,
    PerpCancelByCloidSignature, PerpCancelByOidSignature, PerpDirectiveSigner,
    PerpLimitOrderSignature,
};

/// Default market-order slippage bound used by the Hypercall order panel:
/// an IOC limit 2000 bps (20%) through the reference price, rounded to a
/// valid tick inside that bound. Pass a tighter bound where it matters.
pub const PERP_MARKET_SLIPPAGE_BPS: u32 = 2_000;
/// Hyperliquid perp prices carry at most five significant figures.
pub const HYPERLIQUID_PERP_MAX_PRICE_SIGNIFICANT_FIGURES: u32 = 5;
/// Hyperliquid perp prices carry at most `6 - szDecimals` decimals.
pub const HYPERLIQUID_PERP_MAX_PRICE_DECIMALS: u32 = 6;
/// Scale orders split into this many legs at least.
pub const PERP_SCALE_MIN_ORDERS: usize = 2;
/// Scale orders split into this many legs at most.
pub const PERP_SCALE_MAX_ORDERS: usize = 100;
/// Hyperliquid info endpoint on mainnet.
pub const HYPERLIQUID_MAINNET_INFO_URL: &str = "https://api.hyperliquid.xyz/info";
/// Hyperliquid info endpoint on testnet.
pub const HYPERLIQUID_TESTNET_INFO_URL: &str = "https://api.hyperliquid-testnet.xyz/info";

const BPS_DENOMINATOR: u32 = 10_000;
const SERVER_CLOCK_TTL: Duration = Duration::from_secs(60);
const SCALE_WORKING_SCALE: u32 = 12;
const MAX_BATCH_CLOID_INDEX: usize = 255;
pub(crate) const HYPERCALL_API_DOMAIN: &str = "HypercallApiSign";
const HYPERCALL_MANAGER_DOMAIN: &str = "HypercallManagerSign";

sol! {
    /// One signed order. A limit child signs its TIF byte with
    /// `triggerPx = 0`, `isMarket = false`, `tpsl = 0`. A trigger child signs
    /// `encodedTif = 0`, its trigger price, and `tpsl` 1 (TP) or 2 (SL).
    #[derive(Debug, PartialEq, Eq)]
    struct PerpOrderTerms {
        uint32 asset;
        bool isBuy;
        uint64 limitPx;
        uint64 sz;
        bool reduceOnly;
        uint8 encodedTif;
        uint64 triggerPx;
        bool isMarket;
        uint8 tpsl;
        uint128 cloid;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct HCPerpTriggerOrder {
        address account;
        uint64 nonce;
        PerpOrderTerms order;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct HCPerpOrderBatch {
        address account;
        uint64 nonce;
        uint8 grouping;
        PerpOrderTerms[] orders;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct PerpLeverageChange {
        uint32 asset;
        bool isCross;
        uint32 leverage;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct HCPerpLeverageChange {
        address account;
        uint64 nonce;
        PerpLeverageChange action;
    }

    /// Replacement terms of one modify. `targetDirectiveId` is the UUID as a
    /// big-endian u128. Field names and order match the server's
    /// `PerpModifyTerms` exactly; the digest depends on both.
    #[derive(Debug, PartialEq, Eq)]
    struct PerpModifyTerms {
        uint32 asset;
        uint128 targetDirectiveId;
        bool isBuy;
        uint64 limitPx;
        uint64 sz;
        bool reduceOnly;
        uint8 encodedTif;
        uint128 cloid;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct HCPerpOrderModify {
        address account;
        uint64 nonce;
        PerpModifyTerms action;
    }

    /// Every child's replacement terms, in request order.
    #[derive(Debug, PartialEq, Eq)]
    struct HCPerpOrderBatchModify {
        address account;
        uint64 nonce;
        PerpModifyTerms[] modifies;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct PerpTwapTerms {
        uint32 asset;
        bool isBuy;
        uint64 sz;
        bool reduceOnly;
        uint32 minutes;
        bool randomize;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct HCPerpTwapOrder {
        address account;
        uint64 nonce;
        PerpTwapTerms action;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct PerpTwapCancelTerms {
        uint32 asset;
        uint64 twapId;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct HCPerpTwapCancel {
        address account;
        uint64 nonce;
        PerpTwapCancelTerms action;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct AddApiWallet {
        address apiWalletAddress;
        string apiWalletName;
    }

    #[derive(Debug, PartialEq, Eq)]
    struct HLAddApiWallet {
        address account;
        uint64 nonce;
        AddApiWallet action;
    }

    #[sol(rpc)]
    interface HypercallAccountBuilderApproval {
        function approveZeroFeeBuilder(address builder) external;
    }
}

// ---------------------------------------------------------------------------
// Signing
// ---------------------------------------------------------------------------

/// App-key payload for `POST /v1/tee-perp/orders/submit-trigger`.
#[derive(Debug, Clone, Copy)]
pub struct TeePerpTriggerOrderSignature {
    pub account: AccountAddress,
    pub nonce: u64,
    pub order: TeePerpOrderWire,
}

/// App-key payload for `POST /v1/tee-perp/orders/submit-batch`.
#[derive(Debug, Clone, Copy)]
pub struct TeePerpOrderBatchSignature<'a> {
    pub account: AccountAddress,
    pub nonce: u64,
    pub batch: &'a TeePerpOrderBatchAction,
}

/// App-key payload for `POST /v1/tee-perp/leverage/submit`.
#[derive(Debug, Clone, Copy)]
pub struct TeePerpLeverageSignature {
    pub account: AccountAddress,
    pub nonce: u64,
    pub action: TeePerpLeverageAction,
}

/// App-key payload for `POST /v1/tee-perp/orders/modify` (`HCPerpOrderModify`).
#[derive(Debug, Clone, Copy)]
pub struct TeePerpModifySignature {
    pub account: AccountAddress,
    pub nonce: u64,
    pub action: TeePerpModifyAction,
}

/// App-key payload for `POST /v1/tee-perp/orders/batch-modify`
/// (`HCPerpOrderBatchModify`).
#[derive(Debug, Clone, Copy)]
pub struct TeePerpBatchModifySignature<'a> {
    pub account: AccountAddress,
    pub nonce: u64,
    pub batch: &'a TeePerpBatchModifyAction,
}

/// App-key payload for `POST /v1/tee-perp/twap/submit` (`HCPerpTwapOrder`).
#[derive(Debug, Clone, Copy)]
pub struct TeePerpTwapSignature {
    pub account: AccountAddress,
    pub nonce: u64,
    pub action: TeePerpTwapAction,
}

/// App-key payload for `POST /v1/tee-perp/twap/cancel` (`HCPerpTwapCancel`).
#[derive(Debug, Clone, Copy)]
pub struct TeePerpTwapCancelSignature {
    pub account: AccountAddress,
    pub nonce: u64,
    pub action: TeePerpTwapCancelAction,
}

/// Manager payload approving the account's TEE API wallet on Hyperliquid.
#[derive(Debug, Clone, Copy)]
pub struct TeeApiWalletApprovalSignature<'a> {
    pub account: AccountAddress,
    pub nonce: u64,
    pub api_wallet_address: AccountAddress,
    pub api_wallet_name: &'a str,
}

/// App-key signer for TEE perp routes. Plain limit orders and cancels reuse
/// the [`PerpDirectiveSigner`] schemas (`HLOrder`, `HLCancel`,
/// `HLCancelByCloid`); triggers, batches, leverage, modifies and TWAPs use
/// these.
#[expect(
    async_fn_in_trait,
    reason = "signers are used generically, never as Send-bound trait objects"
)]
pub trait TeePerpSigner: PerpDirectiveSigner {
    async fn sign_tee_perp_trigger_order_payload(
        &self,
        payload: TeePerpTriggerOrderSignature,
    ) -> Result<String>;
    async fn sign_tee_perp_order_batch_payload(
        &self,
        payload: TeePerpOrderBatchSignature<'_>,
    ) -> Result<String>;
    async fn sign_tee_perp_leverage_payload(
        &self,
        payload: TeePerpLeverageSignature,
    ) -> Result<String>;
    async fn sign_tee_perp_modify_payload(&self, payload: TeePerpModifySignature)
        -> Result<String>;
    async fn sign_tee_perp_batch_modify_payload(
        &self,
        payload: TeePerpBatchModifySignature<'_>,
    ) -> Result<String>;
    async fn sign_tee_perp_twap_payload(&self, payload: TeePerpTwapSignature) -> Result<String>;
    async fn sign_tee_perp_twap_cancel_payload(
        &self,
        payload: TeePerpTwapCancelSignature,
    ) -> Result<String>;
}

/// Manager signer for TEE API wallet enrollment.
#[expect(
    async_fn_in_trait,
    reason = "signers are used generically, never as Send-bound trait objects"
)]
pub trait TeeApiWalletManagerSigner: HypercallSigner {
    /// EIP-191 signature over Hypercall's timestamped authentication message.
    async fn sign_authentication_payload(&self, timestamp: i64) -> Result<String>;
    async fn sign_tee_api_wallet_approval_payload(
        &self,
        payload: TeeApiWalletApprovalSignature<'_>,
    ) -> Result<String>;
}

fn perp_order_terms(order: &TeePerpOrderWire) -> PerpOrderTerms {
    let (encoded_tif, trigger_px, is_market, tpsl) = match order.order_type {
        TeePerpOrderType::Limit(limit) => (limit.tif.encoded(), 0, false, 0),
        TeePerpOrderType::Trigger(trigger) => (
            0,
            trigger.trigger_px,
            trigger.is_market,
            trigger.tpsl.signed(),
        ),
    };
    PerpOrderTerms {
        asset: order.asset,
        isBuy: order.is_buy,
        limitPx: order.limit_px,
        sz: order.sz,
        reduceOnly: order.reduce_only,
        encodedTif: encoded_tif,
        triggerPx: trigger_px,
        isMarket: is_market,
        tpsl,
        cloid: order.cloid,
    }
}

fn trigger_order_message(payload: &TeePerpTriggerOrderSignature) -> Result<HCPerpTriggerOrder> {
    if !matches!(payload.order.order_type, TeePerpOrderType::Trigger(_)) {
        return Err(ClientError::InvalidInput(
            "a TEE trigger submission needs a trigger order".into(),
        ));
    }
    Ok(HCPerpTriggerOrder {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        order: perp_order_terms(&payload.order),
    })
}

fn order_batch_message(payload: &TeePerpOrderBatchSignature<'_>) -> Result<HCPerpOrderBatch> {
    validate_batch(payload.batch)?;
    Ok(HCPerpOrderBatch {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        grouping: payload.batch.grouping.signed(),
        orders: payload.batch.orders.iter().map(perp_order_terms).collect(),
    })
}

fn leverage_message(payload: &TeePerpLeverageSignature) -> HCPerpLeverageChange {
    HCPerpLeverageChange {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        action: PerpLeverageChange {
            asset: payload.action.asset,
            isCross: payload.action.is_cross,
            leverage: payload.action.leverage,
        },
    }
}

fn perp_modify_terms(action: &TeePerpModifyAction) -> PerpModifyTerms {
    PerpModifyTerms {
        asset: action.asset,
        targetDirectiveId: action.target_directive_id.as_u128(),
        isBuy: action.is_buy,
        limitPx: action.limit_px,
        sz: action.sz,
        reduceOnly: action.reduce_only,
        encodedTif: action.tif.encoded(),
        cloid: action.cloid,
    }
}

fn modify_message(payload: &TeePerpModifySignature) -> Result<HCPerpOrderModify> {
    validate_modify_action(&payload.action)?;
    Ok(HCPerpOrderModify {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        action: perp_modify_terms(&payload.action),
    })
}

fn batch_modify_message(
    payload: &TeePerpBatchModifySignature<'_>,
) -> Result<HCPerpOrderBatchModify> {
    // The signer enforces only the wire bound; the instance cap is the
    // request builder's and the server's to enforce.
    validate_batch_modify(payload.batch, MAX_TEE_PERP_BATCH_MODIFY_CHILDREN)?;
    Ok(HCPerpOrderBatchModify {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        modifies: payload
            .batch
            .modifies
            .iter()
            .map(perp_modify_terms)
            .collect(),
    })
}

fn twap_message(payload: &TeePerpTwapSignature) -> Result<HCPerpTwapOrder> {
    validate_twap_action(&payload.action)?;
    let action = payload.action;
    Ok(HCPerpTwapOrder {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        action: PerpTwapTerms {
            asset: action.asset,
            isBuy: action.is_buy,
            sz: action.sz,
            reduceOnly: action.reduce_only,
            minutes: action.minutes,
            randomize: action.randomize,
        },
    })
}

fn twap_cancel_message(payload: &TeePerpTwapCancelSignature) -> Result<HCPerpTwapCancel> {
    if payload.action.twap_id == 0 {
        return Err(ClientError::InvalidInput("twap_id must not be zero".into()));
    }
    Ok(HCPerpTwapCancel {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        action: PerpTwapCancelTerms {
            asset: payload.action.asset,
            twapId: payload.action.twap_id,
        },
    })
}

fn approval_message(payload: &TeeApiWalletApprovalSignature<'_>) -> HLAddApiWallet {
    HLAddApiWallet {
        account: payload.account.as_wallet_address().inner(),
        nonce: payload.nonce,
        action: AddApiWallet {
            apiWalletAddress: payload.api_wallet_address.as_wallet_address().inner(),
            apiWalletName: payload.api_wallet_name.to_string(),
        },
    }
}

pub(crate) fn signing_domain(name: &'static str, chain_id: u64) -> Result<Eip712Domain> {
    if !matches!(chain_id, 998 | 999) {
        return Err(ClientError::InvalidInput(format!(
            "unsupported directive chain id: {chain_id}"
        )));
    }
    Ok(Eip712Domain {
        name: Some(name.into()),
        version: Some("1".into()),
        chain_id: Some(U256::from(chain_id)),
        verifying_contract: Some(Address::ZERO),
        salt: None,
    })
}

/// EIP-712 digest the app key signs for a TEE trigger order.
pub fn tee_perp_trigger_order_signing_hash(
    chain_id: u64,
    payload: &TeePerpTriggerOrderSignature,
) -> Result<B256> {
    Ok(trigger_order_message(payload)?
        .eip712_signing_hash(&signing_domain(HYPERCALL_API_DOMAIN, chain_id)?))
}

/// EIP-712 digest the app key signs for a TEE order batch.
pub fn tee_perp_order_batch_signing_hash(
    chain_id: u64,
    payload: &TeePerpOrderBatchSignature<'_>,
) -> Result<B256> {
    Ok(order_batch_message(payload)?
        .eip712_signing_hash(&signing_domain(HYPERCALL_API_DOMAIN, chain_id)?))
}

/// EIP-712 digest the app key signs for a TEE leverage change.
pub fn tee_perp_leverage_signing_hash(
    chain_id: u64,
    payload: &TeePerpLeverageSignature,
) -> Result<B256> {
    Ok(leverage_message(payload)
        .eip712_signing_hash(&signing_domain(HYPERCALL_API_DOMAIN, chain_id)?))
}

/// EIP-712 digest the app key signs for a TEE single modify.
pub fn tee_perp_modify_signing_hash(
    chain_id: u64,
    payload: &TeePerpModifySignature,
) -> Result<B256> {
    Ok(modify_message(payload)?
        .eip712_signing_hash(&signing_domain(HYPERCALL_API_DOMAIN, chain_id)?))
}

/// EIP-712 digest the app key signs for a TEE batch modify. Child order is
/// signed.
pub fn tee_perp_batch_modify_signing_hash(
    chain_id: u64,
    payload: &TeePerpBatchModifySignature<'_>,
) -> Result<B256> {
    Ok(batch_modify_message(payload)?
        .eip712_signing_hash(&signing_domain(HYPERCALL_API_DOMAIN, chain_id)?))
}

/// EIP-712 digest the app key signs for a TEE TWAP.
pub fn tee_perp_twap_signing_hash(chain_id: u64, payload: &TeePerpTwapSignature) -> Result<B256> {
    Ok(
        twap_message(payload)?
            .eip712_signing_hash(&signing_domain(HYPERCALL_API_DOMAIN, chain_id)?),
    )
}

/// EIP-712 digest the app key signs for a TEE TWAP cancel.
pub fn tee_perp_twap_cancel_signing_hash(
    chain_id: u64,
    payload: &TeePerpTwapCancelSignature,
) -> Result<B256> {
    Ok(twap_cancel_message(payload)?
        .eip712_signing_hash(&signing_domain(HYPERCALL_API_DOMAIN, chain_id)?))
}

/// EIP-712 digest the manager signs to approve the TEE API wallet.
pub fn tee_api_wallet_approval_signing_hash(
    chain_id: u64,
    payload: &TeeApiWalletApprovalSignature<'_>,
) -> Result<B256> {
    Ok(approval_message(payload)
        .eip712_signing_hash(&signing_domain(HYPERCALL_MANAGER_DOMAIN, chain_id)?))
}

impl TeePerpSigner for HypercallWallet {
    async fn sign_tee_perp_trigger_order_payload(
        &self,
        payload: TeePerpTriggerOrderSignature,
    ) -> Result<String> {
        let message = trigger_order_message(&payload)?;
        self.sign_directive(&message, "HCPerpTriggerOrder", HYPERCALL_API_DOMAIN)
            .await
    }

    async fn sign_tee_perp_order_batch_payload(
        &self,
        payload: TeePerpOrderBatchSignature<'_>,
    ) -> Result<String> {
        let message = order_batch_message(&payload)?;
        self.sign_directive(&message, "HCPerpOrderBatch", HYPERCALL_API_DOMAIN)
            .await
    }

    async fn sign_tee_perp_leverage_payload(
        &self,
        payload: TeePerpLeverageSignature,
    ) -> Result<String> {
        let message = leverage_message(&payload);
        self.sign_directive(&message, "HCPerpLeverageChange", HYPERCALL_API_DOMAIN)
            .await
    }

    async fn sign_tee_perp_modify_payload(
        &self,
        payload: TeePerpModifySignature,
    ) -> Result<String> {
        let message = modify_message(&payload)?;
        self.sign_directive(&message, "HCPerpOrderModify", HYPERCALL_API_DOMAIN)
            .await
    }

    async fn sign_tee_perp_batch_modify_payload(
        &self,
        payload: TeePerpBatchModifySignature<'_>,
    ) -> Result<String> {
        let message = batch_modify_message(&payload)?;
        self.sign_directive(&message, "HCPerpOrderBatchModify", HYPERCALL_API_DOMAIN)
            .await
    }

    async fn sign_tee_perp_twap_payload(&self, payload: TeePerpTwapSignature) -> Result<String> {
        let message = twap_message(&payload)?;
        self.sign_directive(&message, "HCPerpTwapOrder", HYPERCALL_API_DOMAIN)
            .await
    }

    async fn sign_tee_perp_twap_cancel_payload(
        &self,
        payload: TeePerpTwapCancelSignature,
    ) -> Result<String> {
        let message = twap_cancel_message(&payload)?;
        self.sign_directive(&message, "HCPerpTwapCancel", HYPERCALL_API_DOMAIN)
            .await
    }
}

impl TeeApiWalletManagerSigner for HypercallWallet {
    async fn sign_authentication_payload(&self, timestamp: i64) -> Result<String> {
        self.sign_authentication_message(timestamp).await
    }

    async fn sign_tee_api_wallet_approval_payload(
        &self,
        payload: TeeApiWalletApprovalSignature<'_>,
    ) -> Result<String> {
        let message = approval_message(&payload);
        self.sign_directive(&message, "HLAddApiWallet", HYPERCALL_MANAGER_DOMAIN)
            .await
    }
}

// ---------------------------------------------------------------------------
// Server clock and nonces
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy)]
struct ClockSample {
    offset_ms: i64,
    measured_at: Instant,
}

/// Shared cache of the API server's clock offset.
#[derive(Debug, Clone, Default)]
pub(crate) struct ServerClock(Arc<StdMutex<Option<ClockSample>>>);

impl ServerClock {
    fn cached_offset(&self) -> Option<i64> {
        let sample = *self.0.lock().expect("server clock cache poisoned");
        sample
            .filter(|sample| sample.measured_at.elapsed() < SERVER_CLOCK_TTL)
            .map(|sample| sample.offset_ms)
    }

    fn store(&self, offset_ms: i64) {
        *self.0.lock().expect("server clock cache poisoned") = Some(ClockSample {
            offset_ms,
            measured_at: Instant::now(),
        });
    }
}

fn local_now_ms() -> i64 {
    let elapsed = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system clock is before the Unix epoch");
    i64::try_from(elapsed.as_millis()).expect("Unix milliseconds fit in i64")
}

/// Server clock offset from one `Date` header: the header has one-second
/// resolution, so assume mid-second, and assume the server stamped it halfway
/// through the round trip.
pub(crate) fn server_clock_offset_ms(
    date_header: &str,
    sent_ms: i64,
    received_ms: i64,
) -> Option<i64> {
    let server_ms = parse_http_date_ms(date_header)?;
    if received_ms < sent_ms {
        return None;
    }
    Some(server_ms + 500 - (sent_ms + received_ms) / 2)
}

/// Parse an RFC 9110 IMF-fixdate (`Sun, 06 Nov 1994 08:49:37 GMT`).
pub(crate) fn parse_http_date_ms(value: &str) -> Option<i64> {
    let mut parts = value.trim().split_ascii_whitespace();
    let _weekday = parts.next()?;
    let day: i64 = parts.next()?.parse().ok()?;
    let month = match parts.next()? {
        "Jan" => 1,
        "Feb" => 2,
        "Mar" => 3,
        "Apr" => 4,
        "May" => 5,
        "Jun" => 6,
        "Jul" => 7,
        "Aug" => 8,
        "Sep" => 9,
        "Oct" => 10,
        "Nov" => 11,
        "Dec" => 12,
        _ => return None,
    };
    let year: i64 = parts.next()?.parse().ok()?;
    let mut time = parts.next()?.split(':');
    let hour: i64 = time.next()?.parse().ok()?;
    let minute: i64 = time.next()?.parse().ok()?;
    let second: i64 = time.next()?.parse().ok()?;
    if parts.next()? != "GMT" || parts.next().is_some() || time.next().is_some() {
        return None;
    }
    if !(1..=31).contains(&day) || hour > 23 || minute > 59 || second > 60 {
        return None;
    }
    // Days from civil (Howard Hinnant).
    let shifted_year = if month <= 2 { year - 1 } else { year };
    let era = shifted_year.div_euclid(400);
    let year_of_era = shifted_year - era * 400;
    let month_index = (month + 9) % 12;
    let day_of_year = (153 * month_index + 2) / 5 + day - 1;
    let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
    let days = era * 146_097 + day_of_era - 719_468;
    Some(((days * 24 + hour) * 60 + minute) * 60_000 + second * 1_000)
}

fn nonce_registry() -> &'static StdMutex<HashMap<Address, u64>> {
    static NONCES: OnceLock<StdMutex<HashMap<Address, u64>>> = OnceLock::new();
    NONCES.get_or_init(|| StdMutex::new(HashMap::new()))
}

/// Next nonce for `signer`: one past the larger of the last nonce issued in
/// this process and `server_now_ms`.
pub(crate) fn next_nonce_at(signer: Address, server_now_ms: u64) -> u64 {
    let mut nonces = nonce_registry().lock().expect("nonce registry poisoned");
    let last = nonces.entry(signer).or_insert(0);
    *last = (*last).max(server_now_ms) + 1;
    *last
}

/// Venue expiry the server derives from a TEE perp nonce, or `None` when the
/// nonce is zero or overflows.
pub fn tee_perp_venue_expires_after_ms(nonce: u64) -> Option<u64> {
    if nonce == 0 {
        return None;
    }
    nonce.checked_add(TEE_PERP_VENUE_TTL_MS)
}

/// Whether the server would accept `nonce` at `server_now_ms`.
pub fn tee_perp_nonce_in_window(nonce: u64, server_now_ms: u64) -> bool {
    tee_perp_venue_expires_after_ms(nonce).is_some_and(|expires| {
        expires > server_now_ms
            && expires
                <= server_now_ms.saturating_add(
                    hypercall_sdk_types::tee_perp::TEE_PERP_MAX_VENUE_EXPIRY_WINDOW_MS,
                )
    })
}

// ---------------------------------------------------------------------------
// Price, size, and order construction
// ---------------------------------------------------------------------------

fn decimal_units(value: Decimal) -> Result<(u128, u32)> {
    if value <= Decimal::ZERO {
        return Err(ClientError::InvalidInput(format!(
            "{value} must be positive"
        )));
    }
    let normalized = value.normalize();
    let mantissa = u128::try_from(normalized.mantissa())
        .map_err(|_| ClientError::InvalidInput(format!("{value} must be positive")))?;
    Ok((mantissa, normalized.scale()))
}

fn units_to_decimal(units: u128, scale: u32) -> Result<Decimal> {
    let units = i128::try_from(units)
        .map_err(|_| ClientError::InvalidInput("price out of range".into()))?;
    Decimal::try_from_i128_with_scale(units, scale)
        .map(|value| value.normalize())
        .map_err(|error| ClientError::InvalidInput(format!("price out of range: {error}")))
}

fn pow10(exponent: u32) -> Result<u128> {
    10_u128
        .checked_pow(exponent)
        .ok_or(ClientError::ArithmeticOverflow {
            operation: "perp price scaling",
        })
}

fn validate_size_decimals(size_decimals: u32) -> Result<u32> {
    if size_decimals > HYPERLIQUID_PERP_MAX_PRICE_DECIMALS {
        return Err(ClientError::InvalidInput(format!(
            "size decimals {size_decimals} exceed {HYPERLIQUID_PERP_MAX_PRICE_DECIMALS}"
        )));
    }
    Ok(size_decimals)
}

/// The IOC limit price of a market order (or market trigger): `reference`
/// moved `slippage_bps` against the trader, rounded to a valid Hyperliquid
/// tick inside that bound (buys round down, sells round up). `reference` is
/// the best ask for a buy and the best bid for a sell (or the trigger price).
///
/// Fails when the rounded price would no longer cross `reference`, the same
/// refusal as the Hypercall order panel.
pub fn perp_market_limit_price(
    reference: Decimal,
    side: Side,
    size_decimals: u32,
    slippage_bps: u32,
) -> Result<Decimal> {
    let size_decimals = validate_size_decimals(size_decimals)?;
    if slippage_bps >= BPS_DENOMINATOR {
        return Err(ClientError::InvalidInput(format!(
            "slippage {slippage_bps} bps must be below {BPS_DENOMINATOR}"
        )));
    }
    let (units, scale) = decimal_units(reference)?;
    let factor = match side {
        Side::Buy => BPS_DENOMINATOR + slippage_bps,
        Side::Sell => BPS_DENOMINATOR - slippage_bps,
    };
    let overflow = ClientError::ArithmeticOverflow {
        operation: "perp market price",
    };
    let numerator = units.checked_mul(u128::from(factor)).ok_or(overflow)?;
    let denominator = pow10(scale)?
        .checked_mul(u128::from(BPS_DENOMINATOR))
        .ok_or(ClientError::ArithmeticOverflow {
            operation: "perp market price",
        })?;
    let exponent: i64 = if numerator >= denominator {
        i64::try_from((numerator / denominator).to_string().len()).unwrap_or(i64::MAX) - 1
    } else {
        let mut scaled = numerator;
        let mut places = 0_i64;
        while scaled < denominator {
            scaled = scaled
                .checked_mul(10)
                .ok_or(ClientError::ArithmeticOverflow {
                    operation: "perp market price",
                })?;
            places += 1;
        }
        -places
    };
    let max_decimals = i64::from(HYPERLIQUID_PERP_MAX_PRICE_DECIMALS - size_decimals);
    let sig_fig_exponent = exponent - i64::from(HYPERLIQUID_PERP_MAX_PRICE_SIGNIFICANT_FIGURES) + 1;
    let tick_exponent = sig_fig_exponent.min(0).max(-max_decimals);
    let tick_scale = u32::try_from(-tick_exponent).expect("tick scale is non-negative");
    let scaled_numerator =
        numerator
            .checked_mul(pow10(tick_scale)?)
            .ok_or(ClientError::ArithmeticOverflow {
                operation: "perp market price",
            })?;
    let quotient = scaled_numerator / denominator;
    let ticks = if matches!(side, Side::Buy) || scaled_numerator.is_multiple_of(denominator) {
        quotient
    } else {
        quotient + 1
    };
    let price = units_to_decimal(ticks, tick_scale)?;
    let crosses = match side {
        Side::Buy => price >= reference,
        Side::Sell => price <= reference,
    };
    if !crosses || price <= Decimal::ZERO {
        return Err(ClientError::InvalidInput(format!(
            "no marketable tick within {slippage_bps} bps of {reference}; use a limit order"
        )));
    }
    Ok(price)
}

fn round_working_price_to_tick(units: u128, side: Side, size_decimals: u32) -> Result<Decimal> {
    let magnitude = i64::try_from(units.to_string().len()).unwrap_or(i64::MAX)
        - 1
        - i64::from(SCALE_WORKING_SCALE);
    let sig_fig_decimals =
        (i64::from(HYPERLIQUID_PERP_MAX_PRICE_SIGNIFICANT_FIGURES) - 1 - magnitude).max(0);
    let decimals = sig_fig_decimals
        .min(i64::from(
            HYPERLIQUID_PERP_MAX_PRICE_DECIMALS - size_decimals,
        ))
        .max(0);
    let decimals = u32::try_from(decimals).expect("decimals are non-negative");
    let divisor = pow10(SCALE_WORKING_SCALE - decimals)?;
    let quotient = units / divisor;
    let ticks = if matches!(side, Side::Sell) && !units.is_multiple_of(divisor) {
        quotient + 1
    } else {
        quotient
    };
    units_to_decimal(ticks, decimals)
}

fn to_working_units(value: Decimal, scale: u32, field: &str) -> Result<u128> {
    let (units, value_scale) = decimal_units(value)
        .map_err(|_| ClientError::InvalidInput(format!("{field} must be positive")))?;
    if value_scale > scale {
        Ok(units / pow10(value_scale - scale)?)
    } else {
        units
            .checked_mul(pow10(scale - value_scale)?)
            .ok_or(ClientError::ArithmeticOverflow {
                operation: "perp scale order",
            })
    }
}

/// A price in working units, refusing digits past the working scale. Flooring
/// them would let a sell round to a tick below the caller's bound (for
/// example `1000.0000000000001` to `1000`).
fn to_working_price_units(value: Decimal, field: &str) -> Result<u128> {
    let (units, value_scale) = decimal_units(value)
        .map_err(|_| ClientError::InvalidInput(format!("{field} must be positive")))?;
    if value_scale > SCALE_WORKING_SCALE
        && !units.is_multiple_of(pow10(value_scale - SCALE_WORKING_SCALE)?)
    {
        return Err(ClientError::InvalidInput(format!(
            "{field} supports at most {SCALE_WORKING_SCALE} decimal places"
        )));
    }
    to_working_units(value, SCALE_WORKING_SCALE, field)
}

/// Round `price` to Hyperliquid's tick for `side`: buys round down and sells
/// round up, so the result is never worse for the trader than `price`.
pub fn round_perp_price_to_tick(price: Decimal, side: Side, size_decimals: u32) -> Result<Decimal> {
    let size_decimals = validate_size_decimals(size_decimals)?;
    let units = to_working_price_units(price, "price")?;
    round_working_price_to_tick(units, side, size_decimals)
}

/// One resting limit leg of a scale order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PerpScaleLeg {
    pub price: Decimal,
    pub size: Decimal,
}

/// Inputs of [`build_perp_scale_order_legs`].
#[derive(Debug, Clone, Copy)]
pub struct PerpScaleOrderSpec {
    pub start_price: Decimal,
    pub end_price: Decimal,
    pub total_size: Decimal,
    /// 2 to 100 legs.
    pub order_count: usize,
    /// Last leg's size divided by the first leg's (1 = equal sizes), up to 100.
    pub size_skew: Decimal,
    pub side: Side,
    /// Hyperliquid `szDecimals` of the market.
    pub size_decimals: u32,
}

/// Split a scale order into evenly spaced limit legs, exactly as the
/// Hypercall order panel does: leg sizes follow `size_skew`, are floored to
/// the lot size with the remaining lots added to the last legs (so they sum
/// to `total_size`), and prices are rounded to a valid tick on the trader's
/// side.
pub fn build_perp_scale_order_legs(spec: PerpScaleOrderSpec) -> Result<Vec<PerpScaleLeg>> {
    let lot_decimals = validate_size_decimals(spec.size_decimals)?;
    if !(PERP_SCALE_MIN_ORDERS..=PERP_SCALE_MAX_ORDERS).contains(&spec.order_count) {
        return Err(ClientError::InvalidInput(format!(
            "scale orders need {PERP_SCALE_MIN_ORDERS} to {PERP_SCALE_MAX_ORDERS} legs"
        )));
    }
    let one = pow10(SCALE_WORKING_SCALE)?;
    let start = to_working_price_units(spec.start_price, "start price")?;
    let end = to_working_price_units(spec.end_price, "end price")?;
    let skew = to_working_units(spec.size_skew, SCALE_WORKING_SCALE, "size skew")?;
    if skew == 0 || skew > 100 * one {
        return Err(ClientError::InvalidInput(
            "size skew must be positive and at most 100".into(),
        ));
    }
    if start == 0 || end == 0 || start == end {
        return Err(ClientError::InvalidInput(
            "scale start and end prices must be positive and distinct".into(),
        ));
    }
    let (size_units, size_scale) = decimal_units(spec.total_size)?;
    if size_scale > lot_decimals {
        return Err(ClientError::InvalidInput(format!(
            "total size {} has more than {lot_decimals} decimals",
            spec.total_size
        )));
    }
    let size_lots = size_units * pow10(lot_decimals - size_scale)?;
    let count = spec.order_count as u128;
    if size_lots < count {
        return Err(ClientError::InvalidInput(
            "total size is too small for this many legs".into(),
        ));
    }

    let overflow = || ClientError::ArithmeticOverflow {
        operation: "perp scale order",
    };
    let steps = count - 1;
    let weights: Vec<i128> = (0..count)
        .map(|index| {
            let base = i128::try_from(steps * one).map_err(|_| overflow())?;
            let delta = (i128::try_from(skew).map_err(|_| overflow())?
                - i128::try_from(one).map_err(|_| overflow())?)
                * i128::try_from(index).map_err(|_| overflow())?;
            Ok(base + delta)
        })
        .collect::<Result<_>>()?;
    let total_weight: i128 = weights.iter().sum();
    let size_lots_signed = i128::try_from(size_lots).map_err(|_| overflow())?;
    let mut sizes: Vec<i128> = weights
        .iter()
        .map(|weight| {
            size_lots_signed
                .checked_mul(*weight)
                .map(|product| product / total_weight)
                .ok_or_else(overflow)
        })
        .collect::<Result<_>>()?;
    let mut remainder = size_lots_signed - sizes.iter().sum::<i128>();
    let mut index = sizes.len() - 1;
    while remainder > 0 {
        sizes[index] += 1;
        remainder -= 1;
        index = (index + sizes.len() - 1) % sizes.len();
    }
    if sizes.iter().any(|size| *size <= 0) {
        return Err(ClientError::InvalidInput(
            "total size is too small for this size skew".into(),
        ));
    }

    let start_signed = i128::try_from(start).map_err(|_| overflow())?;
    let end_signed = i128::try_from(end).map_err(|_| overflow())?;
    let steps_signed = i128::try_from(steps).map_err(|_| overflow())?;
    sizes
        .into_iter()
        .enumerate()
        .map(|(index, lots)| {
            let index = i128::try_from(index).map_err(|_| overflow())?;
            let price_units = start_signed + (end_signed - start_signed) * index / steps_signed;
            let price_units = u128::try_from(price_units).map_err(|_| overflow())?;
            let price = round_working_price_to_tick(price_units, spec.side, lot_decimals)?;
            let size =
                units_to_decimal(u128::try_from(lots).map_err(|_| overflow())?, lot_decimals)?;
            if price <= Decimal::ZERO {
                return Err(ClientError::InvalidInput(
                    "scale price range rounds to zero".into(),
                ));
            }
            Ok(PerpScaleLeg { price, size })
        })
        .collect()
}

/// Child cloid of one signed batch: the nonce in the high bits and the
/// 1-based child index in the low byte. Distinct per child, never zero, and
/// reproducible from the signed nonce.
pub fn tee_perp_batch_cloid(nonce: u64, index: usize) -> Result<u128> {
    if nonce == 0 {
        return Err(ClientError::InvalidInput("nonce must be nonzero".into()));
    }
    if index >= MAX_BATCH_CLOID_INDEX {
        return Err(ClientError::InvalidInput(format!(
            "batch child index {index} exceeds {}",
            MAX_BATCH_CLOID_INDEX - 1
        )));
    }
    Ok((u128::from(nonce) << 8) + index as u128 + 1)
}

pub(crate) fn nonzero_cloid(cloid: u128) -> Result<u128> {
    if cloid == 0 {
        return Err(ClientError::InvalidInput("cloid must not be zero".into()));
    }
    Ok(cloid)
}

/// A plain limit child for an order batch.
pub fn tee_perp_limit_order_wire(
    asset: u32,
    side: Side,
    price: Decimal,
    size: Decimal,
    tif: PerpTimeInForce,
    reduce_only: bool,
    cloid: u128,
) -> Result<TeePerpOrderWire> {
    Ok(TeePerpOrderWire {
        asset,
        is_buy: matches!(side, Side::Buy),
        limit_px: to_perp_units(price, "limit price")?,
        sz: to_perp_units(size, "size")?,
        reduce_only,
        order_type: TeePerpOrderType::Limit(TeePerpLimitOrderType { tif }),
        cloid: nonzero_cloid(cloid)?,
    })
}

/// One trigger exit or entry. `limit_price` is the signed execution bound:
/// the user's limit for a limit trigger, or the slippage-bounded worst price
/// from [`perp_market_limit_price`] for a market trigger.
#[expect(
    clippy::too_many_arguments,
    reason = "each argument is one independent order term, as in the HL order wire"
)]
pub fn tee_perp_trigger_order_wire(
    asset: u32,
    side: Side,
    trigger_price: Decimal,
    limit_price: Decimal,
    size: Decimal,
    is_market: bool,
    tpsl: TeePerpTpsl,
    reduce_only: bool,
    cloid: u128,
) -> Result<TeePerpOrderWire> {
    Ok(TeePerpOrderWire {
        asset,
        is_buy: matches!(side, Side::Buy),
        limit_px: to_perp_units(limit_price, "limit price")?,
        sz: to_perp_units(size, "size")?,
        reduce_only,
        order_type: TeePerpOrderType::Trigger(TeePerpTriggerOrderType {
            trigger_px: to_perp_units(trigger_price, "trigger price")?,
            is_market,
            tpsl,
        }),
        cloid: nonzero_cloid(cloid)?,
    })
}

/// A take-profit or stop-loss exit executed as a market order once triggered.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TeePerpExit {
    pub trigger_price: Decimal,
    /// Worst execution price once triggered.
    pub limit_price: Decimal,
}

impl TeePerpExit {
    /// An exit whose execution bound is `trigger_price` moved `slippage_bps`
    /// against an exit on `exit_side`, as the Hypercall order panel signs it.
    pub fn market(
        trigger_price: Decimal,
        exit_side: Side,
        size_decimals: u32,
        slippage_bps: u32,
    ) -> Result<Self> {
        Ok(Self {
            trigger_price,
            limit_price: perp_market_limit_price(
                trigger_price,
                exit_side,
                size_decimals,
                slippage_bps,
            )?,
        })
    }
}

fn opposite(side: Side) -> Side {
    match side {
        Side::Buy => Side::Sell,
        Side::Sell => Side::Buy,
    }
}

fn exit_children(
    orders: &mut Vec<TeePerpOrderWire>,
    asset: u32,
    exit_side: Side,
    size: Decimal,
    take_profit: Option<TeePerpExit>,
    stop_loss: Option<TeePerpExit>,
    nonce: u64,
) -> Result<()> {
    if take_profit.is_none() && stop_loss.is_none() {
        return Err(ClientError::InvalidInput(
            "a TP/SL needs a take-profit or stop-loss price".into(),
        ));
    }
    for (tpsl, exit) in [(TeePerpTpsl::Tp, take_profit), (TeePerpTpsl::Sl, stop_loss)] {
        let Some(exit) = exit else { continue };
        let cloid = tee_perp_batch_cloid(nonce, orders.len())?;
        orders.push(tee_perp_trigger_order_wire(
            asset,
            exit_side,
            exit.trigger_price,
            exit.limit_price,
            size,
            true,
            tpsl,
            true,
            cloid,
        )?);
    }
    Ok(())
}

/// The entry of a [`build_tee_perp_bracket_batch`].
#[derive(Debug, Clone, Copy)]
pub struct TeePerpBracketEntry {
    pub asset: u32,
    pub side: Side,
    pub price: Decimal,
    pub size: Decimal,
    pub tif: PerpTimeInForce,
    pub reduce_only: bool,
}

/// An entry with attached TP/SL, grouped `normalTpsl` so Hyperliquid only
/// activates the exits once the entry fills. Exits are reduce-only market
/// triggers on the opposite side for the full entry size.
pub fn build_tee_perp_bracket_batch(
    entry: TeePerpBracketEntry,
    take_profit: Option<TeePerpExit>,
    stop_loss: Option<TeePerpExit>,
    nonce: u64,
) -> Result<TeePerpOrderBatchAction> {
    let mut orders = vec![tee_perp_limit_order_wire(
        entry.asset,
        entry.side,
        entry.price,
        entry.size,
        entry.tif,
        entry.reduce_only,
        tee_perp_batch_cloid(nonce, 0)?,
    )?];
    exit_children(
        &mut orders,
        entry.asset,
        opposite(entry.side),
        entry.size,
        take_profit,
        stop_loss,
        nonce,
    )?;
    Ok(TeePerpOrderBatchAction {
        orders,
        grouping: TeePerpOrderGrouping::NormalTpsl,
    })
}

/// Reduce-only TP/SL exits on an existing position, grouped `positionTpsl`.
/// `position_side` is the position's direction (`Buy` = long); exits trade
/// the opposite side.
pub fn build_tee_perp_position_tpsl_batch(
    asset: u32,
    position_side: Side,
    size: Decimal,
    take_profit: Option<TeePerpExit>,
    stop_loss: Option<TeePerpExit>,
    nonce: u64,
) -> Result<TeePerpOrderBatchAction> {
    let mut orders = Vec::with_capacity(2);
    exit_children(
        &mut orders,
        asset,
        opposite(position_side),
        size,
        take_profit,
        stop_loss,
        nonce,
    )?;
    Ok(TeePerpOrderBatchAction {
        orders,
        grouping: TeePerpOrderGrouping::PositionTpsl,
    })
}

/// Scale legs submitted together as one ungrouped batch.
pub fn build_tee_perp_scale_batch(
    asset: u32,
    side: Side,
    legs: &[PerpScaleLeg],
    tif: PerpTimeInForce,
    reduce_only: bool,
    nonce: u64,
) -> Result<TeePerpOrderBatchAction> {
    let orders = legs
        .iter()
        .enumerate()
        .map(|(index, leg)| {
            tee_perp_limit_order_wire(
                asset,
                side,
                leg.price,
                leg.size,
                tif,
                reduce_only,
                tee_perp_batch_cloid(nonce, index)?,
            )
        })
        .collect::<Result<Vec<_>>>()?;
    let batch = TeePerpOrderBatchAction {
        orders,
        grouping: TeePerpOrderGrouping::Na,
    };
    validate_batch(&batch)?;
    Ok(batch)
}

fn validate_batch(batch: &TeePerpOrderBatchAction) -> Result<()> {
    if batch.orders.is_empty() || batch.orders.len() > MAX_TEE_PERP_ORDER_BATCH_CHILDREN {
        return Err(ClientError::InvalidInput(format!(
            "an order batch needs 1 to {MAX_TEE_PERP_ORDER_BATCH_CHILDREN} orders"
        )));
    }
    let mut cloids = std::collections::BTreeSet::new();
    for order in &batch.orders {
        nonzero_cloid(order.cloid)?;
        if !cloids.insert(order.cloid) {
            return Err(ClientError::InvalidInput(
                "every order in a batch must use a distinct cloid".into(),
            ));
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Modify and TWAP
// ---------------------------------------------------------------------------

/// The new terms for one resting limit order, in human units.
///
/// The order is identified twice, and both must agree: by `target_directive_id`
/// (the `directiveId` its submit returned; for a batch child, that child's
/// `directiveId`) and by `cloid` (the order's client order ID, which the
/// modify preserves). Only limit orders (GTC, IOC, ALO) can be modified on
/// the TEE route; see the crate README for the Hyperliquid parity notes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TeePerpOrderReplacement {
    pub asset: u32,
    pub target_directive_id: uuid::Uuid,
    pub cloid: u128,
    pub side: Side,
    /// New limit price. At most 8 decimal places.
    pub price: Decimal,
    /// New size in coins. At most 8 decimal places.
    pub size: Decimal,
    pub tif: PerpTimeInForce,
    pub reduce_only: bool,
}

impl TeePerpOrderReplacement {
    /// The signed wire action, scaled to 1e8 units and validated as the
    /// server validates it.
    pub fn to_action(&self) -> Result<TeePerpModifyAction> {
        let action = TeePerpModifyAction {
            asset: self.asset,
            target_directive_id: self.target_directive_id,
            is_buy: matches!(self.side, Side::Buy),
            limit_px: to_perp_units(self.price, "limit price")?,
            sz: to_perp_units(self.size, "size")?,
            reduce_only: self.reduce_only,
            tif: self.tif,
            cloid: self.cloid,
        };
        validate_modify_action(&action)?;
        Ok(action)
    }
}

/// One modify's terms, held to the server's rules: a non-nil target, a
/// non-zero cloid, and a positive price and size.
fn validate_modify_action(action: &TeePerpModifyAction) -> Result<()> {
    if action.target_directive_id.is_nil() {
        return Err(ClientError::InvalidInput(
            "target_directive_id must not be nil".into(),
        ));
    }
    nonzero_cloid(action.cloid)?;
    if action.limit_px == 0 || action.sz == 0 {
        return Err(ClientError::InvalidInput(
            "a modify needs a positive limit price and size".into(),
        ));
    }
    Ok(())
}

/// A batch modify's shape: 1 to `max_children` children (never more than
/// [`MAX_TEE_PERP_BATCH_MODIFY_CHILDREN`]), each a valid single modify, with
/// distinct targets and distinct cloids.
fn validate_batch_modify(batch: &TeePerpBatchModifyAction, max_children: usize) -> Result<()> {
    let cap = max_children.min(MAX_TEE_PERP_BATCH_MODIFY_CHILDREN);
    if batch.modifies.is_empty() || batch.modifies.len() > cap {
        return Err(ClientError::InvalidInput(format!(
            "a batch modify needs 1 to {cap} orders, got {}",
            batch.modifies.len()
        )));
    }
    let mut targets = std::collections::BTreeSet::new();
    let mut cloids = std::collections::BTreeSet::new();
    for action in &batch.modifies {
        validate_modify_action(action)?;
        if !targets.insert(action.target_directive_id) {
            return Err(ClientError::InvalidInput(format!(
                "batch modify targets order {} twice",
                action.target_directive_id
            )));
        }
        if !cloids.insert(action.cloid) {
            return Err(ClientError::InvalidInput(
                "every order in a batch modify must use a distinct cloid".into(),
            ));
        }
    }
    Ok(())
}

/// Build the signed action of a batch modify, refusing one the server would
/// refuse. `max_children` is the instance cap (`None` for
/// [`DEFAULT_TEE_PERP_BATCH_MODIFY_MAX_CHILDREN`]); it can lower the cap but
/// never raise it past [`MAX_TEE_PERP_BATCH_MODIFY_CHILDREN`].
pub fn build_tee_perp_batch_modify(
    replacements: &[TeePerpOrderReplacement],
    max_children: Option<usize>,
) -> Result<TeePerpBatchModifyAction> {
    let max_children = max_children.unwrap_or(DEFAULT_TEE_PERP_BATCH_MODIFY_MAX_CHILDREN);
    if max_children == 0 || max_children > MAX_TEE_PERP_BATCH_MODIFY_CHILDREN {
        return Err(ClientError::InvalidInput(format!(
            "max_children must be 1 to {MAX_TEE_PERP_BATCH_MODIFY_CHILDREN}"
        )));
    }
    let batch = TeePerpBatchModifyAction {
        modifies: replacements
            .iter()
            .map(TeePerpOrderReplacement::to_action)
            .collect::<Result<_>>()?,
    };
    validate_batch_modify(&batch, max_children)?;
    Ok(batch)
}

fn validate_twap_action(action: &TeePerpTwapAction) -> Result<()> {
    if !(TEE_PERP_TWAP_MIN_MINUTES..=TEE_PERP_TWAP_MAX_MINUTES).contains(&action.minutes) {
        return Err(ClientError::InvalidInput(format!(
            "TWAP minutes must be {TEE_PERP_TWAP_MIN_MINUTES} to {TEE_PERP_TWAP_MAX_MINUTES}, got {}",
            action.minutes
        )));
    }
    if action.sz == 0 {
        return Err(ClientError::InvalidInput(
            "TWAP size must be positive".into(),
        ));
    }
    Ok(())
}

#[path = "tee_perp_submission.rs"]
mod submission;
use submission::tee_submission_is_ambiguous;
pub(crate) use submission::SignedTeeSubmission;

#[path = "tee_perp_preview.rs"]
mod preview;
pub use preview::{perp_preview_batch_action, perp_preview_order};

// ---------------------------------------------------------------------------
// Request parameters
// ---------------------------------------------------------------------------

/// A market order: an IOC limit at `reference_price` moved `slippage_bps`
/// against the trader and rounded to a valid tick.
#[derive(Debug, Clone)]
pub struct TeePerpMarketOrderParams {
    pub account: AccountAddress,
    pub asset: u32,
    pub side: Side,
    pub size: Decimal,
    /// Best ask for a buy, best bid for a sell.
    pub reference_price: Decimal,
    /// Hyperliquid `szDecimals` of the market.
    pub size_decimals: u32,
    /// Defaults to [`PERP_MARKET_SLIPPAGE_BPS`].
    pub slippage_bps: Option<u32>,
    pub reduce_only: bool,
    pub client_order_id: Option<u128>,
    pub nonce: Option<u64>,
}

/// How a trigger order executes once triggered.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TeePerpTriggerExecution {
    /// Market (IOC) bounded `slippage_bps` beyond the trigger price.
    Market {
        size_decimals: u32,
        /// Defaults to [`PERP_MARKET_SLIPPAGE_BPS`].
        slippage_bps: Option<u32>,
    },
    /// A limit order at `price`.
    Limit { price: Decimal },
}

/// A native stop-loss or take-profit order (market or limit).
#[derive(Debug, Clone)]
pub struct TeePerpTriggerOrderParams {
    pub account: AccountAddress,
    pub asset: u32,
    pub side: Side,
    pub size: Decimal,
    pub trigger_price: Decimal,
    /// `Sl` for a stop, `Tp` for a take-profit.
    pub tpsl: TeePerpTpsl,
    pub execution: TeePerpTriggerExecution,
    pub reduce_only: bool,
    /// Defaults to `tee_perp_batch_cloid(nonce, 0)`.
    pub client_order_id: Option<u128>,
    pub nonce: Option<u64>,
}

/// An entry with attached TP/SL exits (`normalTpsl`).
#[derive(Debug, Clone)]
pub struct TeePerpBracketOrderParams {
    pub account: AccountAddress,
    pub entry: TeePerpBracketEntry,
    pub take_profit: Option<TeePerpExit>,
    pub stop_loss: Option<TeePerpExit>,
    pub nonce: Option<u64>,
}

/// TP/SL exits on an existing position (`positionTpsl`).
#[derive(Debug, Clone)]
pub struct TeePerpPositionTpslParams {
    pub account: AccountAddress,
    pub asset: u32,
    /// The position's direction (`Buy` = long).
    pub position_side: Side,
    pub size: Decimal,
    pub take_profit: Option<TeePerpExit>,
    pub stop_loss: Option<TeePerpExit>,
    pub nonce: Option<u64>,
}

/// A scale order: limit legs submitted as one ungrouped batch.
#[derive(Debug, Clone)]
pub struct TeePerpScaleOrderParams {
    pub account: AccountAddress,
    pub asset: u32,
    pub side: Side,
    /// From [`build_perp_scale_order_legs`] or caller-chosen.
    pub legs: Vec<PerpScaleLeg>,
    pub tif: PerpTimeInForce,
    pub reduce_only: bool,
    pub nonce: Option<u64>,
}

/// A caller-built order batch. Cloids must already be set; derive them from
/// the nonce with [`tee_perp_batch_cloid`] after reserving it with
/// [`HypercallClient::next_tee_perp_nonce`].
#[derive(Debug, Clone)]
pub struct TeePerpOrderBatchParams {
    pub account: AccountAddress,
    pub nonce: u64,
    pub batch: TeePerpOrderBatchAction,
}

/// A leverage and margin-mode change.
#[derive(Debug, Clone, Copy)]
pub struct TeePerpLeverageParams {
    pub account: AccountAddress,
    pub asset: u32,
    /// `true` for cross margin, `false` for isolated.
    pub is_cross: bool,
    /// At least 1.
    pub leverage: u32,
    pub nonce: Option<u64>,
}

/// One resting limit order's replacement
/// ([`HypercallClient::modify_tee_perp_order`]).
#[derive(Debug, Clone, Copy)]
pub struct TeePerpModifyParams {
    pub account: AccountAddress,
    pub replacement: TeePerpOrderReplacement,
    pub nonce: Option<u64>,
}

/// Several resting limit orders' replacements under one signature
/// ([`HypercallClient::batch_modify_tee_perp_orders`]).
#[derive(Debug, Clone)]
pub struct TeePerpBatchModifyParams {
    pub account: AccountAddress,
    /// In the order the venue applies and answers them.
    pub replacements: Vec<TeePerpOrderReplacement>,
    /// The server instance's `api.tee_perp_batch_modify_max_children`, when
    /// it is not the default ([`DEFAULT_TEE_PERP_BATCH_MODIFY_MAX_CHILDREN`]).
    /// At most [`MAX_TEE_PERP_BATCH_MODIFY_CHILDREN`].
    pub max_children: Option<usize>,
    pub nonce: Option<u64>,
}

/// A native Hyperliquid TWAP ([`HypercallClient::place_tee_perp_twap_order`]).
#[derive(Debug, Clone, Copy)]
pub struct TeePerpTwapParams {
    pub account: AccountAddress,
    pub asset: u32,
    pub side: Side,
    /// Total size in coins. At most 8 decimal places.
    pub size: Decimal,
    pub reduce_only: bool,
    /// [`TEE_PERP_TWAP_MIN_MINUTES`] to [`TEE_PERP_TWAP_MAX_MINUTES`].
    pub minutes: u32,
    /// Vary slice timing.
    pub randomize: bool,
    pub nonce: Option<u64>,
}

/// Cancel a running TWAP ([`HypercallClient::cancel_tee_perp_twap`]).
#[derive(Debug, Clone, Copy)]
pub struct TeePerpTwapCancelParams {
    pub account: AccountAddress,
    pub asset: u32,
    /// [`TeePerpTwapResponse::twap_id`] of the TWAP to cancel.
    pub twap_id: u64,
    pub nonce: Option<u64>,
}

/// Parameters for [`HypercallClient::approve_hypercall_builder`].
#[derive(Debug, Clone)]
pub struct ApproveHypercallBuilderParams {
    /// HyperEVM JSON-RPC endpoint.
    pub rpc_url: String,
    /// The Account.sol (0.2.4 or newer) the manager controls.
    pub account: AccountAddress,
    /// Builder to approve. `None` reads `hl_builder_address` from `/exchange-info`.
    pub builder: Option<AccountAddress>,
}

/// The manager-authenticated TEE API wallet approval challenge.
#[derive(Debug, Clone)]
pub struct TeeApiWalletApprovalChallenge {
    pub account: AccountAddress,
    pub manager: AccountAddress,
    /// Unix seconds signed in the authentication message.
    pub timestamp: i64,
    pub authentication_signature: String,
    pub package: TeeApiWalletApprovalPackage,
}

/// Progress of a TEE API wallet approval directive.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TeeApiWalletApprovalProgress {
    Pending,
    Finalized,
    Failed,
}

/// Classify a `/v1/directives/{id}` status of the approval directive.
pub fn tee_api_wallet_approval_progress(
    status: &DirectiveStatusResponse,
) -> TeeApiWalletApprovalProgress {
    if status.domain_status == DirectiveDomainStatus::Completed
        && status.delivery_status == DirectiveDeliveryStatus::Finalized
    {
        return TeeApiWalletApprovalProgress::Finalized;
    }
    if matches!(
        status.domain_status,
        DirectiveDomainStatus::Failed | DirectiveDomainStatus::Rejected
    ) || matches!(
        status.delivery_status,
        DirectiveDeliveryStatus::CoreRejected
            | DirectiveDeliveryStatus::Reverted
            | DirectiveDeliveryStatus::Expired
            | DirectiveDeliveryStatus::DeadLettered
    ) {
        return TeeApiWalletApprovalProgress::Failed;
    }
    TeeApiWalletApprovalProgress::Pending
}

/// Result of [`HypercallClient::enroll_tee_api_wallet`].
#[derive(Debug, Clone)]
pub struct TeeApiWalletEnrollment {
    /// The TEE-controlled Hyperliquid API wallet now bound to the account.
    pub api_wallet: AccountAddress,
    pub api_wallet_name: String,
    /// `None` when Hyperliquid already had the wallet bound.
    pub directive_id: Option<String>,
}

/// Polling of [`HypercallClient::enroll_tee_api_wallet`].
#[derive(Debug, Clone, Copy)]
pub struct TeeApiWalletEnrollmentPolling {
    pub interval: Duration,
    pub attempts: u32,
}

impl Default for TeeApiWalletEnrollmentPolling {
    fn default() -> Self {
        Self {
            interval: Duration::from_secs(2),
            attempts: 60,
        }
    }
}

/// Hypercall builder published by `/exchange-info`. Never guessed.
pub fn resolve_hypercall_builder_address(
    exchange_info: &ExchangeInfoResponse,
) -> Result<AccountAddress> {
    let configured = exchange_info.hl_builder_address.as_deref().ok_or_else(|| {
        ClientError::InvalidInput("exchange-info does not publish hl_builder_address".into())
    })?;
    let builder: AccountAddress = configured
        .parse()
        .map_err(|source| ClientError::invalid_input("hl_builder_address", source))?;
    if builder.as_wallet_address().inner() == Address::ZERO {
        return Err(ClientError::InvalidInput(
            "exchange-info publishes a zero hl_builder_address".into(),
        ));
    }
    Ok(builder)
}

// ---------------------------------------------------------------------------
// Hyperliquid info reads
// ---------------------------------------------------------------------------

/// Hyperliquid margin setting read back from the venue.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HyperliquidLeverage {
    pub is_cross: bool,
    pub value: u32,
}

#[derive(Debug, Deserialize)]
struct ActiveAssetData {
    user: String,
    coin: String,
    leverage: ActiveAssetLeverage,
}

#[derive(Debug, Deserialize)]
struct ActiveAssetLeverage {
    #[serde(rename = "type")]
    kind: String,
    value: u32,
}

/// Hyperliquid `userRole` response.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct HyperliquidUserRole {
    pub role: String,
    #[serde(default)]
    pub data: Option<HyperliquidUserRoleData>,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct HyperliquidUserRoleData {
    #[serde(default)]
    pub user: Option<String>,
}

impl HyperliquidUserRole {
    /// Whether this role is an API wallet (agent) bound to `account`.
    pub fn is_agent_for(&self, account: AccountAddress) -> bool {
        self.role == "agent"
            && self
                .data
                .as_ref()
                .and_then(|data| data.user.as_deref())
                .and_then(|user| user.parse::<AccountAddress>().ok())
                == Some(account)
    }
}

/// Minimal Hyperliquid `/info` reader for TEE perp readbacks.
#[derive(Debug, Clone)]
pub struct HyperliquidInfoClient {
    client: reqwest::Client,
    url: String,
}

impl HyperliquidInfoClient {
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            client: reqwest::Client::builder()
                .timeout(Duration::from_secs(15))
                .build()
                .expect("failed to build reqwest client"),
            url: url.into(),
        }
    }

    pub fn mainnet() -> Self {
        Self::new(HYPERLIQUID_MAINNET_INFO_URL)
    }

    pub fn testnet() -> Self {
        Self::new(HYPERLIQUID_TESTNET_INFO_URL)
    }

    /// The info client for a Hypercall directive chain id (999 or 998).
    pub fn for_chain_id(chain_id: u64) -> Result<Self> {
        match chain_id {
            999 => Ok(Self::mainnet()),
            998 => Ok(Self::testnet()),
            _ => Err(ClientError::InvalidInput(format!(
                "unsupported Hyperliquid chain id: {chain_id}"
            ))),
        }
    }

    async fn post<T: serde::de::DeserializeOwned>(
        &self,
        body: serde_json::Value,
        action: &str,
    ) -> Result<T> {
        HypercallClient::send_reqwest_json(self.client.post(&self.url).json(&body), action).await
    }

    /// Hyperliquid's role for `address`.
    pub async fn user_role(&self, address: AccountAddress) -> Result<HyperliquidUserRole> {
        self.post(
            serde_json::json!({"type": "userRole", "user": address.to_string()}),
            "read Hyperliquid user role",
        )
        .await
    }

    /// Whether Hyperliquid has `api_wallet` bound as an agent of `account`:
    /// the TEE API wallet enrollment status.
    pub async fn is_tee_api_wallet_active(
        &self,
        account: AccountAddress,
        api_wallet: AccountAddress,
    ) -> Result<bool> {
        Ok(self.user_role(api_wallet).await?.is_agent_for(account))
    }

    /// The account's leverage and margin mode for `coin` (e.g. `BTC` or
    /// `xyz:SNDK`), read back from Hyperliquid after a leverage change.
    pub async fn active_asset_leverage(
        &self,
        account: AccountAddress,
        coin: &str,
    ) -> Result<HyperliquidLeverage> {
        let data: ActiveAssetData = self
            .post(
                serde_json::json!({"type": "activeAssetData", "user": account.to_string(), "coin": coin}),
                "read Hyperliquid active asset data",
            )
            .await?;
        let echoed_user = data.user.parse::<AccountAddress>().ok();
        if echoed_user != Some(account) || data.coin != coin {
            return Err(ClientError::Api {
                status: 200,
                message: "Hyperliquid returned leverage for a different account or market".into(),
            });
        }
        let is_cross = match data.leverage.kind.as_str() {
            "cross" => true,
            "isolated" => false,
            _ => {
                return Err(ClientError::Api {
                    status: 200,
                    message: "Hyperliquid returned an invalid leverage setting".into(),
                })
            }
        };
        if data.leverage.value < 1 {
            return Err(ClientError::Api {
                status: 200,
                message: "Hyperliquid returned an invalid leverage setting".into(),
            });
        }
        Ok(HyperliquidLeverage {
            is_cross,
            value: data.leverage.value,
        })
    }
}

// ---------------------------------------------------------------------------
// Client methods
// ---------------------------------------------------------------------------

fn approval_typed_data_mismatch(field: &str) -> ClientError {
    ClientError::InvalidInput(format!(
        "TEE API wallet approval typed data does not match the package: {field}"
    ))
}

fn validate_approval_typed_data(challenge: &TeeApiWalletApprovalChallenge) -> Result<()> {
    let typed = &challenge.package.typed_data;
    if typed["primaryType"] != "HLAddApiWallet" {
        return Err(approval_typed_data_mismatch("primaryType"));
    }
    if typed["domain"]["name"] != HYPERCALL_MANAGER_DOMAIN {
        return Err(approval_typed_data_mismatch("domain.name"));
    }
    let message = &typed["message"];
    let same_address = |value: &serde_json::Value, expected: WalletAddress| {
        value
            .as_str()
            .and_then(|text| text.parse::<WalletAddress>().ok())
            == Some(expected)
    };
    if !same_address(&message["account"], challenge.account.into_wallet_address()) {
        return Err(approval_typed_data_mismatch("message.account"));
    }
    let nonce = match &message["nonce"] {
        serde_json::Value::Number(number) => number.as_u64(),
        serde_json::Value::String(text) => text.parse().ok(),
        _ => None,
    };
    if nonce != Some(challenge.package.nonce) {
        return Err(approval_typed_data_mismatch("message.nonce"));
    }
    let action = &message["action"];
    if !same_address(
        &action["apiWalletAddress"],
        challenge.package.api_wallet_address,
    ) {
        return Err(approval_typed_data_mismatch(
            "message.action.apiWalletAddress",
        ));
    }
    if action["apiWalletName"] != challenge.package.api_wallet_name.as_str() {
        return Err(approval_typed_data_mismatch("message.action.apiWalletName"));
    }
    Ok(())
}

fn onchain_error(
    operation: impl Into<String>,
    source: impl std::error::Error + Send + Sync + 'static,
) -> ClientError {
    ClientError::Onchain {
        operation: operation.into(),
        source: Box::new(source),
    }
}

/// An Account implementation older than 0.2.4 has no `approveZeroFeeBuilder`
/// and reverts with no data.
fn is_missing_function_revert(error: &alloy::contract::Error) -> bool {
    match error.as_revert_data() {
        Some(data) => data.is_empty(),
        None => error
            .to_string()
            .to_ascii_lowercase()
            .contains("execution reverted"),
    }
}

impl HypercallClient {
    /// The API server's current Unix millisecond, from the `/health` `Date`
    /// header (cached for a minute per client and its clones).
    pub async fn server_now_ms(&self) -> Result<u64> {
        let offset = match self.server_clock.cached_offset() {
            Some(offset) => offset,
            None => {
                let sent_ms = local_now_ms();
                let response = self.request(Method::GET, "/health").send().await?;
                let received_ms = local_now_ms();
                let date = response
                    .headers()
                    .get(reqwest::header::DATE)
                    .and_then(|value| value.to_str().ok())
                    .ok_or_else(|| ClientError::Api {
                        status: response.status().as_u16(),
                        message: "/health response carries no Date header to stamp TEE nonces"
                            .into(),
                    })?;
                let offset =
                    server_clock_offset_ms(date, sent_ms, received_ms).ok_or_else(|| {
                        ClientError::Api {
                            status: response.status().as_u16(),
                            message: format!("/health Date header is not an HTTP date: {date}"),
                        }
                    })?;
                self.server_clock.store(offset);
                offset
            }
        };
        u64::try_from(local_now_ms() + offset).map_err(|_| ClientError::ArithmeticOverflow {
            operation: "server clock",
        })
    }

    /// Next TEE perp nonce for `signer`, stamped on the server clock and
    /// strictly increasing per signer within this process.
    pub async fn next_tee_perp_nonce(&self, signer: AccountAddress) -> Result<u64> {
        let now = self.server_now_ms().await?;
        Ok(next_nonce_at(signer.as_wallet_address().inner(), now))
    }

    pub(crate) async fn resolve_tee_nonce(
        &self,
        signer: AccountAddress,
        nonce: Option<u64>,
    ) -> Result<u64> {
        match nonce {
            Some(nonce) => Ok(nonce),
            None => self.next_tee_perp_nonce(signer).await,
        }
    }

    async fn post_tee<T, B>(&self, route: &str, body: &B, action: &str) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
        B: serde::Serialize + ?Sized,
    {
        Self::send_json_body(self.request(Method::POST, route), body, action).await
    }

    /// POST a signed, mutating submission. The body is serialized once and
    /// those exact bytes are sent, so an ambiguous failure can hand them back
    /// in [`ClientError::TeeSubmissionAmbiguous`] for an identical resend.
    pub(crate) async fn post_tee_signed<T, B>(
        &self,
        route: &str,
        body: &B,
        action: &str,
    ) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
        B: SignedTeeSubmission,
    {
        let request_json = sonic_rs::to_string(body)
            .map_err(|source| ClientError::invalid_input("TEE request body", source))?;
        self.send_tee_submission(route, request_json, body.nonce(), action, false)
            .await
    }

    async fn send_tee_submission<T>(
        &self,
        route: &str,
        request_json: String,
        nonce: u64,
        action: &str,
        resend: bool,
    ) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let request = self
            .request(Method::POST, route)
            .header(reqwest::header::CONTENT_TYPE, "application/json")
            .body(request_json.clone());
        match Self::send_reqwest_json(request, action).await {
            // A resend cannot settle the original's outcome: the signed action
            // can expire (and be refused before journal dedup) while the
            // original still executed, so every resend failure stays ambiguous.
            Err(error) if resend || tee_submission_is_ambiguous(&error) => {
                Err(ClientError::TeeSubmissionAmbiguous {
                    action: action.to_owned(),
                    route: route.to_owned(),
                    nonce,
                    request_json,
                    source: Box::new(error),
                })
            }
            result => result,
        }
    }

    /// Resend the exact signed body of a [`ClientError::TeeSubmissionAmbiguous`].
    /// The server recognises the same signed action (same nonce, same terms)
    /// and does not submit it to the venue twice; a repeat comes back with
    /// `duplicate_directive` and stage `unknown`, meaning reconcile. Pass the
    /// error's `route` and `request_json` unchanged.
    ///
    /// Any failure of the resend, including a `4xx` such as an expired
    /// signature, is returned as [`ClientError::TeeSubmissionAmbiguous`]: the
    /// original request may still have executed, so reconcile before signing
    /// a fresh action.
    pub async fn resend_tee_submission<T>(&self, route: &str, request_json: &str) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let nonce = serde_json::from_str::<serde_json::Value>(request_json)
            .ok()
            .and_then(|body| match &body["nonce"] {
                serde_json::Value::Number(number) => number.as_u64(),
                serde_json::Value::String(text) => text.parse().ok(),
                _ => None,
            })
            .ok_or_else(|| {
                ClientError::InvalidInput("request_json carries no signed nonce".into())
            })?;
        self.send_tee_submission(
            route,
            request_json.to_owned(),
            nonce,
            "resend TEE submission",
            true,
        )
        .await
    }

    /// Place one plain limit order (GTC, IOC, or ALO) on the TEE route.
    ///
    /// `client_order_id` defaults to the nonce.
    pub async fn place_tee_perp_limit_order<S>(
        &self,
        signer: &S,
        params: PerpLimitOrderParams,
    ) -> Result<TeePerpOrderResponse>
    where
        S: PerpDirectiveSigner + ?Sized,
    {
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let action = HlLimitOrderAction {
            asset: params.asset,
            is_buy: matches!(params.side, Side::Buy),
            limit_px: to_perp_units(params.price, "perp limit price")?,
            sz: to_perp_units(params.size, "perp size")?,
            reduce_only: params.reduce_only,
            tif: params.tif,
            cloid: nonzero_cloid(params.client_order_id.unwrap_or(u128::from(nonce)))?,
        };
        let signature = signer
            .sign_perp_limit_order_payload(PerpLimitOrderSignature {
                account: params.account,
                nonce,
                action,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_ORDER_SUBMIT_ROUTE,
            &request,
            "submit TEE perp order",
        )
        .await
    }

    /// Place a market order: an IOC limit bounded by `slippage_bps`.
    pub async fn place_tee_perp_market_order<S>(
        &self,
        signer: &S,
        params: TeePerpMarketOrderParams,
    ) -> Result<TeePerpOrderResponse>
    where
        S: PerpDirectiveSigner + ?Sized,
    {
        let size_decimals = validate_size_decimals(params.size_decimals)?;
        if params.size.normalize().scale() > size_decimals {
            return Err(ClientError::InvalidInput(format!(
                "size {} has more than {size_decimals} decimals",
                params.size
            )));
        }
        let price = perp_market_limit_price(
            params.reference_price,
            params.side,
            size_decimals,
            params.slippage_bps.unwrap_or(PERP_MARKET_SLIPPAGE_BPS),
        )?;
        self.place_tee_perp_limit_order(
            signer,
            PerpLimitOrderParams {
                account: params.account,
                asset: params.asset,
                side: params.side,
                price,
                size: params.size,
                tif: PerpTimeInForce::Ioc,
                reduce_only: params.reduce_only,
                client_order_id: params.client_order_id,
                nonce: params.nonce,
            },
        )
        .await
    }

    /// Place one native stop-loss or take-profit order (market or limit).
    pub async fn place_tee_perp_trigger_order<S>(
        &self,
        signer: &S,
        params: TeePerpTriggerOrderParams,
    ) -> Result<TeePerpOrderBatchResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        let (limit_price, is_market) = match params.execution {
            TeePerpTriggerExecution::Market {
                size_decimals,
                slippage_bps,
            } => (
                perp_market_limit_price(
                    params.trigger_price,
                    params.side,
                    size_decimals,
                    slippage_bps.unwrap_or(PERP_MARKET_SLIPPAGE_BPS),
                )?,
                true,
            ),
            TeePerpTriggerExecution::Limit { price } => (price, false),
        };
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let cloid = match params.client_order_id {
            Some(cloid) => cloid,
            None => tee_perp_batch_cloid(nonce, 0)?,
        };
        let order = tee_perp_trigger_order_wire(
            params.asset,
            params.side,
            params.trigger_price,
            limit_price,
            params.size,
            is_market,
            params.tpsl,
            params.reduce_only,
            cloid,
        )?;
        let signature = signer
            .sign_tee_perp_trigger_order_payload(TeePerpTriggerOrderSignature {
                account: params.account,
                nonce,
                order,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action: order,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_TRIGGER_ORDER_SUBMIT_ROUTE,
            &request,
            "submit TEE perp trigger order",
        )
        .await
    }

    /// Submit a caller-built order batch (1 to 100 children).
    pub async fn place_tee_perp_order_batch<S>(
        &self,
        signer: &S,
        params: TeePerpOrderBatchParams,
    ) -> Result<TeePerpOrderBatchResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        validate_batch(&params.batch)?;
        let signature = signer
            .sign_tee_perp_order_batch_payload(TeePerpOrderBatchSignature {
                account: params.account,
                nonce: params.nonce,
                batch: &params.batch,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce: params.nonce,
            action: params.batch,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_ORDER_BATCH_SUBMIT_ROUTE,
            &request,
            "submit TEE perp order batch",
        )
        .await
    }

    /// Place an entry with attached TP/SL exits (`normalTpsl`).
    pub async fn place_tee_perp_bracket_order<S>(
        &self,
        signer: &S,
        params: TeePerpBracketOrderParams,
    ) -> Result<TeePerpOrderBatchResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let batch = build_tee_perp_bracket_batch(
            params.entry,
            params.take_profit,
            params.stop_loss,
            nonce,
        )?;
        self.place_tee_perp_order_batch(
            signer,
            TeePerpOrderBatchParams {
                account: params.account,
                nonce,
                batch,
            },
        )
        .await
    }

    /// Place TP/SL exits on an existing position (`positionTpsl`).
    pub async fn place_tee_perp_position_tpsl<S>(
        &self,
        signer: &S,
        params: TeePerpPositionTpslParams,
    ) -> Result<TeePerpOrderBatchResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let batch = build_tee_perp_position_tpsl_batch(
            params.asset,
            params.position_side,
            params.size,
            params.take_profit,
            params.stop_loss,
            nonce,
        )?;
        self.place_tee_perp_order_batch(
            signer,
            TeePerpOrderBatchParams {
                account: params.account,
                nonce,
                batch,
            },
        )
        .await
    }

    /// Place a scale order: its legs as one ungrouped batch.
    pub async fn place_tee_perp_scale_order<S>(
        &self,
        signer: &S,
        params: TeePerpScaleOrderParams,
    ) -> Result<TeePerpOrderBatchResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let batch = build_tee_perp_scale_batch(
            params.asset,
            params.side,
            &params.legs,
            params.tif,
            params.reduce_only,
            nonce,
        )?;
        self.place_tee_perp_order_batch(
            signer,
            TeePerpOrderBatchParams {
                account: params.account,
                nonce,
                batch,
            },
        )
        .await
    }

    /// Cancel a resting order by Hyperliquid order ID.
    pub async fn cancel_tee_perp_order_by_oid<S>(
        &self,
        signer: &S,
        params: PerpCancelByOidParams,
    ) -> Result<TeePerpVenueActionResponse>
    where
        S: PerpDirectiveSigner + ?Sized,
    {
        if params.order_id == 0 {
            return Err(ClientError::InvalidInput("oid must not be zero".into()));
        }
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let action = HlCancelByOidAction {
            asset: params.asset,
            oid: params.order_id,
        };
        let signature = signer
            .sign_perp_cancel_by_oid_payload(PerpCancelByOidSignature {
                account: params.account,
                nonce,
                action,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_CANCEL_BY_OID_SUBMIT_ROUTE,
            &request,
            "submit TEE perp cancel by oid",
        )
        .await
    }

    /// Cancel a resting order by client order ID.
    pub async fn cancel_tee_perp_order_by_cloid<S>(
        &self,
        signer: &S,
        params: PerpCancelByCloidParams,
    ) -> Result<TeePerpCancelResponse>
    where
        S: PerpDirectiveSigner + ?Sized,
    {
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let action = HlCancelByCloidAction {
            asset: params.asset,
            cloid: nonzero_cloid(params.client_order_id)?,
        };
        let signature = signer
            .sign_perp_cancel_by_cloid_payload(PerpCancelByCloidSignature {
                account: params.account,
                nonce,
                action,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_CANCEL_SUBMIT_ROUTE,
            &request,
            "submit TEE perp cancel by cloid",
        )
        .await
    }

    /// Change leverage and margin mode for one market. Read the venue's
    /// resulting setting back with [`HyperliquidInfoClient::active_asset_leverage`].
    pub async fn update_tee_perp_leverage<S>(
        &self,
        signer: &S,
        params: TeePerpLeverageParams,
    ) -> Result<TeePerpLeverageResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        if params.leverage == 0 {
            return Err(ClientError::InvalidInput(
                "leverage must be greater than zero".into(),
            ));
        }
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let action = TeePerpLeverageAction {
            asset: params.asset,
            is_cross: params.is_cross,
            leverage: params.leverage,
        };
        let signature = signer
            .sign_tee_perp_leverage_payload(TeePerpLeverageSignature {
                account: params.account,
                nonce,
                action,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_LEVERAGE_SUBMIT_ROUTE,
            &request,
            "submit TEE perp leverage",
        )
        .await
    }

    /// Replace one resting limit order's price, size, side, TIF and
    /// reduce-only flag (Hyperliquid `modify`). The cloid is preserved.
    ///
    /// The engine reserves the larger of the old and new terms until the
    /// outcome is known: `rejected` or `not_submitted` restores the old terms
    /// (the original order still rests); `accepted` moves to the new terms.
    /// An `unknown` stage must be reconciled before modifying again.
    pub async fn modify_tee_perp_order<S>(
        &self,
        signer: &S,
        params: TeePerpModifyParams,
    ) -> Result<TeePerpModifyResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        let action = params.replacement.to_action()?;
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let signature = signer
            .sign_tee_perp_modify_payload(TeePerpModifySignature {
                account: params.account,
                nonce,
                action,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_ORDER_MODIFY_ROUTE,
            &request,
            "submit TEE perp modify",
        )
        .await
    }

    /// Replace several resting limit orders in one signed Hyperliquid
    /// `batchModify` (one nonce, one venue call).
    ///
    /// The engine admits every child or none. After the venue answers, each
    /// child settles on its own: read
    /// [`TeePerpBatchModifyResponse::children`] (or
    /// [`TeePerpBatchModifyResponse::children_to_reconcile`]) rather than only
    /// the whole-action stage, which is `accepted` or `rejected` only when
    /// every child agrees. Charged as one order placement per child against
    /// the rate limit.
    ///
    /// Refused locally, before signing, when the batch is empty, exceeds
    /// `max_children` (50 by default), repeats a target or cloid, or carries an
    /// invalid child. A deployment whose enclave release does not enable batch
    /// modify answers `400 unsupported_action`
    /// ([`ClientError::is_unsupported_action`]).
    pub async fn batch_modify_tee_perp_orders<S>(
        &self,
        signer: &S,
        params: TeePerpBatchModifyParams,
    ) -> Result<TeePerpBatchModifyResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        let batch = build_tee_perp_batch_modify(&params.replacements, params.max_children)?;
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let signature = signer
            .sign_tee_perp_batch_modify_payload(TeePerpBatchModifySignature {
                account: params.account,
                nonce,
                batch: &batch,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action: batch,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_ORDER_BATCH_MODIFY_ROUTE,
            &request,
            "submit TEE perp batch modify",
        )
        .await
    }

    /// Start a native Hyperliquid TWAP (`twapOrder`). The engine reserves
    /// the full size at admission and releases it as fills arrive. Keep
    /// [`TeePerpTwapResponse::twap_id`] to cancel it.
    ///
    /// TWAP is disabled at runtime on deployments whose enclave release does
    /// not enable it; those answer `400 unsupported_action`
    /// ([`ClientError::is_unsupported_action`]).
    pub async fn place_tee_perp_twap_order<S>(
        &self,
        signer: &S,
        params: TeePerpTwapParams,
    ) -> Result<TeePerpTwapResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        let action = TeePerpTwapAction {
            asset: params.asset,
            is_buy: matches!(params.side, Side::Buy),
            sz: to_perp_units(params.size, "size")?,
            reduce_only: params.reduce_only,
            minutes: params.minutes,
            randomize: params.randomize,
        };
        validate_twap_action(&action)?;
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let signature = signer
            .sign_tee_perp_twap_payload(TeePerpTwapSignature {
                account: params.account,
                nonce,
                action,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action,
            signature,
        };
        self.post_tee_signed(TEE_PERP_TWAP_SUBMIT_ROUTE, &request, "submit TEE perp TWAP")
            .await
    }

    /// Cancel a running native TWAP (`twapCancel`). Like TWAP submit, this
    /// can answer `400 unsupported_action` where TWAP is disabled.
    pub async fn cancel_tee_perp_twap<S>(
        &self,
        signer: &S,
        params: TeePerpTwapCancelParams,
    ) -> Result<TeePerpVenueActionResponse>
    where
        S: TeePerpSigner + ?Sized,
    {
        if params.twap_id == 0 {
            return Err(ClientError::InvalidInput("twap_id must not be zero".into()));
        }
        let nonce = self
            .resolve_tee_nonce(signer.address(), params.nonce)
            .await?;
        let action = TeePerpTwapCancelAction {
            asset: params.asset,
            twap_id: params.twap_id,
        };
        let signature = signer
            .sign_tee_perp_twap_cancel_payload(TeePerpTwapCancelSignature {
                account: params.account,
                nonce,
                action,
            })
            .await?;
        let request = DirectiveSubmitRequest {
            account: params.account.into_wallet_address(),
            nonce,
            action,
            signature,
        };
        self.post_tee_signed(
            TEE_PERP_TWAP_CANCEL_ROUTE,
            &request,
            "submit TEE perp TWAP cancel",
        )
        .await
    }

    /// Preview an action's admission, margin, liquidation prices and max
    /// size without signing or reserving anything.
    pub async fn preview_tee_perp_action(
        &self,
        request: &PerpPreviewRequest,
    ) -> Result<PerpPreviewResponse> {
        let envelope: ApiResponse<PerpPreviewResponse> = self
            .post_tee(TEE_PERP_PREVIEW_ROUTE, request, "preview TEE perp action")
            .await?;
        if !envelope.success {
            return Err(ClientError::Api {
                status: 200,
                message: envelope
                    .error
                    .unwrap_or_else(|| "TEE perp preview failed".into()),
            });
        }
        envelope.data.ok_or_else(|| ClientError::Api {
            status: 200,
            message: "TEE perp preview returned no data".into(),
        })
    }

    /// Configured TEE perp markets and whether each is supported.
    pub async fn get_tee_perp_markets(&self) -> Result<TeePerpMarketsResponse> {
        Self::send_reqwest_json(
            self.request(Method::GET, TEE_PERP_MARKETS_ROUTE),
            "get TEE perp markets",
        )
        .await
    }

    /// Step 1 of enrollment: authenticate as the manager and fetch the
    /// account's TEE API wallet with its `HLAddApiWallet` approval payload.
    pub async fn request_tee_api_wallet_approval<S>(
        &self,
        manager: &S,
        account: AccountAddress,
    ) -> Result<TeeApiWalletApprovalChallenge>
    where
        S: TeeApiWalletManagerSigner + ?Sized,
    {
        let timestamp = i64::try_from(self.server_now_ms().await? / 1_000).map_err(|_| {
            ClientError::ArithmeticOverflow {
                operation: "authentication timestamp",
            }
        })?;
        let manager_address = manager.address();
        let authentication_signature = manager.sign_authentication_payload(timestamp).await?;
        let request = TeeApiWalletApprovalRequest {
            account: account.into_wallet_address(),
            manager: manager_address.into_wallet_address(),
            timestamp,
            signature: authentication_signature.clone(),
        };
        let package: TeeApiWalletApprovalPackage = self
            .post_tee(
                TEE_API_WALLET_APPROVAL_REQUEST_ROUTE,
                &request,
                "request TEE API wallet approval",
            )
            .await?;
        Ok(TeeApiWalletApprovalChallenge {
            account,
            manager: manager_address,
            timestamp,
            authentication_signature,
            package,
        })
    }

    /// Step 2 of enrollment: sign the approval (after checking the returned
    /// typed data names exactly the packaged wallet) and submit it.
    pub async fn submit_tee_api_wallet_approval<S>(
        &self,
        manager: &S,
        challenge: &TeeApiWalletApprovalChallenge,
    ) -> Result<DirectiveSubmitResponse>
    where
        S: TeeApiWalletManagerSigner + ?Sized,
    {
        validate_approval_typed_data(challenge)?;
        let signature = manager
            .sign_tee_api_wallet_approval_payload(TeeApiWalletApprovalSignature {
                account: challenge.account,
                nonce: challenge.package.nonce,
                api_wallet_address: AccountAddress::from(
                    challenge.package.api_wallet_address.inner(),
                ),
                api_wallet_name: &challenge.package.api_wallet_name,
            })
            .await?;
        let request = TeeApiWalletApprovalSubmitRequest {
            account: challenge.account.into_wallet_address(),
            manager: challenge.manager.into_wallet_address(),
            timestamp: challenge.timestamp,
            authentication_signature: challenge.authentication_signature.clone(),
            tee_wallet_id: challenge.package.tee_wallet_id,
            nonce: challenge.package.nonce,
            signature,
        };
        self.post_tee_signed(
            TEE_API_WALLET_APPROVAL_ROUTE,
            &request,
            "submit TEE API wallet approval",
        )
        .await
    }

    /// Enroll the account's TEE API wallet end to end, as the Hypercall
    /// order panel does: request it, skip the approval when Hyperliquid
    /// already binds it, otherwise submit the approval, poll the directive
    /// until it finalizes, and confirm the binding on Hyperliquid.
    pub async fn enroll_tee_api_wallet<S>(
        &self,
        manager: &S,
        account: AccountAddress,
        hyperliquid: &HyperliquidInfoClient,
        polling: TeeApiWalletEnrollmentPolling,
    ) -> Result<TeeApiWalletEnrollment>
    where
        S: TeeApiWalletManagerSigner + ?Sized,
    {
        let challenge = self
            .request_tee_api_wallet_approval(manager, account)
            .await?;
        let api_wallet = AccountAddress::from(challenge.package.api_wallet_address.inner());
        let api_wallet_name = challenge.package.api_wallet_name.clone();
        if hyperliquid
            .is_tee_api_wallet_active(account, api_wallet)
            .await?
        {
            return Ok(TeeApiWalletEnrollment {
                api_wallet,
                api_wallet_name,
                directive_id: None,
            });
        }
        let response = self
            .submit_tee_api_wallet_approval(manager, &challenge)
            .await?;
        if let Some(rejection) = &response.rejection {
            return Err(ClientError::Api {
                status: 200,
                message: format!(
                    "TEE API wallet approval rejected ({}): {}",
                    rejection.code, rejection.message
                ),
            });
        }
        let mut finalized = false;
        for _ in 0..polling.attempts {
            // The directive can briefly be absent before its row appears.
            if let Ok(status) = self.get_directive_status(&response.directive_id).await {
                match tee_api_wallet_approval_progress(&status) {
                    TeeApiWalletApprovalProgress::Finalized => {
                        finalized = true;
                        break;
                    }
                    TeeApiWalletApprovalProgress::Failed => {
                        return Err(ClientError::Api {
                            status: 200,
                            message: format!(
                                "TEE API wallet approval {} failed: domain {:?}, delivery {:?}",
                                response.directive_id, status.domain_status, status.delivery_status
                            ),
                        });
                    }
                    TeeApiWalletApprovalProgress::Pending => {}
                }
            }
            tokio::time::sleep(polling.interval).await;
        }
        if !finalized {
            return Err(ClientError::Api {
                status: 200,
                message: format!(
                    "TEE API wallet approval {} is still pending",
                    response.directive_id
                ),
            });
        }
        if !hyperliquid
            .is_tee_api_wallet_active(account, api_wallet)
            .await?
        {
            return Err(ClientError::Api {
                status: 200,
                message: "Hyperliquid did not bind the finalized TEE API wallet to this account"
                    .into(),
            });
        }
        Ok(TeeApiWalletEnrollment {
            api_wallet,
            api_wallet_name,
            directive_id: Some(response.directive_id),
        })
    }

    /// Approve the zero-fee Hypercall builder for the account on Hyperliquid
    /// with one manager HyperEVM transaction calling
    /// `Account.approveZeroFeeBuilder(builder)`. Idempotent on the venue.
    ///
    /// Needed once per account before TEE perp orders are accepted (they are
    /// refused with `builder_not_approved` until then). Requires Account
    /// 0.2.4 or newer and a little HYPE on the manager for gas.
    pub async fn approve_hypercall_builder(
        &self,
        manager: &HypercallWallet,
        params: ApproveHypercallBuilderParams,
    ) -> Result<B256> {
        let builder = match params.builder {
            Some(builder) => builder,
            None => resolve_hypercall_builder_address(&self.get_exchange_info().await?)?,
        };
        let builder = builder.as_wallet_address().inner();
        if builder == Address::ZERO {
            return Err(ClientError::InvalidInput("builder must not be zero".into()));
        }
        let rpc_url = params
            .rpc_url
            .parse()
            .map_err(|source| ClientError::invalid_input("HyperEVM RPC URL", source))?;
        let provider = ProviderBuilder::new()
            .wallet(manager.ethereum_wallet())
            .connect_http(rpc_url);
        let rpc_chain_id = provider
            .get_chain_id()
            .await
            .map_err(|error| onchain_error("read HyperEVM chain ID", error))?;
        if rpc_chain_id != manager.chain_id() {
            return Err(ClientError::InvalidInput(format!(
                "HyperEVM RPC chain ID {rpc_chain_id} does not match wallet chain ID {}",
                manager.chain_id()
            )));
        }
        let manager_address = manager.address.as_wallet_address().inner();
        let balance = provider
            .get_balance(manager_address)
            .await
            .map_err(|error| onchain_error("read manager HYPE balance", error))?;
        if balance.is_zero() {
            return Err(ClientError::InvalidInput(
                "the manager wallet needs a small amount of HYPE on HyperEVM to pay the network fee"
                    .into(),
            ));
        }
        let account = HypercallAccountBuilderApproval::new(
            params.account.as_wallet_address().inner(),
            &provider,
        );
        let call = account.approveZeroFeeBuilder(builder).from(manager_address);
        if let Err(error) = call.call().await {
            if is_missing_function_revert(&error) {
                return Err(ClientError::InvalidInput(
                    "this Account implementation predates approveZeroFeeBuilder (needs 0.2.4); trading cannot be enabled yet".into(),
                ));
            }
            return Err(onchain_error("simulate approveZeroFeeBuilder", error));
        }
        let submission_lock = evm_submission_lock(manager_address);
        let _guard = submission_lock.lock().await;
        let receipt = call
            .send()
            .await
            .map_err(|error| onchain_error("submit approveZeroFeeBuilder", error))?
            .with_timeout(Some(Duration::from_secs(60)))
            .get_receipt()
            .await
            .map_err(|error| onchain_error("confirm approveZeroFeeBuilder", error))?;
        if !receipt.status() {
            return Err(ClientError::TransactionReverted {
                operation: "approveZeroFeeBuilder",
                transaction_hash: Some(receipt.transaction_hash),
            });
        }
        Ok(receipt.transaction_hash)
    }
}

#[cfg(test)]
#[path = "tee_perp_test.rs"]
mod tests;

#[cfg(test)]
#[path = "tee_perp_modify_test.rs"]
mod modify_tests;