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
// This file is part of the SORA network and Polkaswap app.

// Copyright (c) 2020, 2021, Polka Biome Ltd. All rights reserved.
// SPDX-License-Identifier: BSD-4-Clause

// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:

// Redistributions of source code must retain the above copyright notice, this list
// of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// All advertising materials mentioning features or use of this software must display
// the following acknowledgement: This product includes software developed by Polka Biome
// Ltd., SORA, and Polkaswap.
//
// Neither the name of the Polka Biome Ltd. nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY Polka Biome Ltd. AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Polka Biome Ltd. BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#![cfg_attr(not(feature = "std"), no_std)]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]
// TODO #167: fix clippy warnings
#![allow(clippy::all)]

extern crate alloc;
use alloc::string::String;
use bridge_types::traits::Verifier;
use bridge_types::{SubNetworkId, H256};
use sp_runtime::traits::Keccak256;

mod bags_thresholds;
/// Constant values used within the runtime.
pub mod constants;
mod impls;
pub mod migrations;
mod xor_fee_impls;

#[cfg(test)]
pub mod mock;

#[cfg(test)]
pub mod tests;
pub mod weights;

#[cfg(feature = "wip")] // EVM bridge
use crate::impls::EVMBridgeCallFilter;
use crate::impls::PreimageWeightInfo;
use crate::impls::{DispatchableSubstrateBridgeCall, SubstrateBridgeCallFilter};
#[cfg(feature = "wip")] // Trustless bridges
use bridge_types::types::LeafExtraData;
#[cfg(feature = "wip")] // EVM bridge
use bridge_types::{evm::AdditionalEVMInboundData, U256};
use common::prelude::constants::{BIG_FEE, SMALL_FEE};
use common::prelude::QuoteAmount;
use common::{Description, PredefinedAssetId};
use common::{XOR, XSTUSD};
use constants::currency::deposit;
use constants::time::*;
#[cfg(feature = "wip")] // order-book
use frame_support::traits::EitherOf;
use frame_support::weights::ConstantMultiplier;

// Make the WASM binary available.
#[cfg(all(feature = "std", feature = "build-wasm-binary"))]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use core::time::Duration;
use currencies::BasicCurrencyAdapter;
use frame_election_provider_support::{generate_solution_type, onchain, SequentialPhragmen};
use frame_support::traits::{ConstU128, ConstU32, Currency, EitherOfDiverse};
use frame_system::offchain::{Account, SigningTypes};
use frame_system::EnsureRoot;
#[cfg(feature = "wip")] // order-book
use frame_system::EnsureSigned;
use hex_literal::hex;
use pallet_grandpa::{
    fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
};
use pallet_session::historical as pallet_session_historical;
use pallet_staking::sora::ValBurnedNotifier;
#[cfg(feature = "std")]
use serde::{Serialize, Serializer};
use sp_api::impl_runtime_apis;
pub use sp_beefy::crypto::AuthorityId as BeefyId;
#[cfg(feature = "wip")] // Trustless bridges
use sp_beefy::mmr::MmrLeafVersion;
use sp_core::crypto::KeyTypeId;
use sp_core::{Encode, OpaqueMetadata, H160};
use sp_mmr_primitives as mmr;
use sp_runtime::traits::{
    BlakeTwo256, Block as BlockT, Convert, IdentifyAccount, IdentityLookup, NumberFor, OpaqueKeys,
    SaturatedConversion, Verify,
};
use sp_runtime::transaction_validity::TransactionLongevity;
use sp_runtime::transaction_validity::{
    TransactionPriority, TransactionSource, TransactionValidity,
};
use sp_runtime::{
    create_runtime_str, generic, impl_opaque_keys, ApplyExtrinsicResult, DispatchError,
    MultiSignature, Perbill, Percent, Perquintill,
};
use sp_std::cmp::Ordering;
use sp_std::prelude::*;
use sp_std::vec::Vec;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use static_assertions::assert_eq_size;
use traits::{parameter_type_with_key, MultiCurrency};
use xor_fee::extension::ChargeTransactionPayment;

// A few exports that help ease life for downstream crates.
pub use common::prelude::{
    Balance, BalanceWrapper, PresetWeightInfo, SwapAmount, SwapOutcome, SwapVariant,
};
pub use common::weights::{BlockLength, BlockWeights, TransactionByteFee};
pub use common::{
    balance, fixed, fixed_from_basis_points, AssetInfoProvider, AssetName, AssetSymbol,
    BalancePrecision, BasisPoints, ContentSource, CrowdloanTag, DexInfoProvider, FilterMode, Fixed,
    FromGenericPair, LiquiditySource, LiquiditySourceFilter, LiquiditySourceId,
    LiquiditySourceType, OnPswapBurned, OnValBurned, SyntheticInfoProvider,
    TradingPairSourceManager,
};
use constants::rewards::{PSWAP_BURN_PERCENT, VAL_BURN_PERCENT};
pub use ethereum_light_client::EthereumHeader;
pub use frame_support::dispatch::DispatchClass;
pub use frame_support::traits::schedule::Named as ScheduleNamed;
pub use frame_support::traits::{
    Contains, KeyOwnerProofSystem, LockIdentifier, OnUnbalanced, Randomness, U128CurrencyToVote,
};
pub use frame_support::weights::constants::{BlockExecutionWeight, RocksDbWeight};
pub use frame_support::weights::Weight;
pub use frame_support::{construct_runtime, debug, parameter_types, StorageValue};
pub use pallet_balances::Call as BalancesCall;
pub use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
pub use pallet_staking::StakerStatus;
pub use pallet_timestamp::Call as TimestampCall;
pub use pallet_transaction_payment::{Multiplier, MultiplierUpdate};
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;

use eth_bridge::offchain::SignatureParams;
use eth_bridge::requests::{AssetKind, OffchainRequest, OutgoingRequestEncoded, RequestStatus};
use impls::{
    CollectiveWeightInfo, DemocracyWeightInfo, NegativeImbalanceOf, OnUnbalancedDemocracySlash,
};

use frame_support::traits::{Everything, ExistenceRequirement, Get, PrivilegeCmp, WithdrawReasons};
#[cfg(all(feature = "private-net", feature = "wip"))] // order-book
pub use qa_tools;
pub use {
    assets, eth_bridge, frame_system, multicollateral_bonding_curve_pool, order_book, trading_pair,
    xst,
};

/// An index to a block.
pub type BlockNumber = u32;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

// This assert is needed for `technical` pallet in order to create
// `AccountId` from the hash type.
assert_eq_size!(AccountId, sp_core::H256);

/// The type for looking up accounts. We don't expect more than 4 billion of them, but you
/// never know...
pub type AccountIndex = u32;

/// Index of a transaction in the chain.
pub type Index = u32;

/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;

/// Digest item type.
pub type DigestItem = generic::DigestItem;

/// Identification of DEX.
pub type DEXId = u32;

pub type Moment = u64;

pub type PeriodicSessions = pallet_session::PeriodicSessions<SessionPeriod, SessionOffset>;

pub type CouncilCollective = pallet_collective::Instance1;
pub type TechnicalCollective = pallet_collective::Instance2;

type MoreThanHalfCouncil = EitherOfDiverse<
    EnsureRoot<AccountId>,
    pallet_collective::EnsureProportionMoreThan<AccountId, CouncilCollective, 1, 2>,
>;
type AtLeastHalfCouncil = EitherOfDiverse<
    pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 1, 2>,
    EnsureRoot<AccountId>,
>;
type AtLeastTwoThirdsCouncil = EitherOfDiverse<
    pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 2, 3>,
    EnsureRoot<AccountId>,
>;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
/// to even the core datastructures.
pub mod opaque {
    use super::*;

    pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;

    /// Opaque block header type.
    pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
    /// Opaque block type.
    pub type Block = generic::Block<Header, UncheckedExtrinsic>;
    /// Opaque block identifier type.
    pub type BlockId = generic::BlockId<Block>;

    impl_opaque_keys! {
        pub struct SessionKeys {
            pub babe: Babe,
            pub grandpa: Grandpa,
            pub im_online: ImOnline,
            pub beefy: Beefy,
        }
    }
}

/// Types used by oracle related pallets
pub mod oracle_types {
    use common::SymbolName;

    pub type Symbol = SymbolName;

    pub type ResolveTime = u64;
}
pub use oracle_types::*;

/// This runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
    spec_name: create_runtime_str!("sora-substrate"),
    impl_name: create_runtime_str!("sora-substrate"),
    authoring_version: 1,
    spec_version: 64,
    impl_version: 1,
    apis: RUNTIME_API_VERSIONS,
    transaction_version: 64,
    state_version: 0,
};

/// The version infromation used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
    NativeVersion {
        runtime_version: VERSION,
        can_author_with: Default::default(),
    }
}

pub const FARMING_PSWAP_PER_DAY: Balance = balance!(2500000);
pub const FARMING_REFRESH_FREQUENCY: BlockNumber = 2 * HOURS;
// Defined in the article
pub const FARMING_VESTING_COEFF: u32 = 3;
pub const FARMING_VESTING_FREQUENCY: BlockNumber = 6 * HOURS;

parameter_types! {
    pub const BlockHashCount: BlockNumber = 250;
    pub const Version: RuntimeVersion = VERSION;
    pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17);
    pub const EpochDuration: u64 = EPOCH_DURATION_IN_BLOCKS as u64;
    pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
    pub const SessionsPerEra: sp_staking::SessionIndex = 6; // 6 hours
    pub const BondingDuration: sp_staking::EraIndex = 28; // 28 eras for unbonding (7 days).
    pub const ReportLongevity: u64 =
        BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * EpochDuration::get();
    pub const SlashDeferDuration: sp_staking::EraIndex = 27; // 27 eras in which slashes can be cancelled (slightly less than 7 days).
    pub const MaxNominatorRewardedPerValidator: u32 = 256;
    pub const ElectionLookahead: BlockNumber = EPOCH_DURATION_IN_BLOCKS / 4;
    pub const MaxIterations: u32 = 10;
    // 0.05%. The higher the value, the more strict solution acceptance becomes.
    pub MinSolutionScoreBump: Perbill = Perbill::from_rational(5u32, 10_000);
    pub const ValRewardCurve: pallet_staking::sora::ValRewardCurve = pallet_staking::sora::ValRewardCurve {
        duration_to_reward_flatline: Duration::from_secs(5 * 365 * 24 * 60 * 60),
        min_val_burned_percentage_reward: Percent::from_percent(35),
        max_val_burned_percentage_reward: Percent::from_percent(90),
    };
    pub const SessionPeriod: BlockNumber = 150;
    pub const SessionOffset: BlockNumber = 0;
    pub const SS58Prefix: u8 = 69;
    /// A limit for off-chain phragmen unsigned solution submission.
    ///
    /// We want to keep it as high as possible, but can't risk having it reject,
    /// so we always subtract the base block execution weight.
    pub OffchainSolutionWeightLimit: Weight = BlockWeights::get()
    .get(DispatchClass::Normal)
    .max_extrinsic
    .expect("Normal extrinsics have weight limit configured by default; qed")
    .saturating_sub(BlockExecutionWeight::get());
    /// A limit for off-chain phragmen unsigned solution length.
    ///
    /// We allow up to 90% of the block's size to be consumed by the solution.
    pub OffchainSolutionLengthLimit: u32 = Perbill::from_rational(90_u32, 100) *
        *BlockLength::get()
        .max
        .get(DispatchClass::Normal);
    pub const DemocracyEnactmentPeriod: BlockNumber = 30 * DAYS;
    pub const DemocracyLaunchPeriod: BlockNumber = 28 * DAYS;
    pub const DemocracyVotingPeriod: BlockNumber = 14 * DAYS;
    pub const DemocracyMinimumDeposit: Balance = balance!(1);
    pub const DemocracyFastTrackVotingPeriod: BlockNumber = 3 * HOURS;
    pub const DemocracyInstantAllowed: bool = true;
    pub const DemocracyCooloffPeriod: BlockNumber = 28 * DAYS;
    pub const DemocracyPreimageByteDeposit: Balance = balance!(0.000002); // 2 * 10^-6, 5 MiB -> 10.48576 XOR
    pub const DemocracyMaxVotes: u32 = 100;
    pub const DemocracyMaxProposals: u32 = 100;
    pub const DemocracyMaxDeposits: u32 = 100;
    pub const DemocracyMaxBlacklisted: u32 = 100;
    pub const CouncilCollectiveMotionDuration: BlockNumber = 5 * DAYS;
    pub const CouncilCollectiveMaxProposals: u32 = 100;
    pub const CouncilCollectiveMaxMembers: u32 = 100;
    pub const TechnicalCollectiveMotionDuration: BlockNumber = 5 * DAYS;
    pub const TechnicalCollectiveMaxProposals: u32 = 100;
    pub const TechnicalCollectiveMaxMembers: u32 = 100;
    pub SchedulerMaxWeight: Weight = Perbill::from_percent(80) * BlockWeights::get().max_block;
    pub const MaxScheduledPerBlock: u32 = 50;
    pub OffencesWeightSoftLimit: Weight = Perbill::from_percent(60) * BlockWeights::get().max_block;
    pub const ImOnlineUnsignedPriority: TransactionPriority = TransactionPriority::max_value();
    pub const SessionDuration: BlockNumber = EPOCH_DURATION_IN_BLOCKS;
    pub const ElectionsCandidacyBond: Balance = balance!(1);
    // 1 storage item created, key size is 32 bytes, value size is 16+16.
    pub const ElectionsVotingBondBase: Balance = balance!(0.000001);
    // additional data per vote is 32 bytes (account id).
    pub const ElectionsVotingBondFactor: Balance = balance!(0.000001);
    pub const ElectionsTermDuration: BlockNumber = 7 * DAYS;
    /// 13 members initially, to be increased to 23 eventually.
    pub const ElectionsDesiredMembers: u32 = 13;
    pub const ElectionsDesiredRunnersUp: u32 = 20;
    pub const ElectionsMaxVoters: u32 = 10000;
    pub const ElectionsMaxCandidates: u32 = 1000;
    pub const ElectionsModuleId: LockIdentifier = *b"phrelect";
    pub FarmingRewardDoublingAssets: Vec<AssetId> = vec![GetPswapAssetId::get(), GetValAssetId::get(), GetDaiAssetId::get(), GetEthAssetId::get(), GetXstAssetId::get(), GetTbcdAssetId::get()];
    pub const MaxAuthorities: u32 = 100_000;
    pub const NoPreimagePostponement: Option<u32> = Some(10);
}

pub struct BaseCallFilter;

impl Contains<RuntimeCall> for BaseCallFilter {
    fn contains(call: &RuntimeCall) -> bool {
        if call.swap_count() > 1 {
            return false;
        }
        if matches!(
            call,
            RuntimeCall::BridgeMultisig(bridge_multisig::Call::register_multisig { .. })
        ) {
            return false;
        }
        true
    }
}

impl frame_system::Config for Runtime {
    type BaseCallFilter = BaseCallFilter;
    type BlockWeights = BlockWeights;
    /// Maximum size of all encoded transactions (in bytes) that are allowed in one block.
    type BlockLength = BlockLength;
    /// The ubiquitous origin type.
    type RuntimeOrigin = RuntimeOrigin;
    /// The aggregated dispatch type that is available for extrinsics.
    type RuntimeCall = RuntimeCall;
    /// The index type for storing how many extrinsics an account has signed.
    type Index = Index;
    /// The index type for blocks.
    type BlockNumber = BlockNumber;
    /// The type for hashing blocks and tries.
    type Hash = Hash;
    /// The hashing algorithm used.
    type Hashing = BlakeTwo256;
    /// The identifier used to distinguish between accounts.
    type AccountId = AccountId;
    /// The lookup mechanism to get account ID from whatever is passed in dispatchers.
    type Lookup = IdentityLookup<AccountId>;
    /// The header type.
    type Header = generic::Header<BlockNumber, BlakeTwo256>;
    /// The ubiquitous event type.
    type RuntimeEvent = RuntimeEvent;
    /// Maximum number of block number to block hash mappings to keep (oldest pruned first).
    type BlockHashCount = BlockHashCount;
    /// The weight of database operations that the runtime can invoke.
    type DbWeight = RocksDbWeight;
    /// Runtime version.
    type Version = Version;
    type PalletInfo = PalletInfo;
    /// Converts a module to an index of this module in the runtime.
    type AccountData = pallet_balances::AccountData<Balance>;
    type OnNewAccount = ();
    type OnKilledAccount = ();
    type SystemWeightInfo = ();
    type SS58Prefix = SS58Prefix;
    type OnSetCode = ();
    type MaxConsumers = frame_support::traits::ConstU32<65536>;
}

