boundless-cli 0.14.1

CLI for the Boundless, the universal ZK protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
// Copyright 2025 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The Boundless CLI is a command-line interface for interacting with Boundless.

const CLI_LONG_ABOUT: &str = r#"
The Boundless CLI is a command-line interface for interacting with Boundless.

# Examples

```sh
RPC_URL=https://ethereum-sepolia-rpc.publicnode.com \
boundless account balance 0x3da7206e104f6d5dd070bfe06c5373cc45c3e65c
```

```sh
RPC_URL=https://ethereum-sepolia-rpc.publicnode.com \
PRIVATE_KEY=0x0000000000000000000000000000000000000000000000000000000000000000 \
boundless request submit-offer --wait --input "hello" \
--program-url http://dweb.link/ipfs/bafkreido62tz2uyieb3s6wmixwmg43hqybga2ztmdhimv7njuulf3yug4e
```

# Required options

An Ethereum RPC URL is required via the `RPC_URL` environment variable or the `--rpc-url`
flag. You can use a public RPC endpoint for most operations, but it is best to use an RPC
endpoint that supports events (e.g. Alchemy or Infura).

Sending, fulfilling, and slashing requests requires a signer provided via the `PRIVATE_KEY`
environment variable or `--private-key`. This CLI only supports in-memory private keys as of
this version. Full signer support is available in the SDK."#;

use std::{
    borrow::Cow,
    fs::File,
    io::BufReader,
    num::ParseIntError,
    ops::Deref,
    path::{Path, PathBuf},
    time::{Duration, SystemTime},
};

use alloy::{
    network::Ethereum,
    primitives::{
        utils::{format_ether, format_units, parse_ether, parse_units},
        Address, FixedBytes, TxKind, B256, U256,
    },
    providers::{Provider, ProviderBuilder},
    rpc::types::{TransactionInput, TransactionRequest},
    signers::local::PrivateKeySigner,
    sol_types::SolValue,
};
use anyhow::{anyhow, bail, ensure, Context, Result};
use bonsai_sdk::non_blocking::Client as BonsaiClient;
use boundless_cli::{convert_timestamp, DefaultProver, OrderFulfilled};
use clap::{Args, CommandFactory, Parser, Subcommand};
use clap_complete::aot::Shell;
use risc0_aggregation::SetInclusionReceiptVerifierParameters;
use risc0_ethereum_contracts::{set_verifier::SetVerifierService, IRiscZeroVerifier};
use risc0_zkvm::{
    compute_image_id, default_executor,
    sha::{Digest, Digestible},
    Journal, SessionInfo,
};
use shadow_rs::shadow;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use url::Url;

use boundless_market::{
    contracts::{
        boundless_market::{BoundlessMarketService, FulfillmentTx, UnlockedRequest},
        Offer, ProofRequest, RequestInputType, Selector,
    },
    input::GuestEnv,
    request_builder::{OfferParams, RequirementParams},
    selector::ProofType,
    storage::{fetch_url, StorageProvider, StorageProviderConfig},
    Client, Deployment, StandardClient,
};

shadow!(build);

#[derive(Subcommand, Clone, Debug)]
enum Command {
    /// Account management commands
    #[command(subcommand)]
    Account(Box<AccountCommands>),

    /// Proof request commands
    #[command(subcommand)]
    Request(Box<RequestCommands>),

    /// Proof execution commands
    #[command(subcommand)]
    Proving(Box<ProvingCommands>),

    /// Operations on the boundless market
    #[command(subcommand)]
    Ops(Box<OpsCommands>),

    /// Display configuration and environment variables
    Config {},

    /// Print shell completions (e.g. for bash or zsh) to stdout.
    Completions { shell: Shell },
}

#[derive(Subcommand, Clone, Debug)]
enum OpsCommands {
    /// Slash a prover for a given request
    Slash {
        /// The proof request identifier
        request_id: U256,
    },
}

#[derive(Subcommand, Clone, Debug)]
enum AccountCommands {
    /// Deposit funds into the market
    Deposit {
        /// Amount in ether to deposit
        #[clap(value_parser = parse_ether)]
        amount: U256,
    },
    /// Withdraw funds from the market
    Withdraw {
        /// Amount in ether to withdraw
        #[clap(value_parser = parse_ether)]
        amount: U256,
    },
    /// Check the balance of an account in the market
    Balance {
        /// Address to check the balance of;
        /// if not provided, defaults to the wallet address
        address: Option<Address>,
    },
    /// Deposit stake funds into the market
    DepositStake {
        /// Amount to deposit in HP or USDC based on the chain ID.
        amount: String,
    },
    /// Withdraw stake funds from the market
    WithdrawStake {
        /// Amount to withdraw in HP or USDC based on the chain ID.
        amount: String,
    },
    /// Check the stake balance of an account in the market
    StakeBalance {
        /// Address to check the balance of;
        /// if not provided, defaults to the wallet address
        address: Option<Address>,
    },
}

#[derive(Subcommand, Clone, Debug)]
enum RequestCommands {
    /// Submit a proof request constructed with the given offer, input, and image
    SubmitOffer(Box<SubmitOfferArgs>),

    /// Submit a fully specified proof request
    Submit {
        /// Path to a YAML file containing the request
        yaml_request: PathBuf,

        /// Wait until the request is fulfilled
        #[clap(short, long, default_value = "false")]
        wait: bool,

        /// Submit the request offchain via the provided order stream service url
        #[clap(short, long)]
        offchain: bool,

        /// Skip preflight check (not recommended)
        #[clap(long, default_value = "false")]
        no_preflight: bool,

        /// Configuration for the StorageProvider to use for uploading programs and inputs.
        #[clap(flatten, next_help_heading = "Storage Provider")]
        storage_config: Box<StorageProviderConfig>,
    },

    /// Get the status of a given request
    Status {
        /// The proof request identifier
        request_id: U256,

        /// The time at which the request expires, in seconds since the UNIX epoch
        expires_at: Option<u64>,
    },

    /// Get the journal and seal for a given request
    GetProof {
        /// The proof request identifier
        request_id: U256,
    },

    /// Verify the proof of the given request against the SetVerifier contract
    VerifyProof {
        /// The proof request identifier
        request_id: U256,

        /// The image id of the original request
        image_id: B256,
    },
}

#[derive(Subcommand, Clone, Debug)]
enum ProvingCommands {
    /// Execute a proof request using the RISC Zero zkVM executor
    Execute {
        /// Path to a YAML file containing the request.
        ///
        /// If provided, the request will be loaded from the given file path.
        #[arg(long, conflicts_with_all = ["request_id", "tx_hash"])]
        request_path: Option<PathBuf>,

        /// The proof request identifier.
        ///
        /// If provided, the request will be fetched from the blockchain.
        #[arg(long, conflicts_with = "request_path")]
        request_id: Option<U256>,

        /// The request digest
        ///
        /// If provided along with request-id, uses the request digest to find the request.
        #[arg(long)]
        request_digest: Option<B256>,

        /// The tx hash of the request submission.
        ///
        /// If provided along with request-id, uses the transaction hash to find the request.
        #[arg(long, conflicts_with = "request_path", requires = "request_id")]
        tx_hash: Option<B256>,
    },
    Benchmark {
        /// Proof request ids to benchmark.
        #[arg(long, value_delimiter = ',')]
        request_ids: Vec<U256>,

        /// Bonsai API URL
        ///
        /// Toggling this disables Bento proving and uses Bonsai as a backend
        #[clap(env = "BONSAI_API_URL")]
        bonsai_api_url: Option<String>,

        /// Bonsai API Key
        ///
        /// Not necessary if using Bento without authentication, which is the default.
        #[clap(env = "BONSAI_API_KEY", hide_env_values = true)]
        bonsai_api_key: Option<String>,

        /// Use the default prover instead of defaulting to Bento.
        ///
        /// When enabled, the prover selection follows the default zkVM behavior
        /// based on environment variables like RISC0_PROVER, RISC0_DEV_MODE, etc.
        #[clap(long, default_value = "false")]
        use_default_prover: bool,
    },
    /// Fulfill one or more proof requests using the RISC Zero zkVM default prover.
    ///
    /// This command can process multiple requests in a single batch, which is more efficient
    /// than fulfilling requests individually.
    ///
    /// Example usage:
    ///   --request-ids 0x123,0x456,0x789  # Comma-separated list of request IDs
    ///   --request-digests 0xabc,0xdef,0x012  # Optional, must match request_ids length and order
    ///   --tx-hashes 0x111,0x222,0x333  # Optional, must match request_ids length and order
    Fulfill {
        /// The proof requests identifiers (comma-separated list of hex values)
        #[arg(long, value_delimiter = ',')]
        request_ids: Vec<U256>,

        /// The request digests (comma-separated list of hex values).
        /// If provided, must have the same length and order as request_ids.
        #[arg(long, value_delimiter = ',')]
        request_digests: Option<Vec<B256>>,

        /// The tx hash of the requests submissions (comma-separated list of hex values).
        /// If provided, must have the same length and order as request_ids.
        #[arg(long, value_delimiter = ',')]
        tx_hashes: Option<Vec<B256>>,

        /// Withdraw the funds after fulfilling the requests
        #[arg(long, default_value = "false")]
        withdraw: bool,

        /// Bonsai API URL
        ///
        /// Toggling this disables Bento proving and uses Bonsai as a backend
        #[clap(env = "BONSAI_API_URL")]
        bonsai_api_url: Option<String>,

        /// Bonsai API Key
        ///
        /// Not necessary if using Bento without authentication, which is the default.
        #[clap(env = "BONSAI_API_KEY", hide_env_values = true)]
        bonsai_api_key: Option<String>,

        /// Use the default prover instead of defaulting to Bento.
        ///
        /// When enabled, the prover selection follows the default zkVM behavior
        /// based on environment variables like RISC0_PROVER, RISC0_DEV_MODE, etc.
        #[clap(long, default_value = "false")]
        use_default_prover: bool,
    },

