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
use anchor_client::solana_sdk::{
    pubkey::Pubkey,
    signer::Signer,
    hash::Hash,
    instruction::Instruction
};
use anchor_client::solana_sdk::{system_program, sysvar};
use anchor_client::Program;
use anchor_lang::prelude::*;
use spl_token;
use crate::instruction;
use crate::accounts;

/// This instruction creates the global config account
/// and sets the DFlow admin.
///
/// The DFlow admin controls access to the protocol
/// by granting roles to accounts that allow them to perform privileged
/// actions such as auction management. This instruction must be
/// the first instruction called after program deployment and can
/// only be called once.
///
/// # Arguments
/// * `dflow_admin` The public key of the DFlow admin. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn init_global_config_instruction(
    program: &Program,
    // Non-PDA Non-Seed Dep Accounts
    dflow_admin: Pubkey,
) -> Instruction {
    let (global_config_account, global_config_account_bump) = Pubkey::find_program_address(
        &[
            b"global_config".as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitGlobalConfigInstruction {
                global_config_account: global_config_account,
                dflow_admin: dflow_admin,
                system_program: system_program::id(),
                rent: sysvar::rent::id()
            })
        .args(instruction::InitGlobalConfigInstruction {
                // Bumps
                global_config_account_bump: global_config_account_bump,
            })
        .instructions()
        .expect("init_global_config_instruction construction failed")
        .swap_remove(0)
}

/// This instruction updates the DFlow admin.
///
/// The current DFlow admin can use this instruction
/// to assign a new DFlow admin.
///
/// # Arguments
/// * `dflow_admin` The public key of the DFlow admin. This account
///    is a signer for the instruction
/// * `new_dflow_admin` The new DFlow admin's public key.
///
/// Returns an instruction
pub fn set_new_dflow_admin(
    program: &Program,
    // Non-PDA Non-Seed Dep Accounts
    dflow_admin: Pubkey,
    new_dflow_admin: Pubkey,
) -> Instruction {
    let (global_config_account, global_config_account_bump) = Pubkey::find_program_address(
        &[
            b"global_config".as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::SetNewDflowAdmin {
                global_config_account: global_config_account,
                dflow_admin: dflow_admin,
                new_dflow_admin: new_dflow_admin
            })
        .args(instruction::SetNewDflowAdmin {
                // Bumps
                global_config_account_bump: global_config_account_bump,
            })
        .instructions()
        .expect("set_new_dflow_admin construction failed")
        .swap_remove(0)
}

/// This instruction creates the principal's whitelist
/// entry account and grants the specified role to the principal.
/// # Arguments
/// * `principal` The public key of the principal account.
/// * `role` The role to grant to the principal.
/// * `dflow_admin` The public key of the DFlow admin. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn grant_role_uninitialized_entry(
    program: &Program,
    // Seed Deps
    principal: Pubkey,
    // Function Args
    role: u8,
    // Non-PDA Non-Seed Dep Accounts
    dflow_admin: Pubkey,
) -> Instruction {
    let (global_config_account, global_config_account_bump) = Pubkey::find_program_address(
        &[
            b"global_config".as_ref(),
        ],
        &program.id(),
    );
    let (whitelist_entry, whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            principal.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::GrantRoleUninitializedEntry {
                global_config_account: global_config_account,
                principal: principal,
                dflow_admin: dflow_admin,
                whitelist_entry: whitelist_entry,
                system_program: system_program::id(),
                rent: sysvar::rent::id()
            })
        .args(instruction::GrantRoleUninitializedEntry {
                // Bumps
                global_config_account_bump: global_config_account_bump,
                whitelist_entry_bump: whitelist_entry_bump,
                // Function Arguments
                role: role,
            })
        .instructions()
        .expect("grant_role_uninitialized_entry construction failed")
        .swap_remove(0)
}

/// This instruction grants the specified role to the principal.
/// # Arguments
/// * `principal` The public key of the principal account.
/// * `role` The role to grant to the principal.
/// * `dflow_admin` The public key of the DFlow admin. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn grant_role(
    program: &Program,
    // Seed Deps
    principal: Pubkey,
    // Function Args
    role: u8,
    // Non-PDA Non-Seed Dep Accounts
    dflow_admin: Pubkey,
) -> Instruction {
    let (global_config_account, global_config_account_bump) = Pubkey::find_program_address(
        &[
            b"global_config".as_ref(),
        ],
        &program.id(),
    );
    let (whitelist_entry, whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            principal.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::GrantRole {
                global_config_account: global_config_account,
                principal: principal,
                dflow_admin: dflow_admin,
                whitelist_entry: whitelist_entry
            })
        .args(instruction::GrantRole {
                // Bumps
                global_config_account_bump: global_config_account_bump,
                whitelist_entry_bump: whitelist_entry_bump,
                // Function Arguments
                role: role,
            })
        .instructions()
        .expect("grant_role construction failed")
        .swap_remove(0)
}

/// This instruction revokes the specified role from
/// the principal and closes the principal's whitelist account if
/// it no longer grants any permissions.
/// # Arguments
/// * `principal` The public key of the principal account.
/// * `role` The role to revoke from the principal.
/// * `dflow_admin` The public key of the DFlow admin. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn revoke_role(
    program: &Program,
    // Seed Deps
    principal: Pubkey,
    // Function Args
    role: u8,
    // Non-PDA Non-Seed Dep Accounts
    dflow_admin: Pubkey,
) -> Instruction {
    let (global_config_account, global_config_account_bump) = Pubkey::find_program_address(
        &[
            b"global_config".as_ref(),
        ],
        &program.id(),
    );
    let (whitelist_entry, whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            principal.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::RevokeRole {
                global_config_account: global_config_account,
                whitelist_entry: whitelist_entry,
                principal: principal,
                dflow_admin: dflow_admin
            })
        .args(instruction::RevokeRole {
                // Bumps
                global_config_account_bump: global_config_account_bump,
                whitelist_entry_bump: whitelist_entry_bump,
                // Function Arguments
                role: role,
            })
        .instructions()
        .expect("revoke_role construction failed")
        .swap_remove(0)
}