impl pallet_babe::Config for Runtime {
    type EpochDuration = EpochDuration;
    type ExpectedBlockTime = ExpectedBlockTime;
    type EpochChangeTrigger = pallet_babe::ExternalTrigger;
    type DisabledValidators = Session;
    type KeyOwnerProof = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(
        KeyTypeId,
        pallet_babe::AuthorityId,
    )>>::Proof;
    type KeyOwnerIdentification = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(
        KeyTypeId,
        pallet_babe::AuthorityId,
    )>>::IdentificationTuple;
    type KeyOwnerProofSystem = Historical;
    type HandleEquivocation =
        pallet_babe::EquivocationHandler<Self::KeyOwnerIdentification, Offences, ReportLongevity>;
    type WeightInfo = ();
    type MaxAuthorities = MaxAuthorities;
}

impl pallet_collective::Config<CouncilCollective> for Runtime {
    type RuntimeOrigin = RuntimeOrigin;
    type Proposal = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type MotionDuration = CouncilCollectiveMotionDuration;
    type MaxProposals = CouncilCollectiveMaxProposals;
    type MaxMembers = CouncilCollectiveMaxMembers;
    type DefaultVote = pallet_collective::PrimeDefaultVote;
    type WeightInfo = CollectiveWeightInfo<Self>;
}

impl pallet_collective::Config<TechnicalCollective> for Runtime {
    type RuntimeOrigin = RuntimeOrigin;
    type Proposal = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type MotionDuration = TechnicalCollectiveMotionDuration;
    type MaxProposals = TechnicalCollectiveMaxProposals;
    type MaxMembers = TechnicalCollectiveMaxMembers;
    type DefaultVote = pallet_collective::PrimeDefaultVote;
    type WeightInfo = CollectiveWeightInfo<Self>;
}

impl pallet_democracy::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Currency = Balances;
    type EnactmentPeriod = DemocracyEnactmentPeriod;
    type LaunchPeriod = DemocracyLaunchPeriod;
    type VotingPeriod = DemocracyVotingPeriod;
    type MinimumDeposit = DemocracyMinimumDeposit;
    /// `external_propose` call condition
    type ExternalOrigin = AtLeastHalfCouncil;
    /// A super-majority can have the next scheduled referendum be a straight majority-carries vote.
    /// `external_propose_majority` call condition
    type ExternalMajorityOrigin = AtLeastHalfCouncil;
    /// `external_propose_default` call condition
    type ExternalDefaultOrigin = AtLeastHalfCouncil;
    /// Two thirds of the technical committee can have an ExternalMajority/ExternalDefault vote
    /// be tabled immediately and with a shorter voting/enactment period.
    type FastTrackOrigin = EitherOfDiverse<
        pallet_collective::EnsureProportionMoreThan<AccountId, TechnicalCollective, 1, 2>,
        EnsureRoot<AccountId>,
    >;
    type InstantOrigin = EitherOfDiverse<
        pallet_collective::EnsureProportionAtLeast<AccountId, TechnicalCollective, 2, 3>,
        EnsureRoot<AccountId>,
    >;
    type InstantAllowed = DemocracyInstantAllowed;
    type FastTrackVotingPeriod = DemocracyFastTrackVotingPeriod;
    /// To cancel a proposal which has been passed, 2/3 of the council must agree to it.
    /// `emergency_cancel` call condition.
    type CancellationOrigin = AtLeastTwoThirdsCouncil;
    type CancelProposalOrigin = AtLeastTwoThirdsCouncil;
    type BlacklistOrigin = EnsureRoot<AccountId>;
    /// `veto_external` - vetoes and blacklists the external proposal hash
    type VetoOrigin = pallet_collective::EnsureMember<AccountId, TechnicalCollective>;
    type CooloffPeriod = DemocracyCooloffPeriod;
    type Slash = OnUnbalancedDemocracySlash<Self>;
    type Scheduler = Scheduler;
    type PalletsOrigin = OriginCaller;
    type MaxVotes = DemocracyMaxVotes;
    type WeightInfo = DemocracyWeightInfo;
    type MaxProposals = DemocracyMaxProposals;
    type VoteLockingPeriod = DemocracyEnactmentPeriod;
    type Preimages = Preimage;
    type MaxDeposits = DemocracyMaxDeposits;
    type MaxBlacklisted = DemocracyMaxBlacklisted;
}

impl pallet_elections_phragmen::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type PalletId = ElectionsModuleId;
    type Currency = Balances;
    type ChangeMembers = Council;
    type InitializeMembers = Council;
    type CurrencyToVote = frame_support::traits::U128CurrencyToVote;
    type CandidacyBond = ElectionsCandidacyBond;
    type VotingBondBase = ElectionsVotingBondBase;
    type VotingBondFactor = ElectionsVotingBondFactor;
    type LoserCandidate = OnUnbalancedDemocracySlash<Self>;
    type KickedMember = OnUnbalancedDemocracySlash<Self>;
    type DesiredMembers = ElectionsDesiredMembers;
    type DesiredRunnersUp = ElectionsDesiredRunnersUp;
    type TermDuration = ElectionsTermDuration;
    type MaxVoters = ElectionsMaxVoters;
    type MaxCandidates = ElectionsMaxCandidates;
    type WeightInfo = ();
}

impl pallet_membership::Config<pallet_membership::Instance1> for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type AddOrigin = MoreThanHalfCouncil;
    type RemoveOrigin = MoreThanHalfCouncil;
    type SwapOrigin = MoreThanHalfCouncil;
    type ResetOrigin = MoreThanHalfCouncil;
    type PrimeOrigin = MoreThanHalfCouncil;
    type MembershipInitialized = TechnicalCommittee;
    type MembershipChanged = TechnicalCommittee;
    type MaxMembers = TechnicalCollectiveMaxMembers;
    type WeightInfo = ();
}

parameter_types! {
    pub const MaxSetIdSessionEntries: u32 = BondingDuration::get() * SessionsPerEra::get();
}

impl pallet_grandpa::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;

    type KeyOwnerProofSystem = Historical;

    type KeyOwnerProof =
        <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(KeyTypeId, GrandpaId)>>::Proof;

    type KeyOwnerIdentification = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(
        KeyTypeId,
        GrandpaId,
    )>>::IdentificationTuple;

    type HandleEquivocation = pallet_grandpa::EquivocationHandler<
        Self::KeyOwnerIdentification,
        Offences,
        ReportLongevity,
    >;
    type WeightInfo = ();
    type MaxAuthorities = MaxAuthorities;
    type MaxSetIdSessionEntries = MaxSetIdSessionEntries;
}

parameter_types! {
    pub const MinimumPeriod: u64 = SLOT_DURATION / 2;
}

impl pallet_timestamp::Config for Runtime {
    /// A timestamp: milliseconds since the unix epoch.
    type Moment = Moment;
    type OnTimestampSet = Babe;
    type MinimumPeriod = MinimumPeriod;
    type WeightInfo = ();
}

impl pallet_session::Config for Runtime {
    type SessionManager = pallet_session::historical::NoteHistoricalRoot<Self, XorFee>;
    type Keys = opaque::SessionKeys;
    type ShouldEndSession = Babe;
    type SessionHandler = <opaque::SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
    type RuntimeEvent = RuntimeEvent;
    type ValidatorId = AccountId;
    type ValidatorIdOf = pallet_staking::StashOf<Self>;
    type NextSessionRotation = Babe;
    type WeightInfo = ();
}

impl pallet_session::historical::Config for Runtime {
    type FullIdentification = pallet_staking::Exposure<AccountId, Balance>;
    type FullIdentificationOf = pallet_staking::ExposureOf<Runtime>;
}

impl pallet_authorship::Config for Runtime {
    type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Babe>;
    type EventHandler = (Staking, ImOnline);
}

/// A reasonable benchmarking config for staking pallet.
pub struct StakingBenchmarkingConfig;
impl pallet_staking::BenchmarkingConfig for StakingBenchmarkingConfig {
    type MaxValidators = ConstU32<1000>;
    type MaxNominators = ConstU32<1000>;
}

parameter_types! {
    pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
    pub const MaxNominations: u32 = <NposCompactSolution24 as frame_election_provider_support::NposSolution>::LIMIT as u32;
}

type StakingAdminOrigin = EitherOfDiverse<
    EnsureRoot<AccountId>,
    pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 3, 4>,
>;

impl pallet_staking::Config for Runtime {
    type Currency = Balances;
    type MultiCurrency = Tokens;
    type CurrencyBalance = Balance;
    type ValTokenId = GetValAssetId;
    type ValRewardCurve = ValRewardCurve;
    type UnixTime = Timestamp;
    type CurrencyToVote = U128CurrencyToVote;
    type RuntimeEvent = RuntimeEvent;
    type Slash = ();
    type SessionsPerEra = SessionsPerEra;
    type BondingDuration = BondingDuration;
    type SlashDeferDuration = SlashDeferDuration;
    type AdminOrigin = StakingAdminOrigin;
    type SessionInterface = Self;
    type NextNewSession = Session;
    type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
    type VoterList = BagsList;
    type ElectionProvider = ElectionProviderMultiPhase;
    type BenchmarkingConfig = StakingBenchmarkingConfig;
    type MaxUnlockingChunks = ConstU32<32>;
    type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
    type MaxNominations = MaxNominations;
    type GenesisElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
    type OnStakerSlash = ();
    type HistoryDepth = frame_support::traits::ConstU32<84>;
    type TargetList = pallet_staking::UseValidatorsMap<Self>;
    type WeightInfo = ();
}

/// The numbers configured here could always be more than the the maximum limits of staking pallet
/// to ensure election snapshot will not run out of memory. For now, we set them to smaller values
/// since the staking is bounded and the weight pipeline takes hours for this single pallet.
pub struct ElectionBenchmarkConfig;
impl pallet_election_provider_multi_phase::BenchmarkingConfig for ElectionBenchmarkConfig {
    const VOTERS: [u32; 2] = [1000, 2000];
    const TARGETS: [u32; 2] = [500, 1000];
    const ACTIVE_VOTERS: [u32; 2] = [500, 800];
    const DESIRED_TARGETS: [u32; 2] = [200, 400];
    const SNAPSHOT_MAXIMUM_VOTERS: u32 = 1000;
    const MINER_MAXIMUM_VOTERS: u32 = 1000;
    const MAXIMUM_TARGETS: u32 = 300;
}

parameter_types! {
    // phase durations. 1/4 of the last session for each.
    // in testing: 1min or half of the session for each
    pub SignedPhase: u32 = EPOCH_DURATION_IN_BLOCKS / 4;
    pub UnsignedPhase: u32 = EPOCH_DURATION_IN_BLOCKS / 4;

    // signed config
    pub const SignedMaxSubmissions: u32 = 16;
    pub const SignedMaxRefunds: u32 = 16 / 4;
    pub const SignedDepositBase: Balance = deposit(2, 0);
    pub const SignedDepositByte: Balance = deposit(0, 10) / 1024;
    pub SignedRewardBase: Balance =  constants::currency::UNITS / 10;
    pub SolutionImprovementThreshold: Perbill = Perbill::from_rational(5u32, 10_000);
    pub BetterUnsignedThreshold: Perbill = Perbill::from_rational(5u32, 10_000);

    // 1 hour session, 15 minutes unsigned phase, 8 offchain executions.
    pub OffchainRepeat: BlockNumber = UnsignedPhase::get() / 8;

    /// We take the top 12500 nominators as electing voters..
    pub const MaxElectingVoters: u32 = 12_500;
    /// ... and all of the validators as electable targets. Whilst this is the case, we cannot and
    /// shall not increase the size of the validator intentions.
    pub const MaxElectableTargets: u16 = u16::MAX;
    /// Setup election pallet to support maximum winners upto 1200. This will mean Staking Pallet
    /// cannot have active validators higher than this count.
    pub const MaxActiveValidators: u32 = 1200;
    pub NposSolutionPriority: TransactionPriority =
        Perbill::from_percent(90) * TransactionPriority::max_value();
}

generate_solution_type!(
    #[compact]
    pub struct NposCompactSolution24::<
        VoterIndex = u32,
        TargetIndex = u16,
        Accuracy = sp_runtime::PerU16,
        MaxVoters = MaxElectingVoters,
    >(24)
);

/// The accuracy type used for genesis election provider;
pub type OnChainAccuracy = sp_runtime::Perbill;

pub struct OnChainSeqPhragmen;
impl onchain::Config for OnChainSeqPhragmen {
    type System = Runtime;
    type Solver = SequentialPhragmen<AccountId, OnChainAccuracy>;
    type DataProvider = Staking;
    type WeightInfo = ();
    type MaxWinners = MaxActiveValidators;
    type VotersBound = MaxElectingVoters;
    type TargetsBound = MaxElectableTargets;
}

impl pallet_election_provider_multi_phase::MinerConfig for Runtime {
    type AccountId = AccountId;
    type MaxLength = OffchainSolutionLengthLimit;
    type MaxWeight = OffchainSolutionWeightLimit;
    type Solution = NposCompactSolution24;
    type MaxVotesPerVoter = <
		<Self as pallet_election_provider_multi_phase::Config>::DataProvider
		as
		frame_election_provider_support::ElectionDataProvider
	>::MaxVotesPerVoter;

    // The unsigned submissions have to respect the weight of the submit_unsigned call, thus their
    // weight estimate function is wired to this call's weight.
    fn solution_weight(v: u32, t: u32, a: u32, d: u32) -> Weight {
        <
			<Self as pallet_election_provider_multi_phase::Config>::WeightInfo
			as
			pallet_election_provider_multi_phase::WeightInfo
		>::submit_unsigned(v, t, a, d)
    }
}

impl pallet_election_provider_multi_phase::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Currency = Balances;
    type EstimateCallFee = TransactionPayment;
    type UnsignedPhase = UnsignedPhase;
    type SignedMaxSubmissions = SignedMaxSubmissions;
    type SignedMaxRefunds = SignedMaxRefunds;
    type SignedRewardBase = SignedRewardBase;
    type SignedDepositBase = SignedDepositBase;
    type SignedDepositByte = SignedDepositByte;
    type SignedDepositWeight = ();
    type SignedMaxWeight =
        <Self::MinerConfig as pallet_election_provider_multi_phase::MinerConfig>::MaxWeight;
    type MinerConfig = Self;
    type SlashHandler = (); // burn slashes
    type RewardHandler = (); // nothing to do upon rewards
    type SignedPhase = SignedPhase;
    type BetterUnsignedThreshold = BetterUnsignedThreshold;
    type BetterSignedThreshold = ();
    type OffchainRepeat = OffchainRepeat;
    type MinerTxPriority = NposSolutionPriority;
    type DataProvider = Staking;
    type Fallback = frame_election_provider_support::NoElection<(
        AccountId,
        BlockNumber,
        Staking,
        MaxActiveValidators,
    )>;
    type GovernanceFallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
    type Solver = SequentialPhragmen<
        AccountId,
        pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
        (),
    >;
    type BenchmarkingConfig = ElectionBenchmarkConfig;
    type ForceOrigin = EitherOfDiverse<
        EnsureRoot<AccountId>,
        EitherOfDiverse<
            pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 2, 3>,
            pallet_collective::EnsureProportionAtLeast<AccountId, TechnicalCollective, 2, 3>,
        >,
    >;
    type WeightInfo = ();
    type MaxElectingVoters = MaxElectingVoters;
    type MaxElectableTargets = MaxElectableTargets;
    type MaxWinners = MaxActiveValidators;
}

parameter_types! {
    pub const BagThresholds: &'static [u64] = &bags_thresholds::THRESHOLDS;
}

impl pallet_bags_list::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type ScoreProvider = Staking;
    type WeightInfo = ();
    type BagThresholds = BagThresholds;
    type Score = sp_npos_elections::VoteWeight;
}

/// Used the compare the privilege of an origin inside the scheduler.
pub struct OriginPrivilegeCmp;

impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {
    fn cmp_privilege(left: &OriginCaller, right: &OriginCaller) -> Option<Ordering> {
        if left == right {
            return Some(Ordering::Equal);
        }

        match (left, right) {
            // Root is greater than anything.
            (OriginCaller::system(frame_system::RawOrigin::Root), _) => Some(Ordering::Greater),
            // Check which one has more yes votes.
            (
                OriginCaller::Council(pallet_collective::RawOrigin::Members(l_yes_votes, l_count)),
                OriginCaller::Council(pallet_collective::RawOrigin::Members(r_yes_votes, r_count)),
            ) => Some((l_yes_votes * r_count).cmp(&(r_yes_votes * l_count))),
            // For every other origin we don't care, as they are not used for `ScheduleOrigin`.
            _ => None,
        }
    }
}

impl pallet_scheduler::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type RuntimeOrigin = RuntimeOrigin;
    type PalletsOrigin = OriginCaller;
    type RuntimeCall = RuntimeCall;
    type MaximumWeight = SchedulerMaxWeight;
    type ScheduleOrigin = frame_system::EnsureRoot<AccountId>;
    type MaxScheduledPerBlock = MaxScheduledPerBlock;
    type WeightInfo = ();
    type OriginPrivilegeCmp = OriginPrivilegeCmp;
    type Preimages = Preimage;
}