    /// Lock a request in the market
    Lock {
        /// The proof request identifier
        #[arg(long)]
        request_id: U256,

        /// The request digest
        #[arg(long)]
        request_digest: Option<B256>,

        /// The tx hash of the request submission
        #[arg(long)]
        tx_hash: Option<B256>,
    },
}

#[derive(Args, Clone, Debug)]
struct SubmitOfferArgs {
    /// Optional identifier for the request
    id: Option<u32>,

    #[clap(flatten)]
    program: SubmitOfferProgram,

    /// Wait until the request is fulfilled
    #[clap(short, long, default_value = "false")]
    wait: bool,

    /// Submit the request offchain via the provided order stream service url
    #[clap(short, long)]
    offchain: bool,

    /// Use risc0_zkvm::serde to encode the input as a `Vec<u8>`
    #[clap(long)]
    encode_input: bool,

    #[clap(flatten)]
    input: SubmitOfferInput,

    #[clap(flatten)]
    requirements: SubmitOfferRequirements,

    #[clap(flatten, next_help_heading = "Offer")]
    offer_params: OfferParams,

    /// Configuration for the StorageProvider to use for uploading programs and inputs.
    #[clap(flatten, next_help_heading = "Storage Provider")]
    storage_config: StorageProviderConfig,
}

#[derive(Args, Clone, Debug)]
#[group(required = true, multiple = false)]
struct SubmitOfferInput {
    /// Input for the guest, given as a string.
    #[clap(long)]
    input: Option<String>,
    /// Input for the guest, given as a path to a file.
    #[clap(long)]
    input_file: Option<PathBuf>,
}

#[derive(Args, Clone, Debug)]
#[group(required = true, multiple = false)]
struct SubmitOfferProgram {
    /// Program binary to use as the guest image, given as a path.
    ///
    /// The program will be uploaded to a public URL using the configured storage provider before
    /// the proof request is sent.
    #[clap(short = 'p', long = "program")]
    path: Option<PathBuf>,
    /// Program binary to use as a guest image, given as a public URL.
    ///
    /// This option accepts a pre-uploaded program. If also using small inputs, a storage provider
    /// is not required when using a pre-uploaded program.
    #[clap(long = "program-url")]
    url: Option<Url>,
}

#[derive(Args, Clone, Debug)]
struct SubmitOfferRequirements {
    /// Address of the callback to use in the requirements.
    #[clap(long, requires = "callback_gas_limit")]
    callback_address: Option<Address>,
    /// Gas limit of the callback to use in the requirements.
    #[clap(long, requires = "callback_address")]
    callback_gas_limit: Option<u64>,
    /// Request a groth16 proof (i.e., a Groth16).
    #[clap(long, default_value = "any")]
    proof_type: ProofType,
}

/// Common configuration options for all commands
#[derive(Args, Debug, Clone)]
struct GlobalConfig {
    /// URL of the Ethereum RPC endpoint
    #[clap(short, long, env = "RPC_URL")]
    rpc_url: Url,

    /// Private key of the wallet (without 0x prefix)
    #[clap(long, env = "PRIVATE_KEY", global = true, hide_env_values = true)]
    private_key: Option<PrivateKeySigner>,

    /// Ethereum transaction timeout in seconds.
    #[clap(long, env = "TX_TIMEOUT", global = true, value_parser = |arg: &str| -> Result<Duration, ParseIntError> {Ok(Duration::from_secs(arg.parse()?))})]
    tx_timeout: Option<Duration>,

    /// Log level (error, warn, info, debug, trace)
    #[clap(long, env = "LOG_LEVEL", global = true, default_value = "info")]
    log_level: LevelFilter,

    #[clap(flatten, next_help_heading = "Boundless Deployment")]
    deployment: Option<Deployment>,
}

#[derive(Parser, Debug)]
#[clap(author, long_version = build::CLAP_LONG_VERSION, about = "CLI for Boundless", long_about = CLI_LONG_ABOUT)]
struct MainArgs {
    /// Subcommand to run
    #[command(subcommand)]
    command: Command,

    #[command(flatten)]
    config: GlobalConfig,
}