/// This instruction creates a recovery vault.
///
/// Recovery vaults are required for all mints used
/// in the protocol. If a transfer recipient does not have a token
/// account set up, the transferor can direct the protocol to transfer
/// the tokens to the recovery vault. The intended recipient can
/// then recover the tokens by submitting a recovery claim.
///
/// # Arguments
/// * `recovery_vault_mint` The public key of the recovery_vault_mint account.
/// * `auction_owner` The public key of the auction_owner account.
///
/// Returns an instruction
pub fn init_recovery_vault(
    program: &Program,
    // Seed Deps
    recovery_vault_mint: Pubkey,
    auction_owner: Pubkey,
) -> Instruction {
    let (recovery_state_account, recovery_state_account_bump) = Pubkey::find_program_address(
        &[
            recovery_vault_mint.as_ref(),
            b"recovery_state".as_ref(),
        ],
        &program.id(),
    );
    let (recovery_vault_account, recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            recovery_vault_mint.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    let (auction_owner_whitelist_entry, auction_owner_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            auction_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitRecoveryVaultInstruction {
                recovery_state_account: recovery_state_account,
                recovery_vault_account: recovery_vault_account,
                recovery_vault_mint: recovery_vault_mint,
                auction_owner: auction_owner,
                auction_owner_whitelist_entry: auction_owner_whitelist_entry,
                system_program: system_program::id(),
                rent: sysvar::rent::id(),
                token_program: spl_token::ID
            })
        .args(instruction::InitRecoveryVaultInstruction {
                // Bumps
                recovery_state_account_bump: recovery_state_account_bump,
                recovery_vault_account_bump: recovery_vault_account_bump,
                auction_owner_whitelist_entry_bump: auction_owner_whitelist_entry_bump,
            })
        .instructions()
        .expect("init_recovery_vault construction failed")
        .swap_remove(0)
}

/// This instruction initializes the auction mapper account.
///
/// The auction mapper account contains a list of
/// auctions that are not deprecated. Auctions will either be in
/// a trading or halted state. This function can only be called by
/// the DFlow admin.
///
/// # Arguments
/// * `dflow_admin` The public key of the DFlow admin. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn init_auction_mapper(
    program: &Program,
    // Non-PDA Non-Seed Dep Accounts
    dflow_admin: Pubkey,
) -> Instruction {
    let (global_config_account, global_config_account_bump) = Pubkey::find_program_address(
        &[
            b"global_config".as_ref(),
        ],
        &program.id(),
    );
    let (auction_mapper, auction_mapper_bump) = Pubkey::find_program_address(
        &[
            b"mapper".as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitAuctionMapperInstruction {
                global_config_account: global_config_account,
                auction_mapper: auction_mapper,
                dflow_admin: dflow_admin,
                system_program: system_program::id(),
                rent: sysvar::rent::id()
            })
        .args(instruction::InitAuctionMapperInstruction {
                // Bumps
                global_config_account_bump: global_config_account_bump,
                auction_mapper_bump: auction_mapper_bump,
            })
        .instructions()
        .expect("init_auction_mapper construction failed")
        .swap_remove(0)
}

/// This instruction initializes an auction for order
/// flow, and describes the specifications of the order flow account.
///
/// This instruction is only callable by an auction
/// owner. Once the auction is initialized, it remains in a Halted
/// state until the auction owner sets the auction state to Trading.
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_owner` The public key of the auction_owner account.
/// * `bid_mint_account` The public key of the bid_mint_account account.
/// * `vote_mint_account` The public key of the vote_mint_account account.
/// * `min_notional_order_size` The minimum notional size accepted by this newly
///    initialized auction. The notional size of all order flow purchased
///    in this auction must be greater than or equal to this value
/// * `max_notional_order_size` The maximum notional size accepted by this newly
///    initialized auction. The notional size of all order flow purchased
///    in this auction must be less than or equal to this value
/// * `notional_decimals` The number of decimals in `min_notional_order_size`,
///    `max_notional_order_size`, and `batch_notional_size`
/// * `batch_notional_size` The total notional size of the orders that will
///    be routed to the market maker when the market maker wins this
///    auction. A winning market maker will receive order flow with
///    a total notional size in the range [batch_notional_size, batch_notional_size
///    + max_notional_order_size)
/// * `vote_size` The amount of tokens in the currency of the vote
///    mint that the arbiter will need to stake every time the arbiter
///    votes on a fill.
/// * `supported_pairs_count` The number of token pairs for which order flow
///    is sold in this auction.
/// * `vote_period` The length in seconds of the voting period for
///    orders routed through this auction.
/// * `claim_period` The length in seconds of the reward claiming period
///    for orders routed through this auction.
///
/// Returns an instruction
pub fn init_auction_state(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    auction_owner: Pubkey,
    bid_mint_account: Pubkey,
    vote_mint_account: Pubkey,
    // Function Args
    min_notional_order_size: u64,
    max_notional_order_size: u64,
    notional_decimals: u8,
    batch_notional_size: u64,
    vote_size: u64,
    supported_pairs_count: u8,
    vote_period: u32,
    claim_period: u32,
) -> Instruction {
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (bid_vault_account, bid_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_vault".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (vote_vault_account, vote_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"vote_vault".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_mapper, auction_mapper_bump) = Pubkey::find_program_address(
        &[
            b"mapper".as_ref(),
        ],
        &program.id(),
    );
    let (auction_owner_whitelist_entry, auction_owner_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            auction_owner.as_ref(),
        ],
        &program.id(),
    );
    let (bid_recovery_state_account, bid_recovery_state_account_bump) = Pubkey::find_program_address(
        &[
            bid_mint_account.as_ref(),
            b"recovery_state".as_ref(),
        ],
        &program.id(),
    );
    let (bid_recovery_vault_account, bid_recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            bid_mint_account.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    let (vote_recovery_state_account, vote_recovery_state_account_bump) = Pubkey::find_program_address(
        &[
            vote_mint_account.as_ref(),
            b"recovery_state".as_ref(),
        ],
        &program.id(),
    );
    let (vote_recovery_vault_account, vote_recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            vote_mint_account.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitAuctionState {
                auction_state_account: auction_state_account,
                bid_vault_account: bid_vault_account,
                vote_vault_account: vote_vault_account,
                auction_mapper: auction_mapper,
                auction_owner: auction_owner,
                auction_owner_whitelist_entry: auction_owner_whitelist_entry,
                bid_mint_account: bid_mint_account,
                vote_mint_account: vote_mint_account,
                bid_recovery_state_account: bid_recovery_state_account,
                bid_recovery_vault_account: bid_recovery_vault_account,
                vote_recovery_state_account: vote_recovery_state_account,
                vote_recovery_vault_account: vote_recovery_vault_account,
                system_program: system_program::id(),
                rent: sysvar::rent::id(),
                token_program: spl_token::ID
            })
        .args(instruction::InitAuctionState {
                // Bumps
                auction_state_account_bump: auction_state_account_bump,
                bid_vault_account_bump: bid_vault_account_bump,
                vote_vault_account_bump: vote_vault_account_bump,
                auction_mapper_bump: auction_mapper_bump,
                auction_owner_whitelist_entry_bump: auction_owner_whitelist_entry_bump,
                bid_recovery_state_account_bump: bid_recovery_state_account_bump,
                bid_recovery_vault_account_bump: bid_recovery_vault_account_bump,
                vote_recovery_state_account_bump: vote_recovery_state_account_bump,
                vote_recovery_vault_account_bump: vote_recovery_vault_account_bump,
                // Seed Dependencies
                auction_id: auction_id,
                // Function Arguments
                min_notional_order_size: min_notional_order_size,
                max_notional_order_size: max_notional_order_size,
                notional_decimals: notional_decimals,
                batch_notional_size: batch_notional_size,
                vote_size: vote_size,
                supported_pairs_count: supported_pairs_count,
                vote_period: vote_period,
                claim_period: claim_period,
            })
        .instructions()
        .expect("init_auction_state construction failed")
        .swap_remove(0)
}

/// This instruction adds a currency pair to an existing
/// auction.
///
/// Only a certain number of token pairs are allowed
/// to exist in an auction and an error is thrown if this instruction
/// is called when the token pairs count is saturated
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `base_currency` The public key of the base_currency account.
/// * `quote_currency` The public key of the quote_currency account.
/// * `auction_owner` The public key of the auction_owner account.
/// * `base_oracle` The Pyth price oracle associated with the base
///    token in a currency pair.
/// * `quote_oracle` The Pyth price oracle associated with the quote
///    token in a currency pair.
///
/// Returns an instruction
pub fn add_pair_to_auction(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    base_currency: Pubkey,
    quote_currency: Pubkey,
    auction_owner: Pubkey,
    // Non-PDA Non-Seed Dep Accounts
    base_oracle: Pubkey,
    quote_oracle: Pubkey,
) -> Instruction {
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (base_recovery_state_account, base_recovery_state_account_bump) = Pubkey::find_program_address(
        &[
            base_currency.as_ref(),
            b"recovery_state".as_ref(),
        ],
        &program.id(),
    );
    let (base_recovery_vault_account, base_recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            base_currency.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    let (quote_recovery_state_account, quote_recovery_state_account_bump) = Pubkey::find_program_address(
        &[
            quote_currency.as_ref(),
            b"recovery_state".as_ref(),
        ],
        &program.id(),
    );
    let (quote_recovery_vault_account, quote_recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            quote_currency.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    let (auction_owner_whitelist_entry, auction_owner_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            auction_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::AddCurrencyPair {
                auction_state_account: auction_state_account,
                base_currency: base_currency,
                quote_currency: quote_currency,
                base_oracle: base_oracle,
                quote_oracle: quote_oracle,
                base_recovery_state_account: base_recovery_state_account,
                base_recovery_vault_account: base_recovery_vault_account,
                quote_recovery_state_account: quote_recovery_state_account,
                quote_recovery_vault_account: quote_recovery_vault_account,
                auction_owner: auction_owner,
                auction_owner_whitelist_entry: auction_owner_whitelist_entry,
                token_program: spl_token::ID
            })
        .args(instruction::AddCurrencyPair {
                // Bumps
                auction_state_account_bump: auction_state_account_bump,
                base_recovery_state_account_bump: base_recovery_state_account_bump,
                base_recovery_vault_account_bump: base_recovery_vault_account_bump,
                quote_recovery_state_account_bump: quote_recovery_state_account_bump,
                quote_recovery_vault_account_bump: quote_recovery_vault_account_bump,
                auction_owner_whitelist_entry_bump: auction_owner_whitelist_entry_bump,
                // Seed Dependencies
                auction_id: auction_id,
            })
        .instructions()
        .expect("add_pair_to_auction construction failed")
        .swap_remove(0)
}

/// This instruction changes the status of an auction
///
/// This instruction can be used to change the status
/// of an auction between Halted, Trading, or Expired. Once Expired,
/// the auction will not be revivable.
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_owner` The public key of the auction_owner account.
/// * `new_status` The new status for the given auction, specified
///    by the auction ID parameter. A value of 0 indicates Trading.
///    A value of 1 indicates Halted. A value of 2 indicates expired.
///    All other values result in a failed transaction.
///
/// Returns an instruction
pub fn change_auction_status(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    auction_owner: Pubkey,
    // Function Args
    new_status: u8,
) -> Instruction {
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_mapper, auction_mapper_bump) = Pubkey::find_program_address(
        &[
            b"mapper".as_ref(),
        ],
        &program.id(),
    );
    let (auction_owner_whitelist_entry, auction_owner_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            auction_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::ChangeAuctionStatus {
                auction_state_account: auction_state_account,
                auction_mapper: auction_mapper,
                auction_owner: auction_owner,
                auction_owner_whitelist_entry: auction_owner_whitelist_entry,
                system_program: system_program::id()
            })
        .args(instruction::ChangeAuctionStatus {
                // Bumps
                auction_state_account_bump: auction_state_account_bump,
                auction_mapper_bump: auction_mapper_bump,
                auction_owner_whitelist_entry_bump: auction_owner_whitelist_entry_bump,
                // Seed Dependencies
                auction_id: auction_id,
                // Function Arguments
                new_status: new_status,
            })
        .instructions()
        .expect("change_auction_status construction failed")
        .swap_remove(0)
}