parameter_types! {
    pub PreimageBaseDeposit: Balance = deposit(2, 64);
    pub PreimageByteDeposit: Balance = deposit(0, 1);
}

impl pallet_preimage::Config for Runtime {
    type WeightInfo = PreimageWeightInfo;
    type RuntimeEvent = RuntimeEvent;
    type Currency = Balances;
    type ManagerOrigin = EnsureRoot<AccountId>;
    type BaseDeposit = PreimageBaseDeposit;
    type ByteDeposit = PreimageByteDeposit;
}

parameter_types! {
    pub const ExistentialDeposit: u128 = 0;
    pub const TransferFee: u128 = 0;
    pub const CreationFee: u128 = 0;
    pub const MaxLocks: u32 = 50;
}

impl pallet_balances::Config for Runtime {
    /// The type for recording an account's balance.
    type Balance = Balance;
    type DustRemoval = ();
    /// The ubiquitous event type.
    type RuntimeEvent = RuntimeEvent;
    type ExistentialDeposit = ExistentialDeposit;
    type AccountStore = System;
    type WeightInfo = ();
    type MaxLocks = MaxLocks;
    type MaxReserves = ();
    type ReserveIdentifier = ();
}

pub type Amount = i128;

parameter_type_with_key! {
    pub ExistentialDeposits: |_currency_id: AssetId| -> Balance {
        0
    };
}

impl tokens::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Balance = Balance;
    type Amount = Amount;
    type CurrencyId = AssetId;
    type WeightInfo = ();
    type ExistentialDeposits = ExistentialDeposits;
    type CurrencyHooks = ();
    type MaxLocks = ();
    type MaxReserves = ();
    type ReserveIdentifier = ();
    type DustRemovalWhitelist = Everything;
}

parameter_types! {
    // This is common::PredefinedAssetId with 0 index, 2 is size, 0 and 0 is code.
    pub const GetXorAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::XOR);
    pub const GetDotAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::DOT);
    pub const GetKsmAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::KSM);
    pub const GetUsdAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::USDT);
    pub const GetValAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::VAL);
    pub const GetPswapAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::PSWAP);
    pub const GetDaiAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::DAI);
    pub const GetEthAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::ETH);
    pub const GetXstAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::XST);
    pub const GetTbcdAssetId: AssetId = common::AssetId32::from_asset_id(PredefinedAssetId::TBCD);

    pub const GetBaseAssetId: AssetId = GetXorAssetId::get();
    pub const GetBuyBackAssetId: AssetId = GetXstAssetId::get();
    pub GetBuyBackSupplyAssets: Vec<AssetId> = vec![GetValAssetId::get(), GetPswapAssetId::get()];
    pub const GetBuyBackPercentage: u8 = 10;
    pub const GetBuyBackAccountId: AccountId = AccountId::new(hex!("feb92c0acb61f75309730290db5cbe8ac9b46db7ad6f3bbb26a550a73586ea71"));
    pub const GetBuyBackDexId: DEXId = 0;
    pub const GetSyntheticBaseAssetId: AssetId = GetXstAssetId::get();
    pub const GetADARAccountId: AccountId = AccountId::new(hex!("dc5201cda01113be2ca9093c49a92763c95c708dd61df70c945df749c365da5d"));
}

impl currencies::Config for Runtime {
    type MultiCurrency = Tokens;
    type NativeCurrency = BasicCurrencyAdapter<Runtime, Balances, Amount, BlockNumber>;
    type GetNativeCurrencyId = <Runtime as assets::Config>::GetBaseAssetId;
    type WeightInfo = ();
}

impl common::Config for Runtime {
    type DEXId = DEXId;
    type LstId = common::LiquiditySourceType;
}

pub struct GetTotalBalance;

impl assets::GetTotalBalance<Runtime> for GetTotalBalance {
    fn total_balance(asset_id: &AssetId, who: &AccountId) -> Result<Balance, DispatchError> {
        if asset_id == &GetXorAssetId::get() {
            Ok(Referrals::referrer_balance(who).unwrap_or(0))
        } else {
            Ok(0)
        }
    }
}

impl assets::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type ExtraAccountId = [u8; 32];
    type ExtraAssetRecordArg =
        common::AssetIdExtraAssetRecordArg<DEXId, common::LiquiditySourceType, [u8; 32]>;
    type AssetId = AssetId;
    type GetBaseAssetId = GetBaseAssetId;
    type GetBuyBackAssetId = GetBuyBackAssetId;
    type GetBuyBackSupplyAssets = GetBuyBackSupplyAssets;
    type GetBuyBackPercentage = GetBuyBackPercentage;
    type GetBuyBackAccountId = GetBuyBackAccountId;
    type GetBuyBackDexId = GetBuyBackDexId;
    type BuyBackLiquidityProxy = liquidity_proxy::Pallet<Runtime>;
    type Currency = currencies::Pallet<Runtime>;
    type GetTotalBalance = GetTotalBalance;
    type WeightInfo = assets::weights::SubstrateWeight<Runtime>;
}

impl trading_pair::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type EnsureDEXManager = dex_manager::Pallet<Runtime>;
    type DexInfoProvider = dex_manager::Pallet<Runtime>;
    type WeightInfo = ();
}

impl dex_manager::Config for Runtime {}

pub type TechAccountId = common::TechAccountId<AccountId, TechAssetId, DEXId>;
pub type TechAssetId = common::TechAssetId<common::PredefinedAssetId>;
pub type AssetId = common::AssetId32<common::PredefinedAssetId>;

impl technical::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type TechAssetId = TechAssetId;
    type TechAccountId = TechAccountId;
    type Trigger = ();
    type Condition = ();
    type SwapAction = pool_xyk::PolySwapAction<AssetId, AccountId, TechAccountId>;
}

parameter_types! {
    pub GetFee: Fixed = fixed!(0.003);
}

parameter_type_with_key! {
    pub GetTradingPairRestrictedFlag: |trading_pair: common::TradingPair<AssetId>| -> bool {
        let common::TradingPair {
            base_asset_id,
            target_asset_id
        } = trading_pair;
        (base_asset_id, target_asset_id) == (&XSTUSD.into(), &XOR.into())
    };
}

impl pool_xyk::Config for Runtime {
    const MIN_XOR: Balance = balance!(0.0007);
    type RuntimeEvent = RuntimeEvent;
    type PairSwapAction = pool_xyk::PairSwapAction<AssetId, AccountId, TechAccountId>;
    type DepositLiquidityAction =
        pool_xyk::DepositLiquidityAction<AssetId, AccountId, TechAccountId>;
    type WithdrawLiquidityAction =
        pool_xyk::WithdrawLiquidityAction<AssetId, AccountId, TechAccountId>;
    type PolySwapAction = pool_xyk::PolySwapAction<AssetId, AccountId, TechAccountId>;
    type EnsureDEXManager = dex_manager::Pallet<Runtime>;
    type GetFee = GetFee;
    type OnPoolCreated = (PswapDistribution, Farming);
    type OnPoolReservesChanged = PriceTools;
    type WeightInfo = pool_xyk::weights::SubstrateWeight<Runtime>;
    type XSTMarketInfo = XSTPool;
    type GetTradingPairRestrictedFlag = GetTradingPairRestrictedFlag;
}

parameter_types! {
    pub GetLiquidityProxyTechAccountId: TechAccountId = {
        // TODO(Harrm): why pswap_distribution?
        let tech_account_id = TechAccountId::from_generic_pair(
            pswap_distribution::TECH_ACCOUNT_PREFIX.to_vec(),
            pswap_distribution::TECH_ACCOUNT_MAIN.to_vec(),
        );
        tech_account_id
    };
    pub GetLiquidityProxyAccountId: AccountId = {
        let tech_account_id = GetLiquidityProxyTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub const GetNumSamples: usize = 5;
    pub const BasicDeposit: Balance = balance!(0.01);
    pub const FieldDeposit: Balance = balance!(0.01);
    pub const SubAccountDeposit: Balance = balance!(0.01);
    pub const MaxSubAccounts: u32 = 100;
    pub const MaxAdditionalFields: u32 = 100;
    pub const MaxRegistrars: u32 = 20;
    pub ReferralsReservesAcc: AccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            b"referrals".to_vec(),
            b"main".to_vec(),
        );
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
}

impl liquidity_proxy::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type LiquidityRegistry = dex_api::Pallet<Runtime>;
    type GetNumSamples = GetNumSamples;
    type GetTechnicalAccountId = GetLiquidityProxyAccountId;
    type PrimaryMarketTBC = multicollateral_bonding_curve_pool::Pallet<Runtime>;
    type PrimaryMarketXST = xst::Pallet<Runtime>;
    type SecondaryMarket = pool_xyk::Pallet<Runtime>;
    type WeightInfo = liquidity_proxy::weights::SubstrateWeight<Runtime>;
    type VestedRewardsPallet = VestedRewards;
    type GetADARAccountId = GetADARAccountId;
    type ADARCommissionRatioUpdateOrigin = EitherOfDiverse<
        pallet_collective::EnsureProportionMoreThan<AccountId, TechnicalCollective, 1, 2>,
        EnsureRoot<AccountId>,
    >;
}

impl mock_liquidity_source::Config<mock_liquidity_source::Instance1> for Runtime {
    type GetFee = GetFee;
    type EnsureDEXManager = dex_manager::Pallet<Runtime>;
    type EnsureTradingPairExists = trading_pair::Pallet<Runtime>;
    type DexInfoProvider = dex_manager::Pallet<Runtime>;
}

impl mock_liquidity_source::Config<mock_liquidity_source::Instance2> for Runtime {
    type GetFee = GetFee;
    type EnsureDEXManager = dex_manager::Pallet<Runtime>;
    type EnsureTradingPairExists = trading_pair::Pallet<Runtime>;
    type DexInfoProvider = dex_manager::Pallet<Runtime>;
}

impl mock_liquidity_source::Config<mock_liquidity_source::Instance3> for Runtime {
    type GetFee = GetFee;
    type EnsureDEXManager = dex_manager::Pallet<Runtime>;
    type EnsureTradingPairExists = trading_pair::Pallet<Runtime>;
    type DexInfoProvider = dex_manager::Pallet<Runtime>;
}

impl mock_liquidity_source::Config<mock_liquidity_source::Instance4> for Runtime {
    type GetFee = GetFee;
    type EnsureDEXManager = dex_manager::Pallet<Runtime>;
    type EnsureTradingPairExists = trading_pair::Pallet<Runtime>;
    type DexInfoProvider = dex_manager::Pallet<Runtime>;
}

impl dex_api::Config for Runtime {
    type MockLiquiditySource =
        mock_liquidity_source::Pallet<Runtime, mock_liquidity_source::Instance1>;
    type MockLiquiditySource2 =
        mock_liquidity_source::Pallet<Runtime, mock_liquidity_source::Instance2>;
    type MockLiquiditySource3 =
        mock_liquidity_source::Pallet<Runtime, mock_liquidity_source::Instance3>;
    type MockLiquiditySource4 =
        mock_liquidity_source::Pallet<Runtime, mock_liquidity_source::Instance4>;
    type MulticollateralBondingCurvePool = multicollateral_bonding_curve_pool::Pallet<Runtime>;
    type XYKPool = pool_xyk::Pallet<Runtime>;
    type XSTPool = xst::Pallet<Runtime>;

    #[cfg(feature = "wip")] // order-book
    type OrderBook = order_book::Pallet<Runtime>;
}

impl pallet_multisig::Config for Runtime {
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type Currency = Balances;
    type DepositBase = DepositBase;
    type DepositFactor = DepositFactor;
    type MaxSignatories = MaxSignatories;
    type WeightInfo = ();
}

impl iroha_migration::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type WeightInfo = iroha_migration::weights::SubstrateWeight<Runtime>;
}

impl pallet_identity::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Currency = Balances;
    type BasicDeposit = BasicDeposit;
    type FieldDeposit = FieldDeposit;
    type SubAccountDeposit = SubAccountDeposit;
    type MaxSubAccounts = MaxSubAccounts;
    type MaxAdditionalFields = MaxAdditionalFields;
    type MaxRegistrars = MaxRegistrars;
    type Slashed = ();
    type ForceOrigin = MoreThanHalfCouncil;
    type RegistrarOrigin = MoreThanHalfCouncil;
    type WeightInfo = ();
}

impl<T: SigningTypes> frame_system::offchain::SignMessage<T> for Runtime {
    type SignatureData = ();

    fn sign_message(&self, _message: &[u8]) -> Self::SignatureData {
        unimplemented!()
    }

    fn sign<TPayload, F>(&self, _f: F) -> Self::SignatureData
    where
        F: Fn(&Account<T>) -> TPayload,
        TPayload: frame_system::offchain::SignedPayload<T>,
    {
        unimplemented!()
    }
}

impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Runtime
where
    RuntimeCall: From<LocalCall>,
{
    fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
        call: RuntimeCall,
        public: <Signature as sp_runtime::traits::Verify>::Signer,
        account: AccountId,
        index: Index,
    ) -> Option<(
        RuntimeCall,
        <UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
    )> {
        let period = BlockHashCount::get() as u64;
        let current_block = System::block_number()
            .saturated_into::<u64>()
            .saturating_sub(1);
        let extra: SignedExtra = (
            frame_system::CheckSpecVersion::<Runtime>::new(),
            frame_system::CheckTxVersion::<Runtime>::new(),
            frame_system::CheckGenesis::<Runtime>::new(),
            frame_system::CheckEra::<Runtime>::from(generic::Era::mortal(period, current_block)),
            frame_system::CheckNonce::<Runtime>::from(index),
            frame_system::CheckWeight::<Runtime>::new(),
            ChargeTransactionPayment::<Runtime>::new(),
        );
        #[cfg_attr(not(feature = "std"), allow(unused_variables))]
        let raw_payload = SignedPayload::new(call, extra)
            .map_err(|e| {
                frame_support::log::warn!("SignedPayload error: {:?}", e);
            })
            .ok()?;

        let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;

        let address = account;
        let (call, extra, _) = raw_payload.deconstruct();
        Some((call, (address, signature, extra)))
    }
}

impl frame_system::offchain::SigningTypes for Runtime {
    type Public = <Signature as sp_runtime::traits::Verify>::Signer;
    type Signature = Signature;
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Runtime
where
    RuntimeCall: From<C>,
{
    type OverarchingCall = RuntimeCall;
    type Extrinsic = UncheckedExtrinsic;
}

impl referrals::Config for Runtime {
    type ReservesAcc = ReferralsReservesAcc;
    type WeightInfo = referrals::weights::SubstrateWeight<Runtime>;
}

impl rewards::Config for Runtime {
    const BLOCKS_PER_DAY: BlockNumber = 1 * DAYS;
    const UPDATE_FREQUENCY: BlockNumber = 10 * MINUTES;
    const MAX_CHUNK_SIZE: usize = 100;
    const MAX_VESTING_RATIO: Percent = Percent::from_percent(55);
    const TIME_TO_SATURATION: BlockNumber = 5 * 365 * DAYS; // 5 years
    const VAL_BURN_PERCENT: Percent = VAL_BURN_PERCENT;
    type RuntimeEvent = RuntimeEvent;
    type WeightInfo = rewards::weights::SubstrateWeight<Runtime>;
}

pub struct ValBurnedAggregator<T>(sp_std::marker::PhantomData<T>);

impl<T> OnValBurned for ValBurnedAggregator<T>
where
    T: ValBurnedNotifier<Balance>,
{
    fn on_val_burned(amount: Balance) {
        Rewards::on_val_burned(amount);
        T::notify_val_burned(amount);
    }
}

parameter_types! {
    pub const DEXIdValue: DEXId = 0;
}

impl xor_fee::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    // Pass native currency.
    type XorCurrency = Balances;
    type ReferrerWeight = ReferrerWeight;
    type XorBurnedWeight = XorBurnedWeight;
    type XorIntoValBurnedWeight = XorIntoValBurnedWeight;
    type BuyBackXSTPercent = BuyBackXSTPercent;
    type XorId = GetXorAssetId;
    type ValId = GetValAssetId;
    type XstId = GetXstAssetId;
    type DEXIdValue = DEXIdValue;
    type LiquidityProxy = LiquidityProxy;
    type OnValBurned = ValBurnedAggregator<Staking>;
    type CustomFees = xor_fee_impls::CustomFees;
    type GetTechnicalAccountId = GetXorFeeAccountId;
    type SessionManager = Staking;
    type FullIdentification = pallet_staking::Exposure<AccountId, Balance>;
    type WeightInfo = xor_fee::weights::SubstrateWeight<Runtime>;
    type WithdrawFee = xor_fee_impls::WithdrawFee;
    type BuyBackHandler = liquidity_proxy::LiquidityProxyBuyBackHandler<Runtime, GetBuyBackDexId>;
    type ReferrerAccountProvider = Referrals;
}

pub struct ConstantFeeMultiplier;

