gmsol-store 0.5.0

GMX-Solana is an extension of GMX on the Solana blockchain.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
//! # The GMX-Solana Store Program
//!
//! The Store Program defines several fundamental accounts that form the core of its functionality:
//!
//! - [`Store`](states::Store): A central account that serves as a global configuration store and
//!   permission manager. It acts as the authority ("owner") for other program accounts and manages
//!   role-based access control. The global [GT](#gt-model) state is also stored in this account.
//! - [`TokenMap`](states::TokenMap): A mapping of token configurations and price feed configurations
//!   that defines supported tokens and their associated price oracle feeds.
//! - [`Market`](states::Market): A market account that stores market-specific configurations and
//!   maintains the global market state.
//! - [`Position`](states::Position): Tracks a user's active position in a market.
//! - Basic Market Action accounts: Accounts that track user actions and positions in markets
//!   - [`Deposit`](states::Deposit): Tracks a user's pending deposit request into a market.
//!   - [`Withdrawal`](states::Withdrawal): Tracks a user's pending withdrawal request from a market.
//!   - [`Shift`](states::Shift): Tracks a user's pending request to shift from one kind of market token to another.
//!   - [`Order`](states::Order): Tracks a user's pending order request in a market, including swap orders and increase/decrease position orders.
//! - GLV (GMX Liquidity Vault) Action accounts: Accounts that track user interactions with GLV tokens.
//!   - [`GlvDeposit`](states::GlvDeposit): Tracks a user's pending deposit of tokens into a GLV pool to mint GLV tokens.
//!   - [`GlvWithdrawal`](states::GlvWithdrawal): Tracks a user's pending withdrawal of tokens from a GLV pool to burn GLV tokens and withdraw the underlying tokens.
//!   - [`GlvShift`](states::GlvShift): Tracks a keeper's pending request to rebalance tokens between different market pools within the GLV pool.
//! - User accounts (see [`UserHeader`](states::UserHeader) for details): Tracks user information including the GT and esGT balances of the user, referral code, and referral relationships.
//!
//! The instructions for the store program are categorized into the following sections:
//!
//! ## Store
//!
//! A [`Store`](states::Store) Account serves as both an authority and a global configuration
//! storage.
//!
//! A store and its associated accounts form a *deployment*. Each deployment operates independently,
//! allowing multiple instances of the protocol to coexist on-chain (only available when the
//! `multi-store` feature is enabled).
//!
//! #### Instructions for Store Accounts
//! - [`initialize`](gmsol_store::initialize): Create a new [`Store`](states::Store) account.
//! - [`transfer_store_authority`]: Transfer the authority of the given store to a new authority.
//! - [`transfer_receiver`](gmsol_store::transfer_receiver): Set the claimable fee receiver address.
//! - [`set_token_map`]: Set the token map account to use.
//!
//! #### Instructions for Config Management
//! - [`insert_amount`]: Insert an amount to the global config.
//! - [`insert_factor`]: Insert a factor to the global config.
//! - [`insert_address`]: Insert an address to the global config.
//! - [`insert_order_fee_discount_for_referred_user`]:
//!   Insert order fee discount for referred user factor to the global config.
//!
//! #### Instructions for Feature Management
//! - [`toggle_feature`]: Enable or disable the given feature.
//!
//! ## Role-based Permission Management
//!
//! The role-based permission system for each GMSOL deployment is managed through its
//! [`Store`](states::Store) Account. The permission structure consists of:
//!
//! - **Administrator Role**: A single administrator address stored in the `authority` field of the
//!   [`Store`](states::Store) Account. This address has exclusive rights to modify the permission table
//!   and use other administrative instructions.
//!
//! - **Custom Roles**: A flexible role system stored in the `role` field as a [`RoleStore`](states::RoleStore)
//!   structure. This allows for granular permission management through custom roles and role assignments.
//!   Each role can be assigned to multiple addresses.
//!
//! #### Instructions for Permission Management
//! - [`check_admin`](gmsol_store::check_admin): Check whether *the signer* is the admin of the given store.
//! - [`check_role`](gmsol_store::check_role): Check whether *the signer* has the given role in the given store.
//! - [`has_admin`](gmsol_store::has_admin): Return whether the given address is the admin of the given store.
//! - [`has_role`](gmsol_store::has_role): Return whether the given address has the given role in the given store.
//! - [`enable_role`]: Insert or enable a role for the given store.
//! - [`disable_role`]: Disable an existing role for the given store.
//! - [`grant_role`]: Grant a role to the given user in the given store.
//! - [`revoke_role`]: Revoke a role from the given user in the given store.
//!
//! ## Token Config and Oracle Management
//!
//! #### Instructions for managing [`TokenConfig`](states::TokenConfig) and token maps.
//! - [`initialize_token_map`](gmsol_store::initialize_token_map): Initialize a new token map account.
//!   This is a permissionless instruction.
//! - [`push_to_token_map`]: Push a new token config for an existing token to the given token map.
//! - [`push_to_token_map_synthetic`]: Push a new token config for a "synthetic"
//!   token to the given token map.
//! - [`toggle_token_config`]: Enable or disable a token config of the given token map.
//! - [`set_expected_provider`]: Set the expected provider for the given token.
//! - [`set_feed_config`]: Set the feed config of the given provider for the given token.
//! - [`is_token_config_enabled`](gmsol_store::is_token_config_enabled): Check if the config for the given token is enabled.
//! - [`token_expected_provider`](gmsol_store::token_expected_provider): Get the expected provider set for the given token.
//! - [`token_feed`](gmsol_store::token_feed): Get the feed address of the given provider set for the given token.
//! - [`token_timestamp_adjustment`](gmsol_store::token_timestamp_adjustment): Get the timestamp adjustment of the given
//!   provider for the give token.
//! - [`token_name`](gmsol_store::token_name): Get the name of the given token.
//! - [`token_decimals`](gmsol_store::token_decimals): Get the token decimals of the given token.
//! - [`token_precision`](gmsol_store::token_precision): Get the price precision of the given token.
//!
//! #### Instructions for [`Oracle`](states::Oracle) accounts
//! - [`initialize_oracle`](gmsol_store::initialize_oracle): Initialize a new [`Oracle`](states::Oracle) account.
//! - [`clear_all_prices`]: Clear the prices of the given oracle account.
//! - [`set_prices_from_price_feed`]: Validate and set prices parsed from the
//!   provided price feed accounts.
//! - [`initialize_price_feed`]: Initialize a custom price feed.
//! - [`update_price_feed_with_chainlink`]: Update a custom Chainlink price feed with Chainlink Data Streams report.
//!
//! ## Market Management
//!
//! *[See also the module level documentation for Market for details.](states::market)*
//!
//! #### Instructions for [`Market`](states::Market) management
//! - [`initialize_market`]: Initialize a [`Market`](states::Market) account.
//! - [`toggle_market`]: Enable or disable the given market.
//! - [`market_transfer_in`]: Transfer tokens into the market and record the amount in its balance.
//! - [`update_market_config`]: Update an item in the market config.
//! - [`update_market_config_with_buffer`]: Update the market config with the given
//!   [`MarketConfigBuffer`](states::market::config::MarketConfigBuffer) account.
//! - [`get_market_status`](gmsol_store::get_market_status): Calculate the market status with the given prices.
//! - [`get_market_token_price`](gmsol_store::get_market_token_price): Calculate the market token price the given prices.
//! - [`toggle_gt_minting`]: Enable or disable GT minting for the given market.
//!
//! #### Instructions for [`MarketConfigBuffer`](states::market::config::MarketConfigBuffer) accounts
//! - [`initialize_market_config_buffer`](gmsol_store::initialize_market_config_buffer): Initialize a market config buffer account.
//! - [`set_market_config_buffer_authority`](gmsol_store::set_market_config_buffer_authority): Replace the authority of the market
//!   config buffer account with the new one.
//! - [`close_market_config_buffer`](gmsol_store::close_market_config_buffer): Close the given market config buffer account.
//! - [`push_to_market_config_buffer`](gmsol_store::push_to_market_config_buffer): Push config items to the given market config
//!   buffer account.
//!
//! #### Instructions for token accounts
//! - [`initialize_market_vault`]: Initialize the market vault for the given token.
//! - [`use_claimable_account`]: Prepare a claimable account to receive tokens during the order execution.
//! - [`close_empty_claimable_account`]: Close a empty claimble account.
//! - [`prepare_associated_token_account`](gmsol_store::prepare_associated_token_account): Prepare an ATA.
//!
//! ## Exchange
//! The instructions for providing functionalities as an exchange are as follows:
//!
//! #### Instructions for [`Deposit`](states::Deposit)
//! - [`create_deposit`]: Create a deposit by the owner.
//! - [`execute_deposit`](gmsol_store::execute_deposit()): Execute a deposit by keepers.
//! - [`close_deposit`]: Close a deposit, either by the owner or by keepers.
//!
//! #### Instructions for [`Withdrawal`](states::Withdrawal)
//! - [`create_withdrawal`]: Create a withdrawal by the owner.
//! - [`execute_withdrawal`](gmsol_store::execute_withdrawal()): Execute a withdrawal by keepers.
//! - [`close_withdrawal`]: Close a withdrawal, either by the owner or by keepers.
//!
//! #### Instructions for [`Shift`](states::Shift)
//! - [`create_shift`]: Create a shift by the owner.
//! - [`execute_shift`](gmsol_store::execute_shift()): Execute a shift by keepers.
//! - [`close_shift`]: Close a shift, either by the owner or by keepers.
//!
//! #### Instructions for [`Order`](states::Order) and [`Position`](states::Position)
//! - [`prepare_position`](gmsol_store::prepare_position): Prepare the position account for orders.
//! - [`prepare_trade_event_buffer`](gmsol_store::prepare_trade_event_buffer): Prepare trade event buffer.
//! - [`create_order`]: Create an order by the owner.
//! - [`update_order`](gmsol_store::update_order): Update an order by the owner.
//! - [`execute_increase_or_swap_order`](gmsol_store::execute_increase_or_swap_order()): Execute an order by keepers.
//! - [`execute_decrease_order`]: Execute a decrease order by keepers.
//! - [`close_order`]: Close an order, either by the owner or by keepers.
//! - [`cancel_order_if_no_position`]: Cancel an order if the position does not exist.
//! - [`liquidate`]: Perform a liquidation by keepers.
//! - [`auto_deleverage`]: Perform an ADL by keepers.
//! - [`update_adl_state`]: Update the ADL state of the market.
//!
//! ## GLV (GMX Liquidity Vault) Pools
//! The instructions for providing functionalities for GLV are as follows:
//!
//! #### Instructions for [`Glv`](states::Glv).
//! - [`initialize_glv`]: Initialize a GLV.
//! - [`update_glv_market_config`]: Update GLV market config.
//! - [`toggle_glv_market_flag`]: Toggle flags of GLV market.
//! - [`update_glv_config`]: Update GLV global config.
//! - [`insert_glv_market`]: Insert a new market to the GLV.
//! - [`remove_glv_market`]: Remove a market from the GLV.
//!
//! #### Instructions for [`GlvDeposit`](states::GlvDeposit)
//! - [`create_glv_deposit`]: Create a GLV deposit by the owner.
//! - [`execute_glv_deposit`]: Execute a GLV deposit by keepers.
//! - [`close_glv_deposit`]: Close a GLV deposit, either by the owner or by keepers.
//!
//! #### Instructions for [`GlvWithdrawal`](states::glv::GlvWithdrawal)
//! - [`create_glv_withdrawal`]: Create a GLV withdrawal by the owner.
//! - [`execute_glv_withdrawal`]: Execute a GLV withdrawal by keepers.
//! - [`close_glv_withdrawal`]: Close a GLV withdrawal, either by the owner or by keepers.
//!
//! #### Instructions for [`GlvShift`](states::glv::GlvShift)
//! - [`create_glv_shift`]: Create a GLV shift by keepers.
//! - [`execute_glv_shift`]: Execute a GLV shift by keepers.
//! - [`close_glv_shift`]: Close a shift by keepers.
//!
//! ## User Accounts and Referrals
//! The instructions for user accounts and referrals are as follows:
//! - [`prepare_user`](gmsol_store::prepare_user): Prepare a user account.
//! - [`initialize_referral_code`](gmsol_store::initialize_referral_code): Initialize and set a referral code.
//! - [`set_referrer`](gmsol_store::set_referrer): Set the referrer.
//! - [`transfer_referral_code`](gmsol_store::transfer_referral_code): Transfer the referral code to others.
//! - [`cancel_referral_code_transfer`](gmsol_store::cancel_referral_code_transfer): Cancel the referral code transfer.
//! - [`accept_referral_code`](gmsol_store::accept_referral_code): Complete the referral code transfer.
//!
//! ## GT Model
//!
//! *[See also the module level documentation for GT for details.](states::gt)*
//!
//! The instructions for GT Model are as follows:
//! - [`initialize_gt`]: Initialize the GT state.
//! - [`gt_set_order_fee_discount_factors`]: Set order fee discount factors.
//! - [`gt_set_referral_reward_factors`]: Set referral reward factors.
//! - [`gt_set_exchange_time_window`]: Set GT exchange time window.
//! - [`prepare_gt_exchange_vault`](gmsol_store::prepare_gt_exchange_vault): Prepare current GT exchange vault.
//! - [`confirm_gt_exchange_vault`]: Confirm GT exchange vault.
//! - [`request_gt_exchange`](gmsol_store::request_gt_exchange): Request a GT exchange.
//! - [`close_gt_exchange`]: Close a confirmed GT exchange.

/// Instructions.
pub mod instructions;

/// States.
pub mod states;

/// Operations.
pub mod ops;

/// Constants.
pub mod constants;

/// Utils.
pub mod utils;

/// Events.
pub mod events;

use self::{
    instructions::*,
    ops::{
        deposit::CreateDepositParams,
        glv::{CreateGlvDepositParams, CreateGlvWithdrawalParams},
        order::{CreateOrderParams, PositionCutKind},
        shift::CreateShiftParams,
        withdrawal::CreateWithdrawalParams,
    },
    states::{
        glv::UpdateGlvParams,
        market::{config::EntryArgs, status::MarketStatus},
        order::UpdateOrderParams,
        token_config::UpdateTokenConfigParams,
        FactorKey, PriceProviderKind,
    },
    utils::internal,
};
use anchor_lang::prelude::*;
use gmsol_model::price::Prices;

#[cfg_attr(test, macro_use)]
extern crate static_assertions;

declare_id!("Gmso1uvJnLbawvw7yezdfCDcPydwW2s2iqG3w6MDucLo");

#[program]
/// Instructions definitions of the GMSOL Store Program.
pub mod gmsol_store {

    use super::*;

    // ===========================================
    //                   Store
    // ===========================================

    /// Create a new [`Store`](states::Store) account.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](Initialize).*
    ///
    /// # Arguments
    /// - `key`: The name of the store, used as a seed to derive the store account's address.
    ///   The length must not exceed [`MAX_LEN`](states::Store::MAX_LEN).
    ///
    /// # Errors
    /// - The `key` must be empty unless the `multi-store` feature is enabled
    /// - The [`payer`](Initialize::payer) must be a signer
    /// - The [`authority`](Initialize::authority) must be as signer if it is provided.
    /// - The [`receiver`](Initialize::receiver) must be as signer if it is provided.
    /// - The [`holding`](Initialize::holding) must be as signer if it is provided.
    /// - The [`store`](Initialize::store) must not be initialized
    /// - The [`store`](Initialize::store) address must match the PDA derived from
    ///   the seed of [`Store`](states::Store) and the SHA-256 hash of `key`
    pub fn initialize(ctx: Context<Initialize>, key: String) -> Result<()> {
        instructions::initialize(ctx, key)
    }

    /// Update last restarted slot.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](UpdateLastRestartedSlot).*
    ///
    /// # Errors
    /// - The [`authority`](UpdateLastRestartedSlot::authority) must be a signer and the current
    ///   admin of the store.
    /// - The [`store`](UpdateLastRestartedSlot::store) must be an initialized store account
    ///   owned by the store program.
    #[access_control(internal::Authenticate::only_admin(&ctx))]
    pub fn update_last_restarted_slot(ctx: Context<UpdateLastRestartedSlot>) -> Result<()> {
        instructions::update_last_restarted_slot(ctx)
    }

    /// Request to transfer the authority (admin) of the given store to a new address.
    /// # Note
    /// This instruction only sets `next_authority`. Use [`accept_store_authority`] to
    /// complete the transfer.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](TransferStoreAuthority).*
    ///
    /// # Errors
    /// - The [`authority`](TransferStoreAuthority::authority) must be a signer and the current
    ///   admin of the store.
    /// - The [`store`](TransferStoreAuthority::store) must be an initialized store account
    ///   owned by the store program.
    /// - The [`next_authority`](TransferStoreAuthority::next_authority) cannot be the same as
    ///   current `next_authority`.
    #[access_control(internal::Authenticate::only_admin(&ctx))]
    pub fn transfer_store_authority(ctx: Context<TransferStoreAuthority>) -> Result<()> {
        instructions::unchecked_transfer_store_authority(ctx)
    }

    /// Accept the transfer of the authority (admin) of the given store.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](AcceptStoreAuthority).*
    ///
    /// # Errors
    /// - The [`next_authority`](AcceptStoreAuthority::next_authority) must be a signer and the current
    ///   `next_authority` of the store.
    /// - The [`store`](TransferStoreAuthority::store) must be an initialized store account
    ///   owned by the store program.
    pub fn accept_store_authority(ctx: Context<AcceptStoreAuthority>) -> Result<()> {
        instructions::accept_store_authority(ctx)
    }

    /// Request to transfer the receiver address to a new one.
    /// # Note
    /// This instruction only sets `next_receiver`. Use [`accept_receiver`] to
    /// complete the transfer.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](TransferReceiver).*
    ///
    /// # Errors
    /// - The [`authority`](TransferReceiver::authority) must be a signer and the current receiver
    ///   of the given store.
    /// - The [`store`](TransferReceiver::store) must be an initialized store account owned by
    ///   the store program.
    /// - The new [`next_receiver`](TransferReceiver::next_receiver) account provided cannot be the same as
    ///   the current `next_receiver`.
    pub fn transfer_receiver(ctx: Context<TransferReceiver>) -> Result<()> {
        instructions::transfer_receiver(ctx)
    }

    /// Accept the transfer of the receiver address of the given store.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](AcceptReceiver).*
    ///
    /// # Errors
    /// - The [`next_receiver`](AcceptReceiver::next_receiver) must be a signer and the current
    ///   `next_receiver` of the store.
    /// - The [`store`](AcceptReceiver::store) must be an initialized store account
    ///   owned by the store program.
    pub fn accept_receiver(ctx: Context<AcceptReceiver>) -> Result<()> {
        instructions::accept_receiver(ctx)
    }