/// This instruction initializes the market maker account
///
/// The market maker account tracks state specific
/// to the market maker's market making activities. This instruction
/// must be called prior to a market maker being active on DFlow.
///
/// # Arguments
/// * `market_maker_account_owner` The public key of the market_maker_account_owner account.
/// * `max_orders_supported` The maximum number of orders supported in the
///    market maker's queue of orders
/// * `encryption_pub_key` The market maker's 256-bit X25519 public key which
///    will be used by retail traders to encrypt their order details
/// * `market_maker_data_account` The account storing state specific to the market
///    maker. This account must be owned by the DFlow program, and this
///    account must be initialized by calling the initialize market
///    maker instruction. This account is expected to be zeroed out
///    upon the start of instruction processing
///
/// Returns an instruction
pub fn init_market_maker_account(
    program: &Program,
    // Seed Deps
    market_maker_account_owner: Pubkey,
    // Function Args
    max_orders_supported: u64,
    encryption_pub_key: [u8; 32],
    // Non-PDA Non-Seed Dep Accounts
    market_maker_data_account: Pubkey,
) -> Instruction {
    let (market_maker_whitelist_entry, market_maker_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            market_maker_account_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitMarketMakerAccountInstruction {
                market_maker_data_account: market_maker_data_account,
                market_maker_account_owner: market_maker_account_owner,
                market_maker_whitelist_entry: market_maker_whitelist_entry
            })
        .args(instruction::InitMarketMakerAccountInstruction {
                // Bumps
                market_maker_whitelist_entry_bump: market_maker_whitelist_entry_bump,
                // Function Arguments
                max_orders_supported: max_orders_supported,
                encryption_pub_key: encryption_pub_key,
            })
        .instructions()
        .expect("init_market_maker_account construction failed")
        .swap_remove(0)
}

/// This instruction updates the market maker's 256-bit
/// X25519 public key.
///
/// Retail traders use the market maker's 256-bit
/// X25519 public key to encrypt their order details when routing
/// orders to the market maker. This instruction updates the market
/// maker's X25519 public key.
///
/// # Arguments
/// * `market_maker_account_owner` The public key of the market_maker_account_owner account.
/// * `encryption_pub_key` The market maker's 256-bit X25519 public key which
///    will be used by retail traders to encrypt their order details
/// * `market_maker_data_account` The account storing state specific to the market
///    maker. This account must be owned by the DFlow program, and this
///    account must be initialized by calling the initialize market
///    maker instruction. This account is expected to be mutable
///
/// Returns an instruction
pub fn update_encryption_key(
    program: &Program,
    // Seed Deps
    market_maker_account_owner: Pubkey,
    // Function Args
    encryption_pub_key: [u8; 32],
    // Non-PDA Non-Seed Dep Accounts
    market_maker_data_account: Pubkey,
) -> Instruction {
    let (market_maker_whitelist_entry, market_maker_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            market_maker_account_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::UpdateEncryptionKeyInstruction {
                market_maker_data_account: market_maker_data_account,
                market_maker_account_owner: market_maker_account_owner,
                market_maker_whitelist_entry: market_maker_whitelist_entry
            })
        .args(instruction::UpdateEncryptionKeyInstruction {
                // Bumps
                market_maker_whitelist_entry_bump: market_maker_whitelist_entry_bump,
                // Function Arguments
                encryption_pub_key: encryption_pub_key,
            })
        .instructions()
        .expect("update_encryption_key construction failed")
        .swap_remove(0)
}

/// This instruction initializes an auction epoch state account
///
/// AuctionEpochState carries state specific to a
/// certain auction that needs to be remembered after the epoch ends.
/// The AuctionEpochState accounts associated with auction epochs
/// N and N + 1 need to be initialized when the auction for epoch
/// N begins (i.e. in the first SubmitAuctionBid that auction N will
/// receive). Recall that if the AuctionState has an epoch field
/// that has value X, then auction X is currently active, and auction
/// X - 1 is currently receiving order flow.
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_epoch` An unsigned 64 bit integer. The integer epoch
///    used to generate the PDA of the auction epoch account
/// * `owner` The creator and owner of AuctionEpochState account.
///    This account is a signer for the instruction. This account is
///    expected to be mutable
///
/// Returns an instruction
pub fn init_auction_epoch_state_account(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    auction_epoch: u64,
    // Non-PDA Non-Seed Dep Accounts
    owner: Pubkey,
) -> Instruction {
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitAuctionEpochStateInstruction {
                auction_epoch_state: auction_epoch_state,
                auction_state_account: auction_state_account,
                owner: owner,
                system_program: system_program::id(),
                rent: sysvar::rent::id()
            })
        .args(instruction::InitAuctionEpochStateInstruction {
                // Bumps
                auction_epoch_state_bump: auction_epoch_state_bump,
                auction_state_account_bump: auction_state_account_bump,
                // Seed Dependencies
                auction_id: auction_id,
                auction_epoch: auction_epoch,
            })
        .instructions()
        .expect("init_auction_epoch_state_account construction failed")
        .swap_remove(0)
}

/// This instruction initializes the retail traders account.
///
/// The retail data account tracks state specific
/// to the retail trader's trading activities on DFlow.
///
/// # Arguments
/// * `max_orders_supported` The maximum number of orders allowed to be active
///    at once by a retail trader
/// * `retail_data_account` The retail data account holds state specific to
///    the retail trader. This account is expected to be zeroed out
///    upon the start of instruction processing
/// * `retail_account_owner` The public key of the retail trader. This account
///    is a signer for the instruction
///
/// Returns an instruction
pub fn init_retail_account(
    program: &Program,
    // Function Args
    max_orders_supported: u64,
    // Non-PDA Non-Seed Dep Accounts
    retail_data_account: Pubkey,
    retail_account_owner: Pubkey,
) -> Instruction {
    program
        .request()
        .accounts(
            accounts::InitRetailDataAccountInstruction {
                retail_data_account: retail_data_account,
                retail_account_owner: retail_account_owner
            })
        .args(instruction::InitRetailDataAccountInstruction {
                // Function Arguments
                max_orders_supported: max_orders_supported,
            })
        .instructions()
        .expect("init_retail_account construction failed")
        .swap_remove(0)
}

/// This instruction closes the retail trader's data account.
///
/// When retail traders wish to close their account,
/// and regain the lamports used to allocate the space for their
/// data account, they can call this instruction. Retail traders
/// may not close their account if they have open orders, and must
/// wait for the orders to be filled, or cancel them.
///
/// # Arguments
/// * `retail_data_account` The retail data account holds state specific to
///    the retail trader. This account is being closed, and the destination
///    of the lamports will be the retail_account_owner account. This
///    account is expected to be mutable
/// * `retail_account_owner` The public key of the retail trader. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn close_retail_account(
    program: &Program,
    // Non-PDA Non-Seed Dep Accounts
    retail_data_account: Pubkey,
    retail_account_owner: Pubkey,
) -> Instruction {
    program
        .request()
        .accounts(
            accounts::CloseRetailDataAccount {
                retail_data_account: retail_data_account,
                retail_account_owner: retail_account_owner,
                system_program: system_program::id()
            })
        .args(instruction::CloseRetailDataAccount {
            })
        .instructions()
        .expect("close_retail_account construction failed")
        .swap_remove(0)
}

/// This instruction is called by the market maker
/// prior to bidding in an auction.
///
/// Before a market maker bids in an auction, they
/// must use this instruction to initialize an account to track their
/// current bid in the auction.
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_epoch` An unsigned 64 bit integer. The integer epoch
///    used to generate the PDA of the auction epoch account
/// * `market_maker_data_account` The public key of the market_maker_data_account account.
/// * `market_maker_account_owner` The public key of the market_maker_account_owner account.
///
/// Returns an instruction
pub fn init_bid_record_account(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    auction_epoch: u64,
    market_maker_data_account: Pubkey,
    market_maker_account_owner: Pubkey,
) -> Instruction {
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (bid_record_account, bid_record_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_record".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
            market_maker_data_account.as_ref(),
        ],
        &program.id(),
    );
    let (market_maker_whitelist_entry, market_maker_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            market_maker_account_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitBidRecordAccount {
                auction_state_account: auction_state_account,
                auction_epoch_state: auction_epoch_state,
                bid_record_account: bid_record_account,
                market_maker_data_account: market_maker_data_account,
                market_maker_account_owner: market_maker_account_owner,
                market_maker_whitelist_entry: market_maker_whitelist_entry,
                system_program: system_program::id(),
                rent: sysvar::rent::id()
            })
        .args(instruction::InitBidRecordAccount {
                // Bumps
                auction_state_account_bump: auction_state_account_bump,
                auction_epoch_state_bump: auction_epoch_state_bump,
                bid_record_account_bump: bid_record_account_bump,
                market_maker_whitelist_entry_bump: market_maker_whitelist_entry_bump,
                // Seed Dependencies
                auction_id: auction_id,
                auction_epoch: auction_epoch,
            })
        .instructions()
        .expect("init_bid_record_account construction failed")
        .swap_remove(0)
}