impl MultiplierUpdate for ConstantFeeMultiplier {
    fn min() -> Multiplier {
        Default::default()
    }
    fn max() -> Multiplier {
        Default::default()
    }
    fn target() -> Perquintill {
        Default::default()
    }
    fn variability() -> Multiplier {
        Default::default()
    }
}
impl Convert<Multiplier, Multiplier> for ConstantFeeMultiplier {
    fn convert(previous: Multiplier) -> Multiplier {
        previous
    }
}

parameter_types! {
    pub const OperationalFeeMultiplier: u8 = 5;
}

impl pallet_transaction_payment::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OnChargeTransaction = XorFee;
    type WeightToFee = XorFee;
    type FeeMultiplierUpdate = ConstantFeeMultiplier;
    type OperationalFeeMultiplier = OperationalFeeMultiplier;
    type LengthToFee = ConstantMultiplier<Balance, ConstU128<0>>;
}

#[cfg(feature = "private-net")]
impl pallet_sudo::Config for Runtime {
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
}

impl permissions::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
}

impl pallet_utility::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type RuntimeCall = RuntimeCall;
    type WeightInfo = ();
    type PalletsOrigin = OriginCaller;
}

parameter_types! {
    pub const DepositBase: u64 = 1;
    pub const DepositFactor: u64 = 1;
    pub const MaxSignatories: u16 = 100;
}

impl bridge_multisig::Config for Runtime {
    type RuntimeCall = RuntimeCall;
    type RuntimeEvent = RuntimeEvent;
    type Currency = Balances;
    type DepositBase = DepositBase;
    type DepositFactor = DepositFactor;
    type MaxSignatories = MaxSignatories;
    type WeightInfo = ();
}

parameter_types! {
    pub const GetEthNetworkId: u32 = 0;
}

pub struct RemoveTemporaryPeerAccountIds;

#[cfg(feature = "private-net")]
impl Get<Vec<(AccountId, H160)>> for RemoveTemporaryPeerAccountIds {
    fn get() -> Vec<(AccountId, H160)> {
        vec![
            // Dev
            (
                AccountId::new(hex!(
                    "aa79aa80b94b1cfba69c4a7d60eeb7b469e6411d1f686cc61de8adc8b1b76a69"
                )),
                H160(hex!("f858c8366f3a2553516a47f3e0503a85ef93bbba")),
            ),
            (
                AccountId::new(hex!(
                    "60dc5adadc262770cbe904e3f65a26a89d46b70447640cd7968b49ddf5a459bc"
                )),
                H160(hex!("ccd7fe44d58640dc79c55b98f8c3474646e5ea2b")),
            ),
            (
                AccountId::new(hex!(
                    "70d61e980602e09ac8b5fb50658ebd345774e73b8248d3b61862ba1a9a035082"
                )),
                H160(hex!("13d26a91f791e884fe6faa7391c4ef401638baa4")),
            ),
            (
                AccountId::new(hex!(
                    "05918034f4a7f7c5d99cd0382aa6574ec2aba148aa3d769e50e0ac7663e36d58"
                )),
                H160(hex!("aa19829ae887212206be8e97ea47d8fed2120d4e")),
            ),
            // Test
            (
                AccountId::new(hex!(
                    "07f5670d08b8f3bd493ff829482a489d94494fd50dd506957e44e9fdc2e98684"
                )),
                H160(hex!("457d710255184dbf63c019ab50f65743c6cb072f")),
            ),
            (
                AccountId::new(hex!(
                    "211bb96e9f746183c05a1d583bccf513f9d8f679d6f36ecbd06609615a55b1cc"
                )),
                H160(hex!("6d04423c97e8ce36d04c9b614926ce0d029d04df")),
            ),
            (
                AccountId::new(hex!(
                    "ef3139b81d14977d5bf6b4a3994872337dfc1d2af2069a058bc26123a3ed1a5c"
                )),
                H160(hex!("e34022904b1ab539729cc7b5bfa5c8a74b165e80")),
            ),
            (
                AccountId::new(hex!(
                    "71124b336fbf3777d743d4390acce6be1cf5e0781e40c51d4cf2e5b5fd8e41e1"
                )),
                H160(hex!("ee74a5b5346915012d103cf1ccee288f25bcbc81")),
            ),
            // Stage
            (
                AccountId::new(hex!(
                    "07f5670d08b8f3bd493ff829482a489d94494fd50dd506957e44e9fdc2e98684"
                )),
                H160(hex!("457d710255184dbf63c019ab50f65743c6cb072f")),
            ),
            (
                AccountId::new(hex!(
                    "211bb96e9f746183c05a1d583bccf513f9d8f679d6f36ecbd06609615a55b1cc"
                )),
                H160(hex!("6d04423c97e8ce36d04c9b614926ce0d029d04df")),
            ),
        ]
    }
}

#[cfg(not(feature = "private-net"))]
impl Get<Vec<(AccountId, H160)>> for RemoveTemporaryPeerAccountIds {
    fn get() -> Vec<(AccountId, H160)> {
        vec![] // the peer is already removed on main-net.
    }
}

#[cfg(not(feature = "private-net"))]
parameter_types! {
    pub const RemovePendingOutgoingRequestsAfter: BlockNumber = 1 * DAYS;
    pub const TrackPendingIncomingRequestsAfter: (BlockNumber, u64) = (1 * DAYS, 12697214);
}

#[cfg(feature = "private-net")]
parameter_types! {
    pub const RemovePendingOutgoingRequestsAfter: BlockNumber = 30 * MINUTES;
    pub const TrackPendingIncomingRequestsAfter: (BlockNumber, u64) = (30 * MINUTES, 0);
}

pub type NetworkId = u32;

impl eth_bridge::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type RuntimeCall = RuntimeCall;
    type PeerId = eth_bridge::offchain::crypto::TestAuthId;
    type NetworkId = NetworkId;
    type GetEthNetworkId = GetEthNetworkId;
    type WeightInfo = eth_bridge::weights::SubstrateWeight<Runtime>;
    type WeightToFee = XorFee;
    type MessageStatusNotifier = BridgeProxy;
    type BridgeAssetLockChecker = BridgeProxy;
}

#[cfg(feature = "private-net")]
impl faucet::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type WeightInfo = faucet::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
    pub QaToolsWhitelistCapacity: u32 = 512;
}

#[cfg(all(feature = "private-net", feature = "wip"))] // order-book
impl qa_tools::Config for Runtime {
    type AssetInfoProvider = Assets;
    type QaToolsWhitelistCapacity = QaToolsWhitelistCapacity;
    type WeightInfo = qa_tools::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
    pub GetPswapDistributionTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            pswap_distribution::TECH_ACCOUNT_PREFIX.to_vec(),
            pswap_distribution::TECH_ACCOUNT_MAIN.to_vec(),
        );
        tech_account_id
    };
    pub GetPswapDistributionAccountId: AccountId = {
        let tech_account_id = GetPswapDistributionTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetParliamentAccountId: AccountId = hex!("881b87c9f83664b95bd13e2bb40675bfa186287da93becc0b22683334d411e4e").into();
    pub GetXorFeeTechAccountId: TechAccountId = {
        TechAccountId::from_generic_pair(
            xor_fee::TECH_ACCOUNT_PREFIX.to_vec(),
            xor_fee::TECH_ACCOUNT_MAIN.to_vec(),
        )
    };
    pub GetXorFeeAccountId: AccountId = {
        let tech_account_id = GetXorFeeTechAccountId::get();
        technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
            .expect("Failed to get ordinary account id for technical account id.")
    };
    pub GetXSTPoolPermissionedTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            xst::TECH_ACCOUNT_PREFIX.to_vec(),
            xst::TECH_ACCOUNT_PERMISSIONED.to_vec(),
        );
        tech_account_id
    };
    pub GetXSTPoolPermissionedAccountId: AccountId = {
        let tech_account_id = GetXSTPoolPermissionedTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetTrustlessBridgeTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            bridge_types::types::TECH_ACCOUNT_PREFIX.to_vec(),
            bridge_types::types::TECH_ACCOUNT_MAIN.to_vec(),
        );
        tech_account_id
    };
    pub GetTrustlessBridgeAccountId: AccountId = {
        let tech_account_id = GetTrustlessBridgeTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetTrustlessBridgeFeesTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            bridge_types::types::TECH_ACCOUNT_PREFIX.to_vec(),
            bridge_types::types::TECH_ACCOUNT_FEES.to_vec(),
        );
        tech_account_id
    };
    pub GetTrustlessBridgeFeesAccountId: AccountId = {
        let tech_account_id = GetTrustlessBridgeFeesTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetTreasuryTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            bridge_types::types::TECH_ACCOUNT_TREASURY_PREFIX.to_vec(),
            bridge_types::types::TECH_ACCOUNT_MAIN.to_vec(),
        );
        tech_account_id
    };
    pub GetTreasuryAccountId: AccountId = {
        let tech_account_id = GetTreasuryTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
}

#[cfg(feature = "reduced-pswap-reward-periods")]
parameter_types! {
    pub const GetDefaultSubscriptionFrequency: BlockNumber = 150;
    pub const GetBurnUpdateFrequency: BlockNumber = 150;
}

#[cfg(not(feature = "reduced-pswap-reward-periods"))]
parameter_types! {
    pub const GetDefaultSubscriptionFrequency: BlockNumber = 14400;
    pub const GetBurnUpdateFrequency: BlockNumber = 14400;
}

pub struct RuntimeOnPswapBurnedAggregator;

impl OnPswapBurned for RuntimeOnPswapBurnedAggregator {
    fn on_pswap_burned(distribution: common::PswapRemintInfo) {
        VestedRewards::on_pswap_burned(distribution);
    }
}

impl farming::Config for Runtime {
    const PSWAP_PER_DAY: Balance = FARMING_PSWAP_PER_DAY;
    const REFRESH_FREQUENCY: BlockNumber = FARMING_REFRESH_FREQUENCY;
    const VESTING_COEFF: u32 = FARMING_VESTING_COEFF;
    const VESTING_FREQUENCY: BlockNumber = FARMING_VESTING_FREQUENCY;
    const BLOCKS_PER_DAY: BlockNumber = 1 * DAYS;
    type RuntimeCall = RuntimeCall;
    type SchedulerOriginCaller = OriginCaller;
    type Scheduler = Scheduler;
    type RewardDoublingAssets = FarmingRewardDoublingAssets;
    type WeightInfo = ();
}

impl pswap_distribution::Config for Runtime {
    const PSWAP_BURN_PERCENT: Percent = PSWAP_BURN_PERCENT;
    type RuntimeEvent = RuntimeEvent;
    type GetIncentiveAssetId = GetPswapAssetId;
    type GetXSTAssetId = GetXstAssetId;
    type LiquidityProxy = LiquidityProxy;
    type CompatBalance = Balance;
    type GetDefaultSubscriptionFrequency = GetDefaultSubscriptionFrequency;
    type GetBurnUpdateFrequency = GetBurnUpdateFrequency;
    type GetTechnicalAccountId = GetPswapDistributionAccountId;
    type EnsureDEXManager = DEXManager;
    type OnPswapBurnedAggregator = RuntimeOnPswapBurnedAggregator;
    type WeightInfo = pswap_distribution::weights::SubstrateWeight<Runtime>;
    type GetParliamentAccountId = GetParliamentAccountId;
    type PoolXykPallet = PoolXYK;
    type BuyBackHandler = liquidity_proxy::LiquidityProxyBuyBackHandler<Runtime, GetBuyBackDexId>;
    type DexInfoProvider = dex_manager::Pallet<Runtime>;
}

parameter_types! {
    pub GetMbcReservesTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            multicollateral_bonding_curve_pool::TECH_ACCOUNT_PREFIX.to_vec(),
            multicollateral_bonding_curve_pool::TECH_ACCOUNT_RESERVES.to_vec(),
        );
        tech_account_id
    };
    pub GetMbcReservesAccountId: AccountId = {
        let tech_account_id = GetMbcReservesTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetMbcPoolRewardsTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            multicollateral_bonding_curve_pool::TECH_ACCOUNT_PREFIX.to_vec(),
            multicollateral_bonding_curve_pool::TECH_ACCOUNT_REWARDS.to_vec(),
        );
        tech_account_id
    };
    pub GetMbcPoolRewardsAccountId: AccountId = {
        let tech_account_id = GetMbcPoolRewardsTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetMbcPoolFreeReservesTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            multicollateral_bonding_curve_pool::TECH_ACCOUNT_PREFIX.to_vec(),
            multicollateral_bonding_curve_pool::TECH_ACCOUNT_FREE_RESERVES.to_vec(),
        );
        tech_account_id
    };
    pub GetMbcPoolFreeReservesAccountId: AccountId = {
        let tech_account_id = GetMbcPoolFreeReservesTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetMarketMakerRewardsTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            vested_rewards::TECH_ACCOUNT_PREFIX.to_vec(),
            vested_rewards::TECH_ACCOUNT_MARKET_MAKERS.to_vec(),
        );
        tech_account_id
    };
    pub GetMarketMakerRewardsAccountId: AccountId = {
        let tech_account_id = GetMarketMakerRewardsTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetFarmingRewardsTechAccountId: TechAccountId = {
        let tech_account_id = TechAccountId::from_generic_pair(
            vested_rewards::TECH_ACCOUNT_PREFIX.to_vec(),
            vested_rewards::TECH_ACCOUNT_FARMING.to_vec(),
        );
        tech_account_id
    };
    pub GetFarmingRewardsAccountId: AccountId = {
        let tech_account_id = GetFarmingRewardsTechAccountId::get();
        let account_id =
            technical::Pallet::<Runtime>::tech_account_id_to_account_id(&tech_account_id)
                .expect("Failed to get ordinary account id for technical account id.");
        account_id
    };
    pub GetTBCBuyBackXSTPercent: Fixed = fixed!(0.025);
}

impl multicollateral_bonding_curve_pool::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type LiquidityProxy = LiquidityProxy;
    type EnsureDEXManager = DEXManager;
    type EnsureTradingPairExists = TradingPair;
    type PriceToolsPallet = PriceTools;
    type VestedRewardsPallet = VestedRewards;
    type WeightInfo = multicollateral_bonding_curve_pool::weights::SubstrateWeight<Runtime>;
    type BuyBackHandler = liquidity_proxy::LiquidityProxyBuyBackHandler<Runtime, GetBuyBackDexId>;
    type BuyBackXSTPercent = GetTBCBuyBackXSTPercent;
}

parameter_types! {
    pub const GetXstPoolConversionAssetId: AssetId = GetXstAssetId::get();
    pub const GetSyntheticBaseBuySellLimit: Balance = Balance::MAX;
}

impl xst::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type GetSyntheticBaseAssetId = GetXstPoolConversionAssetId;
    type GetXSTPoolPermissionedTechAccountId = GetXSTPoolPermissionedTechAccountId;
    type EnsureDEXManager = DEXManager;
    type PriceToolsPallet = PriceTools;
    type WeightInfo = xst::weights::SubstrateWeight<Runtime>;
    type Oracle = OracleProxy;
    type Symbol = <Runtime as band::Config>::Symbol;
    type TradingPairSourceManager = TradingPair;
    type GetSyntheticBaseBuySellLimit = GetSyntheticBaseBuySellLimit;
}

parameter_types! {
    pub const MaxKeys: u32 = 10_000;
    pub const MaxPeerInHeartbeats: u32 = 10_000;
    pub const MaxPeerDataEncodingSize: u32 = 1_000;
}

impl pallet_im_online::Config for Runtime {
    type AuthorityId = ImOnlineId;
    type RuntimeEvent = RuntimeEvent;
    type ValidatorSet = Historical;
    type NextSessionRotation = Babe;
    type ReportUnresponsiveness = Offences;
    type UnsignedPriority = ImOnlineUnsignedPriority;
    type WeightInfo = ();
    type MaxKeys = MaxKeys;
    type MaxPeerInHeartbeats = MaxPeerInHeartbeats;
    type MaxPeerDataEncodingSize = MaxPeerDataEncodingSize;
}

impl pallet_offences::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
    type OnOffenceHandler = Staking;
}

impl vested_rewards::Config for Runtime {
    const BLOCKS_PER_DAY: BlockNumber = 1 * DAYS;
    type RuntimeEvent = RuntimeEvent;
    type GetBondingCurveRewardsAccountId = GetMbcPoolRewardsAccountId;
    type GetFarmingRewardsAccountId = GetFarmingRewardsAccountId;
    type GetMarketMakerRewardsAccountId = GetMarketMakerRewardsAccountId;
    type WeightInfo = vested_rewards::weights::SubstrateWeight<Runtime>;
}

impl price_tools::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type LiquidityProxy = LiquidityProxy;
    type WeightInfo = price_tools::weights::SubstrateWeight<Runtime>;
}

impl pallet_randomness_collective_flip::Config for Runtime {}

#[cfg(not(feature = "wip"))] // Basic impl for session keys
impl pallet_beefy::Config for Runtime {
    type BeefyId = BeefyId;
    type MaxAuthorities = MaxAuthorities;
    type OnNewValidatorSet = ();
}