    /// Set the token map address for the store.
    ///
    /// This instruction allows a MARKET_KEEPER to update which token map account the store uses.
    /// The token map account contains token configurations and price feed configurations.
    ///
    /// We say the token map is *authorized by the store* if the token map address of the store is
    /// the same as the address of the token map account.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](SetTokenMap).*
    ///
    /// # Errors
    /// - The [`authority`](SetTokenMap::authority) must be a signer and have the MARKET_KEEPER
    ///   role in the store.
    /// - The [`store`](SetTokenMap::store) must be an initialized store account owned by the
    ///   store program.
    /// - The [`token_map`](SetTokenMap::token_map) must be an initialized token map account
    ///   and owned by the given store.
    /// - The new token map address cannot be the same as the current one.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn set_token_map(ctx: Context<SetTokenMap>) -> Result<()> {
        instructions::unchecked_set_token_map(ctx)
    }

    // ===========================================
    //      Role-based Permission Management
    // ===========================================

    /// Return whether the signer is the admin of the given store.
    ///
    /// This instruction verifies if the signer has administrator privileges for the given store
    /// and returns a boolean result.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CheckRole).*
    ///
    /// # Returns
    /// Returns `true` if the signer is the admin, `false` otherwise.
    ///
    /// # Errors
    /// - The [`authority`](CheckRole::authority) must be a signer.
    /// - The [`store`](CheckRole::store) must be an initialized store account owned by
    ///   the store program.
    pub fn check_admin(ctx: Context<CheckRole>) -> Result<bool> {
        instructions::check_admin(ctx)
    }

    /// Check that the authority has the given role in the given store.
    ///
    /// This instruction verifies if the authority has the specified role in the given store
    /// and returns a boolean result.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CheckRole).*
    ///
    /// # Arguments
    /// - `role`: The name of the role to check for the authority.
    ///
    /// # Returns
    /// Returns `true` if the authority has the role, `false` otherwise.
    ///
    /// # Errors
    /// - The [`authority`](CheckRole::authority) must be a signer.
    /// - The [`store`](CheckRole::store) must be an initialized store account owned by
    ///   the store program.
    /// - The `role` must exist and be enabled in the store's role configuration.
    pub fn check_role(ctx: Context<CheckRole>, role: String) -> Result<bool> {
        instructions::check_role(ctx, role)
    }

    /// Return whether the given address is the administrator of the given store.
    ///
    /// This instruction checks if the provided address has administrator privileges for the given store
    /// and returns a boolean result.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](HasRole).*
    ///
    /// # Arguments
    /// - `authority`: The address to check for administrator privileges.
    ///
    /// # Returns
    /// Returns `true` if the address is the administrator, `false` otherwise.
    ///
    /// # Errors
    /// - The [`store`](HasRole::store) must be an initialized store account owned by
    ///   the store program.
    pub fn has_admin(ctx: Context<HasRole>, authority: Pubkey) -> Result<bool> {
        instructions::has_admin(ctx, authority)
    }

    /// Return whether the given address has the given role in the given store.
    ///
    /// This instruction checks if the provided address has the specified role in the given store
    /// and returns a boolean result.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](HasRole).*
    ///
    /// # Arguments
    /// - `authority`: The address to check for role membership.
    /// - `role`: The name of the role to check for the authority.
    ///
    /// # Returns
    /// Returns `true` if the address has the specified role, `false` otherwise.
    ///
    /// # Errors
    /// - The [`store`](HasRole::store) must be an initialized store account owned by
    ///   the store program.
    /// - The `role` must exist and be enabled in the store's role configuration.
    pub fn has_role(ctx: Context<HasRole>, authority: Pubkey, role: String) -> Result<bool> {
        instructions::has_role(ctx, authority, role)
    }

    /// Insert or enable a role for the given store.
    ///
    /// This instruction adds a new role or enables an existing disabled role in the store's role configuration.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](EnableRole).*
    ///
    /// # Arguments
    /// - `role`: The name of the role to be added/enabled. The length cannot exceed
    ///   [`MAX_ROLE_NAME_LEN`](states::roles::MAX_ROLE_NAME_LEN).
    ///
    /// # Errors
    /// - The [`authority`](EnableRole::authority) must be a signer and be the `ADMIN` of the store.
    /// - The [`store`](EnableRole::store) must be an initialized store account owned by the store program.
    /// - The `role` name length must not exceed [`MAX_ROLE_NAME_LEN`](states::roles::MAX_ROLE_NAME_LEN).
    /// - The `role` must not be already enabled.
    #[access_control(internal::Authenticate::only_admin(&ctx))]
    pub fn enable_role(ctx: Context<EnableRole>, role: String) -> Result<()> {
        instructions::unchecked_enable_role(ctx, role)
    }

    /// Disable an existing role for the given store.
    ///
    /// This instruction disables an existing role in the store's role configuration.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](DisableRole).*
    ///
    /// # Arguments
    /// - `role`: The name of the role to be disabled.
    ///
    /// # Errors
    /// - The [`authority`](DisableRole::authority) must be a signer and be the `ADMIN` of the store.
    /// - The [`store`](DisableRole::store) must be an initialized store account owned by the store program.
    /// - The `role` must be enabled.
    #[access_control(internal::Authenticate::only_admin(&ctx))]
    pub fn disable_role(ctx: Context<DisableRole>, role: String) -> Result<()> {
        instructions::unchecked_disable_role(ctx, role)
    }

    /// Grant a role to the given user in the given store.
    ///
    /// This instruction grants a role to a user in the store's role configuration. If the user already
    /// has the role, this instruction has no effect.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](GrantRole).*
    ///
    /// # Arguments
    /// - `user`: The address of the user to whom the role should be granted.
    /// - `role`: The name of the role to be granted. Must be an enabled role in the store.
    ///
    /// # Errors
    /// - The [`authority`](GrantRole::authority) must be a signer and be the `ADMIN` of the store.
    /// - The [`store`](GrantRole::store) must be an initialized store account owned by the store program.
    /// - The `role` must exist and be enabled in the store's role table.
    #[access_control(internal::Authenticate::only_admin(&ctx))]
    pub fn grant_role(ctx: Context<GrantRole>, user: Pubkey, role: String) -> Result<()> {
        instructions::unchecked_grant_role(ctx, user, role)
    }

    /// Revoke a role from the given user in the given store.
    ///
    /// This instruction revokes a role from a user in the store's role configuration. If the user does
    /// not have the role, this instruction has no effect.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](RevokeRole).*
    ///
    /// # Arguments
    /// - `user`: The address of the user from whom the role should be revoked.
    /// - `role`: The name of the role to be revoked.
    ///
    /// # Errors
    /// - The [`authority`](RevokeRole::authority) must be a signer and be the `ADMIN` of the store.
    /// - The [`store`](RevokeRole::store) must be an initialized store account owned by the store program.
    /// - The `role` must exist in the store's role table.
    /// - The `user` must exist in the store's member table.
    #[access_control(internal::Authenticate::only_admin(&ctx))]
    pub fn revoke_role(ctx: Context<RevokeRole>, user: Pubkey, role: String) -> Result<()> {
        instructions::unchecked_revoke_role(ctx, user, role)
    }

    // ===========================================
    //              Config Management
    // ===========================================

    /// Insert an amount value into the store's global configuration.
    ///
    /// This instruction allows a CONFIG_KEEPER to set or update an amount value in the store's
    /// configuration. The key must be one of the predefined amount keys.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InsertConfig).*
    ///
    /// # Arguments
    /// - `key`: The configuration key to update. Must be a valid amount key defined in
    ///   [`AmountKey`](crate::states::AmountKey).
    /// - `amount`: The amount value to store for this configuration key.
    ///
    /// # Errors
    /// - The [`authority`](InsertConfig::authority) must be a signer and have the CONFIG_KEEPER role
    ///   in the store.
    /// - The provided `key` must be defined in [`AmountKey`](crate::states::AmountKey).
    /// - The store must be initialized and owned by this program.
    #[access_control(internal::Authenticate::only_config_keeper(&ctx))]
    pub fn insert_amount(ctx: Context<InsertConfig>, key: String, amount: u64) -> Result<()> {
        instructions::unchecked_insert_amount(ctx, &key, amount)
    }

    /// Insert a factor value into the store's global configuration.
    /// This instruction allows a CONFIG_KEEPER to set or update a factor value in the store's
    /// configuration. The key must be one of the predefined factor keys.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InsertConfig).*
    ///
    /// # Arguments
    /// - `key`: The configuration key to update. Must be a valid factor key defined in
    ///   [`FactorKey`](crate::states::FactorKey).
    /// - `factor`: The factor value to store for this configuration key.
    ///
    /// # Errors
    /// - The [`authority`](InsertConfig::authority) must be a signer and have the CONFIG_KEEPER role
    ///   in the store.
    /// - The provided `key` must be defined in [`FactorKey`](crate::states::FactorKey).
    /// - The store must be initialized and owned by this program.
    #[access_control(internal::Authenticate::only_config_keeper(&ctx))]
    pub fn insert_factor(ctx: Context<InsertConfig>, key: String, factor: u128) -> Result<()> {
        instructions::unchecked_insert_factor(ctx, &key, factor)
    }

    /// Insert an address value into the store's global configuration.
    ///
    /// This instruction allows a CONFIG_KEEPER to set or update an address value in the store's
    /// configuration. The key must be one of the predefined address keys.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InsertConfig).*
    ///
    /// # Arguments
    /// - `key`: The configuration key to update. Must be a valid address key defined in
    ///   [`AddressKey`](crate::states::AddressKey).
    /// - `address`: The address value to store for this configuration key.
    ///
    /// # Errors
    /// - The [`authority`](InsertConfig::authority) must be a signer and have the CONFIG_KEEPER role
    ///   in the store.
    /// - The provided `key` must be defined in [`AddressKey`](crate::states::AddressKey).
    /// - The store must be initialized and owned by this program.
    #[access_control(internal::Authenticate::only_config_keeper(&ctx))]
    pub fn insert_address(ctx: Context<InsertConfig>, key: String, address: Pubkey) -> Result<()> {
        instructions::unchecked_insert_address(ctx, &key, address)
    }

    /// Insert order fee discount for referred user factor to the global config.
    ///
    /// This instruction allows a MARKET_KEEPER to set or update the GT minting cost referred
    /// discount factor in the store's configuration. This factor determines the discount
    /// applied to GT minting costs for referred users.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InsertConfig).*
    ///
    /// # Arguments
    /// - `factor`: The discount factor value to set.
    ///
    /// # Errors
    /// - The [`authority`](InsertConfig::authority) must be a signer and have the
    ///   MARKET_KEEPER role in the store.
    /// - The store must be initialized and owned by this program.
    ///
    /// # Notes
    /// - While [`insert_factor`] can also modify this value, it requires CONFIG_KEEPER
    ///   permissions instead of MARKET_KEEPER permissions required by this instruction.
    /// - The factor is stored under the [`FactorKey::OrderFeeDiscountForReferredUser`] key.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn insert_order_fee_discount_for_referred_user(
        ctx: Context<InsertConfig>,
        factor: u128,
    ) -> Result<()> {
        let key = FactorKey::OrderFeeDiscountForReferredUser;
        instructions::unchecked_insert_factor(ctx, &key.to_string(), factor)
    }

    // ===========================================
    //             Feature Management
    // ===========================================

    /// Enable or disable a feature in this deployment.
    ///
    /// This instruction allows a FEATURE_KEEPER to toggle specific features on or off by providing
    /// a domain and action combination. Features are used to control which functionality is available
    /// in this deployment.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ToggleFeature).*
    ///
    /// # Arguments
    /// - `domain`: The domain part of the feature flag, must be a valid domain defined in
    ///   [`DomainDisabledFlag`](crate::states::feature::DomainDisabledFlag).
    /// - `action`: The action part of the feature flag, must be a valid action defined in
    ///   [`ActionDisabledFlag`](crate::states::feature::ActionDisabledFlag).
    /// - `enable`: If true, enables the feature. If false, disables it.
    ///
    /// # Errors
    /// - The [`authority`](ToggleFeature::authority) must be a signer and have the
    ///   FEATURE_KEEPER role in the store.
    /// - The `domain` must be a valid domain defined in [`DomainDisabledFlag`](crate::states::feature::DomainDisabledFlag).
    /// - The `action` must be a valid action defined in [`ActionDisabledFlag`](crate::states::feature::ActionDisabledFlag).
    ///
    /// # Warnings
    /// Although we currently do not provide a feature to disable swaps (only a feature to disable swap orders),
    /// if we were to introduce such a feature, we must be aware that the following operations could still potentially
    /// result in swaps:
    /// - (GLV) Deposits
    /// - (GLV) Withdrawals
    /// - Swap Orders
    /// - Increase Orders
    /// - Decrease Orders
    ///
    /// Therefore, to ensure that this feature effectively prevents swaps from occurring, we need to add
    /// validation of the swap parameters before executing all of these actions to ensure that swaps do not happen.
    #[access_control(internal::Authenticate::only_feature_keeper(&ctx))]
    pub fn toggle_feature(
        ctx: Context<ToggleFeature>,
        domain: String,
        action: String,
        enable: bool,
    ) -> Result<()> {
        let domain = domain
            .parse()
            .map_err(|_| error!(CoreError::InvalidArgument))?;
        let action = action
            .parse()
            .map_err(|_| error!(CoreError::InvalidArgument))?;
        instructions::unchecked_toggle_feature(ctx, domain, action, enable)
    }

    // ===========================================
    //           Token Config Management
    // ===========================================

    /// Initialize a new token map account with its store set to [`store`](InitializeTokenMap::store).
    ///
    /// Anyone can initialize a token map account without any permissions, but after initialization, only
    /// addresses authorized by the store can modify this token map (i.e. have the `MARKET_KEEPER` role).
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InitializeTokenMap)*
    ///
    /// # Errors
    /// - The [`payer`](InitializeTokenMap::payer) must be a signer.
    /// - The [`store`](InitializeTokenMap::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program.
    /// - The [`token_map`](InitializeTokenMap::token_map) must be an uninitialized account.
    pub fn initialize_token_map(ctx: Context<InitializeTokenMap>) -> Result<()> {
        instructions::initialize_token_map(ctx)
    }

    /// Push a new token config to the given token map.
    ///
    /// This instruction is used to add or update the token config for an existing token.
    /// The token's decimals will be automatically set based on the token mint account.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](PushToTokenMap).
    ///
    /// # Arguments
    /// - `name`: The token identifier (e.g. "WSOL", "WBTC")
    /// - `builder`: Configuration builder containing token parameters
    /// - `enable`: If true, enables this token config after pushing. If false, disables it.
    /// - `new`: If true, requires this to be a new token config. An error will be returned if
    ///   a config already exists for this token. If false, allows updating existing configs.
    ///
    /// # Errors
    /// - The [`authority`](PushToTokenMap::authority) must be a signer with the MARKET_KEEPER role
    /// - The [`store`](PushToTokenMap::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program.
    /// - The [`token_map`](PushToTokenMap::token_map) must be initialized and owned by the `store`.
    /// - The [`token`](PushToTokenMap::token) must be a valid SPL token mint account.
    /// - If `new` is true, the token must not already have a config in the map.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn push_to_token_map(
        ctx: Context<PushToTokenMap>,
        name: String,
        builder: UpdateTokenConfigParams,
        enable: bool,
        new: bool,
    ) -> Result<()> {
        instructions::unchecked_push_to_token_map(ctx, &name, builder, enable, new)
    }

    /// Push a new synthetic token config to the given token map.
    ///
    /// This instruction allows adding or updating token configurations for synthetic tokens that don't have
    /// an actual SPL token mint account. Unlike regular tokens where decimals are read from the mint,
    /// synthetic tokens specify their decimals directly through the `token_decimals` parameter.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](PushToTokenMapSynthetic).
    ///
    /// # Arguments
    /// - `name`: The identifier for the synthetic token (e.g. "BTC")
    /// - `token`: The public key to use as the synthetic token's address
    /// - `token_decimals`: Number of decimals for the synthetic token's amounts
    /// - `builder`: Configuration builder containing token parameters
    /// - `enable`: If true, enables this token config after pushing. If false, disables it.
    /// - `new`: If true, requires this to be a new token config. An error will be returned if
    ///   a config already exists for this token. If false, allows updating the existing config.
    ///
    /// # Errors
    /// - The [`authority`](PushToTokenMapSynthetic::authority) must be a signer with the MARKET_KEEPER role.
    /// - The [`store`](PushToTokenMapSynthetic::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program.
    /// - The [`token_map`](PushToTokenMapSynthetic::token_map) must be initialized and owned by the `store`.
    /// - If updating an existing config, the `token_decimals` must match the original value.
    /// - If `new` is true, the token must not already have a config in the map.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn push_to_token_map_synthetic(
        ctx: Context<PushToTokenMapSynthetic>,
        name: String,
        token: Pubkey,
        token_decimals: u8,
        builder: UpdateTokenConfigParams,
        enable: bool,
        new: bool,
    ) -> Result<()> {
        instructions::unchecked_push_to_token_map_synthetic(
            ctx,
            &name,
            token,
            token_decimals,
            builder,
            enable,
            new,
        )
    }

    /// Enable or disable the config for the given token.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ToggleTokenConfig).
    ///
    /// # Arguments
    /// - `token`: The token whose config will be updated.
    /// - `enable`: Enable or disable the config.
    ///
    /// # Errors
    /// - The [`authority`](ToggleTokenConfig::authority) must be a signer
    ///   and a MARKET_KEEPER in the given store.
    /// - The [`store`](ToggleTokenConfig::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program .
    /// - The [`token_map`](ToggleTokenConfig::token_map) must be an initialized token map account
    ///   owned by the `store`.
    /// - The given `token` must exist in the token map.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn toggle_token_config(
        ctx: Context<ToggleTokenConfig>,
        token: Pubkey,
        enable: bool,
    ) -> Result<()> {
        instructions::unchecked_toggle_token_config(ctx, token, enable)
    }

    /// Set the expected provider for the given token.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](SetExpectedProvider).
    ///
    /// # Arguments
    /// - `token`: The token whose config will be updated.
    /// - `provider`: The provider index to be set as the expected provider
    ///   for the token. Must be a valid [`PriceProviderKind`] value.
    ///
    /// # Errors
    /// - The [`authority`](SetExpectedProvider::authority) must be a signer
    ///   and have the MARKET_KEEPER role in the given store.
    /// - The [`store`](SetExpectedProvider::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program.
    /// - The [`token_map`](SetExpectedProvider::token_map) must be an initialized token map account
    ///   owned by the `store`.
    /// - The given `token` must exist in the token map.
    /// - The `provider` index must correspond to a valid [`PriceProviderKind`].
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn set_expected_provider(
        ctx: Context<SetExpectedProvider>,
        token: Pubkey,
        provider: u8,
    ) -> Result<()> {
        instructions::unchecked_set_expected_provider(
            ctx,
            token,
            PriceProviderKind::try_from(provider)
                .map_err(|_| CoreError::InvalidProviderKindIndex)?,
        )
    }

    /// Set the feed config of the given provider for the given token.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](SetFeedConfig).
    ///
    /// # Arguments
    /// - `token`: The token whose config will be updated.
    /// - `provider`: The index of the provider whose feed config will be updated.
    ///   Must be a valid [`PriceProviderKind`] value.
    /// - `feed`: The new feed address.
    /// - `timestamp_adjustment`: The new timestamp adjustment in seconds.
    ///
    /// # Errors
    /// - The [`authority`](SetFeedConfig::authority) must be a signer
    ///   and a MARKET_KEEPER in the given store.
    /// - The [`store`](SetFeedConfig::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program.
    /// - The [`token_map`](SetFeedConfig::token_map) must be an initialized token map account
    ///   owned by the `store`.
    /// - The given `token` must exist in the token map.
    /// - The `provider` index must correspond to a valid [`PriceProviderKind`].
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn set_feed_config(
        ctx: Context<SetFeedConfig>,
        token: Pubkey,
        provider: u8,
        feed: Pubkey,
        timestamp_adjustment: u32,
    ) -> Result<()> {
        instructions::unchecked_set_feed_config(
            ctx,
            token,
            &PriceProviderKind::try_from(provider)
                .map_err(|_| CoreError::InvalidProviderKindIndex)?,
            feed,
            timestamp_adjustment,
        )
    }

    /// Return whether the token config is enabled.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ReadTokenMap).
    ///
    /// # Arguments
    /// - `token`: The address of the token to query for.
    ///
    /// # Errors
    /// - The [`token_map`](ReadTokenMap::token_map) must be an initialized token map account
    ///   owned by the `store`.
    /// - The given `token` must exist in the token map.
    ///
    /// # Returns
    /// Returns `true` if the token config is enabled, `false` otherwise.
    pub fn is_token_config_enabled(ctx: Context<ReadTokenMap>, token: Pubkey) -> Result<bool> {
        instructions::is_token_config_enabled(ctx, &token)
    }

    /// Get the expected provider of the given token.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ReadTokenMap).
    ///
    /// # Arguments
    /// - `token`: The address of the token to query for.
    ///
    /// # Errors
    /// - The [`token_map`](ReadTokenMap::token_map) must be an initialized token map account
    ///   owned by the `store`.
    /// - The given `token` must exist in the token map.
    ///
    /// # Returns
    /// Returns the expected provider kind as a u8 index. See [`PriceProviderKind`] for valid indices.
    pub fn token_expected_provider(ctx: Context<ReadTokenMap>, token: Pubkey) -> Result<u8> {
        instructions::token_expected_provider(ctx, &token).map(|kind| kind as u8)
    }

    /// Get the configured feed of the given token for the provider.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ReadTokenMap).
    ///
    /// # Arguments
    /// - `token`: The address of the token to query for.
    /// - `provider`: The index of provider to query for. Must be a valid index defined in
    ///   [`PriceProviderKind`].
    ///
    /// # Errors
    /// - The [`token_map`](ReadTokenMap::token_map) must be an initialized token map account
    ///   owned by the `store`.
    /// - The given `token` must exist in the token map.
    /// - The `provider` must be a valid index defined in [`PriceProviderKind`], otherwise
    ///   returns [`CoreError::InvalidProviderKindIndex`].
    ///
    /// # Returns
    /// Returns the configured feed address for the given token and provider.
    pub fn token_feed(ctx: Context<ReadTokenMap>, token: Pubkey, provider: u8) -> Result<Pubkey> {
        instructions::token_feed(
            ctx,
            &token,
            &PriceProviderKind::try_from(provider)
                .map_err(|_| CoreError::InvalidProviderKindIndex)?,
        )
    }

    /// Get the configured timestamp adjustment of the given token for the provider.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ReadTokenMap).
    ///
    /// # Arguments
    /// - `token`: The address of the token to query for.
    /// - `provider`: The index of provider to query for. Must be a valid index defined in
    ///   [`PriceProviderKind`].
    ///
    /// # Errors
    /// - The [`token_map`](ReadTokenMap::token_map) must be an initialized token map account
    ///   owned by the `store`.
    /// - The given `token` must exist in the token map.
    /// - The `provider` must be a valid index defined in [`PriceProviderKind`], otherwise
    ///   returns [`CoreError::InvalidProviderKindIndex`].
    ///
    /// # Returns
    /// Returns the configured timestamp adjustment for the given token and provider.
    pub fn token_timestamp_adjustment(
        ctx: Context<ReadTokenMap>,
        token: Pubkey,
        provider: u8,
    ) -> Result<u32> {
        instructions::token_timestamp_adjustment(
            ctx,
            &token,
            &PriceProviderKind::try_from(provider)
                .map_err(|_| CoreError::InvalidProviderKindIndex)?,
        )
    }

    /// Get the name of the token.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ReadTokenMap).
    ///
    /// # Arguments
    /// - `token`: The address of the token to query for.
    ///
    /// # Errors
    /// - The [`token_map`](ReadTokenMap::token_map) must be an initialized token map account
    ///   owned by the store program.
    /// - The given `token` must exist in the token map.
    ///
    /// # Returns
    /// Returns the configured name string for the given token.
    pub fn token_name(ctx: Context<ReadTokenMap>, token: Pubkey) -> Result<String> {
        instructions::token_name(ctx, &token)
    }

    /// Get the decimals of the token.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ReadTokenMap).
    ///
    /// # Arguments
    /// - `token`: The address of the token to query for.
    ///
    /// # Errors
    /// - The [`token_map`](ReadTokenMap::token_map) must be an initialized token map account
    ///   owned by the store program.
    /// - The given `token` must exist in the token map.
    ///
    /// # Returns
    /// Returns the configured number of decimals for the given token.
    pub fn token_decimals(ctx: Context<ReadTokenMap>, token: Pubkey) -> Result<u8> {
        instructions::token_decimals(ctx, &token)
    }

    /// Get the price precision of the token.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts*](ReadTokenMap).
    ///
    /// # Arguments
    /// - `token`: The address of the token to query for.
    ///
    /// # Errors
    /// - The [`token_map`](ReadTokenMap::token_map) must be an initialized token map account
    ///   owned by the store program.
    /// - The given `token` must exist in the token map.
    ///
    /// # Returns
    /// Returns the configured price precision for the given token.
    pub fn token_precision(ctx: Context<ReadTokenMap>, token: Pubkey) -> Result<u8> {
        instructions::token_precision(ctx, &token)
    }

    // ===========================================
    //              Oracle Management
    // ===========================================

    /// Initialize a new oracle account for the given store.
    ///
    /// This instruction creates a new oracle account that will be owned by the store. The oracle
    /// account is used to store price data for tokens configured in the store's token map.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InitializeOracle)*
    ///
    /// # Errors
    /// - The [`store`](InitializeOracle::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program.
    /// - The [`oracle`](InitializeOracle::oracle) account must be uninitialized.
    pub fn initialize_oracle(ctx: Context<InitializeOracle>) -> Result<()> {
        instructions::initialize_oracle(ctx)
    }

    /// Clear all prices from the given oracle.
    ///
    /// This instruction removes all stored price data from the oracle account and resets it to the
    /// cleared state. This can be useful when needing to reset price data or when decommissioning an
    /// oracle.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ClearAllPrices)*
    ///
    /// # Errors
    /// - The [`authority`](ClearAllPrices::authority) must be a signer and have the ORACLE_CONTROLLER
    ///   role in the given store. It must also be the authority of the oracle.
    /// - The [`store`](ClearAllPrices::store) must be an initialized store account owned by the
    ///   store program.
    /// - The [`oracle`](ClearAllPrices::oracle) must be an initialized oracle account owned by
    ///   the given store.
    #[access_control(internal::Authenticate::only_oracle_controller(&ctx))]
    pub fn clear_all_prices(ctx: Context<ClearAllPrices>) -> Result<()> {
        instructions::unchecked_clear_all_prices(ctx)
    }

    /// Set prices from the provided price feeds.
    ///
    /// This instruction updates token prices in the oracle account using data from configured price feeds.
    /// For each token provided, it reads the current price from the corresponding price feed account and
    /// stores it in the oracle.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](SetPricesFromPriceFeed)*
    ///
    /// # Arguments
    /// - `tokens`: The list of token mint addresses to update prices for. Each token must be configured
    ///   in the token map with a valid price feed.
    ///
    /// # Errors
    /// - The [`authority`](SetPricesFromPriceFeed::authority) must be a signer and have the
    ///   ORACLE_CONTROLLER role in the given store. It must also be the authority of the `oracle`.
    /// - The [`store`](SetPricesFromPriceFeed::store) must be an initialized store account owned by
    ///   the store program.
    /// - The [`oracle`](SetPricesFromPriceFeed::oracle) must be an initialized oracle account owned
    ///   by the given store. It must not have any prices set and be in the cleared state.
    /// - The [`token_map`](SetPricesFromPriceFeed::token_map) must be an initialized token map account
    ///   that is owned and authorized by the store.
    /// - The number of tokens provided cannot exceed [`MAX_TOKENS`](crate::states::oracle::price_map::PriceMap::MAX_TOKENS).
    /// - Each token in `tokens` must be configured and enabled in the token map.
    /// - For each token, there must be a valid corresponding price feed account included in the remaining accounts.
    #[access_control(internal::Authenticate::only_oracle_controller(&ctx))]
    pub fn set_prices_from_price_feed<'info>(
        ctx: Context<'_, '_, 'info, 'info, SetPricesFromPriceFeed<'info>>,
        tokens: Vec<Pubkey>,
    ) -> Result<()> {
        instructions::unchecked_set_prices_from_price_feed(ctx, tokens)
    }

    /// Initialize a custom price feed account.
    ///
    /// Creates a new price feed account that can be used to provide custom price data for a token.
    /// The price feed is owned by the store and can only be updated by ORDER_KEEPERs.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InitializePriceFeed)*
    ///
    /// # Arguments
    /// - `index`: The oracle index this price feed will be associated with.
    /// - `provider`: The price provider kind index that will be used for this feed. Must be a valid
    ///   index from [`PriceProviderKind`] that supports custom price feeds.
    /// - `token`: The mint address of the token this price feed will provide prices for.
    /// - `feed_id`: The feed ID defined by the price provider.
    ///
    /// # Errors
    /// - The [`authority`](InitializePriceFeed::authority) must be a signer and have the PRICE_KEEPER
    ///   role in the store.
    /// - The [`store`](InitializePriceFeed::store) must be an initialized store account owned by
    ///   the store program.
    /// - The [`price_feed`](InitializePriceFeed::price_feed) must be uninitialized and its address
    ///   must match the PDA derived from the `store`, `index`, `feed_id`, and other expected seeds.
    /// - The `provider` index must correspond to a valid [`PriceProviderKind`] that supports
    ///   custom price feeds.
    #[access_control(internal::Authenticate::only_price_keeper(&ctx))]
    pub fn initialize_price_feed(
        ctx: Context<InitializePriceFeed>,
        index: u16,
        provider: u8,
        token: Pubkey,
        feed_id: Pubkey,
    ) -> Result<()> {
        let provider = PriceProviderKind::try_from(provider)
            .map_err(|_| error!(CoreError::InvalidProviderKindIndex))?;
        instructions::unchecked_initialize_price_feed(ctx, index, provider, &token, &feed_id)
    }

    /// Updates the price data in a custom price feed account using a signed price report from
    /// Chainlink Data Streams. The price feed must be configured to use the Chainlink Data Streams
    /// provider.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](UpdatePriceFeedWithChainlink)*
    ///
    /// # Arguments
    /// - `signed_report`: A signed price report from Chainlink Data Streams containing the price data.
    ///
    /// # Errors
    /// - The [`authority`](UpdatePriceFeedWithChainlink::authority) must be a signer and have the
    ///   PRICE_KEEPER role in the store.
    /// - The [`store`](UpdatePriceFeedWithChainlink::store) must be an initialized store account
    /// - The [`verifier_account`](UpdatePriceFeedWithChainlink::verifier_account) must be a valid
    ///   Chainlink verifier account.
    /// - The [`price_feed`](UpdatePriceFeedWithChainlink::price_feed) must be initialized, owned by
    ///   the store, and authorized for the `authority`.
    /// - The [`chainlink`](UpdatePriceFeedWithChainlink::chainlink) program ID must be trusted in the
    ///   definition of the [`ChainlinkDataStreamsInterface`](gmsol_chainlink_datastreams::interface::ChainlinkDataStreamsInterface).
    /// - The price feed must be configured to use [`ChainlinkDataStreams`](PriceProviderKind::ChainlinkDataStreams)
    ///   as its provider.
    /// - The `signed_report` must be:
    ///   - Decodable as a valid Chainlink price report
    ///   - Verifiable by the Chainlink Verifier Program
    ///   - Contain valid data for creating a [`PriceFeedPrice`](states::oracle::PriceFeedPrice)
    /// - The current slot and timestamp must be >= the feed's last update.
    /// - The price data timestamp must be >= the feed's last price timestamp
    /// - The price data must meet all validity requirements (see the `update` method of [`PriceFeed`](states::oracle::PriceFeed)).
    #[access_control(internal::Authenticate::only_price_keeper(&ctx))]
    pub fn update_price_feed_with_chainlink(
        ctx: Context<UpdatePriceFeedWithChainlink>,
        compressed_report: Vec<u8>,
    ) -> Result<()> {
        instructions::unchecked_update_price_feed_with_chainlink(ctx, compressed_report)
    }

    // ===========================================
    //              Market Management
    // ===========================================

    /// Initialize a [`Market`](states::Market) account.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](InitializeMarket)
    ///
    /// # Arguments
    /// - `index_token_mint`: The address of the index token.
    /// - `name`: The name of the market.
    /// - `enable`: Whether to enable the market after initialization.
    ///
    /// # Errors
    /// - The [`authority`](InitializeMarket::authority) must be a signer and have the MARKET_KEEPER role
    ///   in the store.
    /// - The [`store`](InitializeMarket::store) must be initialized.
    /// - The [`market_token_mint`](InitializeMarket::market_token_mint) must be uninitialized
    ///   and a PDA derived from the expected seeds.
    /// - The [`market`](InitializeMarket::market) must be uninitialized and a PDA derived from
    ///   the expected seeds (see the documentation for [`market`](InitializeMarket::market) for details).
    /// - The [`token_map`](InitializeMarket::token_map) must be initialized and must be owned and
    ///   authorized by the `store`.
    /// - The [`long_token_vault`](InitializeMarket::long_token_vault) and
    ///   [`short_token_vault`](InitializeMarket::short_token_vault) must be initialized
    ///   and valid market vault accounts of the store for their respective tokens.
    /// - The long and short token mints must be valid Mint accounts.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn initialize_market(
        ctx: Context<InitializeMarket>,
        index_token_mint: Pubkey,
        name: String,
        enable: bool,
    ) -> Result<()> {
        instructions::unchecked_initialize_market(ctx, index_token_mint, &name, enable)
    }

    /// Enable or disable the given market.
    ///
    /// This instruction allows a MARKET_KEEPER to toggle whether a market is enabled or disabled.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](ToggleMarket)
    ///
    /// # Arguments
    /// - `enable`: Whether to enable (`true`) or disable (`false`) the market.
    ///
    /// # Errors
    /// - The [`authority`](ToggleMarket::authority) must be a signer and have the MARKET_KEEPER
    ///   role in the store.
    /// - The [`store`](ToggleMarket::store) must be initialized and owned by this program.
    /// - The [`market`](ToggleMarket::market) must be initialized and owned by the store.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn toggle_market(ctx: Context<ToggleMarket>, enable: bool) -> Result<()> {
        instructions::unchecked_toggle_market(ctx, enable)
    }

    /// Transfer tokens into the market and record the amounts in its balance.
    ///
    /// This instruction allows a MARKET_KEEPER to transfer tokens from a source account into one of
    /// the market vault accounts, updating the market's internal balance tracking.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](MarketTransferIn)
    ///
    /// # Arguments
    /// - `amount`: The amount of tokens to transfer into the market vault.
    ///
    /// # Errors
    /// - The [`authority`](MarketTransferIn::authority) must be a signer and have the MARKET_KEEPER
    ///   role in the store.
    /// - The [`store`](MarketTransferIn::store) must be an initialized store account owned by this program.
    /// - The [`from_authority`](MarketTransferIn::from_authority) must be a signer and have the
    ///   permission to transfer.
    /// - The [`market`](MarketTransferIn::market) must be an initialized market account owned by the store.
    /// - The [`from`](MarketTransferIn::from) must be an initialized token account and cannot be the
    ///   same as the destination vault.
    /// - The [`vault`](MarketTransferIn::vault) must be an initialized and valid market vault token
    ///   account owned by the store. It must have the same mint as the `from` token account.
    /// - The market must be enabled and the token being transferred must be one of the market's
    ///   configured pool tokens (long token or short token).
    /// - The source token account must have sufficient balance for the transfer amount.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn market_transfer_in(ctx: Context<MarketTransferIn>, amount: u64) -> Result<()> {
        instructions::unchecked_market_transfer_in(ctx, amount)
    }

    /// Update an item in the market config.
    ///
    /// This instruction allows a MARKET_KEEPER to update a single configuration value in the market's
    /// configuration. The key must be one of the predefined market config keys.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](UpdateMarketConfig)
    ///
    /// # Arguments
    /// - `key`: The configuration key to update. Must be a valid key defined in
    ///   [`MarketConfigKey`](states::market::config::MarketConfigKey).
    /// - `value`: The new value to set for this configuration key.
    ///
    /// # Errors
    /// - The [`authority`](UpdateMarketConfig::authority) must be a signer and have the MARKET_KEEPER
    ///   role in the store.
    /// - The [`store`](UpdateMarketConfig::store) must be an initialized store account owned by this program.
    /// - The [`market`](UpdateMarketConfig::market) must be an initialized market account owned by the store.
    /// - The provided `key` must be defined in [`MarketConfigKey`](states::market::config::MarketConfigKey).
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn update_market_config(
        ctx: Context<UpdateMarketConfig>,
        key: String,
        value: u128,
    ) -> Result<()> {
        instructions::unchecked_update_market_config(ctx, &key, value)
    }

    /// Update a flag in the market config.
    ///
    /// This instruction allows a MARKET_KEEPER to update a single flag in the market's
    /// configuration. The key must be one of the predefined market config flags.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](UpdateMarketConfig)
    ///
    /// # Arguments
    /// - `key`: The flag to update. Must be a valid key defined in
    ///   [`MarketConfigFlag`](states::market::config::MarketConfigFlag).
    /// - `value`: The new boolean value to set for this flag.
    ///
    /// # Errors
    /// - The [`authority`](UpdateMarketConfig::authority) must be a signer and have the MARKET_KEEPER
    ///   role in the store.
    /// - The [`store`](UpdateMarketConfig::store) must be an initialized store account owned by this program.
    /// - The [`market`](UpdateMarketConfig::market) must be an initialized market account owned by the store.
    /// - The provided `key` must be defined in [`MarketConfigFlag`](states::market::config::MarketConfigFlag).
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn update_market_config_flag(
        ctx: Context<UpdateMarketConfig>,
        key: String,
        value: bool,
    ) -> Result<()> {
        instructions::unchecked_update_market_config_flag(ctx, &key, value)
    }

    /// Update the market configuration using a pre-populated
    /// [`MarketConfigBuffer`](crate::states::market::config::MarketConfigBuffer) account.
    ///
    /// This instruction allows a MARKET_KEEPER to update multiple market configuration values at once
    /// by applying the changes stored in a buffer account. The buffer must contain valid configuration
    /// keys and values.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](UpdateMarketConfigWithBuffer)
    ///
    /// # Errors
    /// - The [`authority`](UpdateMarketConfigWithBuffer::authority) must be a signer and have the
    ///   MARKET_KEEPER role in the store.
    /// - The [`store`](UpdateMarketConfigWithBuffer::store) must be an initialized store account
    ///   owned by this program.
    /// - The [`market`](UpdateMarketConfigWithBuffer::market) must be an initialized market account
    ///   owned by the store.
    /// - The [`buffer`](UpdateMarketConfigWithBuffer::buffer) must be:
    ///   - An initialized market config buffer account
    ///   - Owned by both the store and the authority
    ///   - Not expired
    /// - All configuration keys in the buffer must be valid keys defined in
    ///   [`MarketConfigKey`](states::market::config::MarketConfigKey).
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn update_market_config_with_buffer(
        ctx: Context<UpdateMarketConfigWithBuffer>,
    ) -> Result<()> {
        instructions::unchecked_update_market_config_with_buffer(ctx)
    }

    /// Calculate the current market status.
    ///
    /// This instruction calculates and returns the current status of a market, including metrics like
    /// pool value, PnL, and other key indicators. The calculation can be configured to maximize or
    /// minimize certain values based on the provided flags.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](ReadMarket)
    ///
    /// # Arguments
    /// - `prices`: The current unit prices of tokens in the market, used for calculations.
    /// - `maximize_pnl`: If true, uses the maximum possible PnL values in calculations.
    ///   If false, uses minimum PnL values.
    /// - `maximize_pool_value`: If true, uses the maximum possible pool value in calculations.
    ///   If false, uses minimum pool value.
    ///
    /// # Errors
    /// - The [`market`](ReadMarket::market) account must be properly initialized.
    /// - The provided prices must be non-zero.
    /// - Any calculation errors.
    pub fn get_market_status(
        ctx: Context<ReadMarket>,
        prices: Prices<u128>,
        maximize_pnl: bool,
        maximize_pool_value: bool,
    ) -> Result<MarketStatus> {
        instructions::get_market_status(ctx, &prices, maximize_pnl, maximize_pool_value)
    }

    /// Get the current market token price based on the provided token prices and PnL factor.
    ///
    /// This instruction calculates and returns the current price of the market token, taking into
    /// account the provided token prices and PnL factor. The calculation can be configured to
    /// maximize certain values based on the provided flag.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](ReadMarketWithToken)
    ///
    /// # Arguments
    /// - `prices`: The current unit prices of tokens in the market, used for calculations.
    /// - `pnl_factor`: The PnL factor key to use for price calculations, must be a valid
    ///   [`PnlFactorKind`](gmsol_model::PnlFactorKind).
    /// - `maximize`: If true, uses the maximum possible values in calculations.
    ///   If false, uses minimum values.
    ///
    /// # Errors
    /// - The [`market`](ReadMarketWithToken::market) must be an initialized market account.
    /// - The provided prices must be non-zero.
    /// - The `pnl_factor` must be a valid [`PnlFactorKind`](gmsol_model::PnlFactorKind).
    /// - Any calculation errors.
    pub fn get_market_token_price(
        ctx: Context<ReadMarketWithToken>,
        prices: Prices<u128>,
        pnl_factor: String,
        maximize: bool,
    ) -> Result<u128> {
        instructions::get_market_token_price(
            ctx,
            &prices,
            pnl_factor
                .parse()
                .map_err(|_| error!(CoreError::InvalidArgument))?,
            maximize,
        )
    }

    /// Initialize a market config buffer account.
    ///
    /// This instruction creates a new market config buffer account that can be used to stage market
    /// configuration changes before applying them. The buffer has an expiration time after which
    /// it cannot be used.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](InitializeMarketConfigBuffer)
    ///
    /// # Arguments
    /// - `expire_after_secs`: The number of seconds after which this buffer account will expire.
    ///   Once expired, the buffer can no longer be used and must be closed.
    ///
    /// # Errors
    /// - The [`authority`](InitializeMarketConfigBuffer::authority) must be a signer and will be
    ///   set as the owner of the buffer account.
    /// - The [`store`](InitializeMarketConfigBuffer::store) must be an initialized store account
    ///   owned by the program.
    /// - The [`buffer`](InitializeMarketConfigBuffer::buffer) must be an uninitialized account
    ///   that will store the market configuration data.
    /// - The expiration time must be greater than zero.
    pub fn initialize_market_config_buffer(
        ctx: Context<InitializeMarketConfigBuffer>,
        expire_after_secs: u32,
    ) -> Result<()> {
        instructions::initialize_market_config_buffer(ctx, expire_after_secs)
    }

    /// Transfer ownership of a market config buffer account to a new authority.
    ///
    /// This instruction allows the current authority to transfer ownership of the buffer
    /// account to a new authority. After the transfer, only the new authority will be able
    /// to modify or close the buffer.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](SetMarketConfigBufferAuthority)
    ///
    /// # Arguments
    /// - `new_authority`: The public key of the new authority that will own the buffer account.
    ///
    /// # Errors
    /// - The [`authority`](SetMarketConfigBufferAuthority::authority) must be a signer
    ///   and the current owner of the `buffer` account.
    /// - The [`buffer`](SetMarketConfigBufferAuthority::buffer) must be an initialized
    ///   market config buffer account.
    /// - The `new_authority` cannot be the same as the current authority.
    pub fn set_market_config_buffer_authority(
        ctx: Context<SetMarketConfigBufferAuthority>,
        new_authority: Pubkey,
    ) -> Result<()> {
        instructions::set_market_config_buffer_authority(ctx, new_authority)
    }

    /// Close the given market config buffer account and reclaim its rent.
    ///
    /// This instruction allows the authority to close their market config buffer account
    /// and reclaim the rent.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](CloseMarketConfigBuffer)
    ///
    /// # Errors
    /// - The [`authority`](CloseMarketConfigBuffer::authority) must be a signer
    ///   and the owner of the `buffer` account.
    /// - The [`buffer`](CloseMarketConfigBuffer::buffer) must be an initialized
    ///   market config buffer account.
    pub fn close_market_config_buffer(ctx: Context<CloseMarketConfigBuffer>) -> Result<()> {
        instructions::close_market_config_buffer(ctx)
    }

    /// Push config items to the given market config buffer account.
    ///
    /// This instruction allows the authority to add new configuration items to their market
    /// config buffer account. The buffer will be reallocated to accommodate the new items,
    /// with the authority paying for any additional rent.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](PushToMarketConfigBuffer)
    ///
    /// # Arguments
    /// - `new_configs`: The list of new config items to append to the buffer. Each item
    ///   consists of a string key and a factor value.
    ///
    /// # Errors
    /// - The [`authority`](PushToMarketConfigBuffer::authority) must be a signer
    ///   and the owner of the `buffer` account.
    /// - The [`buffer`](PushToMarketConfigBuffer::buffer) must be an initialized
    ///   market config buffer account.
    /// - The authority must have enough SOL to pay for any additional rent needed.
    /// - The keys in `new_configs` must be valid [`MarketConfigKey`](states::market::config::MarketConfigKey).
    pub fn push_to_market_config_buffer(
        ctx: Context<PushToMarketConfigBuffer>,
        new_configs: Vec<EntryArgs>,
    ) -> Result<()> {
        instructions::push_to_market_config_buffer(ctx, new_configs)
    }

    /// Enable or disable GT minting for the given market.
    ///
    /// This instruction allows a MARKET_KEEPER to control whether GT minting is enabled for the
    /// given market. When disabled, users cannot mint new GT tokens through this market.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](ToggleGTMinting)
    ///
    /// # Arguments
    /// - `enable`: Whether to enable (`true`) or disable (`false`) GT minting for the given market.
    ///
    /// # Errors
    /// - The [`authority`](ToggleGTMinting::authority) must be a signer and be a MARKET_KEEPER
    ///   in the store.
    /// - The [`store`](ToggleGTMinting::store) must be an initialized store account.
    /// - The [`market`](ToggleGTMinting::market) must be an initialized market account and owned
    ///   by the store.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn toggle_gt_minting(ctx: Context<ToggleGTMinting>, enable: bool) -> Result<()> {
        instructions::unchecked_toggle_gt_minting(ctx, enable)
    }

    /// Claim fees from the given market.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](ClaimFeesFromMarket)
    ///
    /// # Return
    /// - Returns the claimed amount in base units of the token.
    ///
    /// # Errors
    /// - The [`authority`](ClaimFeesFromMarket::authority) must be a signer and be the designated
    ///   fee receiver in the given store.
    /// - The [`store`](ClaimFeesFromMarket::store) must be an initialized [`Store`](crate::states::Store)
    ///   account owned by this program.
    /// - The [`market`](ClaimFeesFromMarket::market) must be an initialized [`Market`](crate::states::Market)
    ///   account owned by this program and associated with the given store.
    /// - The token being claimed must be one of the market's configured collateral tokens.
    /// - All provided token accounts must match their expected addresses.
    /// - The market must maintain valid balance requirements after the claim.
    pub fn claim_fees_from_market(ctx: Context<ClaimFeesFromMarket>) -> Result<u64> {
        let claimed = instructions::claim_fees_from_market(ctx)?;
        Ok(claimed)
    }

    /// Initialize a new market vault for a specific token.
    ///
    /// This instruction creates a new vault account that will be used to store tokens for a market.
    /// The vault is a PDA (Program Derived Address) account that can only be controlled by this program.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](InitializeMarketVault)
    ///
    /// # Errors
    /// - The [`authority`](InitializeMarketVault::authority) must be a signer and have MARKET_KEEPER
    ///   permissions in the store.
    /// - The [`store`](InitializeMarketVault::store) must be an initialized store account.
    /// - The [`vault`](InitializeMarketVault::vault) must be an uninitialized account and its address
    ///   must match the PDA derived from the expected seeds.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn initialize_market_vault(ctx: Context<InitializeMarketVault>) -> Result<()> {
        instructions::unchecked_initialize_market_vault(ctx)
    }

    /// Prepare a claimable account to receive tokens during order execution.
    ///
    /// This instruction serves two purposes:
    /// 1. For uninitialized accounts: Creates and prepares the account to receive tokens
    /// 2. For initialized accounts: Unlocks the funds for the owner to claim
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](UseClaimableAccount)
    ///
    /// # Arguments
    /// - `timestamp`: The timestamp for which the claimable account was created.
    /// - `amount`: The token amount to approve for delegation.
    ///
    /// # Errors
    /// - The [`authority`](UseClaimableAccount::authority) must be a signer and have ORDER_KEEPER
    ///   permissions in the store.
    /// - The [`store`](UseClaimableAccount::store) must be an initialized store account.
    /// - The [`account`](UseClaimableAccount::account) must be a PDA derived from
    ///   the time window of the `timestamp` and other expected seeds. It can be uninitialized.
    /// - If the `account` is initialized, it must be owned by the store.
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn use_claimable_account(
        ctx: Context<UseClaimableAccount>,
        timestamp: i64,
        amount: u64,
    ) -> Result<()> {
        instructions::unchecked_use_claimable_account(ctx, timestamp, amount)
    }

    /// Close an empty claimable account.
    ///
    /// # Accounts
    /// [*See the documentation for the accounts.*](CloseEmptyClaimableAccount)
    ///
    /// # Arguments
    /// - `timestamp`: The timestamp for which the claimable account was created.
    ///
    /// # Errors
    /// - The [`authority`](CloseEmptyClaimableAccount::authority) must be a signer and have ORDER_KEEPER
    ///   permissions in the store.
    /// - The [`store`](CloseEmptyClaimableAccount::store) must be initialized.
    /// - The [`account`](CloseEmptyClaimableAccount::account) must be a PDA derived from
    ///   the claimable timestamp and other expected seeds.
    /// - The [`account`](CloseEmptyClaimableAccount::account) must be initialized and owned by the store.
    /// - The balance of the [`account`](CloseEmptyClaimableAccount::account) must be zero.
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn close_empty_claimable_account(
        ctx: Context<CloseEmptyClaimableAccount>,
        timestamp: i64,
    ) -> Result<()> {
        instructions::unchecked_close_empty_claimable_account(ctx, timestamp)
    }

    /// Prepare an associated token account.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](PrepareAssociatedTokenAccount)*
    ///
    /// # Errors
    /// - The [`payer`](PrepareAssociatedTokenAccount::payer) must be a signer.
    /// - The [`mint`](PrepareAssociatedTokenAccount::mint) must be a [`Mint`](anchor_spl::token_interface::Mint)
    ///   account that is owned by the given token program.
    /// - The [`account`](PrepareAssociatedTokenAccount::account) must be an associated token account
    ///   with:
    ///   - mint = [`mint`](PrepareAssociatedTokenAccount::mint)
    ///   - owner = [`owner`](PrepareAssociatedTokenAccount::owner)
    ///   - It can be uninitialized.
    pub fn prepare_associated_token_account(
        ctx: Context<PrepareAssociatedTokenAccount>,
    ) -> Result<()> {
        instructions::prepare_associated_token_account(ctx)
    }

    // ===========================================
    //                  Deposit
    // ===========================================

    /// Create a deposit by the owner.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CreateDeposit)*
    ///
    /// # Arguments
    /// - `nonce`: Nonce bytes used to derive the deposit account address.
    /// - `params`: Parameters specifying the deposit details.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`owner`](CreateDeposit::owner) is not a signer or has insufficient balance
    ///   for the execution fee and rent.
    /// - The [`store`](CreateDeposit::store) is not properly initialized.
    /// - The [`market`](CreateDeposit::market) is not initialized, not owned by the store,
    ///   or is disabled.
    /// - The [`deposit`](CreateDeposit::deposit) account is already initialized or is not
    ///   a valid PDA derived from the provided nonce and other expected seeds.
    /// - The [`market_token`](CreateDeposit::market_token) is not the market token of `market`.
    /// - Any required escrow account is not properly initialized or owned by the `deposit`.
    /// - Any source account has insufficient balance, does not match the initial tokens, or the
    ///   `owner` does not have the permission to transfer the tokens.
    /// - The remaining accounts do not form valid swap paths or reference disabled markets.
    pub fn create_deposit<'info>(
        mut ctx: Context<'_, '_, 'info, 'info, CreateDeposit<'info>>,
        nonce: [u8; 32],
        params: CreateDepositParams,
    ) -> Result<()> {
        internal::Create::create(&mut ctx, &nonce, &params)
    }

    /// Close a deposit, either by the owner or by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseDeposit)*
    ///
    /// # Arguments
    /// - `reason`: The reason for closing the deposit.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`executor`](CloseDeposit::executor) is not a signer or is neither the deposit
    ///   owner nor an ORDER_KEEPER in the store.
    /// - The [`store`](CloseDeposit::store) is not properly initialized.
    /// - The [`owner`](CloseDeposit::owner) does not match the deposit's owner.
    /// - The provided token mint accounts do not match those recorded in the `deposit`.
    /// - The [`deposit`](CloseDeposit::deposit) is not initialized, not owned by the store,
    ///   or not owned by the specified owner.
    /// - Any escrow account is not owned by the `deposit` or does not match the `deposit` records.
    /// - Any associated token account address is invalid.
    /// - The deposit is not in a cancelled or completed state when closed by a non-owner.
    pub fn close_deposit<'info>(
        ctx: Context<'_, '_, 'info, 'info, CloseDeposit<'info>>,
        reason: String,
    ) -> Result<()> {
        internal::Close::close(&ctx, &reason)
    }

    /// Execute a deposit by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteDeposit)*
    ///
    /// # Arguments
    /// - `execution_fee`: The execution fee claimed to be used by the keeper.
    /// - `throw_on_execution_error`: If true, throws an error if execution fails. If false,
    ///   the deposit will be cancelled instead.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`authority`](ExecuteDeposit::authority) is not a signer or is not an ORDER_KEEPER
    ///   in the store.
    /// - The [`store`](ExecuteDeposit::store) is not properly initialized.
    /// - The [`token_map`](ExecuteDeposit::token_map) is not initialized or not authorized by
    ///   the store.
    /// - The [`oracle`](ExecuteDeposit::oracle) is not initialized, cleared and owned by the
    ///   store.
    /// - The [`market`](ExecuteDeposit::market) is not initialized, is disabled, not owned by
    ///   the store, or does not match the market recorded in the `deposit`.
    /// - The [`deposit`](ExecuteDeposit::deposit) is not initialized, not owned by the store,
    ///   or not in the pending state.
    /// - Any token mint accounts do not match those recorded in the `deposit`.
    /// - Any escrow accounts are not properly owned or not recorded in the `deposit`.
    /// - Any vault accounts are not valid market vaults or do not correspond to the initial tokens.
    /// - Any feed accounts in the remaining accounts are invalid or do not match the swap parameters.
    /// - Any market accounts in the remaining accounts are disabled, not owned by the store,
    ///   or do not match the swap parameters.
    /// - Any oracle prices from the feed accounts are incomplete or invalid.
    /// - The execution fails and `throw_on_execution_error` is set to `true`.
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_deposit<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteDeposit<'info>>,
        execution_fee: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_deposit(ctx, execution_fee, throw_on_execution_error)
    }

    // ===========================================
    //                 Withdrawal
    // ===========================================

    /// Create a withdrawal by the owner.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CreateWithdrawal)*
    ///
    /// # Arguments
    /// - `nonce`: Nonce bytes used to derive the address for the withdrawal.
    /// - `params`: Withdrawal Parameters containing the withdrawal configuration.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`owner`](CreateWithdrawal::owner) is not a signer or has insufficient balance
    ///   for the execution fee and rent.
    /// - The [`store`](CreateWithdrawal::store) is not properly initialized.
    /// - The [`market`](CreateWithdrawal::market) is not initialized, is disabled, or not owned
    ///   by the store.
    /// - The [`withdrawal`](CreateWithdrawal::withdrawal) is already initialized or is not a valid
    ///   PDA derived from the provided `nonce` and expected seeds.
    /// - The [`market_token`](CreateWithdrawal::market_token) does not match the market token
    ///   of the specified market.
    /// - Any required escrow accounts are not properly initialized or not owned by the `withdrawal`.
    /// - The source market token account has insufficient balance, or the `owner` does not have the
    ///   permission to transfer the tokens.
    /// - Any market accounts in the remaining accounts are disabled, not owned by the store,
    ///   or do not form valid swap paths.
    pub fn create_withdrawal<'info>(
        mut ctx: Context<'_, '_, 'info, 'info, CreateWithdrawal<'info>>,
        nonce: [u8; 32],
        params: CreateWithdrawalParams,
    ) -> Result<()> {
        internal::Create::create(&mut ctx, &nonce, &params)
    }

    /// Close a withdrawal, either by the owner or by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseWithdrawal)*
    ///
    /// # Arguments
    /// - `reason`: The reason for closing the withdrawal.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`executor`](CloseWithdrawal::executor) is not a signer or is neither the withdrawal
    ///   owner nor an ORDER_KEEPER in the store.
    /// - The [`store`](CloseWithdrawal::store) is not properly initialized.
    /// - The [`owner`](CloseWithdrawal::owner) does not match the withdrawal owner.
    /// - The token mint accounts do not match those recorded in the `withdrawal`.
    /// - The [`withdrawal`](CloseWithdrawal::withdrawal) is not initialized, not owned by the store,
    ///   or not owned by the specified `owner`.
    /// - Any required escrow accounts are not properly initialized or not owned by the `withdrawal`.
    /// - Any associated token accounts have invalid addresses.
    /// - The withdrawal is not in a cancelled or completed state when the executor is not the owner
    pub fn close_withdrawal<'info>(
        ctx: Context<'_, '_, 'info, 'info, CloseWithdrawal<'info>>,
        reason: String,
    ) -> Result<()> {
        internal::Close::close(&ctx, &reason)
    }

    /// Execute a withdrawal by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteWithdrawal)*
    ///
    /// # Arguments
    /// - `execution_fee`: The execution fee to be paid to the keeper for executing the withdrawal.
    /// - `throw_on_execution_error`: If true, throws an error if execution fails. If false, the
    ///   withdrawal will be cancelled instead.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`authority`](ExecuteWithdrawal::authority) is not a signer or is not an ORDER_KEEPER
    ///   in the store.
    /// - The [`store`](ExecuteWithdrawal::store) is not properly initialized.
    /// - The [`token_map`](ExecuteWithdrawal::token_map) is not initialized or not authorized by
    ///   the store.
    /// - The [`oracle`](ExecuteWithdrawal::oracle) is not initialized, cleared and owned by the
    ///   store.
    /// - The [`market`](ExecuteWithdrawal::market) is not initialized, is disabled, not owned by
    ///   the store, or does not match the market recorded in the `withdrawal`.
    /// - The [`withdrawal`](ExecuteWithdrawal::withdrawal) is not initialized, not owned by the
    ///   store, or not in the pending state.
    /// - Any token mint accounts do not match those recorded in the `withdrawal`.
    /// - Any escrow accounts are not properly initialized or not owned by the `withdrawal`.
    /// - Any vault accounts are not valid market vaults or do not correspond to the final tokens.
    /// - Any feed accounts in the remaining accounts are invalid or do not match the swap parameters.
    /// - Any market accounts in the remaining accounts are disabled, not owned by the store, or do
    ///   not match the swap parameters.
    /// - Any oracle prices from the feed accounts are incomplete or invalid.
    /// - The execution fails and `throw_on_execution_error` is set to true.
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_withdrawal<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteWithdrawal<'info>>,
        execution_fee: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_withdrawal(ctx, execution_fee, throw_on_execution_error)
    }

    // ===========================================
    //             Order and Position
    // ===========================================

    /// Prepare the position account for orders.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](PreparePosition)*
    ///
    /// # Arguments
    /// - `params`: Order Parameters.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`owner`](PreparePosition::owner) is not a signer or has insufficient balance for the
    ///   rent.
    /// - The [`store`](PreparePosition::store) is not properly initialized.
    /// - The [`market`](PreparePosition::market) is not initialized, is disabled, or not owned by
    ///   the `store`.
    /// - The [`position`](PreparePosition::position) address is not a valid PDA derived from the
    ///   `owner` and expected seeds.
    /// - The position account is neither uninitialized nor validly initialized with `store` as the
    ///   store and `owner` as the owner.
    pub fn prepare_position(
        ctx: Context<PreparePosition>,
        params: CreateOrderParams,
    ) -> Result<()> {
        instructions::prepare_position(ctx, &params)
    }

    /// Create an order by the owner.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CreateOrder)*
    ///
    /// # Arguments
    /// - `nonce`: Nonce bytes used to derive the address for the order.
    /// - `params`: Order Parameters specifying the market, order kind, and other details.
    ///
    /// # Errors
    /// This instruction will fail if:
    /// - The [`owner`](CreateOrder::owner) is not a signer or has insufficient balance for the
    ///   execution fee and rent.
    /// - The [`store`](CreateOrder::store) is not properly initialized.
    /// - The [`market`](CreateOrder::market) is not initialized, is disabled, or not owned by
    ///   the `store`.
    /// - The [`user`](CreateOrder::user) is not initialized or does not correspond to the owner.
    ///   The address must be a valid PDA derived from the `owner` and expected seeds.
    /// - The [`order`](CreateOrder::order) is not uninitialized or the address is not a valid
    ///   PDA derived from the `owner`, `nonce` and expected seeds.
    /// - For increase/decrease orders:
    ///   - The [`position`](CreateOrder::position) is missing, not validly initialized, or not
    ///     owned by both the `owner` and `store`.
    ///   - The [`long_token`](CreateOrder::long_token) or [`short_token`](CreateOrder::short_token)
    ///     are missing, or do not match the those defined in the [`market`](CreateOrder::market).
    ///   - The [`long_token_escrow`](CreateOrder::long_token_escrow) or
    ///     [`short_token_escrow`](CreateOrder::short_token_escrow) are missing, not valid
    ///     escrow accounts for `long_token` or `short_token` respectively, or not owned by the `order`.
    /// - For increase/swap orders:
    ///   - The [`initial_collateral_token`](CreateOrder::initial_collateral_token) is missing
    ///     or invalid.
    ///   - The [`initial_collateral_token_escrow`](CreateOrder::initial_collateral_token_escrow)
    ///     is missing, not a valid escrow account for `initial_collateral_token`, or not owned by
    ///     the `order`.
    ///   - The [`initial_collateral_token_source`](CreateOrder::initial_collateral_token_source)
    ///     is missing or not a valid source account with `owner` as the authority.
    /// - For decrease/swap orders:
    ///   - The [`final_output_token`](CreateOrder::final_output_token) is invalid.
    ///   - The [`final_output_token_escrow`](CreateOrder::final_output_token_escrow) is missing,
    ///     not a valid escrow account for `final_output_token`, or not owned by the `order`.
    /// - The feature for creating this kind of order is not enabled.
    /// - The remaining market accounts do not match the swap parameters, not all enabled or owned
    ///   by the `store`.
    pub fn create_order<'info>(
        mut ctx: Context<'_, '_, 'info, 'info, CreateOrder<'info>>,
        nonce: [u8; 32],
        params: CreateOrderParams,
    ) -> Result<()> {
        internal::Create::create(&mut ctx, &nonce, &params)
    }

    /// Close an order, either by the owner or by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseOrder)*
    ///
    /// # Arguments
    /// - `reason`: The reason for the close.
    ///
    /// # Errors
    /// - The [`executor`](CloseOrder::executor) must be a signer and either the owner
    ///   of the `order` or a ORDER_KEEPER in the store.
    /// - The [`store`](CloseOrder::store) must be initialized.
    /// - The [`owner`](CloseOrder::owner) must be the owner of the `order`.
    /// - The [`user`](CloseOrder::user) must be initialized and correspond to the `owner`.
    /// - The [`referrer_user`](CloseOrder::referrer_user) must be present if the `owner` has a
    ///   referrer, and it must be initialized and correspond to the referrer of the `owner`.
    /// - The [`order`](CloseOrder::order) must be initialized and owned by the `store` and the
    ///   `owner`.
    /// - The tokens must be those recorded in the `order`.
    /// - The escrow accounts must be owned and recorded in the `order`.
    /// - The addresses of the ATAs must be valid.
    /// - The `order` must be cancelled or completed if the `executor` is not the owner.
    /// - The feature must be enabled for closing the given kind of `order`.
    pub fn close_order<'info>(
        ctx: Context<'_, '_, 'info, 'info, CloseOrder<'info>>,
        reason: String,
    ) -> Result<()> {
        internal::Close::close(&ctx, &reason)
    }

    /// Cancel order if the corresponding position does not exist.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CancelOrderIfNoPosition)*
    ///
    /// # Errors
    /// - The [`authority`](CancelOrderIfNoPosition::authority) must be a signed ORDER_KEEPER
    ///   in the store.
    /// - The [`store`](CancelOrderIfNoPosition::authority) must be initialized.
    /// - The [`order`](CancelOrderIfNoPosition::order) must be initialized and owned by the
    ///   `store`. It must be in the pending state.
    /// - The [`position`](CancelOrderIfNoPosition::position) must be recorded in the order.
    ///   It must be owned by the system program (i.e., considered to be missing).
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn cancel_order_if_no_position(ctx: Context<CancelOrderIfNoPosition>) -> Result<()> {
        instructions::unchecked_cancel_order_if_no_position(ctx)
    }

    /// Prepare a trade event buffer.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](PrepareTradeEventBuffer)*
    ///
    /// # Arguments
    /// - `index`: The index of the trade event buffer to prepare.
    ///
    /// # Errors
    /// - The [`authority`](PrepareTradeEventBuffer::authority) must be a signer.
    /// - The [`store`](PrepareTradeEventBuffer::store) must be initialized.
    /// - The [`event`](PrepareTradeEventBuffer::event) must be either:
    ///   - Uninitialized, or
    ///   - Already initialized with the `authority` as the authority and the `store` as
    ///     the store
    // Note: There is a false positive lint for the doc link of `event`.
    #[allow(rustdoc::broken_intra_doc_links)]
    pub fn prepare_trade_event_buffer(
        ctx: Context<PrepareTradeEventBuffer>,
        index: u16,
    ) -> Result<()> {
        instructions::prepare_trade_event_buffer(ctx, index)
    }

    /// Update an order by the owner.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](UpdateOrder)*
    ///
    /// # Arguments
    /// - `params`: Update Order Parameters.
    ///
    /// # Errors
    /// - The [`owner`](UpdateOrder::owner) must be a signer and the owner of the `order`.
    /// - The [`store`](UpdateOrder::store) must be initialized.
    /// - The [`market`](UpdateOrder::market) must be initialized, enabled and owned by the `store`.
    /// - The [`order`](UpdateOrder::order) must be:
    ///   - Initialized and owned by both the `store` and the `owner`
    ///   - Associated with the provided `market`
    ///   - In a pending state
    ///   - The order type must support updates
    /// - The feature must be enabled in the `store` for updating the given kind of `order`.
    /// - The updated parameters must be valid for the order type.
    pub fn update_order(ctx: Context<UpdateOrder>, params: UpdateOrderParams) -> Result<()> {
        instructions::update_order(ctx, &params)
    }

    /// Execute an increase/swap order by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteIncreaseOrSwapOrder)*
    ///
    /// # Arguments
    /// - `recent_timestamp`: A recent timestamp used for deriving the claimable accounts.
    /// - `execution_fee`: The execution fee claimed to be used the keeper.
    /// - `throw_on_execution_error`: If true, throws an error if order execution fails. If false,
    ///   silently cancels the order on execution failure.
    ///
    /// # Errors
    /// - The [`authority`](ExecuteIncreaseOrSwapOrder::authority) must be a signer and have the
    ///   ORDER_KEEPER role in the `store`.
    /// - The [`store`](ExecuteIncreaseOrSwapOrder::store) must be initialized.
    /// - The [`token_map`](ExecuteIncreaseOrSwapOrder::token_map) must be initialized and authorized
    ///   by the `store`.
    /// - The [`oracle`](ExecuteIncreaseOrSwapOrder::oracle) must be initialized, cleared and owned
    ///   by the `store`.
    /// - The [`market`](ExecuteIncreaseOrSwapOrder::market) must be initialized, enabled and owned
    ///   by the `store`. It must also be associated with the `order`.
    /// - The [`owner`](ExecuteIncreaseOrSwapOrder::owner) must be the owner of the `order`.
    /// - The [`user`](ExecuteIncreaseOrSwapOrder::user) must be initialized and associated with
    ///   the `owner`.
    /// - The [`order`](ExecuteIncreaseOrSwapOrder::order) must be:
    ///   - Initialized and owned by both the `store` and `owner`
    ///   - Associated with the provided `market`
    ///   - In a pending state
    /// - For increase orders:
    ///   - The [`initial_collateral_token`](ExecuteIncreaseOrSwapOrder::initial_collateral_token)
    ///     must be valid.
    ///   - The [`position`](ExecuteIncreaseOrSwapOrder::position) must exist and be owned by the
    ///     `owner` and `store`. It must match the `order`.
    ///   - The [`event`](ExecuteIncreaseOrSwapOrder::event) must be a valid trade event buffer owned
    ///     by both the `store` and `authority`.
    ///   - The [`long_token`](ExecuteIncreaseOrSwapOrder::long_token) and [`short_token`](ExecuteIncreaseOrSwapOrder::short_token)
    ///     must match those defined in the `market`.
    ///   - The corresponding token escrow and vault accounts must be valid, recorded in the `order`
    ///     and owned by the `order`.
    /// - For swap orders:
    ///   - The [`initial_collateral_token`](ExecuteIncreaseOrSwapOrder::initial_collateral_token)
    ///     must be valid.
    ///   - The [`final_output_token`](ExecuteIncreaseOrSwapOrder::final_output_token) must be valid.
    ///   - The corresponding escrow and vault accounts must be valid, recorded in the `order` and
    ///     owned by the `order`.
    /// - The remaining accounts must be valid. See the documentation for the accounts for more
    ///   details.
    /// - The feature for executing this order type must be enabled in the `store`.
    /// - If `throw_on_execution_error` is true, any execution failure will throw an error
    // Note: There is a false positive lint for the doc link of `event`.
    #[allow(rustdoc::broken_intra_doc_links)]
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_increase_or_swap_order<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteIncreaseOrSwapOrder<'info>>,
        recent_timestamp: i64,
        execution_fee: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_increase_or_swap_order(
            ctx,
            recent_timestamp,
            execution_fee,
            throw_on_execution_error,
        )
    }

    /// Execute a decrease order by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteDecreaseOrder)*
    ///
    /// # Arguments
    /// - `recent_timestamp`: A timestamp that must be within a recent time window.
    /// - `execution_fee`: The execution fee claimed to be used by the keeper.
    /// - `throw_on_execution_error`: If true, throws an error if order execution fails. If false,
    ///   silently cancels the order on execution failure.
    ///
    /// # Errors
    /// - The [`authority`](ExecuteDecreaseOrder::authority) must be a signer with the ORDER_KEEPER
    ///   role in the `store`.
    /// - The [`store`](ExecuteDecreaseOrder::store) must be initialized.
    /// - The [`token_map`](ExecuteDecreaseOrder::token_map) must be initialized and authorized
    ///   by the `store`.
    /// - The [`oracle`](ExecuteDecreaseOrder::oracle) must be initialized, cleared and owned
    ///   by the `store`.
    /// - The [`market`](ExecuteDecreaseOrder::market) must be initialized, enabled and owned
    ///   by the `store`.
    /// - The [`owner`](ExecuteDecreaseOrder::owner) must be the owner of the `order`.
    /// - The [`user`](ExecuteDecreaseOrder::user) must be initialized and associated with
    ///   the `owner`.
    /// - The [`order`](ExecuteDecreaseOrder::order) must be:
    ///   - Initialized and owned by both the `store` and `owner`
    ///   - Associated with the provided `market`
    ///   - In the pending state
    /// - The [`position`](ExecuteDecreaseOrder::position) must exist and be validly owned
    ///   by the `owner` and `store`. It must match the `order`.
    /// - The [`event`](ExecuteDecreaseOrder::event) must be a valid trade event buffer
    ///   owned by both the `store` and `authority`.
    /// - The tokens must match those recorded in the `order`.
    /// - All escrow accounts must be valid, recorded in the `order` and owned by the `order`.
    /// - All vault accounts must be valid market vault accounts and owned by the `store`.
    /// - All claimable token accounts must be valid and properly delegated to their owners.
    /// - The remaining accounts must be valid. See the documentation for the accounts for more
    ///   details.
    /// - The feature for executing decrease orders must be enabled in the `store`.
    /// - If `throw_on_execution_error` is true, any execution failure will throw an error.
    // Note: There is a false positive lint for the doc link of `event`.
    #[allow(rustdoc::broken_intra_doc_links)]
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_decrease_order<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteDecreaseOrder<'info>>,
        recent_timestamp: i64,
        execution_fee: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_decrease_order(
            ctx,
            recent_timestamp,
            execution_fee,
            throw_on_execution_error,
        )
    }

    /// Perform a liquidation by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](PositionCut)*
    ///
    /// # Arguments
    /// - `nonce`: The nonce used to derive the `order` PDA address.
    /// - `recent_timestamp`: A recent timestamp that must be within the valid time window.
    /// - `execution_fee`: The execution fee claimed to be used by the keeper.
    ///
    /// # Errors
    /// - The [`authority`](PositionCut::authority) must be a signer with the ORDER_KEEPER
    ///   role in the `store`.
    /// - The [`owner`](PositionCut::owner) must be the owner of the position being liquidated.
    /// - The [`user`](PositionCut::user) must be an initialized user account corresponding to the
    ///   `owner`.
    /// - The [`store`](PositionCut::store) must be initialized.
    /// - The [`token_map`](PositionCut::token_map) must be initialized and authorized by the
    ///   `store`.
    /// - The [`oracle`](PositionCut::oracle) must be initialized, cleared and owned by the `store`.
    /// - The [`market`](PositionCut::market) must be:
    ///   - Initialized and enabled
    ///   - Owned by the `store`
    ///   - Associated with the position being liquidated
    /// - The [`order`](PositionCut::order) must be:
    ///   - Uninitialized
    ///   - Have an address matching the PDA derived from the `store`, `owner`, provided
    ///     `nonce` and other expected seeds
    /// - The [`position`](PositionCut::position) must be:
    ///   - Validly initialized
    ///   - Owned by both the `owner` and `store`
    ///   - In a liquidatable state
    /// - The [`event`](PositionCut::event) must be a valid trade event buffer owned by both the
    ///   `store` and `authority`.
    /// - The [`long_token`](PositionCut::long_token) and [`short_token`](PositionCut::short_token)
    ///   must match those defined in the `market`.
    /// - Token escrow accounts must be:
    ///   - Valid for their respective tokens
    ///   - Owned by the `order`
    /// - Market vault accounts must be:
    ///   - Valid market vault accounts for their respective tokens
    ///   - Owned by the `store`
    /// - Claimable token accounts must be:
    ///   - Valid for their respective tokens
    ///   - Owned by the `store`
    ///   - Properly delegated to their owners
    /// - Price feed accounts must be:
    ///   - Valid and complete
    ///   - Provided in order matching the market's sorted token list
    /// - The liquidation feature must be enabled in the `store`.
    /// - Oracle prices must be valid and complete.
    // Note: There is a false positive lint for the doc link of `event`.
    #[allow(rustdoc::broken_intra_doc_links)]
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn liquidate<'info>(
        ctx: Context<'_, '_, 'info, 'info, PositionCut<'info>>,
        nonce: [u8; 32],
        recent_timestamp: i64,
        execution_fee: u64,
    ) -> Result<()> {
        instructions::unchecked_process_position_cut(
            ctx,
            &nonce,
            recent_timestamp,
            PositionCutKind::Liquidate,
            execution_fee,
            true,
        )
    }

    /// Update the ADL (Auto-Deleveraging) state for the market.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](UpdateAdlState)*
    ///
    /// # Arguments
    /// - `is_long`: Whether to update the ADL state for the long (`true`) or short (`false`) side
    ///   of the market.
    ///
    /// # Errors
    /// - The [`authority`](UpdateAdlState::authority) must be a signer and have the ORDER_KEEPER
    ///   role in the store.
    /// - The [`store`](UpdateAdlState::store) must be an initialized [`Store`](states::Store)
    ///   account owned by the store program.
    /// - The [`oracle`](UpdateAdlState::oracle) must be an initialized [`Oracle`](states::Oracle)
    ///   account that is owned by the store.
    /// - The [`market`](UpdateAdlState::market) must be enabled and owned by the store.
    /// - Price feed accounts must be valid and provided in the market's sorted token list order.
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn update_adl_state<'info>(
        ctx: Context<'_, '_, 'info, 'info, UpdateAdlState<'info>>,
        is_long: bool,
    ) -> Result<()> {
        instructions::unchecked_update_adl_state(ctx, is_long)
    }

    /// Perform an ADL (Auto-Deleveraging) by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](PositionCut)*
    ///
    /// # Arguments
    /// - `nonce`: The nonce used to derive the `order` PDA address.
    /// - `recent_timestamp`: A recent blockchain timestamp for validation.
    /// - `execution_fee`: The execution fee claimed to be used by the keeper.
    ///
    /// # Errors
    /// - The [`authority`](PositionCut::authority) must be a signer with the ORDER_KEEPER role.
    /// - The [`owner`](PositionCut::owner) must be the position owner.
    /// - The [`user`](PositionCut::user) must be initialized and corresponding to the `owner`.
    /// - The [`store`](PositionCut::store) must be initialized.
    /// - The [`token_map`](PositionCut::token_map) must be initialized and authorized by the store.
    /// - The [`oracle`](PositionCut::oracle) must be initialized, cleared and store-owned.
    /// - The [`market`](PositionCut::market) must be initialized, enabled, store-owned and match
    ///   the position's market. The market must be in ADL state.
    /// - The [`order`](PositionCut::order) must be uninitialized with address matching PDA from
    ///   the `store`, `owner`, `nonce` and other expected seeds.
    /// - The [`position`](PositionCut::position) must be initialized, owned by the `owner` and
    ///   `store` and eligible for ADL.
    /// - The [`event`](PositionCut::event) must be a valid trade event buffer owned by the `store`
    ///   and `authority`.
    /// - The [`long_token`](PositionCut::long_token) and [`short_token`](PositionCut::short_token)
    ///   must match those defined in the `market`.
    /// - The [`long_token_escrow`](PositionCut::long_token_escrow) and
    ///   [`short_token_escrow`](PositionCut::short_token_escrow) must be valid order-owned escrow
    ///   accounts for their respective tokens.
    /// - The [`long_token_vault`](PositionCut::long_token_vault) and
    ///   [`short_token_vault`](PositionCut::short_token_vault) must be valid store-owned market
    ///   vault accounts for their tokens.
    /// - The [`claimable_long_token_account_for_user`](PositionCut::claimable_long_token_account_for_user)
    ///   must be a store-owned, owner-delegated claimable account for long token.
    /// - The [`claimable_short_token_account_for_user`](PositionCut::claimable_short_token_account_for_user)
    ///   must be a store-owned, owner-delegated claimable account for short token.
    /// - The [`claimable_pnl_token_account_for_holding`](PositionCut::claimable_pnl_token_account_for_holding)
    ///   must be a store-owned, holding-delegated claimable account for PnL token.
    /// - Price feed accounts must be valid and provided in the market's sorted token list order.
    /// - The ADL feature must be enabled in the `store`.
    /// - Oracle prices must be valid and complete.
    /// - Execution must complete successfully.
    // Note: There is a false positive lint for the doc link of `event`.
    #[allow(rustdoc::broken_intra_doc_links)]
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn auto_deleverage<'info>(
        ctx: Context<'_, '_, 'info, 'info, PositionCut<'info>>,
        nonce: [u8; 32],
        recent_timestamp: i64,
        size_delta_in_usd: u128,
        execution_fee: u64,
    ) -> Result<()> {
        instructions::unchecked_process_position_cut(
            ctx,
            &nonce,
            recent_timestamp,
            PositionCutKind::AutoDeleverage(size_delta_in_usd),
            execution_fee,
            true,
        )
    }

    // ===========================================
    //                  Shift
    // ===========================================

    /// Create a shift by the owner.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CreateShift)*
    ///
    /// # Arguments
    /// - `nonce`: The nonce used to derive the shift's PDA address.
    /// - `params`: The parameters for creating the shift.
    ///
    /// # Errors
    /// - The [`owner`](CreateShift::owner) must be a signer and have sufficient balance for the
    ///   execution fee and rent.
    /// - The [`store`](CreateShift::store) must be initialized.
    /// - The [`from_market`](CreateShift::from_market) must be initialized, enabled
    ///   and store-owned.
    /// - The [`to_market`](CreateShift::to_market) must be initialized, enabled
    ///   and store-owned.
    /// - The [`from_market`](CreateShift::from_market) must be shiftable to the
    ///   [`to_market`](CreateShift::to_market).
    /// - The [`shift`](CreateShift::shift) must be uninitialized. Its address must
    ///   match the PDA derived from the expected seeds.
    /// - The [`from_market_token`](CreateShift::from_market_token) must be the market
    ///   token of the [`from_market`](CreateShift::from_market).
    /// - The [`to_market_token`](CreateShift::to_market_token) must be the market
    ///   token of the [`to_market`](CreateShift::to_market).
    /// - The [`from_market_token_escrow`](CreateShift::from_market_token_escrow) must
    ///   be a valid shift-owned escrow account for the
    ///   [`from_market_token`](CreateShift::from_market_token).
    /// - The [`to_market_token_escrow`](CreateShift::to_market_token_escrow) must be
    ///   a valid shift-owned escrow account for the
    ///   [`to_market_token`](CreateShift::to_market_token).
    /// - The [`from_market_token_source`](CreateShift::from_market_token_source) must
    ///   be a token account for [`from_market_token`](CreateShift::from_market_token)
    ///   with `owner` as authority.
    /// - The [`to_market_token_ata`](CreateShift::to_market_token_ata) must be a valid
    ///   associated token account for [`to_market_token`](CreateShift::to_market_token)
    ///   owned by `owner`.
    pub fn create_shift<'info>(
        mut ctx: Context<'_, '_, 'info, 'info, CreateShift<'info>>,
        nonce: [u8; 32],
        params: CreateShiftParams,
    ) -> Result<()> {
        internal::Create::create(&mut ctx, &nonce, &params)
    }

    /// Execute a shift by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteShift)*
    ///
    /// # Arguments
    /// - `execution_lamports`: The execution fee in lamports claimed to be used by the keeper.
    /// - `throw_on_execution_error`: Whether to throw an error if the execution fails.
    ///
    /// # Errors
    /// - The [`authority`](ExecuteShift::authority) must be a signer and have the ORDER_KEEPER role
    ///   in the store.
    /// - The [`store`](ExecuteShift::store) must be initialized.
    /// - The [`token_map`](ExecuteShift::token_map) must be initialized and authorized by the store.
    /// - The [`oracle`](ExecuteShift::oracle) must be initialized, cleared and store-owned.
    /// - The [`from_market`](ExecuteShift::from_market) must be initialized, enabled and store-owned.
    ///   It must be the from market of the [`shift`](ExecuteShift::shift).
    /// - The [`to_market`](ExecuteShift::to_market) must be initialized, enabled and store-owned.
    ///   It must be the to market of the [`shift`](ExecuteShift::shift).
    /// - The [`from_market`](ExecuteShift::from_market) must be shiftable to the
    ///   [`to_market`](ExecuteShift::to_market).
    /// - The [`shift`](ExecuteShift::shift) must be initialized, store-owned and in the pending state.
    /// - The [`from_market_token`](ExecuteShift::from_market_token) must be the market token of the
    ///   [`from_market`](ExecuteShift::from_market).
    /// - The [`to_market_token`](ExecuteShift::to_market_token) must be the market token of the
    ///   [`to_market`](ExecuteShift::to_market).
    /// - The [`from_market_token_escrow`](ExecuteShift::from_market_token_escrow) must be a valid
    ///   shift-owned escrow account for the [`from_market_token`](ExecuteShift::from_market_token)
    ///   and recorded in the [`shift`](ExecuteShift::shift).
    /// - The [`to_market_token_escrow`](ExecuteShift::to_market_token_escrow) must be a valid
    ///   shift-owned escrow account for the [`to_market_token`](ExecuteShift::to_market_token)
    ///   and recorded in the [`shift`](ExecuteShift::shift).
    /// - The [`from_market_token_vault`](ExecuteShift::from_market_token_vault) must be the market
    ///   vault for the [`from_market_token`](ExecuteShift::from_market_token) and store-owned.
    /// - The feed accounts must be valid and provided in the same order as the unique sorted list
    ///   of tokens in the `from_market` and `to_market`.
    /// - The oracle prices from the feed accounts must be complete and valid.
    /// - If `throw_on_execution_error` is `true`, returns an error if execution fails.
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_shift<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteShift<'info>>,
        execution_lamports: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_shift(ctx, execution_lamports, throw_on_execution_error)
    }

    /// Close a shift, either by the owner or by keepers.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseShift)*
    ///
    /// # Arguments
    /// - `reason`: The reason for closing the shift.
    ///
    /// # Errors
    /// - The [`executor`](CloseShift::executor) must be a signer, and either the owner or have
    ///   the ORDER_KEEPER role.
    /// - The [`store`](CloseShift::store) must be initialized.
    /// - The [`owner`](CloseShift::owner) must be the owner of the shift.
    /// - The [`shift`](CloseShift::shift) must be initialized and owned by both the `store` and
    ///   `owner`.
    /// - The [`from_market_token`](CloseShift::from_market_token) and
    ///   [`to_market_token`](CloseShift::to_market_token) must be valid and match those recorded
    ///   in the [`shift`](CloseShift::shift).
    /// - The [`from_market_token_escrow`](CloseShift::from_market_token_escrow) and
    ///   [`to_market_token_escrow`](CloseShift::to_market_token_escrow) must be valid escrow
    ///   accounts owned by the `shift` and match those recorded in the [`shift`](CloseShift::shift).
    /// - The address of the [`from_market_token_ata`](CloseShift::from_market_token_ata) must match
    ///   the derived associated token account address for the `from_market_token` and `owner`.
    /// - The address of the [`to_market_token_ata`](CloseShift::to_market_token_ata) must match
    ///   the derived associated token account address for the `to_market_token` and `owner`.
    /// - If the `executor` is not the `owner`, the `shift` must be in either cancelled or completed
    ///   state.
    pub fn close_shift<'info>(
        ctx: Context<'_, '_, 'info, 'info, CloseShift<'info>>,
        reason: String,
    ) -> Result<()> {
        internal::Close::close(&ctx, &reason)
    }

    // ===========================================
    //                The GT Model
    // ===========================================

    /// Initialize GT Mint.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InitializeGt)*
    ///
    /// # Arguments
    /// - `decimals`: The number of decimal places for the GT token.
    /// - `initial_minting_cost`: The initial cost for minting GT.
    /// - `grow_factor`: The multiplier that increases minting cost for each step.
    /// - `grow_step`: The step size (in GT amount) for minting cost increase.
    /// - `ranks`: Array of GT token thresholds that define user rank boundaries.
    ///
    /// # Errors
    /// - The [`authority`](InitializeGt::authority) must be a signer and have the MARKET_KEEPER role in the `store`.
    /// - The [`store`](InitializeGt::store) must be properly initialized.
    /// - The GT state must not already be initialized.
    /// - The arguments must be valid. See `init` method of [`GtState`](states::gt::GtState) for detailed validation logic.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn initialize_gt(
        ctx: Context<InitializeGt>,
        decimals: u8,
        initial_minting_cost: u128,
        grow_factor: u128,
        grow_step: u64,
        ranks: Vec<u64>,
    ) -> Result<()> {
        instructions::unchecked_initialize_gt(
            ctx,
            decimals,
            initial_minting_cost,
            grow_factor,
            grow_step,
            &ranks,
        )
    }

    /// Set order fee discount factors.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ConfigurateGt)*
    ///
    /// # Arguments
    /// - `factors`: The order fee discount factors for each user rank.
    ///
    /// # Errors
    /// - The [`authority`](ConfigurateGt::authority) must be a signer and have the MARKET_KEEPER role in the `store`.
    /// - The [`store`](ConfigurateGt::store) must be initialized.
    /// - The GT state of the `store` must be initialized.
    /// - The number of `factors` must match the number of ranks defined in GT state.
    /// - Each factor must be less than or equal to [`MARKET_USD_UNIT`](crate::constants::MARKET_USD_UNIT)(i.e., 100%).
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn gt_set_order_fee_discount_factors(
        ctx: Context<ConfigurateGt>,
        factors: Vec<u128>,
    ) -> Result<()> {
        instructions::unchecked_gt_set_order_fee_discount_factors(ctx, &factors)
    }

    /// Set referral reward factors.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ConfigurateGt)*
    ///
    /// # Arguments
    /// - `factors`: The referral reward factors for each user rank.
    ///
    /// # Errors
    /// - The [`authority`](ConfigurateGt::authority) must be a signer and a
    ///   GT_CONTROLLER in the store.
    /// - The [`store`](ConfigurateGt::store) must be initialized.
    /// - The GT state of the `store` must be initialized.
    /// - The number of `factors` must match the number of ranks defined in GT state.
    /// - Each factor must be less than or equal to [`MARKET_USD_UNIT`](crate::constants::MARKET_USD_UNIT)(i.e., 100%).
    #[access_control(internal::Authenticate::only_gt_controller(&ctx))]
    pub fn gt_set_referral_reward_factors(
        ctx: Context<ConfigurateGt>,
        factors: Vec<u128>,
    ) -> Result<()> {
        instructions::unchecked_gt_set_referral_reward_factors(ctx, &factors)
    }

    /// Set GT exchange time window (in seconds).
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ConfigurateGt)*
    ///
    /// # Arguments
    /// - `window`: The time window in seconds for one GT exchange period.
    ///
    /// # Errors
    /// - The [`authority`](ConfigurateGt::authority) must be a signer and have the GT_CONTROLLER role in the `store`.
    /// - The [`store`](ConfigurateGt::store) must be properly initialized.
    /// - The GT state of the `store` must be initialized.
    /// - The `window` must be greater than 0 seconds to ensure a valid exchange period.
    #[access_control(internal::Authenticate::only_gt_controller(&ctx))]
    pub fn gt_set_exchange_time_window(ctx: Context<ConfigurateGt>, window: u32) -> Result<()> {
        cfg_if::cfg_if! {
            if #[cfg(feature = "test-only")] {
                instructions::unchecked_gt_set_exchange_time_window(ctx, window)
            } else {
                msg!("Trying to set the GT exchange time window to {}, but this is a test-only instruction", window);
                Err(CoreError::Unimplemented.into())
            }
        }
    }

    /// Prepare a GT exchange vault.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](PrepareGtExchangeVault)*
    ///
    /// # Arguments
    /// - `time_window_index`: The index of the current time window.
    /// - `time_window`: The current GT exchange time window in seconds.
    ///
    /// # Errors
    /// - The [`payer`](PrepareGtExchangeVault::payer) must be a signer.
    /// - The [`store`](PrepareGtExchangeVault::store) must be properly initialized.
    /// - The GT state of the `store` must be initialized.
    /// - The [`vault`](PrepareGtExchangeVault::vault) must be either:
    ///   - Uninitialized, or
    ///   - Properly initialized, owned by the `store`, and have matching `time_window_index`
    ///     and `time_window` values
    /// - The provided `time_window_index` must match the current time window index.
    pub fn prepare_gt_exchange_vault(
        ctx: Context<PrepareGtExchangeVault>,
        time_window_index: i64,
    ) -> Result<()> {
        instructions::prepare_gt_exchange_vault(ctx, time_window_index)
    }

    /// Confirm GT exchange vault.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ConfirmGtExchangeVault)*
    ///
    /// # Errors
    /// - The [`authority`](ConfirmGtExchangeVault::authority) must be a signer and have the GT_CONTROLLER role in the `store`.
    /// - The [`store`](ConfirmGtExchangeVault::store) must be properly initialized.
    /// - The GT state of the `store` must be initialized.
    /// - The [`vault`](ConfirmGtExchangeVault::vault) must be validly initialized and owned by
    ///   the `store`.
    /// - The `vault` must be in a confirmable state (deposit window has passed but not yet confirmed).
    #[access_control(internal::Authenticate::only_gt_controller(&ctx))]
    pub fn confirm_gt_exchange_vault(ctx: Context<ConfirmGtExchangeVault>) -> Result<()> {
        instructions::unchecked_confirm_gt_exchange_vault(ctx)
    }

    /// Request a GT exchange.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](RequestGtExchange)*
    ///
    /// # Arguments
    /// - `amount`: The amount of GT to exchange for rewards.
    ///
    /// # Errors
    /// - The [`owner`](RequestGtExchange::owner) must be a signer.
    /// - The [`store`](RequestGtExchange::store) must be properly initialized with an initialized GT state.
    /// - The [`user`](RequestGtExchange::user) must be properly initialized and correspond to the `owner`.
    /// - The [`vault`](RequestGtExchange::vault) must be properly initialized, owned by the `store`,
    ///   and currently accepting deposits (not yet confirmed).
    /// - The [`exchange`](RequestGtExchange::exchange) must be either:
    ///   - Uninitialized, or
    ///   - Properly initialized and owned by both the `owner` and `vault`
    /// - The `amount` must be:
    ///   - Greater than 0
    ///   - Not exceed the owner's available (excluding reserved) GT balance in their user account
    pub fn request_gt_exchange(ctx: Context<RequestGtExchange>, amount: u64) -> Result<()> {
        instructions::request_gt_exchange(ctx, amount)
    }

    /// Close a confirmed GT exchange.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseGtExchange)*
    ///
    /// # Errors
    /// - The [`authority`](CloseGtExchange::authority) must be a signer and have the GT_CONTROLLER role in the `store`.
    /// - The [`store`](CloseGtExchange::store) must be properly initialized with an initialized GT state.
    /// - The [`vault`](CloseGtExchange::vault) must be properly initialized, owned by the `store`,
    ///   and confirmed.
    /// - The [`exchange`](CloseGtExchange::exchange) must be properly initialized and owned by both
    ///   the `owner` and `vault`.
    #[access_control(internal::Authenticate::only_gt_controller(&ctx))]
    pub fn close_gt_exchange(ctx: Context<CloseGtExchange>) -> Result<()> {
        instructions::unchecked_close_gt_exchange(ctx)
    }

    // ===========================================
    //              User & Referral
    // ===========================================

    /// Initialize or validate a User Account.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](PrepareUser)*
    ///
    /// # Errors
    /// - The [`owner`](PrepareUser::owner) must be a signer.
    /// - The [`store`](PrepareUser::store) must be properly initialized.
    /// - The [`user`](PrepareUser::user) must be either:
    ///   - Uninitialized (for new account creation)
    ///   - Or validly initialized and correspond to the `owner`
    pub fn prepare_user(ctx: Context<PrepareUser>) -> Result<()> {
        instructions::prepare_user(ctx)
    }

    /// Initialize referral code.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InitializeReferralCode)*
    ///
    /// # Arguments
    /// - `code`: The referral code to initialize and associate with the user.
    ///
    /// # Errors
    /// - The [`owner`](InitializeReferralCode::owner) must be a signer.
    /// - The [`store`](InitializeReferralCode::store) must be properly initialized.
    /// - The [`referral_code`](InitializeReferralCode::referral_code) account must be uninitialized.
    /// - The [`user`](InitializeReferralCode::user) account must be:
    ///   - Properly initialized
    ///   - Correspond to the `owner`
    ///   - Not already have an associated referral code
    /// - The provided `code` must not already be in use by another user.
    pub fn initialize_referral_code(
        ctx: Context<InitializeReferralCode>,
        code: [u8; 8],
    ) -> Result<()> {
        instructions::initialize_referral_code(ctx, code)
    }

    /// Set referrer.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](SetReferrer)*
    ///
    /// # Arguments
    /// - `code`: The referral code of the referrer.
    ///
    /// # Errors
    /// - The [`owner`](SetReferrer::owner) must be a signer.
    /// - The [`store`](SetReferrer::store) must be properly initialized.
    /// - The [`user`](SetReferrer::user) must be:
    ///   - Properly initialized
    ///   - Correspond to the `owner`
    ///   - Must not already have a referrer set
    /// - The [`referral_code`](SetReferrer::referral_code) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Match the provided `code`
    ///   - Correspond to the `referrer_user`
    /// - The [`referrer_user`](SetReferrer::referrer_user) must be:
    ///   - Properly initialized
    ///   - Different from the `user`
    ///   - Not have the `user` as their referrer (no circular references)
    pub fn set_referrer(ctx: Context<SetReferrer>, code: [u8; 8]) -> Result<()> {
        instructions::set_referrer(ctx, code)
    }

    /// Transfer referral code.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](TransferReferralCode)*
    ///
    /// # Errors
    /// - The [`owner`](TransferReferralCode::owner) must be a signer.
    /// - The [`store`](TransferReferralCode::store) must be properly initialized.
    /// - The [`user`](TransferReferralCode::user) account must be:
    ///   - Properly initialized
    ///   - Correspond to the `owner`
    ///   - Different from the [`receiver_user`](TransferReferralCode::receiver_user)
    /// - The [`referral_code`](TransferReferralCode::referral_code) account must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Correspond to the `owner`
    /// - The [`receiver_user`](TransferReferralCode::receiver_user) account must be:
    ///   - Properly initialized
    ///   - Not have an associated referral code
    pub fn transfer_referral_code(ctx: Context<TransferReferralCode>) -> Result<()> {
        instructions::transfer_referral_code(ctx)
    }

    /// Cancel referral code transfer.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CancelReferralCodeTransfer)*
    ///
    /// # Errors
    /// - The [`owner`](CancelReferralCodeTransfer::owner) must be a signer.
    /// - The [`store`](CancelReferralCodeTransfer::store) must be properly initialized.
    /// - The [`user`](CancelReferralCodeTransfer::user) account must be:
    ///   - Properly initialized
    ///   - Correspond to the `owner`
    /// - The [`referral_code`](CancelReferralCodeTransfer::referral_code) account must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Correspond to the `owner`
    ///   - The next owner must not have been the `owner`
    pub fn cancel_referral_code_transfer(ctx: Context<CancelReferralCodeTransfer>) -> Result<()> {
        instructions::cancel_referral_code_transfer(ctx)
    }

    /// Accept referral code.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](AcceptReferralCode)*
    ///
    /// # Errors
    /// - The [`next_owner`](AcceptReferralCode::next_owner) must be a signer.
    /// - The [`store`](AcceptReferralCode::store) must be properly initialized.
    /// - The [`user`](AcceptReferralCode::user) account must be:
    ///   - Properly initialized
    ///   - Different from the [`receiver_user`](AcceptReferralCode::receiver_user)
    /// - The [`referral_code`](AcceptReferralCode::referral_code) account must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Correspond to the owner of the `user`
    ///   - Have the next owner be the `next_owner`
    /// - The [`receiver_user`](AcceptReferralCode::receiver_user) account must be:
    ///   - Properly initialized
    ///   - Not have an associated referral code
    ///   - Correspond to the `next_owner`
    pub fn accept_referral_code(ctx: Context<AcceptReferralCode>) -> Result<()> {
        instructions::accept_referral_code(ctx)
    }

    // ===========================================
    //                GLV Operations
    // ===========================================

    /// Initialize a GLV token and the corresponding GLV account.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InitializeGlv)*
    ///
    /// # Arguments
    /// - `index`: The index of the GLV. Used to derive the GLV token address.
    /// - `length`: The number of markets to include in the GLV.
    ///
    /// # Errors
    /// - The [`authority`](InitializeGlv::authority) must be a signer and have
    ///   MARKET_KEEPER role in the store.
    /// - The [`store`](InitializeGlv::store) must be properly initialized.
    /// - The [`glv_token`](InitializeGlv::glv_token) must be:
    ///   - Uninitialized
    ///   - Address must be PDA derived from [`GLV_TOKEN_SEED`](crate::states::Glv::GLV_TOKEN_SEED),
    ///     [`store`] and `index`
    /// - The [`glv`](InitializeGlv::glv) must be:
    ///   - Uninitialized
    ///   - Address must be PDA derived from the SEED of [`Glv`](states::Glv) and the address of the
    ///     [`glv_token`](InitializeGlv::glv_token)
    /// - The remaining required accounts are documented in [`InitializeGlv`].
    /// - The `length` must be:
    ///   - Greater than 0
    ///   - Less than or equal to [`Glv::MAX_ALLOWED_NUMBER_OF_MARKETS`](crate::states::Glv::MAX_ALLOWED_NUMBER_OF_MARKETS)
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn initialize_glv<'info>(
        ctx: Context<'_, '_, 'info, 'info, InitializeGlv<'info>>,
        index: u16,
        length: u16,
    ) -> Result<()> {
        instructions::unchecked_initialize_glv(ctx, index, length as usize)
    }

    /// Update the config of a market in the given GLV.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](UpdateGlvMarketConfig)*
    ///
    /// # Arguments
    /// - `max_amount`: The maximum amount of the market token that can be stored in the GLV.
    /// - `max_value`: The maximum value of the market token that can be stored in the GLV.
    ///
    /// # Errors
    /// - The [`authority`](UpdateGlvMarketConfig::authority) must be:
    ///   - A signer
    ///   - Have MARKET_KEEPER role in the `store`
    /// - The [`store`](UpdateGlvMarketConfig::store) must be properly initialized.
    /// - The [`glv`](UpdateGlvMarketConfig::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Have the market token in its list of market tokens
    /// - The [`market_token`](UpdateGlvMarketConfig::market_token) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - At least one of `max_amount` or `max_value` must be provided
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn update_glv_market_config(
        ctx: Context<UpdateGlvMarketConfig>,
        max_amount: Option<u64>,
        max_value: Option<u128>,
    ) -> Result<()> {
        instructions::unchecked_update_glv_market_config(ctx, max_amount, max_value)
    }

    /// Toggle the given flag of a market in the given GLV.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](UpdateGlvMarketConfig)*
    ///
    /// # Arguments
    /// - `flag`: The flag to toggle.
    /// - `enable`: The value to toggle to.
    ///
    /// # Errors
    /// - The [`authority`](UpdateGlvMarketConfig::authority) must be:
    ///   - A signer
    ///   - Have MARKET_KEEPER role in the `store`
    /// - The [`store`](UpdateGlvMarketConfig::store) must be properly initialized.
    /// - The [`glv`](UpdateGlvMarketConfig::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Have the market token in its list of market tokens
    /// - The [`market_token`](UpdateGlvMarketConfig::market_token) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - `flag` must be defined in [`GlvMarketFlag`](crate::states::glv::GlvMarketFlag).
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn toggle_glv_market_flag(
        ctx: Context<UpdateGlvMarketConfig>,
        flag: String,
        enable: bool,
    ) -> Result<()> {
        instructions::unchecked_toggle_glv_market_flag(ctx, &flag, enable)
    }

    /// Update GLV config.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](UpdateGlvConfig)*
    ///
    /// # Arguments
    /// - `params`: The update of the config.
    ///
    /// # Errors
    /// - The [`authority`](UpdateGlvConfig::authority) must be:
    ///   - A signer
    ///   - Have MARKET_KEEPER role in the `store`
    /// - The [`store`](UpdateGlvConfig::store) must be properly initialized.
    /// - The [`glv`](UpdateGlvConfig::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - The `params` must not non-empty.
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn update_glv_config(ctx: Context<UpdateGlvConfig>, params: UpdateGlvParams) -> Result<()> {
        instructions::unchecked_update_glv(ctx, &params)
    }

    /// Insert a new market to the GLV.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](InsertGlvMarket)*
    ///
    /// # Errors
    /// - The [`authority`](InsertGlvMarket::authority) must be:
    ///   - A signer
    ///   - Have MARKET_KEEPER role in the `store`
    /// - The [`store`](InsertGlvMarket::store) must be properly initialized.
    /// - The [`glv`](InsertGlvMarket::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - The [`market_token`](InsertGlvMarket::market_token) must be:
    ///   - A initialized SPL Token / Token-2022 mint
    ///   - Have `store` as its mint authority
    ///   - Not already contains in the given GLV
    /// - The [`market`](InsertGlvMarket::market) must be:
    ///   - A initialized market account owned by the `store`
    ///   - Must have `market_token` as its market token
    ///   - Must have the same long token and short token as the GLV
    ///   - Must be enabled
    /// - The [`vault`](InsertGlvMarket::vault) must be either:
    ///   - The ATA of `market_token` owned by `glv`, or
    ///   - Unintialized ATA account of `market_token` owned by `glv`
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn insert_glv_market(ctx: Context<InsertGlvMarket>) -> Result<()> {
        instructions::unchecked_insert_glv_market(ctx)
    }

    /// Remove a market from the GLV.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](RemoveGlvMarket)*
    ///
    /// # Errors
    /// - The [`authority`](RemoveGlvMarket::authority) must:
    ///   - Be a signer
    ///   - Have MARKET_KEEPER role in the `store`
    /// - The [`store`](RemoveGlvMarket::store) must be properly initialized.
    /// - The [`glv`](RemoveGlvMarket::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - The [`market_token`](RemoveGlvMarket::market_token) must be:
    ///   - A initialized SPL Token mint
    ///   - Having `store` as its mint authority
    ///   - Contained in the given GLV
    ///   - Having deposit disabled in the GLV
    /// - The [`vault`](RemoveGlvMarket::vault) must be:
    ///   - The ATA of `market_token` owned by `glv`
    ///   - Having no remaining balance
    #[access_control(internal::Authenticate::only_market_keeper(&ctx))]
    pub fn remove_glv_market(ctx: Context<RemoveGlvMarket>) -> Result<()> {
        instructions::unchecked_remove_glv_market(ctx)
    }

    /// Create GLV deposit.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CreateGlvDeposit)*
    ///
    /// # Arguments
    /// - `nonce`: A 32-byte used to derive the address of the GLV deposit.
    /// - `params`: The parameters for creating the GLV deposit.
    ///
    /// # Errors
    /// - The [`owner`](CreateGlvDeposit::owner) must be a signer and have sufficient balance
    ///   for the execution fee and rent.
    /// - The [`store`](CreateGlvDeposit::store) must be properly initialized.
    /// - The [`market`](CreateGlvDeposit::market) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Listed in the [`glv`](CreateGlvDeposit::glv)
    /// - The [`glv`](CreateGlvDeposit::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - The [`glv_deposit`](CreateGlvDeposit::glv_deposit) must be:
    ///   - Uninitialized
    ///   - Address must be PDA derived from the SEED of [`GlvDeposit`](states::GlvDeposit),
    ///     [`store`](CreateGlvDeposit::store), [`owner`](CreateGlvDeposit::owner) and `nonce`
    /// - The [`glv_token`](CreateGlvDeposit::glv_token) must be:
    ///   - Properly initialized
    ///   - Correspond to the provided [`glv`](CreateGlvDeposit::glv)
    /// - The [`market_token`](CreateGlvDeposit::market_token) must be:
    ///   - Properly initialized
    ///   - Correspond to the provided [`market`](CreateGlvDeposit::market)
    /// - Token mint account requirements:
    ///   - [`initial_long_token`](CreateGlvDeposit::initial_long_token) must be provided if initial long amount > 0
    ///   - [`initial_short_token`](CreateGlvDeposit::initial_short_token) must be provided if initial short amount > 0
    /// - Escrow account requirements:
    ///   - [`glv_token_escrow`](CreateGlvDeposit::glv_token_escrow) must be:
    ///     - Owned by the [`glv_deposit`](CreateGlvDeposit::glv_deposit)
    ///   - Other escrow accounts must be:
    ///     - Provided for any non-zero initial token amounts
    ///     - Owned by the [`glv_deposit`](CreateGlvDeposit::glv_deposit)
    /// - Source token account requirements:
    ///   - Must be provided for any non-zero initial token amounts
    ///   - Must have sufficient balance
    ///   - Must have the `owner` as its authority
    /// - All token programs must match their corresponding token accounts
    pub fn create_glv_deposit<'info>(
        mut ctx: Context<'_, '_, 'info, 'info, CreateGlvDeposit<'info>>,
        nonce: [u8; 32],
        params: CreateGlvDepositParams,
    ) -> Result<()> {
        internal::Create::create(&mut ctx, &nonce, &params)
    }

    /// Close GLV deposit.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseGlvDeposit)*
    ///
    /// # Arguments
    /// - `reason`: The reason for closing the GLV deposit.
    ///
    /// # Errors
    /// - The [`executor`](CloseGlvDeposit::executor) must be a signer, and must be
    ///   either the owner of the GLV deposit or a `ORDER_KEEPER` in the store
    /// - The [`store`](CloseGlvDeposit::store) must be properly initialized
    /// - The [`owner`](CloseGlvDeposit::owner) must be the owner of the GLV deposit
    /// - The [`glv_deposit`](CloseGlvDeposit::glv_deposit) must be:
    ///   - Properly initialized
    ///   - Owned by the `owner` and `store`
    ///   - In cancelled or executed state if the `executor` is not the `owner`
    /// - Token mint account requirements:
    ///   - All tokens must be valid and recorded in the [`glv_deposit`](CloseGlvDeposit::glv_deposit)
    ///   - [`initial_long_token`](CloseGlvDeposit::initial_long_token) must be provided if initial long amount > 0
    ///   - [`initial_short_token`](CloseGlvDeposit::initial_short_token) must be provided if initial short amount > 0
    /// - Escrow account requirements:
    ///   - Must correspond to their respective tokens
    ///   - Must be owned by the [`glv_deposit`](CloseGlvDeposit::glv_deposit)
    ///   - Must be recorded in the [`glv_deposit`](CloseGlvDeposit::glv_deposit)
    /// - The addresses of the ATAs must be valid associated token addresses derived from the respective tokens and `owner`
    /// - All token programs must match their corresponding token accounts
    pub fn close_glv_deposit<'info>(
        ctx: Context<'_, '_, 'info, 'info, CloseGlvDeposit<'info>>,
        reason: String,
    ) -> Result<()> {
        internal::Close::close(&ctx, &reason)
    }

    /// Execute GLV deposit.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteGlvDeposit)*
    ///
    /// # Arguments
    /// - `execution_lamports`: The execution fee claimed to be used by the keeper.
    /// - `throw_on_execution_error`: Whether to throw an error if the execution fails.
    ///
    /// # Errors
    /// - The [`authority`](ExecuteGlvDeposit::authority) must be a signer and have `ORDER_KEEPER` role in the `store`
    /// - The [`store`](ExecuteGlvDeposit::store) must be properly initialized
    /// - The [`token_map`](ExecuteGlvDeposit::token_map) must be:
    ///   - Properly initialized
    ///   - Authorized by the `store`
    /// - The [`oracle`](ExecuteGlvDeposit::oracle) must be:
    ///   - Cleared
    ///   - Owned by the `store`
    /// - The [`glv`](ExecuteGlvDeposit::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Match the expected GLV of the deposit
    /// - The [`market`](ExecuteGlvDeposit::market) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - Match the expected market of the deposit
    ///   - Must be enabled and listed in the [`glv`](ExecuteGlvDeposit::glv)
    /// - The [`glv_deposit`](ExecuteGlvDeposit::glv_deposit) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - In pending state
    /// - Token requirements:
    ///   - All tokens must be valid and recorded in the [`glv_deposit`](ExecuteGlvDeposit::glv_deposit)
    ///   - [`glv_token`](ExecuteGlvDeposit::glv_token) must be the GLV token of the [`glv`](ExecuteGlvDeposit::glv)
    ///   - [`market_token`](ExecuteGlvDeposit::market_token) must be the market token of the [`market`](ExecuteGlvDeposit::market)
    /// - Vault requirements:
    ///   - [`initial_long_token_vault`](ExecuteGlvDeposit::initial_long_token_vault) must be:
    ///     - The market vault for the initial long token
    ///     - Owned by the `store`
    ///   - [`initial_short_token_vault`](ExecuteGlvDeposit::initial_short_token_vault) must be:
    ///     - The market vault for the initial short token
    ///     - Owned by the `store`
    ///   - [`market_token_vault`](ExecuteGlvDeposit::market_token_vault) must be:
    ///     - The market token vault in the [`glv`](ExecuteGlvDeposit::glv)
    ///     - Owned by the [`glv`](ExecuteGlvDeposit::glv)
    /// - Escrow requirements:
    ///   - Must correspond to their respective tokens
    ///   - Must be owned by the [`glv_deposit`](ExecuteGlvDeposit::glv_deposit)
    ///   - Must be recorded in the [`glv_deposit`](ExecuteGlvDeposit::glv_deposit)
    /// - All token programs must match their corresponding token accounts
    /// - All remaining accounts must be valid per [`ExecuteGlvDeposit`] documentation
    /// - Returns error if execution fails and `throw_on_execution_error` is `true`
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_glv_deposit<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteGlvDeposit<'info>>,
        execution_lamports: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_glv_deposit(
            ctx,
            execution_lamports,
            throw_on_execution_error,
        )
    }

    /// Create GLV withdrawal.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CreateGlvWithdrawal)*
    ///
    /// # Arguments
    /// - `nonce`: A 32-byte used to derive the address of the GLV withdrawal.
    /// - `params`: The parameters for creating the GLV withdrawal.
    ///
    /// # Errors
    /// - The [`owner`](CreateGlvWithdrawal::owner) must be a signer and have sufficient balance
    ///   for the execution fee and rent.
    /// - The [`store`](CreateGlvWithdrawal::store) must be properly initialized.
    /// - The [`market`](CreateGlvWithdrawal::market) must be:
    ///   - Properly initialized
    ///   - Enabled
    ///   - Owned by the `store`
    ///   - One of the markets in the [`glv`](CreateGlvWithdrawal::glv)
    /// - The [`glv`](CreateGlvWithdrawal::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - The [`glv_withdrawal`](CreateGlvWithdrawal::glv_withdrawal) must be:
    ///   - Uninitialized
    ///   - A PDA derived from:
    ///     - the SEED of [`GlvWithdrawal`](states::GlvWithdrawal)
    ///     - [`store`](CreateGlvWithdrawal::store)
    ///     - [`owner`](CreateGlvWithdrawal::owner)
    ///     - `nonce`
    /// - Token requirements:
    ///   - [`glv_token`](CreateGlvWithdrawal::glv_token) must be:
    ///     - Properly initialized
    ///     - The GLV token of the [`glv`](CreateGlvWithdrawal::glv)
    ///   - [`market_token`](CreateGlvWithdrawal::market_token) must be:
    ///     - Properly initialized
    ///     - The market token of the [`market`](CreateGlvWithdrawal::market)
    ///   - All other tokens must be properly initialized
    /// - Source requirements:
    ///   - [`glv_token_source`](CreateGlvWithdrawal::glv_token_source) must be:
    ///     - Properly initialized
    ///     - A GLV token account
    ///     - Have sufficient balance
    ///     - Have the `owner` as its authority
    /// - Escrow requirements:
    ///   - Must correspond to their respective tokens
    ///   - Must be owned by the [`glv_withdrawal`](CreateGlvWithdrawal::glv_withdrawal)
    /// - All token programs must match their corresponding token accounts
    pub fn create_glv_withdrawal<'info>(
        mut ctx: Context<'_, '_, 'info, 'info, CreateGlvWithdrawal<'info>>,
        nonce: [u8; 32],
        params: CreateGlvWithdrawalParams,
    ) -> Result<()> {
        internal::Create::create(&mut ctx, &nonce, &params)
    }

    /// Close GLV withdrawal.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseGlvWithdrawal)*
    ///
    /// # Arguments
    /// - `reason`: The reason for closing the GLV withdrawal.
    ///
    /// # Errors
    /// - The [`executor`](CloseGlvWithdrawal::executor) must be:
    ///   - A signer
    ///   - Either:
    ///     - The owner of the [`glv_withdrawal`](CloseGlvWithdrawal::glv_withdrawal)
    ///     - A `ORDER_KEEPER` in the `store`
    /// - The [`store`](CloseGlvWithdrawal::store) must be properly initialized
    /// - The [`owner`](CloseGlvWithdrawal::owner) must be the owner of the [`glv_withdrawal`](CloseGlvWithdrawal::glv_withdrawal)
    /// - The [`glv_withdrawal`](CloseGlvWithdrawal::glv_withdrawal) must be:
    ///   - Properly initialized
    ///   - Owned by the `owner`
    ///   - Owned by the `store`
    /// - Token requirements:
    ///   - All tokens must be valid and recorded in the [`glv_withdrawal`](CloseGlvWithdrawal::glv_withdrawal)
    /// - Escrow requirements:
    ///   - Must correspond to their respective tokens
    ///   - Must be owned by the [`glv_withdrawal`](CloseGlvWithdrawal::glv_withdrawal)
    ///   - Must be recorded in the [`glv_withdrawal`](CloseGlvWithdrawal::glv_withdrawal)
    /// - The addresses of the ATAs must be valid associated token addresses derived from the respective tokens and `owner`
    /// - All token programs must match their corresponding token accounts
    /// - If the `executor` is not the `owner`, the [`glv_withdrawal`](CloseGlvWithdrawal::glv_withdrawal) must be either cancelled or executed.
    pub fn close_glv_withdrawal<'info>(
        ctx: Context<'_, '_, 'info, 'info, CloseGlvWithdrawal<'info>>,
        reason: String,
    ) -> Result<()> {
        internal::Close::close(&ctx, &reason)
    }

    /// Execute GLV withdrawal.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteGlvWithdrawal)*
    ///
    /// # Arguments
    /// - `execution_lamports`: The execution fee claimed to be used by the keeper.
    /// - `throw_on_execution_error`: Whether to throw an error if the execution fails.
    ///
    /// # Errors
    /// - The [`authority`](ExecuteGlvWithdrawal::authority) must be:
    ///   - A signer
    ///   - A `ORDER_KEEPER` in the `store`
    /// - The [`store`](ExecuteGlvWithdrawal::store) must be properly initialized
    /// - The [`token_map`](ExecuteGlvWithdrawal::token_map) must be:
    ///   - Properly initialized
    ///   - Authorized by the `store`
    /// - The [`oracle`](ExecuteGlvWithdrawal::oracle) must be:
    ///   - Cleared
    ///   - Owned by the `store`
    /// - The [`glv`](ExecuteGlvWithdrawal::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - The expected GLV of the withdrawal
    /// - The [`market`](ExecuteGlvWithdrawal::market) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - The expected market of the withdrawal
    ///   - Must be enabled and listed in the [`glv`](ExecuteGlvWithdrawal::glv)
    /// - The [`glv_withdrawal`](ExecuteGlvWithdrawal::glv_withdrawal) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - In pending state
    /// - Token requirements:
    ///   - All tokens must be valid and recorded in the withdrawal
    ///   - [`glv_token`](ExecuteGlvWithdrawal::glv_token) must be the GLV token of the GLV
    ///   - [`market_token`](ExecuteGlvWithdrawal::market_token) must be the market token of the market
    /// - Escrow requirements:
    ///   - Escrow accounts must correspond to their tokens
    ///   - Escrow accounts must be owned by the [`glv_withdrawal`](ExecuteGlvWithdrawal::glv_withdrawal)
    ///   - Escrow accounts must be recorded in the [`glv_withdrawal`](ExecuteGlvWithdrawal::glv_withdrawal)
    /// - Vault requirements:
    ///   - [`market_token_withdrawal_vault`](ExecuteGlvWithdrawal::market_token_withdrawal_vault) must be the market vault for market token, owned by the `store`
    ///   - [`final_long_token_vault`](ExecuteGlvWithdrawal::final_long_token_vault) must be the market vault for final long token, owned by the `store`
    ///   - [`final_short_token_vault`](ExecuteGlvWithdrawal::final_short_token_vault) must be the market vault for final short token, owned by the `store`
    ///   - [`market_token_vault`](ExecuteGlvWithdrawal::market_token_vault) must be the GLV's market token vault, owned by the [`glv`](ExecuteGlvWithdrawal::glv)
    /// - All token programs must match their corresponding token accounts
    /// - All remaining accounts must be valid per [`ExecuteGlvWithdrawal`] documentation
    /// - Returns error if execution fails and `throw_on_execution_error` is `true`
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_glv_withdrawal<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteGlvWithdrawal<'info>>,
        execution_lamports: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_glv_withdrawal(
            ctx,
            execution_lamports,
            throw_on_execution_error,
        )
    }

    /// Create GLV shift.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CreateGlvShift)*
    ///
    /// # Arguments
    /// - `nonce`: A 32-byte used to derive the address of the GLV shift.
    /// - `params`: The parameters for creating the GLV shift.
    ///
    /// # Errors
    /// - The [`authority`](CreateGlvShift::authority) must be:
    ///   - A signer
    ///   - A `ORDER_KEEPER` in the `store`
    /// - The [`store`](CreateGlvShift::store) must be properly initialized
    /// - The [`glv`](CreateGlvShift::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - Market requirements:
    ///   - [`from_market`](CreateGlvShift::from_market) must be:
    ///     - Enabled
    ///     - Owned by the `store`
    ///     - One of the markets in the [`glv`](CreateGlvShift::glv)
    ///   - [`to_market`](CreateGlvShift::to_market) must be:
    ///     - Enabled
    ///     - Owned by the `store`
    ///     - One of the markets in the [`glv`](CreateGlvShift::glv)
    ///     - Different from `from_market`
    /// - The [`glv_shift`](CreateGlvShift::glv_shift) must be:
    ///   - Uninitialized
    ///   - PDA derived from the SEED of [`GlvShift`](states::GlvShift), `store`, `glv`, and `nonce`
    /// - Token requirements:
    ///   - [`from_market_token`](CreateGlvShift::from_market_token) must be:
    ///     - Properly initialized
    ///     - The market token of `from_market`
    ///   - [`to_market_token`](CreateGlvShift::to_market_token) must be:
    ///     - Properly initialized
    ///     - The market token of `to_market`
    /// - Vault requirements:
    ///   - [`from_market_token_vault`](CreateGlvShift::from_market_token_vault) must be:
    ///     - The market token vault for `from_market_token` in the [`glv`](CreateGlvShift::glv)
    ///     - Owned by the [`glv`](CreateGlvShift::glv)
    ///   - [`to_market_token_vault`](CreateGlvShift::to_market_token_vault) must be:
    ///     - The market token vault for `to_market_token` in the [`glv`](CreateGlvShift::glv)
    ///     - Owned by the [`glv`](CreateGlvShift::glv)
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn create_glv_shift<'info>(
        mut ctx: Context<'_, '_, 'info, 'info, CreateGlvShift<'info>>,
        nonce: [u8; 32],
        params: CreateShiftParams,
    ) -> Result<()> {
        internal::Create::create(&mut ctx, &nonce, &params)
    }

    /// Close a GLV shift.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](CloseGlvShift)*
    ///
    /// # Arguments
    /// - `reason`: The reason for closing the GLV shift.
    ///
    /// # Errors
    /// - The [`authority`](CloseGlvShift::authority) must be:
    ///   - A signer
    ///   - A `ORDER_KEEPER` in the `store`
    /// - The [`funder`](CloseGlvShift::funder) must be the funder of the [`glv`](CloseGlvShift::glv).
    /// - The [`store`](CloseGlvShift::store) must be properly initialized.
    /// - The [`glv`](CloseGlvShift::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - The expected GLV of the GLV shift
    /// - The [`glv_shift`](CloseGlvShift::glv_shift) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - Token requirements:
    ///   - [`from_market_token`](CloseGlvShift::from_market_token) must be:
    ///     - Recorded in the GLV shift
    ///   - [`to_market_token`](CloseGlvShift::to_market_token) must be:
    ///     - Recorded in the GLV shift
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn close_glv_shift<'info>(
        ctx: Context<'_, '_, 'info, 'info, CloseGlvShift<'info>>,
        reason: String,
    ) -> Result<()> {
        internal::Close::close(&ctx, &reason)
    }

    /// Execute GLV shift.
    ///
    /// # Accounts
    /// *[See the documentation for the accounts.](ExecuteGlvShift)*
    ///
    /// # Arguments
    /// - `execution_lamports`: The execution fee claimed to be used by the keeper.
    /// - `throw_on_execution_error`: Whether to throw an error if execution fails.
    ///
    /// # Errors
    /// - The [`authority`](ExecuteGlvShift::authority) must be:
    ///   - A signer
    ///   - A `ORDER_KEEPER` in the `store`
    /// - The [`store`](ExecuteGlvShift::store) must be properly initialized
    /// - The [`token_map`](ExecuteGlvShift::token_map) must be:
    ///   - Properly initialized
    ///   - Authorized by the `store`
    /// - The [`oracle`](ExecuteGlvShift::oracle) must be:
    ///   - Cleared
    ///   - Owned by the `store`
    /// - The [`glv`](ExecuteGlvShift::glv) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    ///   - The expected GLV of the GLV shift
    /// - The [`from_market`](ExecuteGlvShift::from_market) must be:
    ///   - Enabled
    ///   - Owned by the `store`
    ///   - One of the markets in the [`glv`](ExecuteGlvShift::glv)
    /// - The [`to_market`](ExecuteGlvShift::to_market) must be:
    ///   - Enabled
    ///   - Owned by the `store`
    ///   - One of the markets in the [`glv`](ExecuteGlvShift::glv)
    /// - The [`glv_shift`](ExecuteGlvShift::glv_shift) must be:
    ///   - Properly initialized
    ///   - Owned by the `store`
    /// - Token requirements:
    ///   - [`from_market_token`](ExecuteGlvShift::from_market_token) must be:
    ///     - The market token of `from_market`
    ///     - Recorded in the GLV shift
    ///   - [`to_market_token`](ExecuteGlvShift::to_market_token) must be:
    ///     - The market token of `to_market`
    ///     - Recorded in the GLV shift
    /// - Vault requirements:
    ///   - [`from_market_token_glv_vault`](ExecuteGlvShift::from_market_token_glv_vault) must be:
    ///     - The escrow account for `from_market_token` in the GLV
    ///     - Owned by the [`glv`](ExecuteGlvShift::glv)
    ///   - [`to_market_token_glv_vault`](ExecuteGlvShift::to_market_token_glv_vault) must be:
    ///     - The escrow account for `to_market_token` in the GLV
    ///     - Owned by the [`glv`](ExecuteGlvShift::glv)
    ///   - [`from_market_token_vault`](ExecuteGlvShift::from_market_token_vault) must be:
    ///     - The market vault for `from_market_token`
    ///     - Owned by the `store`
    /// - Token programs must match the tokens and token accounts
    /// - The remaining accounts must be valid (see [`ExecuteGlvShift`] docs)
    /// - Returns error if execution fails and `throw_on_execution_error` is `true`
    #[access_control(internal::Authenticate::only_order_keeper(&ctx))]
    pub fn execute_glv_shift<'info>(
        ctx: Context<'_, '_, 'info, 'info, ExecuteGlvShift<'info>>,
        execution_lamports: u64,
        throw_on_execution_error: bool,
    ) -> Result<()> {
        instructions::unchecked_execute_glv_shift(ctx, execution_lamports, throw_on_execution_error)
    }

    #[access_control(internal::Authenticate::only_migration_keeper(&ctx))]
    pub fn migrate_referral_code<'info>(
        ctx: Context<'_, '_, 'info, 'info, MigrateReferralCode<'info>>,
    ) -> Result<()> {
        cfg_if::cfg_if! {
            if #[cfg(feature = "migration")] {
                instructions::unchecked_migrate_referral_code(ctx)
            } else {
                err!(CoreError::Unimplemented)
            }
        }
    }
}