/// This instruction is called by the market maker
/// to bid in an auction.
///
/// When a market maker wishes to bid in an auction,
/// they can use this instruction to specify the parameters of their
/// bid. Note that the first bidder in auction epoch N must ensure
/// that auction epoch state accounts are initialized for auction
/// epochs N and N + 1.
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_epoch` An unsigned 64 bit integer. The integer epoch
///    used to generate the PDA of the auction epoch account
/// * `next_auction_epoch` An unsigned 64 bit integer. The next auction epoch
///    number. If bidding in auction epoch N, this must be N + 1.
/// * `market_maker_data_account` The public key of the market_maker_data_account account.
/// * `market_maker_account_owner` The public key of the market_maker_account_owner account.
/// * `bid_size` The size of the bid being placed into the auction.
///    This number must strictly exceed the best bid in order for the
///    transaction to be executed.
/// * `market_maker_auction_token_account` The SPL token account associated with the market
///    maker used to bid in the auctions. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn submit_auction_bid(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    auction_epoch: u64,
    next_auction_epoch: u64,
    market_maker_data_account: Pubkey,
    market_maker_account_owner: Pubkey,
    // Function Args
    bid_size: u64,
    // Non-PDA Non-Seed Dep Accounts
    market_maker_auction_token_account: Pubkey,
) -> Instruction {
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (next_auction_epoch_state, next_auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &next_auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (bid_vault_account, bid_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_vault".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (bid_record_account, bid_record_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_record".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
            market_maker_data_account.as_ref(),
        ],
        &program.id(),
    );
    let (market_maker_whitelist_entry, market_maker_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            market_maker_account_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::SubmitAuctionBidInstruction {
                auction_state_account: auction_state_account,
                auction_epoch_state: auction_epoch_state,
                next_auction_epoch_state: next_auction_epoch_state,
                bid_vault_account: bid_vault_account,
                market_maker_data_account: market_maker_data_account,
                market_maker_auction_token_account: market_maker_auction_token_account,
                bid_record_account: bid_record_account,
                market_maker_account_owner: market_maker_account_owner,
                market_maker_whitelist_entry: market_maker_whitelist_entry,
                token_program: spl_token::ID
            })
        .args(instruction::SubmitAuctionBidInstruction {
                // Bumps
                auction_state_account_bump: auction_state_account_bump,
                auction_epoch_state_bump: auction_epoch_state_bump,
                next_auction_epoch_state_bump: next_auction_epoch_state_bump,
                bid_vault_account_bump: bid_vault_account_bump,
                bid_record_account_bump: bid_record_account_bump,
                market_maker_whitelist_entry_bump: market_maker_whitelist_entry_bump,
                // Seed Dependencies
                auction_id: auction_id,
                auction_epoch: auction_epoch,
                next_auction_epoch: next_auction_epoch,
                // Function Arguments
                bid_size: bid_size,
            })
        .instructions()
        .expect("submit_auction_bid construction failed")
        .swap_remove(0)
}

/// This instruction is called by the market maker
/// to withdraw its losing auction bids and close its bid record
/// account for the given auction.
///
/// A market maker who is outbid in an auction can
/// use this instruction to withdraw its bids in the auction and
/// close its bid record account for the auction. A market maker
/// who has won an auction can use this instruction to close its
/// bid record account for the auction.
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_epoch` An unsigned 64 bit integer. The integer epoch
///    used to generate the PDA of the auction epoch account
/// * `market_maker_data_account` The public key of the market_maker_data_account account.
/// * `market_maker_account_owner` The public key of the market_maker_account_owner account.
/// * `market_maker_auction_token_account` The SPL token account associated with the market
///    maker used to bid in the auctions. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn reclaim_auction_bid(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    auction_epoch: u64,
    market_maker_data_account: Pubkey,
    market_maker_account_owner: Pubkey,
    // Non-PDA Non-Seed Dep Accounts
    market_maker_auction_token_account: Pubkey,
) -> Instruction {
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (bid_vault_account, bid_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_vault".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (bid_record_account, bid_record_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_record".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
            market_maker_data_account.as_ref(),
        ],
        &program.id(),
    );
    let (market_maker_whitelist_entry, market_maker_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            market_maker_account_owner.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::ReclaimAuctionBidInstruction {
                auction_state_account: auction_state_account,
                auction_epoch_state: auction_epoch_state,
                bid_vault_account: bid_vault_account,
                market_maker_data_account: market_maker_data_account,
                market_maker_auction_token_account: market_maker_auction_token_account,
                bid_record_account: bid_record_account,
                market_maker_account_owner: market_maker_account_owner,
                market_maker_whitelist_entry: market_maker_whitelist_entry,
                token_program: spl_token::ID
            })
        .args(instruction::ReclaimAuctionBidInstruction {
                // Bumps
                auction_state_account_bump: auction_state_account_bump,
                auction_epoch_state_bump: auction_epoch_state_bump,
                bid_vault_account_bump: bid_vault_account_bump,
                bid_record_account_bump: bid_record_account_bump,
                market_maker_whitelist_entry_bump: market_maker_whitelist_entry_bump,
                // Seed Dependencies
                auction_id: auction_id,
                auction_epoch: auction_epoch,
            })
        .instructions()
        .expect("reclaim_auction_bid construction failed")
        .swap_remove(0)
}