#[cfg(feature = "wip")] // Trustless bridges
impl pallet_beefy::Config for Runtime {
    type BeefyId = BeefyId;
    type MaxAuthorities = MaxAuthorities;
    type OnNewValidatorSet = MmrLeaf;
}

#[cfg(feature = "wip")] // Trustless bridges
impl pallet_mmr::Config for Runtime {
    const INDEXING_PREFIX: &'static [u8] = b"mmr";
    type Hashing = Keccak256;
    type Hash = <Keccak256 as sp_runtime::traits::Hash>::Output;
    type OnNewRoot = pallet_beefy_mmr::DepositBeefyDigest<Runtime>;
    type WeightInfo = ();
    type LeafData = pallet_beefy_mmr::Pallet<Runtime>;
}

impl leaf_provider::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Hashing = Keccak256;
    type Hash = <Keccak256 as sp_runtime::traits::Hash>::Output;
    type Randomness = pallet_babe::RandomnessFromTwoEpochsAgo<Self>;
}

#[cfg(feature = "wip")] // Trustless bridges
parameter_types! {
    /// Version of the produced MMR leaf.
    ///
    /// The version consists of two parts;
    /// - `major` (3 bits)
    /// - `minor` (5 bits)
    ///
    /// `major` should be updated only if decoding the previous MMR Leaf format from the payload
    /// is not possible (i.e. backward incompatible change).
    /// `minor` should be updated if fields are added to the previous MMR Leaf, which given SCALE
    /// encoding does not prevent old leafs from being decoded.
    ///
    /// Hence we expect `major` to be changed really rarely (think never).
    /// See [`MmrLeafVersion`] type documentation for more details.
    pub LeafVersion: MmrLeafVersion = MmrLeafVersion::new(0, 0);
}

#[cfg(feature = "wip")] // Trustless bridges
impl pallet_beefy_mmr::Config for Runtime {
    type LeafVersion = LeafVersion;
    type BeefyAuthorityToMerkleLeaf = pallet_beefy_mmr::BeefyEcdsaToEthereum;
    type LeafExtra =
        LeafExtraData<<Self as leaf_provider::Config>::Hash, <Self as frame_system::Config>::Hash>;
    type BeefyDataProvider = leaf_provider::Pallet<Runtime>;
}

parameter_types! {
    pub const CeresPerDay: Balance = balance!(6.66666666667);
    pub const CeresAssetId: AssetId = common::AssetId32::from_bytes
        (hex!("008bcfd2387d3fc453333557eecb0efe59fcba128769b2feefdd306e98e66440"));
    pub const MaximumCeresInStakingPool: Balance = balance!(14400);
}

impl ceres_launchpad::Config for Runtime {
    const MILLISECONDS_PER_DAY: Moment = 86_400_000;
    type RuntimeEvent = RuntimeEvent;
    type WeightInfo = ceres_launchpad::weights::SubstrateWeight<Runtime>;
}

impl ceres_staking::Config for Runtime {
    const BLOCKS_PER_ONE_DAY: BlockNumber = 1 * DAYS;
    type RuntimeEvent = RuntimeEvent;
    type CeresPerDay = CeresPerDay;
    type CeresAssetId = CeresAssetId;
    type MaximumCeresInStakingPool = MaximumCeresInStakingPool;
    type WeightInfo = ceres_staking::weights::SubstrateWeight<Runtime>;
}

impl ceres_liquidity_locker::Config for Runtime {
    const BLOCKS_PER_ONE_DAY: BlockNumber = 1 * DAYS;
    type RuntimeEvent = RuntimeEvent;
    type XYKPool = PoolXYK;
    type DemeterFarmingPlatform = DemeterFarmingPlatform;
    type CeresAssetId = CeresAssetId;
    type WeightInfo = ceres_liquidity_locker::weights::SubstrateWeight<Runtime>;
}

impl ceres_token_locker::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type CeresAssetId = CeresAssetId;
    type WeightInfo = ceres_token_locker::weights::SubstrateWeight<Runtime>;
}

impl ceres_governance_platform::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type CeresAssetId = CeresAssetId;
    type WeightInfo = ceres_governance_platform::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
    pub const DemeterAssetId: AssetId = common::DEMETER_ASSET_ID;
}

impl demeter_farming_platform::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type DemeterAssetId = DemeterAssetId;
    const BLOCKS_PER_HOUR_AND_A_HALF: BlockNumber = 3 * HOURS / 2;
    type WeightInfo = demeter_farming_platform::weights::SubstrateWeight<Runtime>;
}

impl oracle_proxy::Config for Runtime {
    type Symbol = Symbol;
    type RuntimeEvent = RuntimeEvent;
    type WeightInfo = oracle_proxy::weights::SubstrateWeight<Runtime>;
    type BandChainOracle = band::Pallet<Runtime>;
}

parameter_types! {
    pub const GetBandRateStalePeriod: Moment = 60*5*1000; // 5 minutes
    pub const GetBandRateStaleBlockPeriod: u32 = 600; // 1 hour in blocks
    pub const BandMaxRelaySymbols: u32 = 100;
}

impl band::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Symbol = Symbol;
    type WeightInfo = band::weights::SubstrateWeight<Runtime>;
    type OnNewSymbolsRelayedHook = oracle_proxy::Pallet<Runtime>;
    type Time = Timestamp;
    type GetBandRateStalePeriod = GetBandRateStalePeriod;
    type GetBandRateStaleBlockPeriod = GetBandRateStaleBlockPeriod;
    type OnSymbolDisabledHook = xst::Pallet<Runtime>;
    type MaxRelaySymbols = BandMaxRelaySymbols;
}

parameter_types! {
    pub const HermesAssetId: AssetId = common::HERMES_ASSET_ID;
    pub const StringLimit: u32 = 64;
    pub const OptionsLimit: u32 = 5;
    pub const TitleLimit: u32 = 128;
    pub const DescriptionLimit: u32 = 4096;
}

impl hermes_governance_platform::Config for Runtime {
    const MIN_DURATION_OF_POLL: Moment = 14_400_000;
    const MAX_DURATION_OF_POLL: Moment = 604_800_000;
    type StringLimit = StringLimit;
    type OptionsLimit = OptionsLimit;
    type RuntimeEvent = RuntimeEvent;
    type HermesAssetId = HermesAssetId;
    type TitleLimit = TitleLimit;
    type DescriptionLimit = DescriptionLimit;
    type WeightInfo = hermes_governance_platform::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
    // small value for test environment in order to check postponing expirations
    pub ExpirationsSchedulerMaxWeight: Weight = Perbill::from_percent(15) * BlockWeights::get().max_block; // TODO: order-book clarify
}

#[cfg(feature = "wip")] // order-book
impl order_book::Config for Runtime {
    const MAX_ORDER_LIFESPAN: Moment = 30 * (DAYS as Moment) * MILLISECS_PER_BLOCK; // 30 days // TODO: order-book clarify
    const MIN_ORDER_LIFESPAN: Moment = MILLISECS_PER_BLOCK; // TODO: order-book clarify
    const MILLISECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK;
    const MAX_PRICE_SHIFT: Perbill = Perbill::from_percent(50); // TODO: order-book clarify
    type RuntimeEvent = RuntimeEvent;
    type OrderId = u128;
    type Locker = OrderBook;
    type Unlocker = OrderBook;
    type Scheduler = OrderBook;
    type Delegate = OrderBook;
    type MaxOpenedLimitOrdersPerUser = ConstU32<1000>; // TODO: order-book clarify
    type MaxLimitOrdersForPrice = ConstU32<10000>; // TODO: order-book clarify
    type MaxSidePriceCount = ConstU32<10000>; // TODO: order-book clarify
    type MaxExpiringOrdersPerBlock = ConstU32<1000>; // TODO: order-book clarify
    type MaxExpirationWeightPerBlock = ExpirationsSchedulerMaxWeight;
    type EnsureTradingPairExists = TradingPair;
    type TradingPairSourceManager = TradingPair;
    type AssetInfoProvider = Assets;
    type SyntheticInfoProvider = XSTPool;
    type DexInfoProvider = DEXManager;
    type Time = Timestamp;
    type ParameterUpdateOrigin = EitherOfDiverse<
        EnsureSigned<AccountId>,
        EitherOf<
            pallet_collective::EnsureProportionMoreThan<AccountId, TechnicalCollective, 1, 2>,
            EnsureRoot<AccountId>,
        >,
    >;
    type StatusUpdateOrigin = EitherOf<
        pallet_collective::EnsureProportionMoreThan<AccountId, TechnicalCollective, 1, 2>,
        EnsureRoot<AccountId>,
    >;
    type RemovalOrigin = EitherOf<
        pallet_collective::EnsureProportionMoreThan<AccountId, TechnicalCollective, 1, 2>,
        EnsureRoot<AccountId>,
    >;
    type WeightInfo = order_book::weights::SubstrateWeight<Runtime>;
}

/// Payload data to be signed when making signed transaction from off-chain workers,
///   inside `create_transaction` function.
pub type SignedPayload = generic::SignedPayload<RuntimeCall, SignedExtra>;

parameter_types! {
    pub const ReferrerWeight: u32 = 10;
    pub const XorBurnedWeight: u32 = 40;
    pub const XorIntoValBurnedWeight: u32 = 50;
    pub const BuyBackXSTPercent: Percent = Percent::from_percent(10);
}

// Ethereum bridge pallets

#[cfg(feature = "wip")] // EVM bridge
impl dispatch::Config<dispatch::Instance1> for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OriginOutput =
        bridge_types::types::CallOriginOutput<EVMChainId, H256, AdditionalEVMInboundData>;
    type Origin = RuntimeOrigin;
    type MessageId = bridge_types::types::MessageId;
    type Hashing = Keccak256;
    type Call = RuntimeCall;
    type CallFilter = EVMBridgeCallFilter;
    type WeightInfo = dispatch::weights::SubstrateWeight<Runtime>;
}

#[cfg(feature = "wip")]
use bridge_types::EVMChainId;

parameter_types! {
    pub const BridgeMaxMessagePayloadSize: u32 = 256;
    pub const BridgeMaxMessagesPerCommit: u32 = 20;
    pub const BridgeMaxTotalGasLimit: u64 = 5_000_000;
    pub const Decimals: u32 = 12;
}

#[cfg(feature = "wip")] // EVM bridge
pub struct FeeConverter;

#[cfg(feature = "wip")] // EVM bridge
impl Convert<U256, Balance> for FeeConverter {
    fn convert(amount: U256) -> Balance {
        common::eth::unwrap_balance(amount, Decimals::get())
            .expect("Should not panic unless runtime is misconfigured")
    }
}

parameter_types! {
    pub const FeeCurrency: AssetId = XOR;
    pub const ThisNetworkId: bridge_types::GenericNetworkId = bridge_types::GenericNetworkId::Sub(bridge_types::SubNetworkId::Mainnet);
}

#[cfg(feature = "wip")] // EVM bridge
impl bridge_inbound_channel::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Verifier = ethereum_light_client::Pallet<Runtime>;
    type MessageDispatch = Dispatch;
    type Hashing = Keccak256;
    type GasTracker = BridgeProxy;
    type MessageStatusNotifier = BridgeProxy;
    type FeeConverter = FeeConverter;
    type WeightInfo = ();
    type FeeAssetId = FeeCurrency;
    type OutboundChannel = BridgeOutboundChannel;
    type FeeTechAccountId = GetTrustlessBridgeFeesTechAccountId;
    type TreasuryTechAccountId = GetTreasuryTechAccountId;
    type ThisNetworkId = ThisNetworkId;
}

#[cfg(feature = "wip")] // EVM bridge
impl bridge_outbound_channel::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type MaxMessagePayloadSize = BridgeMaxMessagePayloadSize;
    type MaxMessagesPerCommit = BridgeMaxMessagesPerCommit;
    type MaxTotalGasLimit = BridgeMaxTotalGasLimit;
    type FeeCurrency = FeeCurrency;
    type FeeTechAccountId = GetTrustlessBridgeFeesTechAccountId;
    type MessageStatusNotifier = BridgeProxy;
    type AuxiliaryDigestHandler = LeafProvider;
    type ThisNetworkId = ThisNetworkId;
    type WeightInfo = ();
}

#[cfg(feature = "wip")] // EVM bridge
parameter_types! {
    pub const DescendantsUntilFinalized: u8 = 30;
    pub const VerifyPoW: bool = true;
    // Not as important as some essential transactions (e.g. im_online or similar ones)
    pub EthereumLightClientPriority: TransactionPriority = Perbill::from_percent(10) * TransactionPriority::max_value();
    // We don't want to have not relevant imports be stuck in transaction pool
    // for too long
    pub EthereumLightClientLongevity: TransactionLongevity = EPOCH_DURATION_IN_BLOCKS as u64;
}

#[cfg(feature = "wip")] // EVM bridge
impl ethereum_light_client::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type DescendantsUntilFinalized = DescendantsUntilFinalized;
    type VerifyPoW = VerifyPoW;
    type WeightInfo = ();
    type UnsignedPriority = EthereumLightClientPriority;
    type UnsignedLongevity = EthereumLightClientLongevity;
    type ImportSignature = Signature;
    type Submitter = <Signature as Verify>::Signer;
}

#[cfg(feature = "wip")] // EVM bridge
impl eth_app::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OutboundChannel = BridgeOutboundChannel;
    type CallOrigin = dispatch::EnsureAccount<
        bridge_types::types::CallOriginOutput<EVMChainId, H256, AdditionalEVMInboundData>,
    >;
    type MessageStatusNotifier = BridgeProxy;
    type AssetRegistry = BridgeProxy;
    type BalancePrecisionConverter = impls::BalancePrecisionConverter;
    type AssetIdConverter = sp_runtime::traits::ConvertInto;
    type BridgeAssetLocker = BridgeProxy;
    type WeightInfo = ();
}

#[cfg(feature = "wip")] // EVM bridge
impl erc20_app::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OutboundChannel = BridgeOutboundChannel;
    type CallOrigin = dispatch::EnsureAccount<
        bridge_types::types::CallOriginOutput<EVMChainId, H256, AdditionalEVMInboundData>,
    >;
    type AppRegistry = BridgeInboundChannel;
    type MessageStatusNotifier = BridgeProxy;
    type AssetRegistry = BridgeProxy;
    type BalancePrecisionConverter = impls::BalancePrecisionConverter;
    type AssetIdConverter = sp_runtime::traits::ConvertInto;
    type BridgeAssetLocker = BridgeProxy;
    type WeightInfo = ();
}

#[cfg(feature = "wip")] // EVM bridge
impl migration_app::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OutboundChannel = BridgeOutboundChannel;
    type WeightInfo = ();
}

parameter_types! {
    pub const GetReferenceAssetId: AssetId = GetDaiAssetId::get();
    pub const GetReferenceDexId: DEXId = 0;
}

impl bridge_proxy::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;

    #[cfg(feature = "wip")] // EVM bridge
    type ERC20App = ERC20App;
    #[cfg(not(feature = "wip"))] // EVM bridge
    type ERC20App = ();

    #[cfg(feature = "wip")] // EVM bridge
    type EthApp = EthApp;
    #[cfg(not(feature = "wip"))] // EVM bridge
    type EthApp = ();

    type HashiBridge = EthBridge;
    type ParachainApp = ParachainBridgeApp;
    type TimepointProvider = GenericTimepointProvider;
    type ReferencePriceProvider =
        liquidity_proxy::ReferencePriceProvider<Runtime, GetReferenceDexId, GetReferenceAssetId>;
    type ManagerOrigin = EitherOfDiverse<
        pallet_collective::EnsureProportionMoreThan<AccountId, TechnicalCollective, 2, 3>,
        EnsureRoot<AccountId>,
    >;
    type WeightInfo = ();
}

#[cfg(feature = "wip")] // Trustless substrate bridge
impl beefy_light_client::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Randomness = pallet_babe::RandomnessFromTwoEpochsAgo<Self>;
}

impl dispatch::Config<dispatch::Instance2> for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OriginOutput = bridge_types::types::CallOriginOutput<SubNetworkId, H256, ()>;
    type Origin = RuntimeOrigin;
    type MessageId = bridge_types::types::MessageId;
    type Hashing = Keccak256;
    type Call = DispatchableSubstrateBridgeCall;
    type CallFilter = SubstrateBridgeCallFilter;
    type WeightInfo = crate::weights::dispatch::WeightInfo<Runtime>;
}

impl substrate_bridge_channel::inbound::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type Verifier = MultiVerifier;
    type MessageDispatch = SubstrateDispatch;
    type UnsignedPriority = DataSignerPriority;
    type UnsignedLongevity = DataSignerLongevity;
    type MaxMessagePayloadSize = BridgeMaxMessagePayloadSize;
    type MaxMessagesPerCommit = BridgeMaxMessagesPerCommit;
    type ThisNetworkId = ThisNetworkId;
    type WeightInfo = crate::weights::substrate_inbound_channel::WeightInfo<Runtime>;
}