/// Result type with [`CoreError`] as error type.
pub type CoreResult<T> = std::result::Result<T, CoreError>;

#[error_code]
pub enum CoreError {
    // ===========================================
    //                Common Errors
    // ===========================================
    /// Non-defualt store is not allowed.
    #[msg("non-default store is not allowed")]
    NonDefaultStore,
    /// Internal error.
    #[msg("internal error")]
    Internal,
    /// Unimplemented.
    Unimplemented,
    /// Not an Admin.
    #[msg("not an admin")]
    NotAnAdmin,
    /// Permission denied.
    #[msg("permission denied")]
    PermissionDenied,
    /// Feature disabled.
    #[msg("feature disabled")]
    FeatureDisabled,
    /// Model Error.
    #[msg("model")]
    Model,
    /// Invalid Argument.
    #[msg("invalid argument")]
    InvalidArgument,
    /// Preconditions are not met.
    #[msg("preconditions are not met")]
    PreconditionsAreNotMet,
    /// Not found.
    #[msg("not found")]
    NotFound,
    /// Exceed max length limit.
    #[msg("exceed max length limit")]
    ExceedMaxLengthLimit,
    /// Not enough space.
    #[msg("not enough space")]
    NotEnoughSpace,
    /// Token amount overflow.
    #[msg("token amount overflow")]
    TokenAmountOverflow,
    /// Value overflow.
    #[msg("value overflow")]
    ValueOverflow,
    /// Unknown Action State.
    #[msg("unknown action state")]
    UnknownActionState,
    /// Load account error.
    #[msg("load zero-copy account error")]
    LoadAccountError,
    /// Token account is not provided.
    #[msg("required token account is not provided")]
    TokenAccountNotProvided,
    /// Token mint is not provided.
    #[msg("required token mint is not provided")]
    TokenMintNotProvided,
    /// Token decimals mismatched.
    #[msg("token decimals mismatched")]
    TokenDecimalsMismatched,
    /// Market account is not provided.
    #[msg("market account is not provided")]
    MarketAccountIsNotProvided,
    /// Store Mismatched.
    #[msg("store mismatched")]
    StoreMismatched,
    /// Owner mismatched.
    #[msg("owner mismatched")]
    OwnerMismatched,
    /// Receiver mismatched.
    #[msg("receiver mismatched")]
    ReceiverMismatched,
    /// Rent Receiver mismatched.
    #[msg("rent receiver mismatched")]
    RentReceiverMismatched,
    /// Market mismatched.
    #[msg("market mismatched")]
    MarketMismatched,
    /// Market token mint mismatched.
    #[msg("market token mint mismatched")]
    MarketTokenMintMismatched,
    /// Mint account not provided.
    #[msg("mint account not provided")]
    MintAccountNotProvided,
    /// Market token account mismatched.
    #[msg("market token account mismatched")]
    MarketTokenAccountMismatched,
    /// Token mint mismatched.
    #[msg("token mint mismatched")]
    TokenMintMismatched,
    /// Token account mismatched.
    #[msg("token account mismatched")]
    TokenAccountMismatched,
    /// Not an ATA for the given token.
    #[msg("not an ATA for the given token")]
    NotAnATA,
    /// Not enough token amounts.
    #[msg("not enough token amount")]
    NotEnoughTokenAmount,
    /// Token amount exceeds limit.
    #[msg("token amount exceeds limit")]
    TokenAmountExceedsLimit,
    /// Unknown token.
    #[msg("unknown token")]
    UnknownToken,
    /// Not enough execution fee.
    #[msg("not enough execution fee")]
    NotEnoughExecutionFee,
    /// Invalid Swap Path length.
    #[msg("invalid swap path length")]
    InvalidSwapPathLength,
    /// Not enough swap markets in the path.
    #[msg("not enough swap markets in the path")]
    NotEnoughSwapMarkets,
    /// Invalid Swap Path.
    #[msg("invalid swap path")]
    InvalidSwapPath,
    /// Insufficient output amounts.
    #[msg("insufficient output amounts")]
    InsufficientOutputAmount,
    /// Store Outdated.
    #[msg("store outdated")]
    StoreOutdated,
    // ===========================================
    //                 Store Errors
    // ===========================================
    /// Invalid Store Config Key.
    #[msg("invalid store config key")]
    InvalidStoreConfigKey,
    // ===========================================
    //                Oracle Errors
    // ===========================================
    /// Invalid Provider Kind Index.
    #[msg("invalid provider kind index")]
    InvalidProviderKindIndex,
    /// Chainlink Program is required.
    #[msg("chainlink program is required")]
    ChainlinkProgramIsRequired,
    /// Not supported price provider for custom price feed.
    #[msg("this price provider is not supported to be used with custom price feed")]
    NotSupportedCustomPriceProvider,
    /// Not enough token feeds.
    #[msg("not enough token feeds")]
    NotEnoughTokenFeeds,
    /// Oracle timestamps are larger than required.
    #[msg("oracle timestamps are larger than required")]
    OracleTimestampsAreLargerThanRequired,
    /// Oracle timestamps are smaller than required.
    #[msg("oracle timestamps are smaller than required")]
    OracleTimestampsAreSmallerThanRequired,
    /// Invalid Oracle timestamps range.
    #[msg("invalid oracle timestamps range")]
    InvalidOracleTimestampsRange,
    /// Max oracle timestamps range exceeded.
    #[msg("max oracle timestamps range exceeded")]
    MaxOracleTimestampsRangeExceeded,
    /// Oracle not updated.
    #[msg("oracle not updated")]
    OracleNotUpdated,
    /// Max price age exceeded.
    #[msg("max price age exceeded")]
    MaxPriceAgeExceeded,
    /// Max prcie's timestamp exceeded.
    MaxPriceTimestampExceeded,
    /// Negative price.
    #[msg("negative price is not supported")]
    NegativePriceIsNotSupported,
    /// Invalid Oracle slot.
    #[msg("invalid oracle slot")]
    InvalidOracleSlot,
    /// Missing oracle price.
    #[msg("missing oracle price")]
    MissingOraclePrice,
    /// Invalid Price feed price.
    #[msg("invalid price feed price")]
    InvalidPriceFeedPrice,
    /// Price Overflow.
    #[msg("price overflow")]
    PriceOverflow,
    /// Invalid price feed account.
    #[msg("invalid price feed account")]
    InvalidPriceFeedAccount,
    /// Price feed is not updated.
    #[msg("price feed is not updated")]
    PriceFeedNotUpdated,
    /// Prices are already set.
    #[msg("prices are already set")]
    PricesAreAlreadySet,
    /// Price is already set.
    #[msg("price is already set")]
    PriceIsAlreadySet,
    /// Token config is diabled.
    #[msg("token config is disabled")]
    TokenConfigDisabled,
    /// Synthetic token price is not allowed.
    #[msg("synthetic token price is not allowed")]
    SyntheticTokenPriceIsNotAllowed,
    /// Invalid Price Report.
    #[msg("invalid price report")]
    InvalidPriceReport,
    /// Market not opened.
    #[msg("market is not open")]
    MarketNotOpen,
    // ===========================================
    //                Deposit Errors
    // ===========================================
    /// Empty Deposit.
    #[msg("empty deposit")]
    EmptyDeposit,
    /// Invalid owner for the first deposit.
    #[msg("invalid owner for the first deposit")]
    InvalidReceiverForFirstDeposit,
    /// Not enough market token amount for the first deposit.
    #[msg("not enough market token amount for the first deposit")]
    NotEnoughMarketTokenAmountForFirstDeposit,
    /// Not enough GLV token amount for the first deposit.
    #[msg("not enough GLV token amount for the first deposit")]
    NotEnoughGlvTokenAmountForFirstDeposit,
    // ===========================================
    //               Withdrawal Errors
    // ===========================================
    /// Empty Withdrawal.
    #[msg("emtpy withdrawal")]
    EmptyWithdrawal,
    // ===========================================
    //                 Order Errors
    // ===========================================
    /// Empty Order.
    #[msg("emtpy order")]
    EmptyOrder,
    /// Invalid min output amount for limit swap.
    #[msg("invalid min output amount for limit swap order")]
    InvalidMinOutputAmount,
    /// Invalid trigger price.
    #[msg("invalid trigger price")]
    InvalidTriggerPrice,
    /// Invalid position.
    #[msg("invalid position")]
    InvalidPosition,
    /// Invalid position kind.
    #[msg("invalid position kind")]
    InvalidPositionKind,
    /// Position mismatched.
    #[msg("position mismatched")]
    PositionMismatched,
    /// Position is not required.
    #[msg("position is not required")]
    PositionItNotRequired,
    /// Position is required.
    #[msg("position is required")]
    PositionIsRequired,
    /// Order kind is not allowed.
    #[msg("the order kind is not allowed by this instruction")]
    OrderKindNotAllowed,
    /// Unknown Order Kind.
    #[msg("unknown order kind")]
    UnknownOrderKind,
    /// Unknown Order Side.
    #[msg("unknown order side")]
    UnknownOrderSide,
    /// Unknown Decrease Position Swap Type.
    #[msg("unknown decrease position swap type")]
    UnknownDecreasePositionSwapType,
    /// Missing initial collateral token.
    #[msg("missing initial collateral token")]
    MissingInitialCollateralToken,
    /// Missing final output token.
    #[msg("missing final output token")]
    MissingFinalOutputToken,
    /// Missing pool tokens.
    #[msg("missing pool tokens")]
    MissingPoolTokens,
    /// Invalid Trade ID.
    #[msg("invalid trade ID")]
    InvalidTradeID,
    /// Invalid Trade delta size.
    #[msg("invalid trade delta size")]
    InvalidTradeDeltaSize,
    /// Invalid Trade delta tokens.
    #[msg("invalid trade delta tokens")]
    InvalidTradeDeltaTokens,
    /// Invalid Borrowing Factor.
    #[msg("invalid borrowing factor")]
    InvalidBorrowingFactor,
    /// Invalid funding factors.
    #[msg("invalid funding factors")]
    InvalidFundingFactors,
    /// No delegated authority is set.
    #[msg("no delegated authority is set")]
    NoDelegatedAuthorityIsSet,
    /// Claimable collateral for holding cannot be in output tokens.
    #[msg("claimable collateral for holding cannot be in output tokens")]
    ClaimableCollateralForHoldingCannotBeInOutputTokens,
    /// ADL is not enabled.
    #[msg("ADL is not enabled")]
    AdlNotEnabled,
    /// ADL is not required.
    #[msg("ADL is not required")]
    AdlNotRequired,
    /// Invalid ADL.
    #[msg("invalid ADL")]
    InvalidAdl,
    /// The output token and the secondary output token are the same,
    /// but the token amounts are not merged togather.
    #[msg("same output tokens not merged")]
    SameOutputTokensNotMerged,
    /// Event buffer is not provided.
    #[msg("event buffer is not provided")]
    EventBufferNotProvided,
    // ===========================================
    //                 Shift Errors
    // ===========================================
    /// Empty Shift.
    #[msg("emtpy shift")]
    EmptyShift,
    /// Invalid Shift Markets
    #[msg("invalid shift markets")]
    InvalidShiftMarkets,
    // ===========================================
    //        GT and User Accounts Errors
    // ===========================================
    /// GT State has been initialized.
    #[msg("GT State has been initialized")]
    GTStateHasBeenInitialized,
    /// Invalid GT config.
    #[msg("invalid GT config")]
    InvalidGTConfig,
    /// Invalid GT discount.
    #[msg("invalid GT discount")]
    InvalidGTDiscount,
    /// User account has been initialized.
    #[msg("user account has been initialized")]
    UserAccountHasBeenInitialized,
    // ===========================================
    //               Referral Errors
    // ===========================================
    /// Referral Code has been set.
    #[msg("referral code has been set")]
    ReferralCodeHasBeenSet,
    /// Referrer has been set.
    #[msg("referrer has been set")]
    ReferrerHasBeenSet,
    /// Invalid User Account.
    #[msg("invalid user account")]
    InvalidUserAccount,
    /// Referral Code Mismatched.
    #[msg("referral code mismatched")]
    ReferralCodeMismatched,
    /// Self-referral is not allowed.
    #[msg("self-referral is not allowed")]
    SelfReferral,
    /// Mutual-referral is not allowed.
    #[msg("mutual-referral is not allowed")]
    MutualReferral,
    // ===========================================
    //                Market Errors
    // ===========================================
    /// Invalid market config key.
    #[msg("invalid market config key")]
    InvalidMarketConfigKey,
    /// Invalid collateral token.
    #[msg("invalid collateral token")]
    InvalidCollateralToken,
    /// Disabled market.
    #[msg("disabled market")]
    DisabledMarket,
    // ===========================================
    //                  GLV Errors
    // ===========================================
    /// Failed to calculate GLV value for market.
    #[msg("failed to calculate GLV value for this market")]
    FailedToCalculateGlvValueForMarket,
    /// Failed to calculate GLV amount to mint.
    #[msg("failed to calculate GLV amount to mint")]
    FailedToCalculateGlvAmountToMint,
    /// Failed to calculate market token amount to burn.
    FailedTOCalculateMarketTokenAmountToBurn,
    /// Exceed max market token balance amount of GLV.
    #[msg("GLV max market token balance amount exceeded")]
    ExceedMaxGlvMarketTokenBalanceAmount,
    /// Exceed max market token balance value of GLV.
    #[msg("GLV max market token balance value exceeded")]
    ExceedMaxGlvMarketTokenBalanceValue,
    /// Empty GLV withdrawal.
    #[msg("Empty GLV withdrawal")]
    EmptyGlvWithdrawal,
    /// Negative Market Pool Value.
    #[msg("GLV: negative market pool value")]
    GlvNegativeMarketPoolValue,
    /// Deposit is not allowed with the given market.
    #[msg("GLV: deposit is not allowed with the given market")]
    GlvDepositIsNotAllowed,
    /// Shift interval not yet passed.
    #[msg("GLV: shift interval not yet passed")]
    GlvShiftIntervalNotYetPassed,
    /// Shift max price impact exceeded.
    #[msg("GLV: shift max price impact exceeded")]
    GlvShiftMaxPriceImpactExceeded,
    /// Shift value too small.
    #[msg("GLV: shift value is not large enough")]
    GlvShiftValueNotLargeEnough,
    // ===========================================
    //                Other Errors
    // ===========================================
    /// The decimals of token is immutable.
    #[msg("The decimals of token is immutable")]
    TokenDecimalsChanged,
    /// Price is stale.
    #[msg("Price is stale")]
    PriceIsStale,
}