/// Return true if the subcommand requires a private key.
// NOTE: It does not appear this is possible with clap natively
fn private_key_required(cmd: &Command) -> bool {
    match cmd {
        Command::Ops(cmd) => match cmd.deref() {
            OpsCommands::Slash { .. } => true,
        },
        Command::Config { .. } => false,
        Command::Account(cmd) => match cmd.deref() {
            AccountCommands::Balance { .. } => false,
            AccountCommands::Deposit { .. } => true,
            AccountCommands::DepositStake { .. } => true,
            AccountCommands::StakeBalance { .. } => false,
            AccountCommands::Withdraw { .. } => true,
            AccountCommands::WithdrawStake { .. } => true,
        },
        Command::Request(cmd) => match cmd.deref() {
            RequestCommands::GetProof { .. } => false,
            RequestCommands::Status { .. } => false,
            RequestCommands::Submit { .. } => true,
            RequestCommands::SubmitOffer { .. } => true,
            RequestCommands::VerifyProof { .. } => false,
        },
        Command::Proving(cmd) => match cmd.deref() {
            ProvingCommands::Benchmark { .. } => false,
            ProvingCommands::Execute { .. } => false,
            ProvingCommands::Fulfill { .. } => true,
            ProvingCommands::Lock { .. } => true,
        },
        Command::Completions { .. } => false,
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let args = match MainArgs::try_parse() {
        Ok(args) => args,
        Err(err) => {
            if err.kind() == clap::error::ErrorKind::DisplayHelp {
                // If it's a help request, print the help and exit successfully
                err.print()?;
                return Ok(());
            }
            if err.kind() == clap::error::ErrorKind::DisplayVersion {
                // If it's a version request, print the version and exit successfully
                err.print()?;
                return Ok(());
            }
            return Err(err.into());
        }
    };

    tracing_subscriber::registry()
        .with(fmt::layer())
        .with(
            EnvFilter::builder()
                .with_default_directive(args.config.log_level.into())
                .from_env_lossy(),
        )
        .init();

    run(&args).await
}

pub(crate) async fn run(args: &MainArgs) -> Result<()> {
    if private_key_required(&args.command) && args.config.private_key.is_none() {
        eprintln!("A private key is required to run this subcommand");
        eprintln!("Please provide a private key with --private-key or the PRIVATE_KEY environment variable");
        bail!("Private key required");
    }

    // If the config command is being run, don't create a client.
    if let Command::Config {} = &args.command {
        return handle_config_command(args).await;
    }
    if let Command::Completions { shell } = &args.command {
        // TODO: Because of where this is, running the completions command requires an RPC_URL to
        // be set. We should address this, but its also not a major issue.
        clap_complete::generate(
            *shell,
            &mut MainArgs::command(),
            "boundless",
            &mut std::io::stdout(),
        );
        return Ok(());
    }

    let storage_config = match args.command {
        Command::Request(ref req_cmd) => match **req_cmd {
            RequestCommands::Submit { ref storage_config, .. } => (**storage_config).clone(),
            RequestCommands::SubmitOffer(ref args) => args.storage_config.clone(),
            _ => StorageProviderConfig::default(),
        },
        _ => StorageProviderConfig::default(),
    };

    let client = Client::builder()
        .with_signer(args.config.private_key.clone())
        .with_rpc_url(args.config.rpc_url.clone())
        .with_deployment(args.config.deployment.clone())
        .with_storage_provider_config(&storage_config)?
        .with_timeout(args.config.tx_timeout)
        .build()
        .await
        .context("Failed to build Boundless client")?;

    match &args.command {
        Command::Account(account_cmd) => handle_account_command(account_cmd, client).await,
        Command::Request(request_cmd) => handle_request_command(request_cmd, client).await,
        Command::Proving(proving_cmd) => handle_proving_command(proving_cmd, client).await,
        Command::Ops(operation_cmd) => handle_ops_command(operation_cmd, client).await,
        Command::Config {} => unreachable!(),
        Command::Completions { .. } => unreachable!(),
    }
}

/// Handle ops-related commands
async fn handle_ops_command(cmd: &OpsCommands, client: StandardClient) -> Result<()> {
    match cmd {
        OpsCommands::Slash { request_id } => {
            tracing::info!("Slashing prover for request 0x{:x}", request_id);
            client.boundless_market.slash(*request_id).await?;
            tracing::info!("Successfully slashed prover for request 0x{:x}", request_id);
            Ok(())
        }
    }
}

/// Helper function to parse stake amounts with validation
async fn parse_stake_amount(
    client: &StandardClient,
    amount: &str,
) -> Result<(U256, String, String)> {
    let symbol = client.boundless_market.stake_token_symbol().await?;
    let decimals = client.boundless_market.stake_token_decimals().await?;
    let parsed_amount =
        parse_units(amount, decimals).map_err(|e| anyhow!("Failed to parse amount: {}", e))?.into();
    if parsed_amount == U256::from(0) {
        bail!("Amount is below the denomination minimum: {}", amount);
    }
    let formatted_amount = format_units(parsed_amount, decimals)?;
    Ok((parsed_amount, formatted_amount, symbol))
}

/// Handle account-related commands
async fn handle_account_command(cmd: &AccountCommands, client: StandardClient) -> Result<()> {
    match cmd {
        AccountCommands::Deposit { amount } => {
            tracing::info!("Depositing {} ETH into the market", format_ether(*amount));
            client.boundless_market.deposit(*amount).await?;
            tracing::info!("Successfully deposited {} ETH into the market", format_ether(*amount));
            Ok(())
        }
        AccountCommands::Withdraw { amount } => {
            tracing::info!("Withdrawing {} ETH from the market", format_ether(*amount));
            client.boundless_market.withdraw(*amount).await?;
            tracing::info!("Successfully withdrew {} ETH from the market", format_ether(*amount));
            Ok(())
        }
        AccountCommands::Balance { address } => {
            let addr = address.unwrap_or(client.boundless_market.caller());
            if addr == Address::ZERO {
                bail!("No address specified for balance query. Please provide an address or a private key.")
            }
            tracing::info!("Checking balance for address {}", addr);
            let balance = client.boundless_market.balance_of(addr).await?;
            tracing::info!("Balance for address {}: {} ETH", addr, format_ether(balance));
            Ok(())
        }
        AccountCommands::DepositStake { amount } => {
            let (parsed_amount, formatted_amount, symbol) =
                parse_stake_amount(&client, amount).await?;

            tracing::info!("Depositing {formatted_amount} {symbol} as stake");
            match client
                .boundless_market
                .deposit_stake_with_permit(parsed_amount, &client.signer.unwrap())
                .await
            {
                Ok(_) => {
                    tracing::info!("Successfully deposited {formatted_amount} {symbol} as stake");
                    Ok(())
                }
                Err(e) => {
                    if e.to_string().contains("TRANSFER_FROM_FAILED") {
                        let addr = client.boundless_market.caller();
                        Err(anyhow!(
                            "Failed to deposit stake: Ensure your address ({}) has funds on the {symbol} contract", addr
                        ))
                    } else {
                        Err(anyhow!("Failed to deposit stake: {}", e))
                    }
                }
            }
        }
        AccountCommands::WithdrawStake { amount } => {
            let (parsed_amount, formatted_amount, symbol) =
                parse_stake_amount(&client, amount).await?;
            tracing::info!("Withdrawing {formatted_amount} {symbol} from stake");
            client.boundless_market.withdraw_stake(parsed_amount).await?;
            tracing::info!("Successfully withdrew {formatted_amount} {symbol} from stake");
            Ok(())
        }
        AccountCommands::StakeBalance { address } => {
            let symbol = client.boundless_market.stake_token_symbol().await?;
            let decimals = client.boundless_market.stake_token_decimals().await?;
            let addr = address.unwrap_or(client.boundless_market.caller());
            if addr == Address::ZERO {
                bail!("No address specified for stake balance query. Please provide an address or a private key.")
            }
            tracing::info!("Checking stake balance for address {}", addr);
            let balance = client.boundless_market.balance_of_stake(addr).await?;
            let balance = format_units(balance, decimals)
                .map_err(|e| anyhow!("Failed to format stake balance: {}", e))?;
            tracing::info!("Stake balance for address {}: {} {}", addr, balance, symbol);
            Ok(())
        }
    }
}

/// Handle request-related commands
async fn handle_request_command(cmd: &RequestCommands, client: StandardClient) -> Result<()> {
    match cmd {
        RequestCommands::SubmitOffer(offer_args) => {
            tracing::info!("Submitting new proof request with offer");
            submit_offer(client, offer_args).await
        }
        RequestCommands::Submit { yaml_request, wait, offchain, no_preflight, .. } => {
            tracing::info!("Submitting proof request from YAML file");

            submit_request(
                yaml_request,
                client,
                SubmitOptions { wait: *wait, offchain: *offchain, preflight: !*no_preflight },
            )
            .await
        }
        RequestCommands::Status { request_id, expires_at } => {
            tracing::info!("Checking status for request 0x{:x}", request_id);
            let status = client.boundless_market.get_status(*request_id, *expires_at).await?;
            tracing::info!("Request 0x{:x} status: {:?}", request_id, status);
            Ok(())
        }
        RequestCommands::GetProof { request_id } => {
            tracing::info!("Fetching proof for request 0x{:x}", request_id);
            let (journal, seal) =
                client.boundless_market.get_request_fulfillment(*request_id).await?;
            tracing::info!("Successfully retrieved proof for request 0x{:x}", request_id);
            tracing::info!(
                "Journal: {} - Seal: {}",
                serde_json::to_string_pretty(&journal)?,
                serde_json::to_string_pretty(&seal)?
            );
            Ok(())
        }
        RequestCommands::VerifyProof { request_id, image_id } => {
            tracing::info!("Verifying proof for request 0x{:x}", request_id);
            let (journal, seal) =
                client.boundless_market.get_request_fulfillment(*request_id).await?;
            let journal_digest = <[u8; 32]>::from(Journal::new(journal.to_vec()).digest()).into();
            let verifier_address = client.deployment.verifier_router_address.context("no address provided for the verifier router; specify a verifier address with --verifier-address")?;
            let verifier = IRiscZeroVerifier::new(verifier_address, client.provider());

            verifier
                .verify(seal, *image_id, journal_digest)
                .call()
                .await
                .map_err(|_| anyhow::anyhow!("Verification failed"))?;

            tracing::info!("Successfully verified proof for request 0x{:x}", request_id);
            Ok(())
        }
    }
}

/// Handle proving-related commands
async fn handle_proving_command(cmd: &ProvingCommands, client: StandardClient) -> Result<()> {
    match cmd {
        ProvingCommands::Execute { request_path, request_id, request_digest, tx_hash } => {
            tracing::info!("Executing proof request");
            let request: ProofRequest = if let Some(file_path) = request_path {
                tracing::debug!("Loading request from file: {:?}", file_path);
                let file = File::open(file_path).context("failed to open request file")?;
                let reader = BufReader::new(file);
                serde_yaml::from_reader(reader).context("failed to parse request from YAML")?
            } else if let Some(request_id) = request_id {
                tracing::debug!("Loading request from blockchain: 0x{:x}", request_id);
                let (req, _signature) =
                    client.fetch_proof_request(*request_id, *tx_hash, *request_digest).await?;
                // TODO: We should check the signature here. If the signature is invalid, this
                // might lead to wasted time. Note though that if the signature is invalid it can
                // never be used to effect onchain state (e.g. locking or fulfilling).
                req
            } else {
                bail!("execute requires either a request file path or request ID")
            };

            let session_info = execute(&request).await?;
            let journal = session_info.journal.bytes;

            if !request.requirements.predicate.eval(&journal) {
                tracing::error!("Predicate evaluation failed for request");
                bail!("Predicate evaluation failed");
            }

            tracing::info!("Successfully executed request 0x{:x}", request.id);
            tracing::debug!("Journal: {:?}", journal);
            Ok(())
        }
        ProvingCommands::Fulfill {
            request_ids,
            request_digests,
            tx_hashes,
            withdraw,
            bonsai_api_url,
            bonsai_api_key,
            use_default_prover,
        } => {
            if request_digests.is_some()
                && request_ids.len() != request_digests.as_ref().unwrap().len()
            {
                bail!("request_ids and request_digests must have the same length");
            }
            if tx_hashes.is_some() && request_ids.len() != tx_hashes.as_ref().unwrap().len() {
                bail!("request_ids and tx_hashes must have the same length");
            }

            let request_ids_string =
                request_ids.iter().map(|id| format!("0x{id:x}")).collect::<Vec<_>>().join(", ");
            tracing::info!("Fulfilling proof requests {}", request_ids_string);

            // Configure proving backend (defaults to bento like benchmark command)
            configure_proving_backend(bonsai_api_url, bonsai_api_key, *use_default_prover);

            let (_, market_url) = client.boundless_market.image_info().await?;
            tracing::debug!("Fetching Assessor program from {}", market_url);
            let assessor_program = fetch_url(&market_url).await?;
            let domain = client.boundless_market.eip712_domain().await?;

            let (_, set_builder_url) = client.set_verifier.image_info().await?;
            tracing::debug!("Fetching SetBuilder program from {}", set_builder_url);
            let set_builder_program = fetch_url(&set_builder_url).await?;

            let prover = DefaultProver::new(
                set_builder_program,
                assessor_program,
                client.boundless_market.caller(),
                domain,
            )?;

            let fetch_order_jobs = request_ids.iter().enumerate().map(|(i, request_id)| {
                let client = client.clone();
                let boundless_market = client.boundless_market.clone();
                async move {
                    let (req, sig) = client
                        .fetch_proof_request(
                            *request_id,
                            tx_hashes.as_ref().map(|tx_hashes| tx_hashes[i]),
                            request_digests.as_ref().map(|request_digests| request_digests[i]),
                        )
                        .await?;
                    tracing::debug!("Fetched order details: {req:?}");

                    if !req.is_smart_contract_signed() {
                        req.verify_signature(
                            &sig,
                            client.deployment.boundless_market_address,
                            boundless_market.get_chain_id().await?,
                        )?;
                    } else {
                        // TODO: Provide a way to check the EIP1271 auth.
                        tracing::debug!(
                            "Skipping authorization check on smart contract signed request 0x{:x}",
                            U256::from(req.id)
                        );
                    }
                    let is_locked = boundless_market.is_locked(*request_id).await?;
                    Ok::<_, anyhow::Error>((req, sig, is_locked))
                }
            });

            let results = futures::future::join_all(fetch_order_jobs).await;
            let mut orders = Vec::new();
            let mut unlocked_requests = Vec::new();

            for result in results {
                let (req, sig, is_locked) = result?;
                // If the request is not locked in, we need to "price" which checks the requirements
                // and assigns a price. Otherwise, we don't. This vec will be a singleton if not locked
                // and empty if the request is locked.
                if !is_locked {
                    unlocked_requests.push(UnlockedRequest::new(req.clone(), sig.clone()));
                }
                orders.push((req, sig));
            }

            let (fills, root_receipt, assessor_receipt) = prover.fulfill(&orders).await?;
            let order_fulfilled = OrderFulfilled::new(fills, root_receipt, assessor_receipt)?;
            let boundless_market = client.boundless_market.clone();

            let fulfillment_tx =
                FulfillmentTx::new(order_fulfilled.fills, order_fulfilled.assessorReceipt)
                    .with_submit_root(
                        client.deployment.set_verifier_address,
                        order_fulfilled.root,
                        order_fulfilled.seal,
                    )
                    .with_unlocked_requests(unlocked_requests)
                    .with_withdraw(*withdraw);
            match boundless_market.fulfill(fulfillment_tx).await {
                Ok(_) => {
                    tracing::info!("Successfully fulfilled requests {}", request_ids_string);
                    Ok(())
                }
                Err(e) => {
                    tracing::error!("Failed to fulfill requests {}: {}", request_ids_string, e);
                    bail!("Failed to fulfill request: {}", e)
                }
            }
        }
        ProvingCommands::Lock { request_id, request_digest, tx_hash } => {
            tracing::info!("Locking proof request 0x{:x}", request_id);

            let (request, signature) =
                client.fetch_proof_request(*request_id, *tx_hash, *request_digest).await?;
            tracing::debug!("Fetched order details: {request:?}");

            // If the request is smart contract signed, the preflight of the lock request
            // transaction will revert, since it includes the ERC1271 signature check.
            if !request.is_smart_contract_signed() {
                request.verify_signature(
                    &signature,
                    client.deployment.boundless_market_address,
                    client.boundless_market.get_chain_id().await?,
                )?;
            }

            client.boundless_market.lock_request(&request, signature, None).await?;
            tracing::info!("Successfully locked request 0x{:x}", request_id);
            Ok(())
        }
        ProvingCommands::Benchmark {
            request_ids,
            bonsai_api_url,
            bonsai_api_key,
            use_default_prover,
        } => {
            benchmark(client, request_ids, bonsai_api_url, bonsai_api_key, *use_default_prover)
                .await
        }
    }
}

/// Configure proving backend to default to Bento unless Bonsai is explicitly specified or default prover is requested
fn configure_proving_backend(
    bonsai_api_url: &Option<String>,
    bonsai_api_key: &Option<String>,
    use_default_prover: bool,
) {
    if use_default_prover {
        tracing::info!(
            "Using default prover behavior (respects RISC0_PROVER, RISC0_DEV_MODE, etc.)"
        );
        return;
    }

    const DEFAULT_BENTO_API_URL: &str = "http://localhost:8081";
    if let Some(url) = bonsai_api_url.as_ref() {
        tracing::info!("Using Bonsai endpoint: {}", url);
    } else {
        tracing::info!("Defaulting to Bento endpoint: {}", DEFAULT_BENTO_API_URL);
        std::env::set_var("BONSAI_API_URL", DEFAULT_BENTO_API_URL);
    };
    if bonsai_api_key.is_none() {
        tracing::debug!("Assuming Bento, setting BONSAI_API_KEY to empty string");
        std::env::set_var("BONSAI_API_KEY", "");
    }
}

/// Execute a proof request using the RISC Zero zkVM executor and measure performance
async fn benchmark(
    client: StandardClient,
    request_ids: &[U256],
    bonsai_api_url: &Option<String>,
    bonsai_api_key: &Option<String>,
    use_default_prover: bool,
) -> Result<()> {
    tracing::info!("Starting benchmark for {} requests", request_ids.len());
    if request_ids.is_empty() {
        bail!("No request IDs provided");
    }

    configure_proving_backend(bonsai_api_url, bonsai_api_key, use_default_prover);
    let prover = BonsaiClient::from_env(risc0_zkvm::VERSION)?;

    // Track performance metrics across all runs
    let mut worst_khz = f64::MAX;
    let mut worst_time = 0.0;
    let mut worst_cycles = 0.0;
    let mut worst_request_id = U256::ZERO;

    // Check if we can connect to PostgreSQL using environment variables
    let pg_pool = match create_pg_pool().await {
        Ok(pool) => {
            tracing::info!("Successfully connected to PostgreSQL database");
            Some(pool)
        }
        Err(e) => {
            tracing::warn!("Failed to connect to PostgreSQL database: {}", e);
            None
        }
    };

    for (idx, request_id) in request_ids.iter().enumerate() {
        tracing::info!(
            "Benchmarking request {}/{}: 0x{:x}",
            idx + 1,
            request_ids.len(),
            request_id
        );

        let (request, _signature) = client.fetch_proof_request(*request_id, None, None).await?;
        // TODO: We should check the signature here. If the signature is invalid, this might lead
        // to wasted time on an invalid request. This is acceptable for now because the purpose of
        // this command is benchmarking.

        tracing::debug!("Fetched request 0x{:x}", request_id);
        tracing::debug!("Image URL: {}", request.imageUrl);

        // Fetch ELF and input
        tracing::debug!("Fetching ELF from {}", request.imageUrl);
        let elf = fetch_url(&request.imageUrl).await?;

        tracing::debug!("Processing input");
        let input = match request.input.inputType {
            RequestInputType::Inline => GuestEnv::decode(&request.input.data)?.stdin,
            RequestInputType::Url => {
                let input_url = std::str::from_utf8(&request.input.data)
                    .context("Input URL is not valid UTF-8")?;
                tracing::debug!("Fetching input from {}", input_url);
                GuestEnv::decode(&fetch_url(input_url).await?)?.stdin
            }
            _ => bail!("Unsupported input type"),
        };

        // Upload ELF
        let image_id = compute_image_id(&elf)?.to_string();
        prover.upload_img(&image_id, elf).await.unwrap();
        tracing::debug!("Uploaded ELF to {}", image_id);

        // Upload input
        let input_id =
            prover.upload_input(input).await.context("Failed to upload set-builder input")?;
        tracing::debug!("Uploaded input to {}", input_id);

        let assumptions = vec![];

        // Start timing
        let start_time = std::time::Instant::now();

        let proof_id =
            prover.create_session(image_id, input_id, assumptions.clone(), false).await?;
        tracing::debug!("Created session {}", proof_id.uuid);

        let (stats, elapsed_time) = loop {
            let status = proof_id.status(&prover).await?;

            match status.status.as_ref() {
                "RUNNING" => {
                    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
                    continue;
                }
                "SUCCEEDED" => {
                    let Some(stats) = status.stats else {
                        bail!("Bento failed to return proof stats in response");
                    };
                    break (stats, status.elapsed_time);
                }
                _ => {
                    let err_msg = status.error_msg.unwrap_or_default();
                    bail!("stark proving failed: {err_msg}");
                }
            }
        };

        // Try to get effective KHz from PostgreSQL if available
        let (total_cycles, elapsed_secs) = if let Some(ref pool) = pg_pool {
            let total_cycles_query = r#"
                SELECT (output->>'total_cycles')::FLOAT8
                FROM tasks
                WHERE task_id = 'init' AND job_id = $1::uuid
            "#;

            let elapsed_secs_query = r#"
                SELECT EXTRACT(EPOCH FROM (MAX(updated_at) - MIN(started_at)))::FLOAT8
                FROM tasks
                WHERE job_id = $1::uuid
            "#;

            let total_cycles: f64 =
                sqlx::query_scalar(total_cycles_query).bind(&proof_id.uuid).fetch_one(pool).await?;

            let elapsed_secs: f64 =
                sqlx::query_scalar(elapsed_secs_query).bind(&proof_id.uuid).fetch_one(pool).await?;

            (total_cycles, elapsed_secs)
        } else {
            // Calculate the hz based on the duration and total cycles as observed by the client
            tracing::debug!("No PostgreSQL data found for job, using client-side calculation.");
            let total_cycles: f64 = stats.total_cycles as f64;
            let elapsed_secs = start_time.elapsed().as_secs_f64();
            (total_cycles, elapsed_secs)
        };

        let khz = (total_cycles / 1000.0) / elapsed_secs;

        tracing::info!("KHz: {:.2} proved in {:.2}s", khz, elapsed_secs);

        if let Some(time) = elapsed_time {
            tracing::debug!("Server side time: {:?}", time);
        }

        // Track worst-case performance
        if khz < worst_khz {
            worst_khz = khz;
            worst_time = elapsed_secs;
            worst_cycles = total_cycles;
            worst_request_id = *request_id;
        }
    }

    if worst_cycles < 1_000_000.0 {
        tracing::warn!("Worst case performance proof is one with less than 1M cycles, \
            which might lead to a lower khz than expected. Benchmark using a larger proof if possible.");
    }

    // Report worst-case performance
    tracing::info!("Worst-case performance:");
    tracing::info!("  Request ID: 0x{:x}", worst_request_id);
    tracing::info!("  Performance: {:.2} KHz", worst_khz);
    tracing::info!("  Time: {:.2} seconds", worst_time);
    tracing::info!("  Cycles: {}", worst_cycles);

    println!("It is recommended to update this entry in broker.toml:");
    println!("peak_prove_khz = {:.0}\n", worst_khz.round());
    println!("Note: setting a lower value does not limit the proving speed, but will reduce the \
              total throughput of the orders locked by the broker. It is recommended to set a value \
              lower than this recommmendation, and increase it over time to increase capacity.");

    Ok(())
}

/// Create a PostgreSQL connection pool using environment variables
async fn create_pg_pool() -> Result<sqlx::PgPool, sqlx::Error> {
    let user = std::env::var("POSTGRES_USER").unwrap_or_else(|_| "worker".to_string());
    let password = std::env::var("POSTGRES_PASSWORD").unwrap_or_else(|_| "password".to_string());
    let db = std::env::var("POSTGRES_DB").unwrap_or_else(|_| "taskdb".to_string());
    let host = match std::env::var("POSTGRES_HOST").unwrap_or_else(|_| "postgres".to_string()) {
        host if host != "postgres" => host,
        // Use local connection for postgres, as "postgres" not compatible with docker
        _ => "127.0.0.1".to_string(),
    };

    let port = std::env::var("POSTGRES_PORT").unwrap_or_else(|_| "5432".to_string());

    let connection_string = format!("postgres://{user}:{password}@{host}:{port}/{db}");

    sqlx::PgPool::connect(&connection_string).await
}

/// Submit an offer and create a proof request
async fn submit_offer(client: StandardClient, args: &SubmitOfferArgs) -> Result<()> {
    let request = client.new_request();

    // Resolve the program from command line arguments.
    let request = match (args.program.path.clone(), args.program.url.clone()) {
        (Some(path), None) => {
            if client.storage_provider.is_none() {
                bail!("A storage provider is required to upload programs.\nPlease provide a storage provider (see --help for options) or upload your program and set --program-url.")
            }
            let program: Cow<'static, [u8]> = std::fs::read(&path)
                .context(format!("Failed to read program file at {:?}", args.program))?
                .into();
            request.with_program(program)
        }
        (None, Some(url)) => request.with_program_url(url).map_err(|e| match e {}).unwrap(),
        _ => bail!("Exactly one of program path and program-url args must be provided"),
    };

    // Process input based on provided arguments
    let stdin: Vec<u8> = match (&args.input.input, &args.input.input_file) {
        (Some(input), None) => input.as_bytes().to_vec(),
        (None, Some(input_file)) => std::fs::read(input_file)
            .context(format!("Failed to read input file at {input_file:?}"))?,
        _ => bail!("Exactly one of input or input-file args must be provided"),
    };

    // Prepare the input environment
    let env = if args.encode_input {
        GuestEnv::builder().write(&stdin)?
    } else {
        GuestEnv::builder().write_slice(&stdin)
    };
    let request = request.with_env(env);

    // Configure callback if provided
    let mut requirements = RequirementParams::builder();
    if let Some(address) = args.requirements.callback_address {
        requirements.callback_address(address);
        if let Some(gas_limit) = args.requirements.callback_gas_limit {
            requirements.callback_gas_limit(gas_limit);
        }
    }
    match args.requirements.proof_type {
        // TODO(risc0-ethereum/#597): This needs to be kept up to date with releases of
        // risc0-ethereum. Add a Selector::inclusion_latest() function to risc0-ethereum and use it
        // here.
        ProofType::Inclusion => requirements.selector(Selector::set_inclusion_latest() as u32),
        ProofType::Groth16 => requirements.selector(Selector::groth16_latest() as u32),
        ProofType::Any => &mut requirements,
        ty => bail!("unsupported proof type provided in proof-type flag: {:?}", ty),
    };
    let request = request.with_requirements(requirements);

    let request = client.build_request(request).await.context("failed to build proof request")?;
    tracing::debug!("Request details: {}", serde_yaml::to_string(&request)?);

    // Submit the request
    let (request_id, expires_at) = if args.offchain {
        tracing::info!("Submitting request offchain");
        client.submit_request_offchain(&request).await?
    } else {
        tracing::info!("Submitting request onchain");
        client.submit_request_onchain(&request).await?
    };

    tracing::info!(
        "Submitted request 0x{request_id:x}, bidding starts at {}",
        convert_timestamp(request.offer.biddingStart)
    );

    // Wait for fulfillment if requested
    if args.wait {
        tracing::info!("Waiting for request fulfillment...");
        let (journal, seal) = client
            .boundless_market
            .wait_for_request_fulfillment(request_id, Duration::from_secs(5), expires_at)
            .await?;

        tracing::info!("Request fulfilled!");
        tracing::info!(
            "Journal: {} - Seal: {}",
            serde_json::to_string_pretty(&journal)?,
            serde_json::to_string_pretty(&seal)?
        );
    }

    Ok(())
}

struct SubmitOptions {
    wait: bool,
    offchain: bool,
    preflight: bool,
}

/// Submit a proof request from a YAML file
async fn submit_request<P, S>(
    request_path: impl AsRef<Path>,
    client: Client<P, S>,
    opts: SubmitOptions,
) -> Result<()>
where
    P: Provider<Ethereum> + 'static + Clone,
    S: StorageProvider + Clone,
{
    // Read the YAML request file
    let file = File::open(request_path.as_ref())
        .context(format!("Failed to open request file at {:?}", request_path.as_ref()))?;
    let reader = BufReader::new(file);
    let mut request: ProofRequest =
        serde_yaml::from_reader(reader).context("Failed to parse request from YAML")?;

    // Fill in some of the request parameters, this command supports filling a few of the request
    // parameters that new need to updated on every reqeust. Namely, ID and bidding start.
    //
    // If set to 0, override the offer bidding_start field with the current timestamp + 30s
    if request.offer.biddingStart == 0 {
        // Adding a delay to bidding start lets provers see and evaluate the request
        // before the price starts to ramp up
        request.offer = Offer { biddingStart: now_timestamp() + 30, ..request.offer };
    }
    if request.id == U256::ZERO {
        request.id = client.boundless_market.request_id_from_rand().await?;
        tracing::info!("Assigned request ID {:x}", request.id);
    };

    // Run preflight check if enabled
    if opts.preflight {
        tracing::info!("Running request preflight check");
        let session_info = execute(&request).await?;
        let journal = session_info.journal.bytes;

        // Verify image ID if available
        if let Some(claim) = session_info.receipt_claim {
            ensure!(
                claim.pre.digest().as_bytes() == request.requirements.imageId.as_slice(),
                "Image ID mismatch: requirements ({}) do not match the given program ({})",
                hex::encode(request.requirements.imageId),
                hex::encode(claim.pre.digest().as_bytes())
            );
        } else {
            tracing::debug!("Cannot check image ID; session info doesn't have receipt claim");
        }

        // Verify predicate
        ensure!(
            request.requirements.predicate.eval(&journal),
            "Preflight failed: Predicate evaluation failed. Journal: {}, Predicate type: {:?}, Predicate data: {}",
            hex::encode(&journal),
            request.requirements.predicate.predicateType,
            hex::encode(&request.requirements.predicate.data)
        );

        tracing::info!("Preflight check passed");
    } else {
        tracing::warn!("Skipping preflight check");
    }

    // Submit the request
    let (request_id, expires_at) = if opts.offchain {
        tracing::info!("Submitting request offchain");
        client.submit_request_offchain(&request).await?
    } else {
        tracing::info!("Submitting request onchain");
        client.submit_request_onchain(&request).await?
    };

    tracing::info!(
        "Submitted request 0x{request_id:x}, bidding starts at {}",
        convert_timestamp(request.offer.biddingStart)
    );

    // Wait for fulfillment if requested
    if opts.wait {
        tracing::info!("Waiting for request fulfillment...");
        let (journal, seal) = client
            .wait_for_request_fulfillment(request_id, Duration::from_secs(5), expires_at)
            .await?;

        tracing::info!("Request fulfilled!");
        tracing::info!(
            "Journal: {} - Seal: {}",
            serde_json::to_string_pretty(&journal)?,
            serde_json::to_string_pretty(&seal)?
        );
    }

    Ok(())
}

/// Execute a proof request using the RISC Zero zkVM executor
async fn execute(request: &ProofRequest) -> Result<SessionInfo> {
    tracing::info!("Fetching program from {}", request.imageUrl);
    let program = fetch_url(&request.imageUrl).await?;

    tracing::info!("Processing input");
    let env = match request.input.inputType {
        RequestInputType::Inline => GuestEnv::decode(&request.input.data)?,
        RequestInputType::Url => {
            let input_url =
                std::str::from_utf8(&request.input.data).context("Input URL is not valid UTF-8")?;
            tracing::info!("Fetching input from {}", input_url);
            GuestEnv::decode(&fetch_url(input_url).await?)?
        }
        _ => bail!("Unsupported input type"),
    };

    tracing::info!("Executing program in zkVM");
    r0vm_is_installed()?;
    default_executor().execute(env.try_into()?, &program)
}

fn r0vm_is_installed() -> Result<()> {
    // Try to run the binary with the --version flag
    let result = std::process::Command::new("r0vm").arg("--version").output();

    match result {
        Ok(_) => Ok(()),
        Err(_) => Err(anyhow!("r0vm is not installed or could not be executed. Please check instructions at https://dev.risczero.com/api/zkvm/install")),
    }
}

// Get current timestamp with appropriate error handling
fn now_timestamp() -> u64 {
    SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("Time went backwards").as_secs()
}

/// Handle config command
async fn handle_config_command(args: &MainArgs) -> Result<()> {
    tracing::info!("Displaying CLI configuration");
    println!("\n=== Boundless CLI Configuration ===\n");

    // Show configuration
    println!("RPC URL: {}", args.config.rpc_url);
    println!(
        "Wallet Address: {}",
        args.config
            .private_key
            .as_ref()
            .map(|sk| sk.address().to_string())
            .unwrap_or("[no wallet provided]".to_string())
    );
    if let Some(timeout) = args.config.tx_timeout {
        println!("Transaction Timeout: {} seconds", timeout.as_secs());
    } else {
        println!("Transaction Timeout: <not set>");
    }
    println!("Log Level: {:?}", args.config.log_level);
    if let Some(ref deployment) = args.config.deployment {
        println!("Using custom Boundless deployment");
        println!("Chain ID: {:?}", deployment.chain_id);
        println!("Boundless Market Address: {}", deployment.boundless_market_address);
        println!("Verifier Address: {:?}", deployment.verifier_router_address);
        println!("Set Verifier Address: {}", deployment.set_verifier_address);
        println!("Order Stream URL: {:?}", deployment.order_stream_url);
    }

    // Validate RPC connection
    println!("\n=== Environment Validation ===\n");
    print!("Testing RPC connection... ");
    let provider = ProviderBuilder::new().connect_http(args.config.rpc_url.clone());

    let chain_id = match provider.get_chain_id().await {
        Ok(chain_id) => {
            println!("✅ Connected to chain ID: {chain_id}");
            chain_id
        }
        Err(e) => {
            println!("❌ Failed to connect: {e}");
            // Do not run remaining checks, which require an RPC connection.
            return Ok(());
        }
    };

    let Some(deployment) =
        args.config.deployment.clone().or_else(|| Deployment::from_chain_id(chain_id))
    else {
        println!("❌ No Boundless deployment config provided for unknown chain ID: {chain_id}");
        return Ok(());
    };

    // Check market contract
    print!("Testing Boundless Market contract... ");
    let boundless_market = BoundlessMarketService::new(
        deployment.boundless_market_address,
        provider.clone(),
        Address::ZERO,
    );

    let market_ok = match boundless_market.get_chain_id().await {
        Ok(_) => {
            println!("✅ Contract responds");
            true
        }
        Err(e) => {
            println!("❌ Contract error: {e}");
            false
        }
    };

    // Check set verifier contract
    print!("Testing Set Verifier contract... ");
    let set_verifier =
        SetVerifierService::new(deployment.set_verifier_address, provider.clone(), Address::ZERO);

    let (image_id, _) = match set_verifier.image_info().await {
        Ok(image_info) => {
            println!("✅ Contract responds");
            image_info
        }
        Err(e) => {
            println!("❌ Contract error: {e}");
            (B256::default(), String::default())
        }
    };

    // Create a transaction request with the call data
    if let Some(verifier_router_address) = deployment.verifier_router_address {
        let verifier_parameters =
            SetInclusionReceiptVerifierParameters { image_id: Digest::from_bytes(*image_id) };
        let selector: [u8; 4] = verifier_parameters.digest().as_bytes()[0..4].try_into()?;

        // Build the call data:
        // 1. Append the function selector for getVerifier(bytes4) ("3cadf449")
        // 2. Append the ABI encoding for the bytes4 parameter (padded to 32 bytes)
        let mut call_data = Vec::new();
        call_data.extend_from_slice(&hex::decode("3cadf449")?);
        call_data.extend_from_slice(&FixedBytes::from(selector).abi_encode());
        let tx = TransactionRequest {
            to: Some(TxKind::Call(verifier_router_address)),
            input: TransactionInput::new(call_data.into()),
            ..Default::default()
        };

        // Check verifier contract
        print!("Testing VerifierRouter contract... ");
        match provider.call(tx).await {
            Ok(_) => {
                println!("✅ Contract responds");
                true
            }
            Err(e) => {
                println!("❌ Contract error: {e}");
                false
            }
        };
    } else {
        // Verifier router is recommended, but not required for most operations.
        println!("⚠️ Verifier router address not configured");
    }

    println!(
        "\nEnvironment Setup: {}",
        if market_ok { "✅ Ready to use" } else { "❌ Issues detected" }
    );

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::net::{Ipv4Addr, SocketAddr};

    use alloy::primitives::aliases::U96;
    use boundless_market::contracts::{
        Predicate, PredicateType, RequestId, RequestInput, Requirements,
    };

    use super::*;

    use alloy::{
        node_bindings::{Anvil, AnvilInstance},
        primitives::utils::format_units,
        providers::WalletProvider,
    };
    use boundless_market::{
        contracts::{hit_points::default_allowance, RequestStatus},
        selector::is_groth16_selector,
    };
    use boundless_market_test_utils::{
        create_test_ctx, deploy_mock_callback, get_mock_callback_count, TestCtx, ECHO_ID, ECHO_PATH,
    };
    use order_stream::{run_from_parts, AppState, ConfigBuilder};
    use sqlx::PgPool;
    use tempfile::tempdir;
    use tokio::task::JoinHandle;
    use tracing_test::traced_test;

    // generate a test request
    fn generate_request(id: u32, addr: &Address) -> ProofRequest {
        ProofRequest::new(
            RequestId::new(*addr, id),
            Requirements::new(
                Digest::from(ECHO_ID),
                Predicate { predicateType: PredicateType::PrefixMatch, data: Default::default() },
            ),
            format!("file://{ECHO_PATH}"),
            RequestInput::builder().write_slice(&[0x41, 0x41, 0x41, 0x41]).build_inline().unwrap(),
            Offer {
                minPrice: U256::from(20000000000000u64),
                maxPrice: U256::from(40000000000000u64),
                biddingStart: now_timestamp(),
                timeout: 420,
                lockTimeout: 420,
                rampUpPeriod: 1,
                lockStake: U256::from(10),
            },
        )
    }

    enum AccountOwner {
        Customer,
        Prover,
    }

    /// Test setup helper that creates common test infrastructure
    async fn setup_test_env(
        owner: AccountOwner,
    ) -> (TestCtx<impl Provider + WalletProvider + Clone + 'static>, AnvilInstance, GlobalConfig)
    {
        let anvil = Anvil::new().spawn();

        let ctx = create_test_ctx(&anvil).await.unwrap();

        let private_key = match owner {
            AccountOwner::Customer => {
                ctx.prover_market
                    .deposit_stake_with_permit(default_allowance(), &ctx.prover_signer)
                    .await
                    .unwrap();
                ctx.customer_signer.clone()
            }
            AccountOwner::Prover => ctx.prover_signer.clone(),
        };

        let config = GlobalConfig {
            rpc_url: anvil.endpoint_url(),
            private_key: Some(private_key),
            deployment: Some(ctx.deployment.clone()),
            tx_timeout: None,
            log_level: LevelFilter::INFO,
        };

        (ctx, anvil, config)
    }

    async fn setup_test_env_with_order_stream(
        owner: AccountOwner,
        pool: PgPool,
    ) -> (
        TestCtx<impl Provider + WalletProvider + Clone + 'static>,
        AnvilInstance,
        GlobalConfig,
        JoinHandle<()>,
    ) {
        let (mut ctx, anvil, mut global_config) = setup_test_env(owner).await;

        // Create listener first
        let listener = tokio::net::TcpListener::bind(SocketAddr::from((Ipv4Addr::UNSPECIFIED, 0)))
            .await
            .unwrap();
        let order_stream_address = listener.local_addr().unwrap();
        let order_stream_url = Url::parse(&format!("http://{order_stream_address}")).unwrap();
        let domain = order_stream_address.to_string();

        let config = ConfigBuilder::default()
            .rpc_url(anvil.endpoint_url())
            .market_address(ctx.deployment.boundless_market_address)
            .domain(domain)
            .build()
            .unwrap();

        // Start order stream server
        let order_stream = AppState::new(&config, Some(pool)).await.unwrap();
        let order_stream_clone = order_stream.clone();
        let order_stream_handle = tokio::spawn(async move {
            run_from_parts(order_stream_clone, listener).await.unwrap();
        });

        // Add the order_stream_url to the deployment config.
        ctx.deployment.order_stream_url = Some(order_stream_url.to_string().into());
        global_config.deployment = Some(ctx.deployment.clone());

        (ctx, anvil, global_config, order_stream_handle)
    }

    #[tokio::test]
    #[traced_test]
    async fn test_deposit_withdraw() {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let mut args = MainArgs {
            config,
            command: Command::Account(Box::new(AccountCommands::Deposit {
                amount: default_allowance(),
            })),
        };

        run(&args).await.unwrap();
        assert!(logs_contain(&format!(
            "Depositing {} ETH",
            format_units(default_allowance(), "ether").unwrap()
        )));
        assert!(logs_contain(&format!(
            "Successfully deposited {} ETH",
            format_units(default_allowance(), "ether").unwrap()
        )));

        let balance = ctx.prover_market.balance_of(ctx.customer_signer.address()).await.unwrap();
        assert_eq!(balance, default_allowance());

        args.command = Command::Account(Box::new(AccountCommands::Balance {
            address: Some(ctx.customer_signer.address()),
        }));
        run(&args).await.unwrap();
        assert!(logs_contain(&format!(
            "Checking balance for address {}",
            ctx.customer_signer.address()
        )));
        assert!(logs_contain(&format!(
            "Balance for address {}: {} ETH",
            ctx.customer_signer.address(),
            format_units(default_allowance(), "ether").unwrap()
        )));

        args.command =
            Command::Account(Box::new(AccountCommands::Withdraw { amount: default_allowance() }));

        run(&args).await.unwrap();
        assert!(logs_contain(&format!(
            "Withdrawing {} ETH",
            format_units(default_allowance(), "ether").unwrap()
        )));
        assert!(logs_contain(&format!(
            "Successfully withdrew {} ETH",
            format_units(default_allowance(), "ether").unwrap()
        )));

        let balance = ctx.prover_market.balance_of(ctx.customer_signer.address()).await.unwrap();
        assert_eq!(balance, U256::from(0));
    }

    #[tokio::test]
    #[traced_test]
    async fn test_fail_deposit_withdraw() {
        let (_ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let amount = U256::from(10000000000000000000000_u128);
        let mut args = MainArgs {
            config,
            command: Command::Account(Box::new(AccountCommands::Deposit { amount })),
        };

        let err = run(&args).await.unwrap_err();
        assert!(err.to_string().contains("Insufficient funds"));

        args.command = Command::Account(Box::new(AccountCommands::Withdraw { amount }));

        let err = run(&args).await.unwrap_err();
        assert!(err.to_string().contains("InsufficientBalance"));
    }

    #[tokio::test]
    #[traced_test]
    async fn test_deposit_withdraw_stake() {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Prover).await;

        let mut args = MainArgs {
            config,
            command: Command::Account(Box::new(AccountCommands::DepositStake {
                amount: format_ether(default_allowance()),
            })),
        };

        run(&args).await.unwrap();
        assert!(logs_contain(&format!(
            "Depositing {} HP as stake",
            format_ether(default_allowance())
        )));
        assert!(logs_contain(&format!(
            "Successfully deposited {} HP as stake",
            format_ether(default_allowance())
        )));

        let balance =
            ctx.prover_market.balance_of_stake(ctx.prover_signer.address()).await.unwrap();
        assert_eq!(balance, default_allowance());

        args.command = Command::Account(Box::new(AccountCommands::StakeBalance {
            address: Some(ctx.prover_signer.address()),
        }));
        run(&args).await.unwrap();
        assert!(logs_contain(&format!(
            "Checking stake balance for address {}",
            ctx.prover_signer.address()
        )));
        assert!(logs_contain(&format!(
            "Stake balance for address {}: {} HP",
            ctx.prover_signer.address(),
            format_units(default_allowance(), "ether").unwrap()
        )));

        args.command = Command::Account(Box::new(AccountCommands::WithdrawStake {
            amount: format_ether(default_allowance()),
        }));

        run(&args).await.unwrap();
        assert!(logs_contain(&format!(
            "Withdrawing {} HP from stake",
            format_ether(default_allowance())
        )));
        assert!(logs_contain(&format!(
            "Successfully withdrew {} HP from stake",
            format_ether(default_allowance())
        )));

        let balance =
            ctx.prover_market.balance_of_stake(ctx.prover_signer.address()).await.unwrap();
        assert_eq!(balance, U256::from(0));
    }

    #[tokio::test]
    #[traced_test]
    async fn test_deposit_stake_amount_below_denom_min() -> Result<()> {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        // Use amount below denom min
        let amount = "0.00000000000000000000000001".to_string();
        let args = MainArgs {
            config,
            command: Command::Account(Box::new(AccountCommands::DepositStake {
                amount: amount.clone(),
            })),
        };

        // Sanity check to make sure that the amount is below the denom min
        let decimals = ctx.customer_market.stake_token_decimals().await?;
        let parsed_amount: U256 = parse_units(&amount, decimals).unwrap().into();
        assert_eq!(parsed_amount, U256::from(0));

        let err = run(&args).await.unwrap_err();
        assert!(err.to_string().contains("Amount is below the denomination minimum"));

        Ok(())
    }

    #[tokio::test]
    #[traced_test]
    async fn test_fail_deposit_withdraw_stake() {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let mut args = MainArgs {
            config,
            command: Command::Account(Box::new(AccountCommands::DepositStake {
                amount: format_ether(default_allowance()),
            })),
        };

        let err = run(&args).await.unwrap_err();
        assert!(err.to_string().contains(&format!(
            "Failed to deposit stake: Ensure your address ({}) has funds on the HP contract",
            ctx.customer_signer.address()
        )));

        args.command = Command::Account(Box::new(AccountCommands::WithdrawStake {
            amount: format_ether(default_allowance()),
        }));

        let err = run(&args).await.unwrap_err();
        assert!(err.to_string().contains("InsufficientBalance"));
    }

    #[tokio::test]
    #[traced_test]
    async fn test_submit_request_onchain() {
        let (_ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        // Submit a request onchain
        let args = MainArgs {
            config,
            command: Command::Request(Box::new(RequestCommands::Submit {
                storage_config: Box::new(StorageProviderConfig::dev_mode()),
                yaml_request: "../../request.yaml".to_string().into(),
                wait: false,
                offchain: false,
                no_preflight: false,
            })),
        };
        run(&args).await.unwrap();
        assert!(logs_contain("Submitting request onchain"));
        assert!(logs_contain("Submitted request"));
    }

    #[sqlx::test]
    #[traced_test]
    async fn test_submit_request_offchain(pool: PgPool) {
        let (ctx, _anvil, config, order_stream_handle) =
            setup_test_env_with_order_stream(AccountOwner::Customer, pool).await;

        // Deposit funds into the market
        ctx.customer_market.deposit(parse_ether("1").unwrap()).await.unwrap();

        // Submit a request offchain
        let args = MainArgs {
            config,
            command: Command::Request(Box::new(RequestCommands::Submit {
                storage_config: Box::new(StorageProviderConfig::dev_mode()),
                yaml_request: "../../request.yaml".to_string().into(),
                wait: false,
                offchain: true,
                no_preflight: true,
            })),
        };
        run(&args).await.unwrap();
        assert!(logs_contain("Submitting request offchain"));
        assert!(logs_contain("Submitted request"));

        // Clean up
        order_stream_handle.abort();
    }

    #[tokio::test]
    #[traced_test]
    async fn test_submit_offer_onchain() {
        let (_ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        // Submit a request onchain
        let args = MainArgs {
            config,
            command: Command::Request(Box::new(RequestCommands::SubmitOffer(Box::new(
                SubmitOfferArgs {
                    storage_config: StorageProviderConfig::dev_mode(),
                    id: None,
                    wait: false,
                    offchain: false,
                    encode_input: false,
                    input: SubmitOfferInput {
                        input: Some(hex::encode([0x41, 0x41, 0x41, 0x41])),
                        input_file: None,
                    },
                    program: SubmitOfferProgram { path: Some(PathBuf::from(ECHO_PATH)), url: None },
                    requirements: SubmitOfferRequirements {
                        callback_address: None,
                        callback_gas_limit: None,
                        proof_type: ProofType::Any,
                    },
                    offer_params: OfferParams::default(),
                },
            )))),
        };
        run(&args).await.unwrap();
        assert!(logs_contain("Submitting request onchain"));
        assert!(logs_contain("Submitted request"));
    }

    #[tokio::test]
    #[traced_test]
    async fn test_request_status_onchain() {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let request = generate_request(
            ctx.customer_market.index_from_nonce().await.unwrap(),
            &ctx.customer_signer.address(),
        );

        // Deposit funds into the market
        ctx.customer_market.deposit(parse_ether("1").unwrap()).await.unwrap();

        // Submit the request onchain
        ctx.customer_market.submit_request(&request, &ctx.customer_signer).await.unwrap();

        // Create a new args struct to test the Status command
        let status_args = MainArgs {
            config,
            command: Command::Request(Box::new(RequestCommands::Status {
                request_id: request.id,
                expires_at: None,
            })),
        };

        run(&status_args).await.unwrap();

        assert!(logs_contain(&format!("Request 0x{:x} status: Unknown", request.id)));
    }

    #[tokio::test]
    #[traced_test]
    async fn test_slash() {
        let (ctx, anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let mut request = generate_request(
            ctx.customer_market.index_from_nonce().await.unwrap(),
            &ctx.customer_signer.address(),
        );
        request.offer.timeout = 50;
        request.offer.lockTimeout = 50;

        // Deposit funds into the market
        ctx.customer_market.deposit(parse_ether("1").unwrap()).await.unwrap();

        // Submit the request onchain
        ctx.customer_market.submit_request(&request, &ctx.customer_signer).await.unwrap();

        let client_sig = request
            .sign_request(
                &ctx.customer_signer,
                ctx.deployment.boundless_market_address,
                anvil.chain_id(),
            )
            .await
            .unwrap();

        // Lock the request
        ctx.prover_market
            .lock_request(&request, client_sig.as_bytes().to_vec(), None)
            .await
            .unwrap();

        // Create a new args struct to test the Status command
        let status_args = MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::Status {
                request_id: request.id,
                expires_at: None,
            })),
        };
        run(&status_args).await.unwrap();
        assert!(logs_contain(&format!("Request 0x{:x} status: Locked", request.id)));

        loop {
            // Wait for the timeout to expire
            tokio::time::sleep(Duration::from_secs(1)).await;
            let status = ctx
                .customer_market
                .get_status(request.id, Some(request.expires_at()))
                .await
                .unwrap();
            if status == RequestStatus::Expired {
                break;
            }
        }

        // test the Slash command
        run(&MainArgs {
            config,
            command: Command::Ops(Box::new(OpsCommands::Slash { request_id: request.id })),
        })
        .await
        .unwrap();
        assert!(logs_contain(&format!(
            "Successfully slashed prover for request 0x{:x}",
            request.id
        )));
    }

    #[tokio::test]
    #[traced_test]
    #[ignore = "Generates a proof. Slow without RISC0_DEV_MODE=1"]
    async fn test_proving_onchain() {
        let (ctx, anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let request = generate_request(
            ctx.customer_market.index_from_nonce().await.unwrap(),
            &ctx.customer_signer.address(),
        );

        let request_id = request.id;

        // Dump the request to a tmp file; tmp is deleted on drop.
        let tmp = tempdir().unwrap();
        let request_path = tmp.path().join("request.yaml");
        let request_file = File::create(&request_path).unwrap();
        serde_yaml::to_writer(request_file, &request).unwrap();

        // send the request onchain
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::Submit {
                storage_config: Box::new(StorageProviderConfig::dev_mode()),
                yaml_request: request_path,
                wait: false,
                offchain: false,
                no_preflight: true,
            })),
        })
        .await
        .unwrap();

        // test the Execute command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Proving(Box::new(ProvingCommands::Execute {
                request_path: None,
                request_id: Some(request_id),
                request_digest: None,
                tx_hash: None,
            })),
        })
        .await
        .unwrap();

        assert!(logs_contain(&format!("Successfully executed request 0x{:x}", request.id)));

        let prover_config = GlobalConfig {
            rpc_url: anvil.endpoint_url(),
            private_key: Some(ctx.prover_signer.clone()),
            deployment: Some(ctx.deployment),
            tx_timeout: None,
            log_level: LevelFilter::INFO,
        };

        // test the Lock command
        run(&MainArgs {
            config: prover_config,
            command: Command::Proving(Box::new(ProvingCommands::Lock {
                request_id,
                request_digest: None,
                tx_hash: None,
            })),
        })
        .await
        .unwrap();
        assert!(logs_contain(&format!("Successfully locked request 0x{:x}", request.id)));

        // test the Status command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::Status {
                request_id,
                expires_at: None,
            })),
        })
        .await
        .unwrap();
        assert!(logs_contain(&format!("Request 0x{:x} status: Locked", request.id)));

        // test the Fulfill command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Proving(Box::new(ProvingCommands::Fulfill {
                request_ids: vec![request_id],
                request_digests: None,
                tx_hashes: None,
                withdraw: false,
                bonsai_api_url: None,
                bonsai_api_key: None,
                use_default_prover: true,
            })),
        })
        .await
        .unwrap();

        assert!(logs_contain(&format!("Successfully fulfilled requests 0x{:x}", request.id)));

        // test the Status command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::Status {
                request_id,
                expires_at: None,
            })),
        })
        .await
        .unwrap();
        assert!(logs_contain(&format!("Request 0x{:x} status: Fulfilled", request.id)));

        // test the GetProof command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::GetProof { request_id })),
        })
        .await
        .unwrap();
        assert!(logs_contain(&format!(
            "Successfully retrieved proof for request 0x{:x}",
            request.id
        )));

        // test the Verify command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::VerifyProof {
                request_id,
                image_id: request.requirements.imageId,
            })),
        })
        .await
        .unwrap();
        assert!(logs_contain(&format!(
            "Successfully verified proof for request 0x{:x}",
            request.id
        )));
    }

    #[tokio::test]
    #[traced_test]
    #[ignore = "Generates a proof. Slow without RISC0_DEV_MODE=1"]
    async fn test_proving_multiple_requests() {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let mut request_ids = Vec::new();
        for _ in 0..3 {
            let request = generate_request(
                ctx.customer_market.index_from_nonce().await.unwrap(),
                &ctx.customer_signer.address(),
            );

            ctx.customer_market.submit_request(&request, &ctx.customer_signer).await.unwrap();
            request_ids.push(request.id);
        }

        // test the Fulfill command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Proving(Box::new(ProvingCommands::Fulfill {
                request_ids: request_ids.clone(),
                request_digests: None,
                tx_hashes: None,
                withdraw: false,
                bonsai_api_url: None,
                bonsai_api_key: None,
                use_default_prover: true,
            })),
        })
        .await
        .unwrap();

        let request_ids_str =
            request_ids.iter().map(|id| format!("0x{id:x}")).collect::<Vec<_>>().join(", ");
        assert!(logs_contain(&format!("Successfully fulfilled requests {request_ids_str}")));

        for request_id in request_ids {
            // test the Status command
            run(&MainArgs {
                config: config.clone(),
                command: Command::Request(Box::new(RequestCommands::Status {
                    request_id,
                    expires_at: None,
                })),
            })
            .await
            .unwrap();
            assert!(logs_contain(&format!("Request 0x{request_id:x} status: Fulfilled")));
        }
    }

    #[tokio::test]
    #[traced_test]
    #[ignore = "Generates a proof. Slow without RISC0_DEV_MODE=1"]
    async fn test_callback() {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let mut request = generate_request(
            ctx.customer_market.index_from_nonce().await.unwrap(),
            &ctx.customer_signer.address(),
        );

        // Deploy MockCallback contract
        let callback_address = deploy_mock_callback(
            &ctx.prover_provider,
            ctx.deployment.verifier_router_address.unwrap(),
            ctx.deployment.boundless_market_address,
            ECHO_ID,
            U256::ZERO,
        )
        .await
        .unwrap();

        // Update the request with the callback address
        request.requirements.callback.addr = callback_address;
        request.requirements.callback.gasLimit = U96::from(100000);

        // Dump the request to a tmp file; tmp is deleted on drop.
        let tmp = tempdir().unwrap();
        let request_path = tmp.path().join("request.yaml");
        let request_file = File::create(&request_path).unwrap();
        serde_yaml::to_writer(request_file, &request).unwrap();

        // send the request onchain
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::Submit {
                storage_config: Box::new(StorageProviderConfig::dev_mode()),
                yaml_request: request_path,
                wait: false,
                offchain: false,
                no_preflight: true,
            })),
        })
        .await
        .unwrap();

        // fulfill the request
        run(&MainArgs {
            config,
            command: Command::Proving(Box::new(ProvingCommands::Fulfill {
                request_ids: vec![request.id],
                request_digests: None,
                tx_hashes: None,
                withdraw: false,
                bonsai_api_url: None,
                bonsai_api_key: None,
                use_default_prover: true,
            })),
        })
        .await
        .unwrap();

        // check the callback was called
        let count =
            get_mock_callback_count(&ctx.customer_provider, callback_address).await.unwrap();
        assert!(count == U256::from(1));
    }

    #[tokio::test]
    #[traced_test]
    #[ignore = "Generates a proof. Slow without RISC0_DEV_MODE=1"]
    async fn test_selector() {
        let (ctx, _anvil, config) = setup_test_env(AccountOwner::Customer).await;

        let mut request = generate_request(
            ctx.customer_market.index_from_nonce().await.unwrap(),
            &ctx.customer_signer.address(),
        );

        // Explicitly set the selector to a compatible value for the test
        // In dev mode, instead of Groth16, use FakeReceipt
        request.requirements.selector = FixedBytes::from(Selector::FakeReceipt as u32);

        // Dump the request to a tmp file; tmp is deleted on drop.
        let tmp = tempdir().unwrap();
        let request_path = tmp.path().join("request.yaml");
        let request_file = File::create(&request_path).unwrap();
        serde_yaml::to_writer(request_file, &request).unwrap();

        // send the request onchain
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::Submit {
                storage_config: Box::new(StorageProviderConfig::dev_mode()),
                yaml_request: request_path,
                wait: false,
                offchain: false,
                no_preflight: true,
            })),
        })
        .await
        .unwrap();

        // fulfill the request
        run(&MainArgs {
            config,
            command: Command::Proving(Box::new(ProvingCommands::Fulfill {
                request_ids: vec![request.id],
                request_digests: None,
                tx_hashes: None,
                withdraw: false,
                bonsai_api_url: None,
                bonsai_api_key: None,
                use_default_prover: true,
            })),
        })
        .await
        .unwrap();

        // check the seal is aggregated
        let (_journal, seal) =
            ctx.customer_market.get_request_fulfillment(request.id).await.unwrap();
        let selector: FixedBytes<4> = seal[0..4].try_into().unwrap();
        assert!(is_groth16_selector(selector))
    }

    #[sqlx::test]
    #[traced_test]
    #[ignore = "Generates a proof. Slow without RISC0_DEV_MODE=1"]
    async fn test_proving_offchain(pool: PgPool) {
        let (ctx, anvil, config, order_stream_handle) =
            setup_test_env_with_order_stream(AccountOwner::Customer, pool).await;

        // Deposit funds into the market
        ctx.customer_market.deposit(parse_ether("1").unwrap()).await.unwrap();

        let request = generate_request(
            ctx.customer_market.index_from_nonce().await.unwrap(),
            &ctx.customer_signer.address(),
        );

        let request_id = request.id;

        // Dump the request to a tmp file; tmp is deleted on drop.
        let tmp = tempdir().unwrap();
        let request_path = tmp.path().join("request.yaml");
        let request_file = File::create(&request_path).unwrap();
        serde_yaml::to_writer(request_file, &request).unwrap();

        // send the request offchain
        run(&MainArgs {
            config: config.clone(),
            command: Command::Request(Box::new(RequestCommands::Submit {
                storage_config: Box::new(StorageProviderConfig::dev_mode()),
                yaml_request: request_path,
                wait: false,
                offchain: true,
                no_preflight: true,
            })),
        })
        .await
        .unwrap();

        // test the Execute command
        run(&MainArgs {
            config: config.clone(),
            command: Command::Proving(Box::new(ProvingCommands::Execute {
                request_path: None,
                request_id: Some(request_id),
                request_digest: None,
                tx_hash: None,
            })),
        })
        .await
        .unwrap();

        assert!(logs_contain(&format!("Successfully executed request 0x{:x}", request.id)));

        let prover_config = GlobalConfig {
            rpc_url: anvil.endpoint_url(),
            private_key: Some(ctx.prover_signer.clone()),
            deployment: Some(ctx.deployment),
            tx_timeout: None,
            log_level: LevelFilter::INFO,
        };

        // test the Lock command
        run(&MainArgs {
            config: prover_config,
            command: Command::Proving(Box::new(ProvingCommands::Lock {
                request_id,
                request_digest: None,
                tx_hash: None,
            })),
        })
        .await
        .unwrap();
        assert!(logs_contain(&format!("Successfully locked request 0x{:x}", request.id)));

        // test the Fulfill command
        run(&MainArgs {
            config,
            command: Command::Proving(Box::new(ProvingCommands::Fulfill {
                request_ids: vec![request_id],
                request_digests: None,
                tx_hashes: None,
                withdraw: true,
                bonsai_api_url: None,
                bonsai_api_key: None,
                use_default_prover: true,
            })),
        })
        .await
        .unwrap();

        assert!(logs_contain(&format!("Successfully fulfilled requests 0x{:x}", request.id)));

        // test the automated withdraw
        let balance = ctx.prover_market.balance_of(ctx.prover_signer.address()).await.unwrap();
        assert_eq!(balance, U256::from(0));

        // Clean up
        order_stream_handle.abort();
    }
}