pub struct MultiVerifier;

#[derive(Clone, Debug, PartialEq, codec::Encode, codec::Decode, scale_info::TypeInfo)]
pub enum MultiProof {
    #[cfg(feature = "wip")] // Trustless substrate bridge
    #[codec(index = 0)]
    Beefy(<BeefyLightClient as Verifier>::Proof),
    #[codec(index = 1)]
    Multisig(<MultisigVerifier as Verifier>::Proof),
    /// This proof is only used for benchmarking purposes
    #[cfg(feature = "runtime-benchmarks")]
    #[codec(index = 2)]
    Empty,
}

impl Verifier for MultiVerifier {
    type Proof = MultiProof;

    fn verify(
        network_id: bridge_types::GenericNetworkId,
        message: H256,
        proof: &Self::Proof,
    ) -> frame_support::pallet_prelude::DispatchResult {
        match proof {
            #[cfg(feature = "wip")] // Trustless substrate bridge
            MultiProof::Beefy(proof) => BeefyLightClient::verify(network_id, message, proof),
            MultiProof::Multisig(proof) => MultisigVerifier::verify(network_id, message, proof),
            #[cfg(feature = "runtime-benchmarks")]
            MultiProof::Empty => Ok(()),
        }
    }

    fn verify_weight(proof: &Self::Proof) -> Weight {
        match proof {
            #[cfg(feature = "wip")] // Trustless substrate bridge
            MultiProof::Beefy(proof) => BeefyLightClient::verify_weight(proof),
            MultiProof::Multisig(proof) => MultisigVerifier::verify_weight(proof),
            #[cfg(feature = "runtime-benchmarks")]
            MultiProof::Empty => Default::default(),
        }
    }

    #[cfg(feature = "runtime-benchmarks")]
    fn valid_proof() -> Option<Self::Proof> {
        Some(MultiProof::Empty)
    }
}

pub struct GenericTimepointProvider;

impl bridge_types::traits::TimepointProvider for GenericTimepointProvider {
    fn get_timepoint() -> bridge_types::GenericTimepoint {
        bridge_types::GenericTimepoint::Sora(System::block_number())
    }
}

impl substrate_bridge_channel::outbound::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type MessageStatusNotifier = BridgeProxy;
    type MaxMessagePayloadSize = BridgeMaxMessagePayloadSize;
    type MaxMessagesPerCommit = BridgeMaxMessagesPerCommit;
    type AuxiliaryDigestHandler = LeafProvider;
    type AssetId = AssetId;
    type Balance = Balance;
    type TimepointProvider = GenericTimepointProvider;
    type ThisNetworkId = ThisNetworkId;
    type WeightInfo = crate::weights::substrate_outbound_channel::WeightInfo<Runtime>;
}

impl parachain_bridge_app::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OutboundChannel = SubstrateBridgeOutboundChannel;
    type CallOrigin =
        dispatch::EnsureAccount<bridge_types::types::CallOriginOutput<SubNetworkId, H256, ()>>;
    type MessageStatusNotifier = BridgeProxy;
    type AssetRegistry = BridgeProxy;
    type AccountIdConverter = sp_runtime::traits::Identity;
    type AssetIdConverter = sp_runtime::traits::ConvertInto;
    type BalancePrecisionConverter = impls::BalancePrecisionConverter;
    type BridgeAssetLocker = BridgeProxy;
    type WeightInfo = crate::weights::parachain_bridge_app::WeightInfo<Runtime>;
}

parameter_types! {
    pub const BridgeMaxPeers: u32 = 50;
    // Not as important as some essential transactions (e.g. im_online or similar ones)
    pub DataSignerPriority: TransactionPriority = Perbill::from_percent(10) * TransactionPriority::max_value();
    // We don't want to have not relevant imports be stuck in transaction pool
    // for too long
    pub DataSignerLongevity: TransactionLongevity = EPOCH_DURATION_IN_BLOCKS as u64;
}

impl bridge_data_signer::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type OutboundChannel = SubstrateBridgeOutboundChannel;
    type CallOrigin =
        dispatch::EnsureAccount<bridge_types::types::CallOriginOutput<SubNetworkId, H256, ()>>;
    type MaxPeers = BridgeMaxPeers;
    type UnsignedPriority = DataSignerPriority;
    type UnsignedLongevity = DataSignerLongevity;
    type WeightInfo = crate::weights::bridge_data_signer::WeightInfo<Runtime>;
}

impl multisig_verifier::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type CallOrigin =
        dispatch::EnsureAccount<bridge_types::types::CallOriginOutput<SubNetworkId, H256, ()>>;
    type OutboundChannel = SubstrateBridgeOutboundChannel;
    type MaxPeers = BridgeMaxPeers;
    type WeightInfo = crate::weights::multisig_verifier::WeightInfo<Runtime>;
}

construct_runtime! {
    pub enum Runtime where
        Block = Block,
        NodeBlock = opaque::Block,
        UncheckedExtrinsic = UncheckedExtrinsic
    {
        System: frame_system::{Pallet, Call, Storage, Config, Event<T>} = 0,

        Babe: pallet_babe::{Pallet, Call, Storage, Config, ValidateUnsigned} = 14,

        Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent} = 1,
        // Balances in native currency - XOR.
        Balances: pallet_balances::{Pallet, Storage, Config<T>, Event<T>} = 2,
        RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage} = 4,
        TransactionPayment: pallet_transaction_payment::{Pallet, Storage, Event<T>} = 5,
        Permissions: permissions::{Pallet, Call, Storage, Config<T>, Event<T>} = 6,
        Referrals: referrals::{Pallet, Call, Storage} = 7,
        Rewards: rewards::{Pallet, Call, Config<T>, Storage, Event<T>} = 8,
        XorFee: xor_fee::{Pallet, Call, Storage, Event<T>} = 9,
        BridgeMultisig: bridge_multisig::{Pallet, Call, Storage, Config<T>, Event<T>} = 10,
        Utility: pallet_utility::{Pallet, Call, Event} = 11,

        // Consensus and staking.
        Authorship: pallet_authorship::{Pallet, Storage} = 16,
        Staking: pallet_staking::{Pallet, Call, Config<T>, Storage, Event<T>} = 17,
        Offences: pallet_offences::{Pallet, Storage, Event} = 37,
        Historical: pallet_session_historical::{Pallet} = 13,
        Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 12,
        Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event} = 15,
        ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 36,

        // Non-native tokens - everything apart of XOR.
        Tokens: tokens::{Pallet, Storage, Config<T>, Event<T>} = 18,
        // Unified interface for XOR and non-native tokens.
        Currencies: currencies::{Pallet} = 19,
        TradingPair: trading_pair::{Pallet, Call, Storage, Config<T>, Event<T>} = 20,
        Assets: assets::{Pallet, Call, Storage, Config<T>, Event<T>} = 21,
        DEXManager: dex_manager::{Pallet, Storage, Config<T>} = 22,
        MulticollateralBondingCurvePool: multicollateral_bonding_curve_pool::{Pallet, Call, Storage, Config<T>, Event<T>} = 23,
        Technical: technical::{Pallet, Call, Config<T>, Event<T>, Storage} = 24,
        PoolXYK: pool_xyk::{Pallet, Call, Storage, Event<T>} = 25,
        LiquidityProxy: liquidity_proxy::{Pallet, Call, Event<T>} = 26,
        Council: pallet_collective::<Instance1>::{Pallet, Call, Storage, Origin<T>, Event<T>, Config<T>} = 27,
        TechnicalCommittee: pallet_collective::<Instance2>::{Pallet, Call, Storage, Origin<T>, Event<T>, Config<T>} = 28,
        Democracy: pallet_democracy::{Pallet, Call, Storage, Config<T>, Event<T>} = 29,
        DEXAPI: dex_api::{Pallet, Call, Storage, Config} = 30,
        EthBridge: eth_bridge::{Pallet, Call, Storage, Config<T>, Event<T>} = 31,
        PswapDistribution: pswap_distribution::{Pallet, Call, Storage, Config<T>, Event<T>} = 32,
        Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 33,
        Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 34,
        IrohaMigration: iroha_migration::{Pallet, Call, Storage, Config<T>, Event<T>} = 35,
        TechnicalMembership: pallet_membership::<Instance1>::{Pallet, Call, Storage, Event<T>, Config<T>} = 38,
        ElectionsPhragmen: pallet_elections_phragmen::{Pallet, Call, Storage, Event<T>, Config<T>} = 39,
        VestedRewards: vested_rewards::{Pallet, Call, Storage, Event<T>} = 40,
        Identity: pallet_identity::{Pallet, Call, Storage, Event<T>} = 41,
        Farming: farming::{Pallet, Storage} = 42,
        XSTPool: xst::{Pallet, Call, Storage, Config<T>, Event<T>} = 43,
        PriceTools: price_tools::{Pallet, Storage, Event<T>} = 44,
        CeresStaking: ceres_staking::{Pallet, Call, Storage, Event<T>} = 45,
        CeresLiquidityLocker: ceres_liquidity_locker::{Pallet, Call, Storage, Event<T>} = 46,
        CeresTokenLocker: ceres_token_locker::{Pallet, Call, Storage, Event<T>} = 47,
        CeresGovernancePlatform: ceres_governance_platform::{Pallet, Call, Storage, Event<T>} = 48,
        CeresLaunchpad: ceres_launchpad::{Pallet, Call, Storage, Event<T>} = 49,
        DemeterFarmingPlatform: demeter_farming_platform::{Pallet, Call, Storage, Event<T>} = 50,
        // Provides a semi-sorted list of nominators for staking.
        BagsList: pallet_bags_list::{Pallet, Call, Storage, Event<T>} = 51,
        ElectionProviderMultiPhase: pallet_election_provider_multi_phase::{Pallet, Call, Storage, Event<T>, ValidateUnsigned} = 52,
        Band: band::{Pallet, Call, Storage, Event<T>} = 53,
        OracleProxy: oracle_proxy::{Pallet, Call, Storage, Event<T>} = 54,
        HermesGovernancePlatform: hermes_governance_platform::{Pallet, Call, Storage, Event<T>} = 55,
        Preimage: pallet_preimage::{Pallet, Call, Storage, Event<T>} = 56,

        #[cfg(feature = "wip")] // order-book
        OrderBook: order_book::{Pallet, Call, Storage, Event<T>} = 57,

        // Trustless bridges
        #[cfg(feature = "wip")] // Trustless bridges
        Mmr: pallet_mmr::{Pallet, Storage} = 90,
        // In production needed for session keys
        Beefy: pallet_beefy::{Pallet, Config<T>, Storage} = 91,
        #[cfg(feature = "wip")] // Trustless bridges
        MmrLeaf: pallet_beefy_mmr::{Pallet, Storage} = 92,

        // Generic bridges pallets
        LeafProvider: leaf_provider::{Pallet, Storage, Event<T>} = 99,
        BridgeProxy: bridge_proxy::{Pallet, Call, Storage, Event} = 103,

        // Trustless EVM bridge
        #[cfg(feature = "wip")] // EVM bridge
        EthereumLightClient: ethereum_light_client::{Pallet, Call, Storage, Event<T>, Config, ValidateUnsigned} = 93,
        #[cfg(feature = "wip")] // EVM bridge
        BridgeInboundChannel: bridge_inbound_channel::{Pallet, Call, Config, Storage, Event<T>} = 96,
        #[cfg(feature = "wip")] // EVM bridge
        BridgeOutboundChannel: bridge_outbound_channel::{Pallet, Config<T>, Storage, Event<T>} = 97,
        #[cfg(feature = "wip")] // EVM bridge
        Dispatch: dispatch::<Instance1>::{Pallet, Storage, Event<T>, Origin<T>} = 98,
        #[cfg(feature = "wip")] // EVM bridge
        EthApp: eth_app::{Pallet, Call, Storage, Event<T>, Config<T>} = 100,
        #[cfg(feature = "wip")] // EVM bridge
        ERC20App: erc20_app::{Pallet, Call, Storage, Event<T>, Config<T>} = 101,
        #[cfg(feature = "wip")] // EVM bridge
        MigrationApp: migration_app::{Pallet, Call, Storage, Event<T>, Config} = 102,

        // Trustless substrate bridge
        #[cfg(feature = "wip")] // Trustless substrate bridge
        BeefyLightClient: beefy_light_client::{Pallet, Call, Storage, Event<T>, Config} = 104,

        // Federated substrate bridge
        SubstrateBridgeInboundChannel: substrate_bridge_channel::inbound::{Pallet, Call, Storage, Event<T>, ValidateUnsigned} = 106,
        SubstrateBridgeOutboundChannel: substrate_bridge_channel::outbound::{Pallet, Config<T>, Storage, Event<T>} = 107,
        SubstrateDispatch: dispatch::<Instance2>::{Pallet, Storage, Event<T>, Origin<T>} = 108,
        ParachainBridgeApp: parachain_bridge_app::{Pallet, Config<T>, Storage, Event<T>, Call} = 109,
        BridgeDataSigner: bridge_data_signer::{Pallet, Storage, Event<T>, Call, ValidateUnsigned} = 110,
        MultisigVerifier: multisig_verifier::{Pallet, Storage, Event<T>, Call, Config} = 111,

        // Dev
        #[cfg(feature = "private-net")]
        Sudo: pallet_sudo::{Pallet, Call, Storage, Config<T>, Event<T>} = 3,

        // Available only for test net
        #[cfg(feature = "private-net")]
        Faucet: faucet::{Pallet, Call, Config<T>, Event<T>} = 80,
        #[cfg(all(feature = "private-net", feature = "wip"))] // order-book
        QATools: qa_tools::{Pallet, Call} = 112,
    }
}

// This is needed, because the compiler automatically places `Serialize` bound
// when `derive` is used, but the method is never actually used
#[cfg(feature = "std")]
impl Serialize for Runtime {
    fn serialize<S>(
        &self,
        _serializer: S,
    ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        unreachable!("we never serialize runtime; qed")
    }
}