/// This instruction is called by retail traders when
/// they submit their order. A transaction containing this instruction
/// must be signed by a signatory server.
///
/// When submitting orders, retail traders must encrypt
/// the details of their order in order to hide information regarding
/// which token is being received.
///
/// # Arguments
/// * `retail_account_owner` The public key of the retail_account_owner account.
/// * `retail_data_account_nonce` The unsigned integer field named nonce in the
///    retail_data_account account. The nonce in the retail trader's
///    account used to generate a PDA for the vault token account. This
///    nonce must be saved and used when referencing this order in the
///    future.
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_epoch` An unsigned 64 bit integer. The integer epoch
///    used to generate the PDA of the auction epoch account
/// * `previous_auction_epoch` An unsigned 64 bit integer. The previous epoch.
///    This must be equal to auction_epoch - 1.
/// * `signatory_server` The public key of the signatory_server account.
/// * `retail_encryption_pub_key` The retail trader's X25519 public key.
/// * `mm_encryption_pub_key` The market maker's X25519 public key.
/// * `encrypted_order_details` The ciphertext of the order details, encrypted
///    using a 24-byte nonce of zeros and the key derived from the market
///    maker's X25519 public key and the retail trader's X25519 private
///    key.
/// * `retail_signing_pub_key` The public key of the Ed25519 key pair that the
///    retail trader used to generate the signature.
/// * `retail_signature` The signature of the plaintext order details.
/// * `deposit_amount` The amount of tokens being sent by the retail
///    trader in the trade.
/// * `order_type` The order type, must be either 0 for limit or
///    1 for market. All other values result in a failed transaction.
/// * `retail_x_token_account` The SPL token account associated with the retail
///    trader which is used to deposit the sold token into escrow prior
///    to the trade occurring. This account is expected to be mutable
/// * `rebate_receiver_token_account` The SPL token account associated with the order
///    used to receive a payment from the network, with the same mint
///    as the token accounts used by market makers to bid in the associated
///    auction.
/// * `retail_data_account` The retail data account holds state specific to
///    the retail trader. This account is expected to be mutable
/// * `market_maker_data_account` The account storing state specific to the market
///    maker. This account must be owned by the DFlow program, and this
///    account must be initialized by calling the initialize market
///    maker instruction. This account is expected to be mutable
/// * `x_mint` The SPL mint account associated with the token
///    that is sold by the retail trader during the swap.
/// * `price_oracles` These price oracles are the Pyth price accounts
///    associated with the products that could possibly be received
///    by the retail trader based on which token they are selling. For
///    example, if a retail trader is swapping X for some other token,
///    then the price oracles that must be passed here are all price
///    oracles which exist as a pair with X in this auction.
///
/// Returns an instruction
pub fn new_order(
    program: &Program,
    // Seed Deps
    retail_account_owner: Pubkey,
    retail_data_account_nonce: u64,
    auction_id: u64,
    auction_epoch: u64,
    previous_auction_epoch: u64,
    signatory_server: Pubkey,
    // Function Args
    retail_encryption_pub_key: [u8; 32],
    mm_encryption_pub_key: [u8; 32],
    encrypted_order_details: String,
    retail_signing_pub_key: [u8; 32],
    retail_signature: [u8; 64],
    deposit_amount: u64,
    order_type: u8,
    // Non-PDA Non-Seed Dep Accounts
    retail_x_token_account: Pubkey,
    rebate_receiver_token_account: Pubkey,
    retail_data_account: Pubkey,
    market_maker_data_account: Pubkey,
    x_mint: Pubkey,
    // Remaining Accounts
    price_oracles: Vec<Pubkey>,
) -> Instruction {
    let (vault_token_account, vault_token_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault".as_ref(),
            &retail_data_account_nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (vault_meta_account, vault_meta_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault_meta".as_ref(),
            &retail_data_account_nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (current_auction_epoch_state, current_auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (previous_auction_epoch_state, previous_auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &previous_auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (signatory_record, signatory_record_bump) = Pubkey::find_program_address(
        &[
            signatory_server.as_ref(),
        ],
        &program.id(),
    );
    let mut remaining_accounts: Vec<AccountMeta> = Vec::new();
    remaining_accounts.extend(price_oracles.iter().map(|&x| AccountMeta {pubkey: x, is_signer: false, is_writable: false}));
    program
        .request()
        .accounts(
            accounts::NewOrderInstruction {
                vault_token_account: vault_token_account,
                vault_meta_account: vault_meta_account,
                current_auction_epoch_state: current_auction_epoch_state,
                previous_auction_epoch_state: previous_auction_epoch_state,
                retail_x_token_account: retail_x_token_account,
                rebate_receiver_token_account: rebate_receiver_token_account,
                auction_state_account: auction_state_account,
                retail_data_account: retail_data_account,
                market_maker_data_account: market_maker_data_account,
                retail_account_owner: retail_account_owner,
                x_mint: x_mint,
                signatory_record: signatory_record,
                signatory_server: signatory_server,
                token_program: spl_token::ID,
                system_program: system_program::id(),
                rent: sysvar::rent::id()
            })
        .accounts(remaining_accounts.to_account_metas(None))
        .args(instruction::NewOrderInstruction {
                // Bumps
                vault_token_account_bump: vault_token_account_bump,
                vault_meta_account_bump: vault_meta_account_bump,
                current_auction_epoch_state_bump: current_auction_epoch_state_bump,
                previous_auction_epoch_state_bump: previous_auction_epoch_state_bump,
                auction_state_account_bump: auction_state_account_bump,
                signatory_record_bump: signatory_record_bump,
                // Seed Dependencies
                auction_id: auction_id,
                auction_epoch: auction_epoch,
                previous_auction_epoch: previous_auction_epoch,
                // Function Arguments
                retail_encryption_pub_key: retail_encryption_pub_key,
                mm_encryption_pub_key: mm_encryption_pub_key,
                encrypted_order_details: encrypted_order_details,
                retail_signing_pub_key: retail_signing_pub_key,
                retail_signature: retail_signature,
                deposit_amount: deposit_amount,
                order_type: order_type,
            })
        .instructions()
        .expect("new_order construction failed")
        .swap_remove(0)
}

/// This instruction cancels an open order.
///
/// Cancelling an open order can only be done if it
/// has not been filled and if the order has been filled partially,
/// the retail trader can only cancel the part of the order which
/// is still open.
///
/// # Arguments
/// * `retail_account_owner` The public key of the retail_account_owner account.
/// * `nonce` An unsigned 64 bit integer. The nonce used as
///    a seed to generate the PDA for the vault token account, taken
///    from the retail trader's account when the order in question was
///    initially created.
/// * `vault_meta_account_auction_id` The unsigned integer field named auction_id in
///    the vault_meta_account account.
/// * `vault_meta_account_auction_epoch` The unsigned integer field named auction_epoch
///    in the vault_meta_account account.
/// * `retail_data_account` The retail data account holds state specific to
///    the retail trader. This account is expected to be mutable
/// * `market_maker_data_account` The account storing state specific to the market
///    maker. This account must be owned by the DFlow program, and this
///    account must be initialized by calling the initialize market
///    maker instruction. This account is expected to be mutable
/// * `retail_x_token_account` The SPL token account associated with the retail
///    trader which is used to deposit the sold token into escrow prior
///    to the trade occurring. This account is expected to be mutable
/// * `x_mint` The SPL mint account associated with the token
///    that is sold by the retail trader during the swap.
///
/// Returns an instruction
pub fn cancel_order(
    program: &Program,
    // Seed Deps
    retail_account_owner: Pubkey,
    nonce: u64,
    vault_meta_account_auction_id: u64,
    vault_meta_account_auction_epoch: u64,
    // Non-PDA Non-Seed Dep Accounts
    retail_data_account: Pubkey,
    market_maker_data_account: Pubkey,
    retail_x_token_account: Pubkey,
    x_mint: Pubkey,
) -> Instruction {
    let (vault_token_account, vault_token_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault".as_ref(),
            &nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (vault_meta_account, vault_meta_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault_meta".as_ref(),
            &nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
            &vault_meta_account_auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::CancelOrderInstruction {
                vault_token_account: vault_token_account,
                vault_meta_account: vault_meta_account,
                auction_epoch_state: auction_epoch_state,
                retail_data_account: retail_data_account,
                market_maker_data_account: market_maker_data_account,
                retail_x_token_account: retail_x_token_account,
                retail_account_owner: retail_account_owner,
                x_mint: x_mint,
                token_program: spl_token::ID,
                system_program: system_program::id()
            })
        .args(instruction::CancelOrderInstruction {
                // Bumps
                vault_token_account_bump: vault_token_account_bump,
                vault_meta_account_bump: vault_meta_account_bump,
                auction_epoch_state_bump: auction_epoch_state_bump,
                // Seed Dependencies
                nonce: nonce,
            })
        .instructions()
        .expect("cancel_order construction failed")
        .swap_remove(0)
}

/// This instruction is used to fill an open order
///
/// When a market maker fills an order that is assigned
/// to them, this instruction will partially settle the trade; the
/// retail trader will receive the tokens from the market maker,
/// but the market maker must wait until arbiters have voted on the
/// fairness of the fill in order to receive the retail trader's
/// tokens.
///
/// # Arguments
/// * `retail_account_owner` The public key of the retail_account_owner account.
/// * `nonce` An unsigned 64 bit integer. The nonce used as
///    a seed to generate the PDA for the vault meta account, taken
///    from the retail trader's account when the order in question was
///    initially created.
/// * `vault_meta_account_fill_nonce` The unsigned integer field named fill_nonce in
///    the vault_meta_account account. The fill nonce taken from the
///    vault meta account at fill time, and used to generate the PDA
///    for the fill record account.
/// * `vault_meta_account_auction_id` The unsigned integer field named auction_id in
///    the vault_meta_account account.
/// * `vault_meta_account_auction_epoch` The unsigned integer field named auction_epoch
///    in the vault_meta_account account.
/// * `market_maker_account_owner` The public key of the market_maker_account_owner account.
/// * `y_mint` The public key of the y_mint account.
/// * `order_details` The unencrypted order details sent by the retail trader.
/// * `fill_amount` The fill amount, referring to the amount of tokens
///    to be received by the retail trader in the trade.
/// * `fill_price` The price, quoted in the quote currency for the
///    given pair, at which the fill is being completed.
/// * `retail_data_account` The retail data account holds state specific to
///    the retail trader. This account is expected to be mutable
/// * `market_maker_data_account` The account storing state specific to the market
///    maker. This account must be owned by the DFlow program, and this
///    account must be initialized by calling the initialize market
///    maker instruction. This account is expected to be mutable
/// * `x_mint` The SPL mint account associated with the token
///    that is sold by the retail trader during the swap.
/// * `market_maker_y_token_account` The SPL token account associated with the market
///    maker, and which sends the asset being received by the retail
///    trader during a trade. This account is expected to be mutable
/// * `retail_y_token_account` The SPL token account associated with the retail
///    trader, and which receives the asset being sent by the market
///    maker during a trade. This account is expected to be mutable.
///    This account is the retail_y_token_account's associated token
///    account for the mint of the asset sent by the market maker
/// * `is_y_token_account_uninitialized` True if and only if the retail trader's Y token
///    account is uninitialized
///
/// Returns an instruction
pub fn fill_order(
    program: &Program,
    // Seed Deps
    retail_account_owner: Pubkey,
    nonce: u64,
    vault_meta_account_fill_nonce: u16,
    vault_meta_account_auction_id: u64,
    vault_meta_account_auction_epoch: u64,
    market_maker_account_owner: Pubkey,
    y_mint: Pubkey,
    // Function Args
    order_details: String,
    fill_amount: u64,
    fill_price: u64,
    // Non-PDA Non-Seed Dep Accounts
    retail_data_account: Pubkey,
    market_maker_data_account: Pubkey,
    x_mint: Pubkey,
    market_maker_y_token_account: Pubkey,
    retail_y_token_account: Pubkey,
    // Optional Account Flags
    is_y_token_account_uninitialized: bool,
) -> Instruction {
    let (vault_meta_account, vault_meta_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault_meta".as_ref(),
            &nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (fill_record_account, fill_record_account_bump) = Pubkey::find_program_address(
        &[
            vault_meta_account.as_ref(),
            &vault_meta_account_fill_nonce.to_le_bytes(),
            b"fra".as_ref(),
        ],
        &program.id(),
    );
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
            &vault_meta_account_auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (market_maker_whitelist_entry, market_maker_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            market_maker_account_owner.as_ref(),
        ],
        &program.id(),
    );
    let (y_recovery_vault_account, y_recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            y_mint.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    let mut remaining_accounts: Vec<AccountMeta> = Vec::new();
    if is_y_token_account_uninitialized {
        remaining_accounts.push(AccountMeta {pubkey: y_recovery_vault_account, is_signer: false, is_writable: true});
    }
    program
        .request()
        .accounts(
            accounts::FillOrderInstruction {
                vault_meta_account: vault_meta_account,
                fill_record_account: fill_record_account,
                retail_data_account: retail_data_account,
                market_maker_data_account: market_maker_data_account,
                auction_epoch_state: auction_epoch_state,
                auction_state_account: auction_state_account,
                retail_account_owner: retail_account_owner,
                x_mint: x_mint,
                y_mint: y_mint,
                market_maker_y_token_account: market_maker_y_token_account,
                market_maker_account_owner: market_maker_account_owner,
                market_maker_whitelist_entry: market_maker_whitelist_entry,
                retail_y_token_account: retail_y_token_account,
                system_program: system_program::id(),
                rent: sysvar::rent::id(),
                token_program: spl_token::ID,
                instructions: sysvar::instructions::id()
            })
        .accounts(remaining_accounts.to_account_metas(None))
        .args(instruction::FillOrderInstruction {
                // Bumps
                vault_meta_account_bump: vault_meta_account_bump,
                fill_record_account_bump: fill_record_account_bump,
                auction_epoch_state_bump: auction_epoch_state_bump,
                auction_state_account_bump: auction_state_account_bump,
                market_maker_whitelist_entry_bump: market_maker_whitelist_entry_bump,
                y_recovery_vault_account_bump: y_recovery_vault_account_bump,
                // Seed Dependencies
                nonce: nonce,
                // Function Arguments
                order_details: order_details,
                fill_amount: fill_amount,
                fill_price: fill_price,
            })
        .instructions()
        .expect("fill_order construction failed")
        .swap_remove(0)
}

/// This instruction is used to vote on the fairness of a fill
///
/// Arbiters vote on the fairness of a fill when the
/// fill is open for voting. In order to vote on a fill, the arbiter
/// must stake tokens on the accuracy of their vote. If the arbiter
/// voted in the majority, they receive their tokens back. Otherwise
/// the tokens are deposited into a DAO governed backstop fund for
/// the protocol.
///
/// # Arguments
/// * `retail_account_owner` The public key of the retail_account_owner account.
/// * `nonce` An unsigned 64 bit integer. The nonce used as
///    a seed to generate the PDA for the vault meta account, taken
///    from the retail trader's account when the order in question was
///    initially created.
/// * `fill_nonce` An unsigned 16 bit integer. The fill nonce taken
///    from the vault meta account at fill time, and used to generate
///    the PDA for the fill record account.
/// * `arbiter` The public key of the arbiter account.
/// * `vault_meta_account_auction_id` The unsigned integer field named auction_id in
///    the vault_meta_account account.
/// * `vote` The vote value made by the retail trader. Must
///    be populated with 0 for an unfair vote, and 1 for a fair vote.
/// * `arbiter_token_account` The SPL token account associated with the arbiter
///    used to vote on the fairness of the fill by the market maker.
///    This account is expected to be mutable
///
/// Returns an instruction
pub fn fill_vote(
    program: &Program,
    // Seed Deps
    retail_account_owner: Pubkey,
    nonce: u64,
    fill_nonce: u16,
    arbiter: Pubkey,
    vault_meta_account_auction_id: u64,
    // Function Args
    vote: u8,
    // Non-PDA Non-Seed Dep Accounts
    arbiter_token_account: Pubkey,
) -> Instruction {
    let (vault_meta_account, vault_meta_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault_meta".as_ref(),
            &nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (fill_record_account, fill_record_account_bump) = Pubkey::find_program_address(
        &[
            vault_meta_account.as_ref(),
            &fill_nonce.to_le_bytes(),
            b"fra".as_ref(),
        ],
        &program.id(),
    );
    let (vote_record_account, vote_record_account_bump) = Pubkey::find_program_address(
        &[
            fill_record_account.as_ref(),
            arbiter.as_ref(),
            b"vra".as_ref(),
        ],
        &program.id(),
    );
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (arbiter_whitelist_entry, arbiter_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            arbiter.as_ref(),
        ],
        &program.id(),
    );
    let (vote_vault_account, vote_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"vote_vault".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::FillVoteInstruction {
                vault_meta_account: vault_meta_account,
                fill_record_account: fill_record_account,
                vote_record_account: vote_record_account,
                auction_state_account: auction_state_account,
                arbiter: arbiter,
                arbiter_whitelist_entry: arbiter_whitelist_entry,
                retail_account_owner: retail_account_owner,
                arbiter_token_account: arbiter_token_account,
                vote_vault_account: vote_vault_account,
                system_program: system_program::id(),
                rent: sysvar::rent::id(),
                token_program: spl_token::ID
            })
        .args(instruction::FillVoteInstruction {
                // Bumps
                vault_meta_account_bump: vault_meta_account_bump,
                fill_record_account_bump: fill_record_account_bump,
                vote_record_account_bump: vote_record_account_bump,
                auction_state_account_bump: auction_state_account_bump,
                arbiter_whitelist_entry_bump: arbiter_whitelist_entry_bump,
                vote_vault_account_bump: vote_vault_account_bump,
                // Seed Dependencies
                nonce: nonce,
                fill_nonce: fill_nonce,
                // Function Arguments
                vote: vote,
            })
        .instructions()
        .expect("fill_vote construction failed")
        .swap_remove(0)
}

/// This instruction is used to claim back the tokens
/// staked during a fill vote by an arbiter.
///
/// If voting with the majority, the arbiter will
/// receieve a payment from the network along with their original
/// vote. However, if the arbiter did not vote in the majority on
/// the fill, they will not receive their tokens back.
///
/// # Arguments
/// * `retail_account_owner` The public key of the retail_account_owner account.
/// * `nonce` An unsigned 64 bit integer. The nonce used as
///    a seed to generate the PDA for the vault meta account, taken
///    from the retail trader's account when the order in question was
///    initially created.
/// * `fill_nonce` An unsigned 16 bit integer. The fill nonce taken
///    from the vault meta account at fill time, and used to generate
///    the PDA for the fill record account.
/// * `arbiter` The public key of the arbiter account.
/// * `vault_meta_account_auction_id` The unsigned integer field named auction_id in
///    the vault_meta_account account.
/// * `vault_meta_account_auction_epoch` The unsigned integer field named auction_epoch
///    in the vault_meta_account account.
/// * `arbiter_bid_token_account` The SPL token account associated with the arbiter
///    used to receive a payment from the network, with the same mint
///    as the token accounts used by market makers to bid in the associated
///    auction. This account is expected to be mutable
/// * `arbiter_token_account` The SPL token account associated with the arbiter
///    used to vote on the fairness of the fill by the market maker.
///    This account is expected to be mutable
///
/// Returns an instruction
pub fn claim_vote_reward(
    program: &Program,
    // Seed Deps
    retail_account_owner: Pubkey,
    nonce: u64,
    fill_nonce: u16,
    arbiter: Pubkey,
    vault_meta_account_auction_id: u64,
    vault_meta_account_auction_epoch: u64,
    // Non-PDA Non-Seed Dep Accounts
    arbiter_bid_token_account: Pubkey,
    arbiter_token_account: Pubkey,
) -> Instruction {
    let (vault_meta_account, vault_meta_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault_meta".as_ref(),
            &nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (fill_record_account, fill_record_account_bump) = Pubkey::find_program_address(
        &[
            vault_meta_account.as_ref(),
            &fill_nonce.to_le_bytes(),
            b"fra".as_ref(),
        ],
        &program.id(),
    );
    let (vote_record_account, vote_record_account_bump) = Pubkey::find_program_address(
        &[
            fill_record_account.as_ref(),
            arbiter.as_ref(),
            b"vra".as_ref(),
        ],
        &program.id(),
    );
    let (vote_vault_account, vote_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"vote_vault".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (bid_vault_account, bid_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_vault".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
            &vault_meta_account_auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (arbiter_whitelist_entry, arbiter_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            arbiter.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::ClaimVoteRewardInstruction {
                vault_meta_account: vault_meta_account,
                fill_record_account: fill_record_account,
                vote_record_account: vote_record_account,
                vote_vault_account: vote_vault_account,
                bid_vault_account: bid_vault_account,
                auction_state_account: auction_state_account,
                auction_epoch_state: auction_epoch_state,
                arbiter_bid_token_account: arbiter_bid_token_account,
                arbiter: arbiter,
                arbiter_whitelist_entry: arbiter_whitelist_entry,
                retail_account_owner: retail_account_owner,
                arbiter_token_account: arbiter_token_account,
                system_program: system_program::id(),
                token_program: spl_token::ID
            })
        .args(instruction::ClaimVoteRewardInstruction {
                // Bumps
                vault_meta_account_bump: vault_meta_account_bump,
                fill_record_account_bump: fill_record_account_bump,
                vote_record_account_bump: vote_record_account_bump,
                vote_vault_account_bump: vote_vault_account_bump,
                bid_vault_account_bump: bid_vault_account_bump,
                auction_state_account_bump: auction_state_account_bump,
                auction_epoch_state_bump: auction_epoch_state_bump,
                arbiter_whitelist_entry_bump: arbiter_whitelist_entry_bump,
                // Seed Dependencies
                nonce: nonce,
                fill_nonce: fill_nonce,
            })
        .instructions()
        .expect("claim_vote_reward construction failed")
        .swap_remove(0)
}

/// This instruction is used to close the various
/// accounts opened during the lifecycle of the trade, and additionally
/// give a rebate to the order flow source for its order flow.
///
/// Lamports used to open the accounts will be returned
/// to the payers who opened the accounts.
///
/// # Arguments
/// * `retail_account_owner` The public key of the retail_account_owner account.
/// * `nonce` An unsigned 64 bit integer. The nonce used as
///    a seed to generate the PDA for the vault token account, taken
///    from the retail trader's account when the order in question was
///    initially created.
/// * `fill_nonce` An unsigned 16 bit integer. The fill nonce taken
///    from the vault meta account at fill time, and used to generate
///    the PDA for the fill record account.
/// * `vault_meta_account_auction_id` The unsigned integer field named auction_id in
///    the vault_meta_account account.
/// * `vault_meta_account_auction_epoch` The unsigned integer field named auction_epoch
///    in the vault_meta_account account.
/// * `arbiter` The public key of the arbiter account.
/// * `auction_state_account_bid_mint` The public key field named bid_mint in the auction_state_account
///    account.
/// * `vault_token_account_mint` The public key field named mint in the vault_token_account
///    account.
/// * `market_maker_x_token_account` The SPL token account associated with the market
///    maker, and which receives the asset sold by the retail trader
///    during a trade. This account is expected to be mutable. This
///    account is the market_maker_account_owner's associated token
///    account for the mint of the asset sent by the retail trader
/// * `retail_data_account` The retail data account holds state specific to
///    the retail trader. This account is expected to be mutable. The
///    key of retail_account_owner must match the field named retail_account_owner
///    in this account
/// * `market_maker_account_owner` The public key of the market maker. This account
///    is expected to be mutable
/// * `rebate_receiver_token_account` The SPL token account associated with the order
///    used to receive a payment from the network, with the same mint
///    as the token accounts used by market makers to bid in the associated
///    auction. This account is expected to be mutable
/// * `is_rebate_receiver_token_account_uninitialized` True if and only if the rebate receiver token
///    account is uninitialized
/// * `is_x_token_account_uninitialized` True if and only if the market maker's X token
///    account is uninitialized
///
/// Returns an instruction
pub fn close_fill_accounts(
    program: &Program,
    // Seed Deps
    retail_account_owner: Pubkey,
    nonce: u64,
    fill_nonce: u16,
    vault_meta_account_auction_id: u64,
    vault_meta_account_auction_epoch: u64,
    arbiter: Pubkey,
    auction_state_account_bid_mint: Pubkey,
    vault_token_account_mint: Pubkey,
    // Non-PDA Non-Seed Dep Accounts
    market_maker_x_token_account: Pubkey,
    retail_data_account: Pubkey,
    market_maker_account_owner: Pubkey,
    rebate_receiver_token_account: Pubkey,
    // Optional Account Flags
    is_rebate_receiver_token_account_uninitialized: bool,
    is_x_token_account_uninitialized: bool,
) -> Instruction {
    let (vault_token_account, vault_token_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault".as_ref(),
            &nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (vault_meta_account, vault_meta_account_bump) = Pubkey::find_program_address(
        &[
            retail_account_owner.as_ref(),
            b"vault_meta".as_ref(),
            &nonce.to_le_bytes(),
        ],
        &program.id(),
    );
    let (fill_record_account, fill_record_account_bump) = Pubkey::find_program_address(
        &[
            vault_meta_account.as_ref(),
            &fill_nonce.to_le_bytes(),
            b"fra".as_ref(),
        ],
        &program.id(),
    );
    let (bid_vault_account, bid_vault_account_bump) = Pubkey::find_program_address(
        &[
            b"bid_vault".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &vault_meta_account_auction_id.to_le_bytes(),
            &vault_meta_account_auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (arbiter_whitelist_entry, arbiter_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            arbiter.as_ref(),
        ],
        &program.id(),
    );
    let (bid_recovery_vault_account, bid_recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            auction_state_account_bid_mint.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    let (x_recovery_vault_account, x_recovery_vault_account_bump) = Pubkey::find_program_address(
        &[
            vault_token_account_mint.as_ref(),
            b"recovery_vault".as_ref(),
        ],
        &program.id(),
    );
    let mut remaining_accounts: Vec<AccountMeta> = Vec::new();
    if is_rebate_receiver_token_account_uninitialized {
        remaining_accounts.push(AccountMeta {pubkey: bid_recovery_vault_account, is_signer: false, is_writable: true});
    }
    if is_x_token_account_uninitialized {
        remaining_accounts.push(AccountMeta {pubkey: x_recovery_vault_account, is_signer: false, is_writable: true});
    }
    program
        .request()
        .accounts(
            accounts::CloseFillAccounts {
                vault_token_account: vault_token_account,
                vault_meta_account: vault_meta_account,
                fill_record_account: fill_record_account,
                bid_vault_account: bid_vault_account,
                auction_state_account: auction_state_account,
                auction_epoch_state: auction_epoch_state,
                market_maker_x_token_account: market_maker_x_token_account,
                retail_data_account: retail_data_account,
                market_maker_account_owner: market_maker_account_owner,
                arbiter: arbiter,
                arbiter_whitelist_entry: arbiter_whitelist_entry,
                retail_account_owner: retail_account_owner,
                rebate_receiver_token_account: rebate_receiver_token_account,
                token_program: spl_token::ID,
                system_program: system_program::id()
            })
        .accounts(remaining_accounts.to_account_metas(None))
        .args(instruction::CloseFillAccounts {
                // Bumps
                vault_token_account_bump: vault_token_account_bump,
                vault_meta_account_bump: vault_meta_account_bump,
                fill_record_account_bump: fill_record_account_bump,
                bid_vault_account_bump: bid_vault_account_bump,
                auction_state_account_bump: auction_state_account_bump,
                auction_epoch_state_bump: auction_epoch_state_bump,
                arbiter_whitelist_entry_bump: arbiter_whitelist_entry_bump,
                bid_recovery_vault_account_bump: bid_recovery_vault_account_bump,
                x_recovery_vault_account_bump: x_recovery_vault_account_bump,
                // Seed Dependencies
                nonce: nonce,
                fill_nonce: fill_nonce,
            })
        .instructions()
        .expect("close_fill_accounts construction failed")
        .swap_remove(0)
}

/// This instruction is used to initialize global
/// state for tracking the system of signatory servers.
///
/// This instruction can only be called by the DFlow
/// admin and can only be called once.
///
/// # Arguments
/// * `signatory_stake_mint` The SPL mint account associated with the token
///    that is staked by the signatory server.
/// * `dflow_admin` The public key of the DFlow admin. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn init_signatory_system(
    program: &Program,
    // Non-PDA Non-Seed Dep Accounts
    signatory_stake_mint: Pubkey,
    dflow_admin: Pubkey,
) -> Instruction {
    let (global_config_account, global_config_account_bump) = Pubkey::find_program_address(
        &[
            b"global_config".as_ref(),
        ],
        &program.id(),
    );
    let (signatory_state, signatory_state_bump) = Pubkey::find_program_address(
        &[
            b"sigstate".as_ref(),
        ],
        &program.id(),
    );
    let (signatory_stake_vault, signatory_stake_vault_bump) = Pubkey::find_program_address(
        &[
            b"sigstake".as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::InitSignatorySystem {
                global_config_account: global_config_account,
                signatory_state: signatory_state,
                signatory_stake_vault: signatory_stake_vault,
                signatory_stake_mint: signatory_stake_mint,
                dflow_admin: dflow_admin,
                system_program: system_program::id(),
                rent: sysvar::rent::id(),
                token_program: spl_token::ID
            })
        .args(instruction::InitSignatorySystem {
                // Bumps
                global_config_account_bump: global_config_account_bump,
                signatory_state_bump: signatory_state_bump,
                signatory_stake_vault_bump: signatory_stake_vault_bump,
            })
        .instructions()
        .expect("init_signatory_system construction failed")
        .swap_remove(0)
}

/// This instruction is used to stake the requisite
/// amount of tokens necessary to endorse flow as retail. This stake
/// is subject to slashing based on the quality of the order flow
/// sent by the signatory server.
///
/// Signatory server operators provide helpful network
/// participation by endorsing and forwarding retail order flow to
/// the network. This helpful network participation is rewarded,
/// or slashed based on the quality of order flow sent.
///
/// # Arguments
/// * `signatory_server` The public key of the signatory_server account.
/// * `signatory_server_token_account` The signatory server's token account used for
///    originating the stake transfer. This account is expected to be
///    mutable
///
/// Returns an instruction
pub fn create_signatory(
    program: &Program,
    // Seed Deps
    signatory_server: Pubkey,
    // Non-PDA Non-Seed Dep Accounts
    signatory_server_token_account: Pubkey,
) -> Instruction {
    let (signatory_record, signatory_record_bump) = Pubkey::find_program_address(
        &[
            signatory_server.as_ref(),
        ],
        &program.id(),
    );
    let (signatory_stake_vault, signatory_stake_vault_bump) = Pubkey::find_program_address(
        &[
            b"sigstake".as_ref(),
        ],
        &program.id(),
    );
    let (signatory_server_whitelist_entry, signatory_server_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            signatory_server.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::CreateSignatory {
                signatory_record: signatory_record,
                signatory_stake_vault: signatory_stake_vault,
                signatory_server_token_account: signatory_server_token_account,
                signatory_server: signatory_server,
                signatory_server_whitelist_entry: signatory_server_whitelist_entry,
                system_program: system_program::id(),
                rent: sysvar::rent::id(),
                token_program: spl_token::ID
            })
        .args(instruction::CreateSignatory {
                // Bumps
                signatory_record_bump: signatory_record_bump,
                signatory_stake_vault_bump: signatory_stake_vault_bump,
                signatory_server_whitelist_entry_bump: signatory_server_whitelist_entry_bump,
            })
        .instructions()
        .expect("create_signatory construction failed")
        .swap_remove(0)
}

/// This instruction is used to stake additional tokens
/// into an existing signatory server stake vault.
///
/// Signatory servers are subject to slashing, and
/// if slashed to below the stake minimum they may no longer be qualified
/// to endorse order flow as originated from a retail source.
///
/// # Arguments
/// * `signatory_server` The public key of the signatory_server account.
/// * `stake_size` The amount of signatory 'stake mint' tokens to stake
/// * `signatory_server_token_account` The signatory server's token account used for
///    originating the stake transfer. This account is expected to be
///    mutable
///
/// Returns an instruction
pub fn stake_signatory(
    program: &Program,
    // Seed Deps
    signatory_server: Pubkey,
    // Function Args
    stake_size: u64,
    // Non-PDA Non-Seed Dep Accounts
    signatory_server_token_account: Pubkey,
) -> Instruction {
    let (signatory_record, signatory_record_bump) = Pubkey::find_program_address(
        &[
            signatory_server.as_ref(),
        ],
        &program.id(),
    );
    let (signatory_stake_vault, signatory_stake_vault_bump) = Pubkey::find_program_address(
        &[
            b"sigstake".as_ref(),
        ],
        &program.id(),
    );
    let (signatory_server_whitelist_entry, signatory_server_whitelist_entry_bump) = Pubkey::find_program_address(
        &[
            b"whitelist_entry".as_ref(),
            signatory_server.as_ref(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::StakeSignatory {
                signatory_record: signatory_record,
                signatory_stake_vault: signatory_stake_vault,
                signatory_server_token_account: signatory_server_token_account,
                signatory_server: signatory_server,
                signatory_server_whitelist_entry: signatory_server_whitelist_entry,
                token_program: spl_token::ID
            })
        .args(instruction::StakeSignatory {
                // Bumps
                signatory_record_bump: signatory_record_bump,
                signatory_stake_vault_bump: signatory_stake_vault_bump,
                signatory_server_whitelist_entry_bump: signatory_server_whitelist_entry_bump,
                // Function Arguments
                stake_size: stake_size,
            })
        .instructions()
        .expect("stake_signatory construction failed")
        .swap_remove(0)
}

/// This instruction is used to close an AuctionEpochState
/// account.
///
/// The auction owner can use this instruction to
/// close the AuctionEpochState account for an old epoch that has
/// no open orders or fills pending settlement. The auction owner
/// can also use this instruction close the AuctionEpochState account
/// for the last, current, or next epoch if the auction is in the
/// Draining state.
///
/// # Arguments
/// * `auction_id` An unsigned 64 bit integer. The auction ID used
///    as a seed to generate the program derived address
/// * `auction_epoch` An unsigned 64 bit integer. The integer epoch
///    used to generate the PDA of the auction epoch account
/// * `owner` The account that created the AuctionEpochState
///    account by calling init_auction_epoch_state_account. This account
///    is a signer for the instruction. This account is expected to
///    be mutable
///
/// Returns an instruction
pub fn close_auction_epoch_state(
    program: &Program,
    // Seed Deps
    auction_id: u64,
    auction_epoch: u64,
    // Non-PDA Non-Seed Dep Accounts
    owner: Pubkey,
) -> Instruction {
    let (auction_epoch_state, auction_epoch_state_bump) = Pubkey::find_program_address(
        &[
            b"epoch".as_ref(),
            &auction_id.to_le_bytes(),
            &auction_epoch.to_le_bytes(),
        ],
        &program.id(),
    );
    let (auction_state_account, auction_state_account_bump) = Pubkey::find_program_address(
        &[
            b"auction_state".as_ref(),
            &auction_id.to_le_bytes(),
        ],
        &program.id(),
    );
    program
        .request()
        .accounts(
            accounts::CloseAuctionEpochState {
                auction_epoch_state: auction_epoch_state,
                auction_state_account: auction_state_account,
                owner: owner,
                system_program: system_program::id()
            })
        .args(instruction::CloseAuctionEpochState {
                // Bumps
                auction_epoch_state_bump: auction_epoch_state_bump,
                auction_state_account_bump: auction_state_account_bump,
                // Seed Dependencies
                auction_id: auction_id,
                auction_epoch: auction_epoch,
            })
        .instructions()
        .expect("close_auction_epoch_state construction failed")
        .swap_remove(0)
}