/// The address format for describing accounts.
pub type Address = AccountId;
/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Block type as expected by this runtime.
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// A Block signed with a Justification
pub type SignedBlock = generic::SignedBlock<Block>;
/// BlockId type as expected by this runtime.
pub type BlockId = generic::BlockId<Block>;
/// The SignedExtension to the basic transaction logic.
pub type SignedExtra = (
    frame_system::CheckSpecVersion<Runtime>,
    frame_system::CheckTxVersion<Runtime>,
    frame_system::CheckGenesis<Runtime>,
    frame_system::CheckEra<Runtime>,
    frame_system::CheckNonce<Runtime>,
    frame_system::CheckWeight<Runtime>,
    ChargeTransactionPayment<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
pub type UncheckedExtrinsic =
    generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
    Runtime,
    Block,
    frame_system::ChainContext<Runtime>,
    Runtime,
    AllPalletsWithSystem,
    migrations::Migrations,
>;

#[cfg(feature = "wip")] // Trustless bridges
pub type MmrHashing = <Runtime as pallet_mmr::Config>::Hashing;

impl_runtime_apis! {
    impl sp_api::Core<Block> for Runtime {
        fn version() -> RuntimeVersion {
            VERSION
        }

        fn execute_block(block: Block) {
            Executive::execute_block(block)
        }

        fn initialize_block(header: &<Block as BlockT>::Header) {
            Executive::initialize_block(header)
        }
    }

    impl sp_api::Metadata<Block> for Runtime {
        fn metadata() -> OpaqueMetadata {
            OpaqueMetadata::new(Runtime::metadata().into())
        }
    }

    impl sp_block_builder::BlockBuilder<Block> for Runtime {
        fn apply_extrinsic(
            extrinsic: <Block as BlockT>::Extrinsic,
        ) -> ApplyExtrinsicResult {
            Executive::apply_extrinsic(extrinsic)
        }

        fn finalize_block() -> <Block as BlockT>::Header {
            Executive::finalize_block()
        }

        fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
            data.create_extrinsics()
        }

        fn check_inherents(block: Block, data: sp_inherents::InherentData) -> sp_inherents::CheckInherentsResult {
            data.check_extrinsics(&block)
        }

        // fn random_seed() -> <Block as BlockT>::Hash {
        //     RandomnessCollectiveFlip::random_seed()
        // }
    }

    impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {
        fn validate_transaction(
            source: TransactionSource,
            tx: <Block as BlockT>::Extrinsic,
            block_hash: <Block as BlockT>::Hash,
        ) -> TransactionValidity {
            Executive::validate_transaction(source, tx, block_hash)
        }
    }

    impl sp_offchain::OffchainWorkerApi<Block> for Runtime {
        fn offchain_worker(header: &<Block as BlockT>::Header) {
            Executive::offchain_worker(header)
        }
    }

    impl sp_session::SessionKeys<Block> for Runtime {
        fn decode_session_keys(
            encoded: Vec<u8>,
        ) -> Option<Vec<(Vec<u8>, sp_core::crypto::KeyTypeId)>> {
            opaque::SessionKeys::decode_into_raw_public_keys(&encoded)
        }

        fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
            opaque::SessionKeys::generate(seed)
        }
    }

    impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<
        Block,
        Balance,
    > for Runtime {
        fn query_info(uxt: <Block as BlockT>::Extrinsic, len: u32) -> pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo<Balance> {
            let call = &uxt.function;
            XorFee::query_info(&uxt, call, len)
        }

        fn query_fee_details(uxt: <Block as BlockT>::Extrinsic, len: u32) -> pallet_transaction_payment_rpc_runtime_api::FeeDetails<Balance> {
            let call = &uxt.function;
            XorFee::query_fee_details(&uxt, call, len)
        }

        fn query_weight_to_fee(weight: Weight) -> Balance {
            TransactionPayment::weight_to_fee(weight)
        }

        fn query_length_to_fee(length: u32) -> Balance {
            TransactionPayment::length_to_fee(length)
        }
    }

    impl dex_manager_runtime_api::DEXManagerAPI<Block, DEXId> for Runtime {
        fn list_dex_ids() -> Vec<DEXId> {
            DEXManager::list_dex_ids()
        }
    }

    impl dex_runtime_api::DEXAPI<
        Block,
        AssetId,
        DEXId,
        Balance,
        LiquiditySourceType,
        SwapVariant,
    > for Runtime {
        #[cfg_attr(not(feature = "private-net"), allow(unused))]
        fn quote(
            dex_id: DEXId,
            liquidity_source_type: LiquiditySourceType,
            input_asset_id: AssetId,
            output_asset_id: AssetId,
            desired_input_amount: BalanceWrapper,
            swap_variant: SwapVariant,
        ) -> Option<dex_runtime_api::SwapOutcomeInfo<Balance>> {
            #[cfg(feature = "private-net")]
            {
                DEXAPI::quote(
                    &LiquiditySourceId::new(dex_id, liquidity_source_type),
                    &input_asset_id,
                    &output_asset_id,
                    QuoteAmount::with_variant(swap_variant, desired_input_amount.into()),
                    true,
                ).ok().map(|(sa, _)| dex_runtime_api::SwapOutcomeInfo::<Balance> { amount: sa.amount, fee: sa.fee})
            }
            #[cfg(not(feature = "private-net"))]
            {
                // Mainnet should not be able to access liquidity source quote directly, to avoid arbitrage exploits.
                None
            }
        }

        fn can_exchange(
            dex_id: DEXId,
            liquidity_source_type: LiquiditySourceType,
            input_asset_id: AssetId,
            output_asset_id: AssetId,
        ) -> bool {
            DEXAPI::can_exchange(
                &LiquiditySourceId::new(dex_id, liquidity_source_type),
                &input_asset_id,
                &output_asset_id,
            )
        }

        fn list_supported_sources() -> Vec<LiquiditySourceType> {
            DEXAPI::get_supported_types()
        }
    }

    impl trading_pair_runtime_api::TradingPairAPI<Block, DEXId, common::TradingPair<AssetId>, AssetId, LiquiditySourceType> for Runtime {
        fn list_enabled_pairs(dex_id: DEXId) -> Vec<common::TradingPair<AssetId>> {
            // TODO: error passing PR fixes this crunch return
            TradingPair::list_trading_pairs(&dex_id).unwrap_or(Vec::new())
        }

        fn is_pair_enabled(dex_id: DEXId, asset_id_a: AssetId, asset_id_b: AssetId) -> bool {
            // TODO: error passing PR fixes this crunch return
            TradingPair::is_trading_pair_enabled(&dex_id, &asset_id_a, &asset_id_b).unwrap_or(false)
                || TradingPair::is_trading_pair_enabled(&dex_id, &asset_id_b, &asset_id_a).unwrap_or(false)
        }

        fn list_enabled_sources_for_pair(
            dex_id: DEXId,
            base_asset_id: AssetId,
            target_asset_id: AssetId,
        ) -> Vec<LiquiditySourceType> {
            // TODO: error passing PR fixes this crunch return
            TradingPair::list_enabled_sources_for_trading_pair(&dex_id, &base_asset_id, &target_asset_id).map(|bts| bts.into_iter().collect::<Vec<_>>()).unwrap_or(Vec::new())
        }

        fn is_source_enabled_for_pair(
            dex_id: DEXId,
            base_asset_id: AssetId,
            target_asset_id: AssetId,
            source_type: LiquiditySourceType,
        ) -> bool {
            // TODO: error passing PR fixes this crunch return
            TradingPair::is_source_enabled_for_trading_pair(&dex_id, &base_asset_id, &target_asset_id, source_type).unwrap_or(false)
        }
    }

    impl assets_runtime_api::AssetsAPI<Block, AccountId, AssetId, Balance, AssetSymbol, AssetName, BalancePrecision, ContentSource, Description> for Runtime {
        fn free_balance(account_id: AccountId, asset_id: AssetId) -> Option<assets_runtime_api::BalanceInfo<Balance>> {
            Assets::free_balance(&asset_id, &account_id).ok().map(|balance|
                assets_runtime_api::BalanceInfo::<Balance> {
                    balance: balance.clone(),
                }
            )
        }

        fn usable_balance(account_id: AccountId, asset_id: AssetId) -> Option<assets_runtime_api::BalanceInfo<Balance>> {
            let usable_balance = if asset_id == <Runtime as currencies::Config>::GetNativeCurrencyId::get() {
                Balances::usable_balance(account_id)
            } else {
                let account_data = Tokens::accounts(account_id, asset_id);
                account_data.free.saturating_sub(account_data.frozen)
            };
            Some(assets_runtime_api::BalanceInfo { balance: usable_balance })
        }

        fn total_balance(account_id: AccountId, asset_id: AssetId) -> Option<assets_runtime_api::BalanceInfo<Balance>> {
            Assets::total_balance(&asset_id, &account_id).ok().map(|balance|
                assets_runtime_api::BalanceInfo::<Balance> {
                    balance: balance.clone(),
                }
            )
        }

        fn total_supply(asset_id: AssetId) -> Option<assets_runtime_api::BalanceInfo<Balance>> {
            Assets::total_issuance(&asset_id).ok().map(|balance|
                assets_runtime_api::BalanceInfo::<Balance> {
                    balance: balance.clone(),
                }
            )
        }

        fn list_asset_ids() -> Vec<AssetId> {
            Assets::list_registered_asset_ids()
        }

        fn list_asset_infos() -> Vec<assets_runtime_api::AssetInfo<AssetId, AssetSymbol, AssetName, u8, ContentSource, Description>> {
            Assets::list_registered_asset_infos().into_iter().map(|(asset_id, symbol, name, precision, is_mintable, content_source, description)|
                assets_runtime_api::AssetInfo::<AssetId, AssetSymbol, AssetName, BalancePrecision, ContentSource, Description> {
                    asset_id,
                    symbol,
                    name,
                    precision,
                    is_mintable,
                    content_source,
                    description
                }
            ).collect()
        }

        fn get_asset_info(asset_id: AssetId) -> Option<assets_runtime_api::AssetInfo<AssetId, AssetSymbol, AssetName, BalancePrecision, ContentSource, Description>> {
            let (symbol, name, precision, is_mintable, content_source, description) = Assets::get_asset_info(&asset_id);
            Some(assets_runtime_api::AssetInfo::<AssetId, AssetSymbol, AssetName, BalancePrecision, ContentSource, Description> {
                asset_id,
                symbol,
                name,
                precision,
                is_mintable,
                content_source,
                description
            })
        }

        fn get_asset_content_src(asset_id: AssetId) -> Option<ContentSource> {
            Assets::get_asset_content_src(&asset_id)
        }
    }

    impl
        eth_bridge_runtime_api::EthBridgeRuntimeApi<
            Block,
            sp_core::H256,
            SignatureParams,
            AccountId,
            AssetKind,
            AssetId,
            sp_core::H160,
            OffchainRequest<Runtime>,
            RequestStatus,
            OutgoingRequestEncoded,
            NetworkId,
            BalancePrecision,
        > for Runtime
    {
        fn get_requests(
            hashes: Vec<sp_core::H256>,
            network_id: Option<NetworkId>,
            redirect_finished_load_requests: bool,
        ) -> Result<
            Vec<(
                OffchainRequest<Runtime>,
                RequestStatus,
            )>,
            DispatchError,
        > {
            EthBridge::get_requests(&hashes, network_id, redirect_finished_load_requests)
        }

        fn get_approved_requests(
            hashes: Vec<sp_core::H256>,
            network_id: Option<NetworkId>
        ) -> Result<
            Vec<(
                OutgoingRequestEncoded,
                Vec<SignatureParams>,
            )>,
            DispatchError,
        > {
            EthBridge::get_approved_requests(&hashes, network_id)
        }

        fn get_approvals(
            hashes: Vec<sp_core::H256>,
            network_id: Option<NetworkId>
        ) -> Result<Vec<Vec<SignatureParams>>, DispatchError> {
            EthBridge::get_approvals(&hashes, network_id)
        }

        fn get_account_requests(account_id: AccountId, status_filter: Option<RequestStatus>) -> Result<Vec<(NetworkId, sp_core::H256)>, DispatchError> {
            EthBridge::get_account_requests(&account_id, status_filter)
        }

        fn get_registered_assets(
            network_id: Option<NetworkId>
        ) -> Result<Vec<(
                AssetKind,
                (AssetId, BalancePrecision),
                Option<(sp_core::H160, BalancePrecision)
        >)>, DispatchError> {
            EthBridge::get_registered_assets(network_id)
        }
    }

    impl iroha_migration_runtime_api::IrohaMigrationAPI<Block> for Runtime {
        fn needs_migration(iroha_address: String) -> bool {
            IrohaMigration::needs_migration(&iroha_address)
        }
    }

    #[cfg(feature = "wip")] // Trustless substrate bridge
    impl beefy_light_client_runtime_api::BeefyLightClientAPI<Block, beefy_light_client::BitField> for Runtime {
        fn get_random_bitfield(network_id: SubNetworkId, prior: beefy_light_client::BitField, num_of_validators: u32) -> beefy_light_client::BitField {
            let len = prior.len() as usize;
            BeefyLightClient::create_random_bit_field(network_id, prior, num_of_validators).unwrap_or(beefy_light_client::BitField::with_capacity(len))
        }
    }

    impl liquidity_proxy_runtime_api::LiquidityProxyAPI<
        Block,
        DEXId,
        AssetId,
        Balance,
        SwapVariant,
        LiquiditySourceType,
        FilterMode,
    > for Runtime {
        fn quote(
            dex_id: DEXId,
            input_asset_id: AssetId,
            output_asset_id: AssetId,
            amount: BalanceWrapper,
            swap_variant: SwapVariant,
            selected_source_types: Vec<LiquiditySourceType>,
            filter_mode: FilterMode,
        ) -> Option<liquidity_proxy_runtime_api::SwapOutcomeInfo<Balance, AssetId>> {
            if LiquidityProxy::is_forbidden_filter(&input_asset_id, &output_asset_id, &selected_source_types, &filter_mode) {
                return None;
            }

            LiquidityProxy::inner_quote(
                dex_id,
                &input_asset_id,
                &output_asset_id,
                QuoteAmount::with_variant(swap_variant, amount.into()),
                LiquiditySourceFilter::with_mode(dex_id, filter_mode, selected_source_types),
                false,
                true,
            ).ok().map(|(quote_info, _)| liquidity_proxy_runtime_api::SwapOutcomeInfo::<Balance, AssetId> {
                amount: quote_info.outcome.amount,
                amount_without_impact: quote_info.amount_without_impact.unwrap_or(0),
                fee: quote_info.outcome.fee,
                rewards: quote_info.rewards.into_iter()
                                .map(|(amount, currency, reason)| liquidity_proxy_runtime_api::RewardsInfo::<Balance, AssetId> {
                                    amount,
                                    currency,
                                    reason
                                }).collect(),
                route: quote_info.path
                })
        }

        fn is_path_available(
            dex_id: DEXId,
            input_asset_id: AssetId,
            output_asset_id: AssetId
        ) -> bool {
            LiquidityProxy::is_path_available(
                dex_id, input_asset_id, output_asset_id
            ).unwrap_or(false)
        }

        fn list_enabled_sources_for_path(
            dex_id: DEXId,
            input_asset_id: AssetId,
            output_asset_id: AssetId,
        ) -> Vec<LiquiditySourceType> {
            LiquidityProxy::list_enabled_sources_for_path_with_xyk_forbidden(
                dex_id, input_asset_id, output_asset_id
            ).unwrap_or(Vec::new())
        }
    }

    impl oracle_proxy_runtime_api::OracleProxyAPI<
        Block,
        Symbol,
        ResolveTime
    > for Runtime {
        fn quote(symbol: Symbol) -> Result<Option<oracle_proxy_runtime_api::RateInfo>, DispatchError>  {
            let rate_wrapped = <
                OracleProxy as common::DataFeed<Symbol, common::Rate, ResolveTime>
            >::quote(&symbol);
            match rate_wrapped {
                Ok(rate) => Ok(rate.map(|rate| oracle_proxy_runtime_api::RateInfo{
                    value: rate.value,
                    last_updated: rate.last_updated
                })),
                Err(e) => Err(e)
            }
        }

        fn list_enabled_symbols() -> Result<Vec<(Symbol, ResolveTime)>, DispatchError> {
            <
                OracleProxy as common::DataFeed<Symbol, common::Rate, ResolveTime>
            >::list_enabled_symbols()
        }
    }

    impl pswap_distribution_runtime_api::PswapDistributionAPI<
        Block,
        AccountId,
        Balance,
    > for Runtime {
        fn claimable_amount(
            account_id: AccountId,
        ) -> pswap_distribution_runtime_api::BalanceInfo<Balance> {
            let claimable = PswapDistribution::claimable_amount(&account_id).unwrap_or(0);
            pswap_distribution_runtime_api::BalanceInfo::<Balance> {
                balance: claimable
            }
        }
    }

    impl rewards_runtime_api::RewardsAPI<Block, sp_core::H160, Balance> for Runtime {
        fn claimables(eth_address: sp_core::H160) -> Vec<rewards_runtime_api::BalanceInfo<Balance>> {
            Rewards::claimables(&eth_address).into_iter().map(|balance| rewards_runtime_api::BalanceInfo::<Balance> { balance }).collect()
        }
    }

    impl sp_consensus_babe::BabeApi<Block> for Runtime {
            fn configuration() -> sp_consensus_babe::BabeConfiguration {
                    // The choice of `c` parameter (where `1 - c` represents the
                    // probability of a slot being empty), is done in accordance to the
                    // slot duration and expected target block time, for safely
                    // resisting network delays of maximum two seconds.
                    // <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
                    sp_consensus_babe::BabeConfiguration {
                            slot_duration: Babe::slot_duration(),
                            epoch_length: EpochDuration::get(),
                            c: PRIMARY_PROBABILITY,
                            authorities: Babe::authorities().to_vec(),
                            randomness: Babe::randomness(),
                            allowed_slots: sp_consensus_babe::AllowedSlots::PrimaryAndSecondaryVRFSlots,
                    }
            }

            fn current_epoch() -> sp_consensus_babe::Epoch {
                Babe::current_epoch()
            }

            fn current_epoch_start() -> sp_consensus_babe::Slot {
                Babe::current_epoch_start()
            }

            fn next_epoch() -> sp_consensus_babe::Epoch {
                Babe::next_epoch()
            }

            fn generate_key_ownership_proof(
                    _slot_number: sp_consensus_babe::Slot,
                    authority_id: sp_consensus_babe::AuthorityId,
            ) -> Option<sp_consensus_babe::OpaqueKeyOwnershipProof> {
                    use codec::Encode;
                    Historical::prove((sp_consensus_babe::KEY_TYPE, authority_id))
                            .map(|p| p.encode())
                            .map(sp_consensus_babe::OpaqueKeyOwnershipProof::new)
            }

            fn submit_report_equivocation_unsigned_extrinsic(
                    equivocation_proof: sp_consensus_babe::EquivocationProof<<Block as BlockT>::Header>,
                    key_owner_proof: sp_consensus_babe::OpaqueKeyOwnershipProof,
            ) -> Option<()> {
                    let key_owner_proof = key_owner_proof.decode()?;
                    Babe::submit_unsigned_equivocation_report(
                            equivocation_proof,
                            key_owner_proof,
                    )
            }
    }

    impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Index> for Runtime {
        fn account_nonce(account: AccountId) -> Index {
            System::account_nonce(account)
        }
    }

    // For BEEFY gadget
    impl sp_beefy::BeefyApi<Block> for Runtime {
        fn validator_set() -> Option<sp_beefy::ValidatorSet<BeefyId>> {
            #[cfg(not(feature = "wip"))] // Trustless bridges
            return None;

            #[cfg(feature = "wip")] // Trustless bridges
            Beefy::validator_set()
        }
    }

    impl mmr::MmrApi<Block, Hash, BlockNumber> for Runtime {
        fn mmr_root() -> Result<Hash, mmr::Error> {
            #[cfg(not(feature = "wip"))] // Trustless bridges
            return Err(mmr::Error::PalletNotIncluded);

            #[cfg(feature = "wip")] // Trustless bridges
            Ok(Mmr::mmr_root())
        }

        fn mmr_leaf_count() -> Result<mmr::LeafIndex, mmr::Error> {
            #[cfg(not(feature = "wip"))] // Trustless bridges
            return Err(mmr::Error::PalletNotIncluded);

            #[cfg(feature = "wip")] // Trustless bridges
            Ok(Mmr::mmr_leaves())
        }

        fn generate_proof(
            _block_numbers: Vec<BlockNumber>,
            _best_known_block_number: Option<BlockNumber>,
        ) -> Result<(Vec<mmr::EncodableOpaqueLeaf>, mmr::Proof<Hash>), mmr::Error> {
            #[cfg(not(feature = "wip"))] // Trustless bridges
            return Err(mmr::Error::PalletNotIncluded);

            #[cfg(feature = "wip")] // Trustless bridges
            Mmr::generate_proof(_block_numbers, _best_known_block_number).map(
                |(leaves, proof)| {
                    (
                        leaves
                            .into_iter()
                            .map(|leaf| mmr::EncodableOpaqueLeaf::from_leaf(&leaf))
                            .collect(),
                        proof,
                    )
                },
            )
        }

        fn verify_proof(_leaves: Vec<mmr::EncodableOpaqueLeaf>, _proof: mmr::Proof<Hash>)
            -> Result<(), mmr::Error>
        {
            #[cfg(not(feature = "wip"))] // Trustless bridges
            return Err(mmr::Error::PalletNotIncluded);

            #[cfg(feature = "wip")] // Trustless bridges
            {
                pub type MmrLeaf = <<Runtime as pallet_mmr::Config>::LeafData as mmr::LeafDataProvider>::LeafData;
                let leaves = _leaves.into_iter().map(|leaf|
                    leaf.into_opaque_leaf()
                    .try_decode()
                    .ok_or(mmr::Error::Verify)).collect::<Result<Vec<MmrLeaf>, mmr::Error>>()?;
                Mmr::verify_leaves(leaves, _proof)
            }
        }

        fn verify_proof_stateless(
            _root: Hash,
            _leaves: Vec<mmr::EncodableOpaqueLeaf>,
            _proof: mmr::Proof<Hash>
        ) -> Result<(), mmr::Error> {
            #[cfg(not(feature = "wip"))] // Trustless bridges
            return Err(mmr::Error::PalletNotIncluded);

            #[cfg(feature = "wip")] // Trustless bridges
            {
                let nodes = _leaves.into_iter().map(|leaf|mmr::DataOrHash::Data(leaf.into_opaque_leaf())).collect();
                pallet_mmr::verify_leaves_proof::<MmrHashing, _>(_root, nodes, _proof)
            }
        }
    }

    impl fg_primitives::GrandpaApi<Block> for Runtime {
        fn grandpa_authorities() -> GrandpaAuthorityList {
            Grandpa::grandpa_authorities()
        }

        fn current_set_id() -> fg_primitives::SetId {
            Grandpa::current_set_id()
        }

        fn submit_report_equivocation_unsigned_extrinsic(
            equivocation_proof: fg_primitives::EquivocationProof<
                <Block as BlockT>::Hash,
                NumberFor<Block>,
            >,
            key_owner_proof: fg_primitives::OpaqueKeyOwnershipProof,
        ) -> Option<()> {
            let key_owner_proof = key_owner_proof.decode()?;
            Grandpa::submit_unsigned_equivocation_report(
                equivocation_proof,
                key_owner_proof,
            )
        }

        fn generate_key_ownership_proof(
            _set_id: fg_primitives::SetId,
            authority_id: GrandpaId,
        ) -> Option<fg_primitives::OpaqueKeyOwnershipProof> {
            use codec::Encode;
            Historical::prove((fg_primitives::KEY_TYPE, authority_id))
                .map(|p| p.encode())
                .map(fg_primitives::OpaqueKeyOwnershipProof::new)
        }
    }

    impl leaf_provider_runtime_api::LeafProviderAPI<Block> for Runtime {
        fn latest_digest() -> Option<bridge_types::types::AuxiliaryDigest> {
                LeafProvider::latest_digest().map(|logs| bridge_types::types::AuxiliaryDigest{ logs })
        }

    }

    impl bridge_proxy_runtime_api::BridgeProxyAPI<Block, AssetId> for Runtime {
        fn list_apps() -> Vec<bridge_types::types::BridgeAppInfo> {
            BridgeProxy::list_apps()
        }

        fn list_supported_assets(network_id: bridge_types::GenericNetworkId) -> Vec<bridge_types::types::BridgeAssetInfo> {
            BridgeProxy::list_supported_assets(network_id)
        }
    }

    #[cfg(feature = "runtime-benchmarks")]
    impl frame_benchmarking::Benchmark<Block> for Runtime {
        fn benchmark_metadata(extra: bool) -> (
            Vec<frame_benchmarking::BenchmarkList>,
            Vec<frame_support::traits::StorageInfo>,
        ) {
            use frame_benchmarking::{list_benchmark, Benchmarking, BenchmarkList};
            use frame_support::traits::StorageInfoTrait;

            use liquidity_proxy_benchmarking::Pallet as LiquidityProxyBench;
            use pool_xyk_benchmarking::Pallet as XYKPoolBench;
            use pswap_distribution_benchmarking::Pallet as PswapDistributionBench;
            use ceres_liquidity_locker_benchmarking::Pallet as CeresLiquidityLockerBench;
            use demeter_farming_platform_benchmarking::Pallet as DemeterFarmingPlatformBench;
            use xst_benchmarking::Pallet as XSTPoolBench;

            let mut list = Vec::<BenchmarkList>::new();

            list_benchmark!(list, extra, assets, Assets);
            #[cfg(feature = "private-net")]
            list_benchmark!(list, extra, faucet, Faucet);
            list_benchmark!(list, extra, farming, Farming);
            list_benchmark!(list, extra, iroha_migration, IrohaMigration);
            list_benchmark!(list, extra, liquidity_proxy, LiquidityProxyBench::<Runtime>);
            list_benchmark!(list, extra, multicollateral_bonding_curve_pool, MulticollateralBondingCurvePool);
            list_benchmark!(list, extra, pswap_distribution, PswapDistributionBench::<Runtime>);
            list_benchmark!(list, extra, rewards, Rewards);
            list_benchmark!(list, extra, trading_pair, TradingPair);
            list_benchmark!(list, extra, pool_xyk, XYKPoolBench::<Runtime>);
            list_benchmark!(list, extra, eth_bridge, EthBridge);
            list_benchmark!(list, extra, vested_rewards, VestedRewards);
            list_benchmark!(list, extra, price_tools, PriceTools);
            list_benchmark!(list, extra, xor_fee, XorFee);
            list_benchmark!(list, extra, referrals, Referrals);
            list_benchmark!(list, extra, ceres_staking, CeresStaking);
            list_benchmark!(list, extra, hermes_governance_platform, HermesGovernancePlatform);
            list_benchmark!(list, extra, ceres_liquidity_locker, CeresLiquidityLockerBench::<Runtime>);
            list_benchmark!(list, extra, ceres_token_locker, CeresTokenLocker);
            list_benchmark!(list, extra, ceres_governance_platform, CeresGovernancePlatform);
            list_benchmark!(list, extra, ceres_launchpad, CeresLaunchpad);
            list_benchmark!(list, extra, demeter_farming_platform, DemeterFarmingPlatformBench::<Runtime>);
            list_benchmark!(list, extra, band, Band);
            list_benchmark!(list, extra, xst, XSTPoolBench::<Runtime>);
            list_benchmark!(list, extra, oracle_proxy, OracleProxy);

            #[cfg(feature = "wip")] // order-book
            list_benchmark!(list, extra, order_book, OrderBook);

            // Trustless bridge
            #[cfg(feature = "wip")] // EVM bridge
            list_benchmark!(list, extra, ethereum_light_client, EthereumLightClient);
            #[cfg(feature = "wip")] // EVM bridge
            list_benchmark!(list, extra, bridge_inbound_channel, BridgeInboundChannel);
            #[cfg(feature = "wip")] // EVM bridge
            list_benchmark!(list, extra, bridge_outbound_channel, BridgeOutboundChannel);
            #[cfg(feature = "wip")] // EVM bridge
            list_benchmark!(list, extra, eth_app, EthApp);
            #[cfg(feature = "wip")] // EVM bridge
            list_benchmark!(list, extra, erc20_app, ERC20App);
            #[cfg(feature = "wip")] // EVM bridge
            list_benchmark!(list, extra, migration_app, MigrationApp);

            list_benchmark!(list, extra, evm_bridge_proxy, BridgeProxy);
            // Dispatch pallet benchmarks is strictly linked to EVM bridge params
            // TODO: fix
            #[cfg(feature = "wip")] // EVM bridge
            list_benchmark!(list, extra, dispatch, Dispatch);
            list_benchmark!(list, extra, substrate_bridge_channel::inbound, SubstrateBridgeInboundChannel);
            list_benchmark!(list, extra, substrate_bridge_channel::outbound, SubstrateBridgeOutboundChannel);
            list_benchmark!(list, extra, parachain_bridge_app, ParachainBridgeApp);
            list_benchmark!(list, extra, bridge_data_signer, BridgeDataSigner);
            list_benchmark!(list, extra, multisig_verifier, MultisigVerifier);

            let storage_info = AllPalletsWithSystem::storage_info();

            return (list, storage_info)
        }

        fn dispatch_benchmark(
            config: frame_benchmarking::BenchmarkConfig
        ) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
            use frame_benchmarking::{Benchmarking, BenchmarkBatch, add_benchmark, TrackedStorageKey};

            use liquidity_proxy_benchmarking::Pallet as LiquidityProxyBench;
            use pool_xyk_benchmarking::Pallet as XYKPoolBench;
            use pswap_distribution_benchmarking::Pallet as PswapDistributionBench;
            use ceres_liquidity_locker_benchmarking::Pallet as CeresLiquidityLockerBench;
            use demeter_farming_platform_benchmarking::Pallet as DemeterFarmingPlatformBench;
            use xst_benchmarking::Pallet as XSTPoolBench;

            impl liquidity_proxy_benchmarking::Config for Runtime {}
            impl pool_xyk_benchmarking::Config for Runtime {}
            impl pswap_distribution_benchmarking::Config for Runtime {}
            impl ceres_liquidity_locker_benchmarking::Config for Runtime {}
            impl xst_benchmarking::Config for Runtime {}

            let whitelist: Vec<TrackedStorageKey> = vec![
                // Block Number
                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(),
                // Total Issuance
                hex_literal::hex!("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80").to_vec().into(),
                // Execution Phase
                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7ff553b5a9862a516939d82b3d3d8661a").to_vec().into(),
                // Event Count
                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850").to_vec().into(),
                // System Events
                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7").to_vec().into(),
                // Treasury Account
                hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef7b99d880ec681799c0cf30e8886371da95ecffd7b6c0f78751baa9d281e0bfa3a6d6f646c70792f74727372790000000000000000000000000000000000000000").to_vec().into(),
            ];

            let mut batches = Vec::<BenchmarkBatch>::new();
            let params = (&config, &whitelist);

            add_benchmark!(params, batches, assets, Assets);
            #[cfg(feature = "private-net")]
            add_benchmark!(params, batches, faucet, Faucet);
            add_benchmark!(params, batches, farming, Farming);
            add_benchmark!(params, batches, iroha_migration, IrohaMigration);
            add_benchmark!(params, batches, liquidity_proxy, LiquidityProxyBench::<Runtime>);
            add_benchmark!(params, batches, multicollateral_bonding_curve_pool, MulticollateralBondingCurvePool);
            add_benchmark!(params, batches, pswap_distribution, PswapDistributionBench::<Runtime>);
            add_benchmark!(params, batches, rewards, Rewards);
            add_benchmark!(params, batches, trading_pair, TradingPair);
            add_benchmark!(params, batches, pool_xyk, XYKPoolBench::<Runtime>);
            add_benchmark!(params, batches, eth_bridge, EthBridge);
            add_benchmark!(params, batches, vested_rewards, VestedRewards);
            add_benchmark!(params, batches, price_tools, PriceTools);
            add_benchmark!(params, batches, xor_fee, XorFee);
            add_benchmark!(params, batches, referrals, Referrals);
            add_benchmark!(params, batches, ceres_staking, CeresStaking);
            add_benchmark!(params, batches, ceres_liquidity_locker, CeresLiquidityLockerBench::<Runtime>);
            add_benchmark!(params, batches, ceres_token_locker, CeresTokenLocker);
            add_benchmark!(params, batches, ceres_governance_platform, CeresGovernancePlatform);
            add_benchmark!(params, batches, ceres_launchpad, CeresLaunchpad);
            add_benchmark!(params, batches, demeter_farming_platform, DemeterFarmingPlatformBench::<Runtime>);
            add_benchmark!(params, batches, band, Band);
            add_benchmark!(params, batches, xst, XSTPoolBench::<Runtime>);
            add_benchmark!(params, batches, hermes_governance_platform, HermesGovernancePlatform);
            add_benchmark!(params, batches, oracle_proxy, OracleProxy);

            #[cfg(feature = "wip")] // order-book
            add_benchmark!(params, batches, order_book, OrderBook);

            // Trustless bridge
            #[cfg(feature = "wip")] // EVM bridge
            add_benchmark!(params, batches, ethereum_light_client, EthereumLightClient);
            #[cfg(feature = "wip")] // EVM bridge
            add_benchmark!(params, batches, bridge_inbound_channel, BridgeInboundChannel);
            #[cfg(feature = "wip")] // EVM bridge
            add_benchmark!(params, batches, bridge_outbound_channel, BridgeOutboundChannel);
            #[cfg(feature = "wip")] // EVM bridge
            add_benchmark!(params, batches, eth_app, EthApp);
            #[cfg(feature = "wip")] // EVM bridge
            add_benchmark!(params, batches, erc20_app, ERC20App);
            #[cfg(feature = "wip")] // EVM bridge
            add_benchmark!(params, batches, migration_app, MigrationApp);

            add_benchmark!(params, batches, evm_bridge_proxy, BridgeProxy);
            // Dispatch pallet benchmarks is strictly linked to EVM bridge params
            // TODO: fix
            #[cfg(feature = "wip")] // EVM bridge
            add_benchmark!(params, batches, dispatch, Dispatch);
            add_benchmark!(params, batches, substrate_bridge_channel::inbound, SubstrateBridgeInboundChannel);
            add_benchmark!(params, batches, substrate_bridge_channel::outbound, SubstrateBridgeOutboundChannel);
            add_benchmark!(params, batches, parachain_bridge_app, ParachainBridgeApp);
            add_benchmark!(params, batches, bridge_data_signer, BridgeDataSigner);
            add_benchmark!(params, batches, multisig_verifier, MultisigVerifier);

            if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
            Ok(batches)
        }
    }

    impl vested_rewards_runtime_api::VestedRewardsApi<Block, AccountId, AssetId, Balance, CrowdloanTag> for Runtime {
        fn crowdloan_claimable(tag: CrowdloanTag, account_id: AccountId, asset_id: AssetId) -> Option<vested_rewards_runtime_api::BalanceInfo<Balance>> {
            let balance = VestedRewards::get_claimable_crowdloan_reward(&tag, &account_id, &asset_id)?;
            Some(vested_rewards_runtime_api::BalanceInfo::<Balance> {
                balance
            })
        }

        fn crowdloan_lease(tag: CrowdloanTag) -> Option<vested_rewards_runtime_api::CrowdloanLease> {
            let crowdloan_info = vested_rewards::CrowdloanInfos::<Runtime>::get(&tag)?;

            Some(vested_rewards_runtime_api::CrowdloanLease {
                start_block: crowdloan_info.start_block as u128,
                total_days: crowdloan_info.length as u128 / DAYS as u128,
                blocks_per_day: DAYS as u128,
            })
        }
    }

    impl farming_runtime_api::FarmingApi<Block, AssetId> for Runtime {
        fn reward_doubling_assets() -> Vec<AssetId> {
            Farming::reward_doubling_assets()
        }
    }

    #[cfg(feature = "try-runtime")]
    impl frame_try_runtime::TryRuntime<Block> for Runtime {
        fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) {
            log::info!("try-runtime::on_runtime_upgrade");
            let weight = Executive::try_runtime_upgrade(checks).unwrap();
            (weight, BlockWeights::get().max_block)
        }

        fn execute_block(
            block: Block,
            state_root_check: bool,
            signature_check: bool,
            select: frame_try_runtime::TryStateSelect,
        ) -> Weight {
            // NOTE: intentional unwrap: we don't want to propagate the error backwards, and want to
            // have a backtrace here.
            Executive::try_execute_block(block, state_root_check, signature_check, select).unwrap()
        }
    }
}