-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
main.rs
1327 lines (1151 loc) · 52.8 KB
/
main.rs
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
//! Contains various tests for checking cast commands
use alloy_chains::NamedChain;
use alloy_primitives::{b256, B256};
use anvil::{EthereumHardfork, NodeConfig};
use foundry_test_utils::{
casttest, file,
rpc::{
next_http_rpc_endpoint, next_mainnet_etherscan_api_key, next_rpc_endpoint,
next_ws_rpc_endpoint,
},
str,
util::OutputExt,
};
use std::{fs, io::Write, path::Path, str::FromStr};
// tests `--help` is printed to std out
casttest!(print_help, |_prj, cmd| {
cmd.arg("--help").assert_success().stdout_eq(str![[r#"
Perform Ethereum RPC calls from the comfort of your command line
Usage: cast[..] <COMMAND>
Commands:
...
Options:
-h, --help Print help
-V, --version Print version
Find more information in the book: http://book.getfoundry.sh/reference/cast/cast.html
"#]]);
});
// tests that the `cast block` command works correctly
casttest!(latest_block, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
// Call `cast find-block`
cmd.args(["block", "latest", "--rpc-url", eth_rpc_url.as_str()]);
cmd.assert_success().stdout_eq(str![[r#"
baseFeePerGas [..]
difficulty [..]
extraData [..]
gasLimit [..]
gasUsed [..]
hash [..]
logsBloom [..]
miner [..]
mixHash [..]
nonce [..]
number [..]
parentHash [..]
transactionsRoot [..]
receiptsRoot [..]
sha3Uncles [..]
size [..]
stateRoot [..]
timestamp [..]
withdrawalsRoot [..]
totalDifficulty [..]
transactions: [
...
]
"#]]);
// <https://etherscan.io/block/15007840>
cmd.cast_fuse().args(["block", "15007840", "-f", "hash", "--rpc-url", eth_rpc_url.as_str()]);
cmd.assert_success().stdout_eq(str![[r#"
0x950091817a57e22b6c1f3b951a15f52d41ac89b299cc8f9c89bb6d185f80c415
"#]]);
});
// tests that the `cast find-block` command works correctly
casttest!(finds_block, |_prj, cmd| {
// Construct args
let timestamp = "1647843609".to_string();
let eth_rpc_url = next_http_rpc_endpoint();
// Call `cast find-block`
// <https://etherscan.io/block/14428082>
cmd.args(["find-block", "--rpc-url", eth_rpc_url.as_str(), ×tamp])
.assert_success()
.stdout_eq(str![[r#"
14428082
"#]]);
});
// tests that we can create a new wallet with keystore
casttest!(new_wallet_keystore_with_password, |_prj, cmd| {
cmd.args(["wallet", "new", ".", "--unsafe-password", "test"]).assert_success().stdout_eq(str![
[r#"
Created new encrypted keystore file: [..]
[ADDRESS]
"#]
]);
});
// tests that we can get the address of a keystore file
casttest!(wallet_address_keystore_with_password_file, |_prj, cmd| {
let keystore_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/keystore");
cmd.args([
"wallet",
"address",
"--keystore",
keystore_dir
.join("UTC--2022-12-20T10-30-43.591916000Z--ec554aeafe75601aaab43bd4621a22284db566c2")
.to_str()
.unwrap(),
"--password-file",
keystore_dir.join("password-ec554").to_str().unwrap(),
])
.assert_success()
.stdout_eq(str![[r#"
0xeC554aeAFE75601AaAb43Bd4621A22284dB566C2
"#]]);
});
// tests that `cast wallet sign message` outputs the expected signature
casttest!(wallet_sign_message_utf8_data, |_prj, cmd| {
let pk = "0x0000000000000000000000000000000000000000000000000000000000000001";
let address = "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf";
let msg = "test";
let expected = "0xfe28833983d6faa0715c7e8c3873c725ddab6fa5bf84d40e780676e463e6bea20fc6aea97dc273a98eb26b0914e224c8dd5c615ceaab69ddddcf9b0ae3de0e371c";
cmd.args(["wallet", "sign", "--private-key", pk, msg]).assert_success().stdout_eq(str![[r#"
0xfe28833983d6faa0715c7e8c3873c725ddab6fa5bf84d40e780676e463e6bea20fc6aea97dc273a98eb26b0914e224c8dd5c615ceaab69ddddcf9b0ae3de0e371c
"#]]);
// Success.
cmd.cast_fuse()
.args(["wallet", "verify", "-a", address, msg, expected])
.assert_success()
.stdout_eq(str![[r#"
Validation succeeded. Address 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf signed this message.
"#]]);
// Fail.
cmd.cast_fuse()
.args(["wallet", "verify", "-a", address, "other msg", expected])
.assert_failure()
.stderr_eq(str![[r#"
Error:
Validation failed. Address 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf did not sign this message.
"#]]);
});
// tests that `cast wallet sign message` outputs the expected signature, given a 0x-prefixed data
casttest!(wallet_sign_message_hex_data, |_prj, cmd| {
cmd.args([
"wallet",
"sign",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000000",
]).assert_success().stdout_eq(str![[r#"
0x23a42ca5616ee730ff3735890c32fc7b9491a9f633faca9434797f2c845f5abf4d9ba23bd7edb8577acebaa3644dc5a4995296db420522bb40060f1693c33c9b1c
"#]]);
});
// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON string
casttest!(wallet_sign_typed_data_string, |_prj, cmd| {
cmd.args([
"wallet",
"sign",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--data",
"{\"types\": {\"EIP712Domain\": [{\"name\": \"name\",\"type\": \"string\"},{\"name\": \"version\",\"type\": \"string\"},{\"name\": \"chainId\",\"type\": \"uint256\"},{\"name\": \"verifyingContract\",\"type\": \"address\"}],\"Message\": [{\"name\": \"data\",\"type\": \"string\"}]},\"primaryType\": \"Message\",\"domain\": {\"name\": \"example.metamask.io\",\"version\": \"1\",\"chainId\": \"1\",\"verifyingContract\": \"0x0000000000000000000000000000000000000000\"},\"message\": {\"data\": \"Hello!\"}}",
]).assert_success().stdout_eq(str![[r#"
0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b
"#]]);
});
// tests that `cast wallet sign typed-data` outputs the expected signature, given a JSON file
casttest!(wallet_sign_typed_data_file, |_prj, cmd| {
cmd.args([
"wallet",
"sign",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--data",
"--from-file",
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/sign_typed_data.json")
.into_os_string()
.into_string()
.unwrap()
.as_str(),
]).assert_success().stdout_eq(str![[r#"
0x06c18bdc8163219fddc9afaf5a0550e381326474bb757c86dc32317040cf384e07a2c72ce66c1a0626b6750ca9b6c035bf6f03e7ed67ae2d1134171e9085c0b51b
"#]]);
});
// tests that `cast wallet sign-auth message` outputs the expected signature
casttest!(wallet_sign_auth, |_prj, cmd| {
cmd.args([
"wallet",
"sign-auth",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--nonce",
"100",
"--chain",
"1",
"0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf"]).assert_success().stdout_eq(str![[r#"
0xf85a01947e5f4552091a69125d5dfcb7b8c2659029395bdf6401a0ad489ee0314497c3f06567f3080a46a63908edc1c7cdf2ac2d609ca911212086a065a6ba951c8748dd8634740fe498efb61770097d99ff5fdcb9a863b62ea899f6
"#]]);
});
// tests that `cast wallet list` outputs the local accounts
casttest!(wallet_list_local_accounts, |prj, cmd| {
let keystore_path = prj.root().join("keystore");
fs::create_dir_all(keystore_path).unwrap();
cmd.set_current_dir(prj.root());
// empty results
cmd.cast_fuse()
.args(["wallet", "list", "--dir", "keystore"])
.assert_success()
.stdout_eq(str![""]);
// create 10 wallets
cmd.cast_fuse()
.args(["wallet", "new", "keystore", "-n", "10", "--unsafe-password", "test"])
.assert_success()
.stdout_eq(str![[r#"
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
Created new encrypted keystore file: [..]
[ADDRESS]
"#]]);
// test list new wallet
cmd.cast_fuse().args(["wallet", "list", "--dir", "keystore"]).assert_success().stdout_eq(str![
[r#"
[..] (Local)
[..] (Local)
[..] (Local)
[..] (Local)
[..] (Local)
[..] (Local)
[..] (Local)
[..] (Local)
[..] (Local)
[..] (Local)
"#]
]);
});
// tests that `cast wallet new-mnemonic --entropy` outputs the expected mnemonic
casttest!(wallet_mnemonic_from_entropy, |_prj, cmd| {
cmd.args(["wallet", "new-mnemonic", "--entropy", "0xdf9bf37e6fcdf9bf37e6fcdf9bf37e3c"])
.assert_success()
.stdout_eq(str![[r#"
Generating mnemonic from provided entropy...
Successfully generated a new mnemonic.
Phrase:
test test test test test test test test test test test junk
Accounts:
- Account 0:
Address: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Private key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
"#]]);
});
// tests that `cast wallet private-key` with arguments outputs the private key
casttest!(wallet_private_key_from_mnemonic_arg, |_prj, cmd| {
cmd.args([
"wallet",
"private-key",
"test test test test test test test test test test test junk",
"1",
])
.assert_success()
.stdout_eq(str![[r#"
0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
"#]]);
});
// tests that `cast wallet private-key` with options outputs the private key
casttest!(wallet_private_key_from_mnemonic_option, |_prj, cmd| {
cmd.args([
"wallet",
"private-key",
"--mnemonic",
"test test test test test test test test test test test junk",
"--mnemonic-index",
"1",
])
.assert_success()
.stdout_eq(str![[r#"
0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
"#]]);
});
// tests that `cast wallet private-key` with derivation path outputs the private key
casttest!(wallet_private_key_with_derivation_path, |_prj, cmd| {
cmd.args([
"wallet",
"private-key",
"--mnemonic",
"test test test test test test test test test test test junk",
"--mnemonic-derivation-path",
"m/44'/60'/0'/0/1",
])
.assert_success()
.stdout_eq(str![[r#"
0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
"#]]);
});
// tests that `cast wallet import` creates a keystore for a private key and that `cast wallet
// decrypt-keystore` can access it
casttest!(wallet_import_and_decrypt, |prj, cmd| {
let keystore_path = prj.root().join("keystore");
cmd.set_current_dir(prj.root());
let account_name = "testAccount";
// Default Anvil private key
let test_private_key =
b256!("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80");
// import private key
cmd.cast_fuse()
.args([
"wallet",
"import",
account_name,
"--private-key",
&test_private_key.to_string(),
"-k",
"keystore",
"--unsafe-password",
"test",
])
.assert_success()
.stdout_eq(str![[r#"
`testAccount` keystore was saved successfully. [ADDRESS]
"#]]);
// check that the keystore file was created
let keystore_file = keystore_path.join(account_name);
assert!(keystore_file.exists());
// decrypt the keystore file
let decrypt_output = cmd.cast_fuse().args([
"wallet",
"decrypt-keystore",
account_name,
"-k",
"keystore",
"--unsafe-password",
"test",
]);
// get the PK out of the output (last word in the output)
let decrypt_output = decrypt_output.assert_success().get_output().stdout_lossy();
let private_key_string = decrypt_output.split_whitespace().last().unwrap();
// check that the decrypted private key matches the imported private key
let decrypted_private_key = B256::from_str(private_key_string).unwrap();
// the form
assert_eq!(decrypted_private_key, test_private_key);
});
// tests that `cast estimate` is working correctly.
casttest!(estimate_function_gas, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
// ensure we get a positive non-error value for gas estimate
let output: u32 = cmd
.args([
"estimate",
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // vitalik.eth
"--value",
"100",
"deposit()",
"--rpc-url",
eth_rpc_url.as_str(),
])
.assert_success()
.get_output()
.stdout_lossy()
.trim()
.parse()
.unwrap();
assert!(output.ge(&0));
});
// tests that `cast estimate --create` is working correctly.
casttest!(estimate_contract_deploy_gas, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
// sample contract code bytecode. Wouldn't run but is valid bytecode that the estimate method
// accepts and could be deployed.
let output = cmd
.args([
"estimate",
"--rpc-url",
eth_rpc_url.as_str(),
"--create",
"0000",
"ERC20(uint256,string,string)",
"100",
"Test",
"TST",
])
.assert_success()
.get_output()
.stdout_lossy();
// ensure we get a positive non-error value for gas estimate
let output: u32 = output.trim().parse().unwrap();
assert!(output > 0);
});
// tests that the `cast upload-signatures` command works correctly
casttest!(upload_signatures, |_prj, cmd| {
// test no prefix is accepted as function
let output = cmd
.args(["upload-signature", "transfer(address,uint256)"])
.assert_success()
.get_output()
.stdout_lossy();
assert!(output.contains("Function transfer(address,uint256): 0xa9059cbb"), "{}", output);
// test event prefix
cmd.args(["upload-signature", "event Transfer(address,uint256)"]);
let output = cmd.assert_success().get_output().stdout_lossy();
assert!(output.contains("Event Transfer(address,uint256): 0x69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2"), "{}", output);
// test error prefix
cmd.args(["upload-signature", "error ERC20InsufficientBalance(address,uint256,uint256)"]);
let output = cmd.assert_success().get_output().stdout_lossy();
assert!(
output.contains("Function ERC20InsufficientBalance(address,uint256,uint256): 0xe450d38c"),
"{}",
output
); // Custom error is interpreted as function
// test multiple sigs
cmd.args([
"upload-signature",
"event Transfer(address,uint256)",
"transfer(address,uint256)",
"approve(address,uint256)",
]);
let output = cmd.assert_success().get_output().stdout_lossy();
assert!(output.contains("Event Transfer(address,uint256): 0x69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2"), "{}", output);
assert!(output.contains("Function transfer(address,uint256): 0xa9059cbb"), "{}", output);
assert!(output.contains("Function approve(address,uint256): 0x095ea7b3"), "{}", output);
// test abi
cmd.args([
"upload-signature",
"event Transfer(address,uint256)",
"transfer(address,uint256)",
"error ERC20InsufficientBalance(address,uint256,uint256)",
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/ERC20Artifact.json")
.into_os_string()
.into_string()
.unwrap()
.as_str(),
]);
let output = cmd.assert_success().get_output().stdout_lossy();
assert!(output.contains("Event Transfer(address,uint256): 0x69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de2"), "{}", output);
assert!(output.contains("Function transfer(address,uint256): 0xa9059cbb"), "{}", output);
assert!(output.contains("Function approve(address,uint256): 0x095ea7b3"), "{}", output);
assert!(output.contains("Function decimals(): 0x313ce567"), "{}", output);
assert!(output.contains("Function allowance(address,address): 0xdd62ed3e"), "{}", output);
assert!(
output.contains("Function ERC20InsufficientBalance(address,uint256,uint256): 0xe450d38c"),
"{}",
output
);
});
// tests that the `cast to-rlp` and `cast from-rlp` commands work correctly
casttest!(rlp, |_prj, cmd| {
cmd.args(["--to-rlp", "[\"0xaa\", [[\"bb\"]], \"0xcc\"]"]).assert_success().stdout_eq(str![[
r#"
0xc881aac3c281bb81cc
"#
]]);
cmd.cast_fuse();
cmd.args(["--from-rlp", "0xcbc58455556666c0c0c2c1c0"]).assert_success().stdout_eq(str![[r#"
[["0x55556666"],[],[],[[[]]]]
"#]]);
});
// test for cast_rpc without arguments
casttest!(rpc_no_args, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
// Call `cast rpc eth_chainId`
cmd.args(["rpc", "--rpc-url", eth_rpc_url.as_str(), "eth_chainId"]).assert_success().stdout_eq(
str![[r#"
"0x1"
"#]],
);
});
// test for cast_rpc without arguments using websocket
casttest!(ws_rpc_no_args, |_prj, cmd| {
let eth_rpc_url = next_ws_rpc_endpoint();
// Call `cast rpc eth_chainId`
cmd.args(["rpc", "--rpc-url", eth_rpc_url.as_str(), "eth_chainId"]).assert_success().stdout_eq(
str![[r#"
"0x1"
"#]],
);
});
// test for cast_rpc with arguments
casttest!(rpc_with_args, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
// Call `cast rpc eth_getBlockByNumber 0x123 false`
cmd.args(["rpc", "--rpc-url", eth_rpc_url.as_str(), "eth_getBlockByNumber", "0x123", "false"])
.assert_success()
.stdout_eq(str![[r#"
{"number":"0x123","hash":"0xc5dab4e189004a1312e9db43a40abb2de91ad7dd25e75880bf36016d8e9df524","transactions":[],"totalDifficulty":"0x4dea420908b","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","extraData":"0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32","nonce":"0x29d6547c196e00e0","miner":"0xbb7b8287f3f0a933474a79eae42cbca977791171","difficulty":"0x494433b31","gasLimit":"0x1388","gasUsed":"0x0","uncles":[],"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x220","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","stateRoot":"0x3fe6bd17aa85376c7d566df97d9f2e536f37f7a87abb3a6f9e2891cf9442f2e4","mixHash":"0x943056aa305aa6d22a3c06110942980342d1f4d4b11c17711961436a0f963ea0","parentHash":"0x7abfd11e862ccde76d6ea8ee20978aac26f4bcb55de1188cc0335be13e817017","timestamp":"0x55ba4564"}
"#]]);
});
// test for cast_rpc with raw params
casttest!(rpc_raw_params, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
// Call `cast rpc eth_getBlockByNumber --raw '["0x123", false]'`
cmd.args([
"rpc",
"--rpc-url",
eth_rpc_url.as_str(),
"eth_getBlockByNumber",
"--raw",
r#"["0x123", false]"#,
])
.assert_success()
.stdout_eq(str![[r#"
{"number":"0x123","hash":"0xc5dab4e189004a1312e9db43a40abb2de91ad7dd25e75880bf36016d8e9df524","transactions":[],"totalDifficulty":"0x4dea420908b","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","extraData":"0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32","nonce":"0x29d6547c196e00e0","miner":"0xbb7b8287f3f0a933474a79eae42cbca977791171","difficulty":"0x494433b31","gasLimit":"0x1388","gasUsed":"0x0","uncles":[],"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x220","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","stateRoot":"0x3fe6bd17aa85376c7d566df97d9f2e536f37f7a87abb3a6f9e2891cf9442f2e4","mixHash":"0x943056aa305aa6d22a3c06110942980342d1f4d4b11c17711961436a0f963ea0","parentHash":"0x7abfd11e862ccde76d6ea8ee20978aac26f4bcb55de1188cc0335be13e817017","timestamp":"0x55ba4564"}
"#]]);
});
// test for cast_rpc with direct params
casttest!(rpc_raw_params_stdin, |_prj, cmd| {
let eth_rpc_url = next_http_rpc_endpoint();
// Call `echo "\n[\n\"0x123\",\nfalse\n]\n" | cast rpc eth_getBlockByNumber --raw
cmd.args(["rpc", "--rpc-url", eth_rpc_url.as_str(), "eth_getBlockByNumber", "--raw"]).stdin(
|mut stdin| {
stdin.write_all(b"\n[\n\"0x123\",\nfalse\n]\n").unwrap();
},
)
.assert_success()
.stdout_eq(str![[r#"
{"number":"0x123","hash":"0xc5dab4e189004a1312e9db43a40abb2de91ad7dd25e75880bf36016d8e9df524","transactions":[],"totalDifficulty":"0x4dea420908b","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","extraData":"0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32","nonce":"0x29d6547c196e00e0","miner":"0xbb7b8287f3f0a933474a79eae42cbca977791171","difficulty":"0x494433b31","gasLimit":"0x1388","gasUsed":"0x0","uncles":[],"sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x220","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","stateRoot":"0x3fe6bd17aa85376c7d566df97d9f2e536f37f7a87abb3a6f9e2891cf9442f2e4","mixHash":"0x943056aa305aa6d22a3c06110942980342d1f4d4b11c17711961436a0f963ea0","parentHash":"0x7abfd11e862ccde76d6ea8ee20978aac26f4bcb55de1188cc0335be13e817017","timestamp":"0x55ba4564"}
"#]]);
});
// checks `cast calldata` can handle arrays
casttest!(calldata_array, |_prj, cmd| {
cmd.args(["calldata", "propose(string[])", "[\"\"]"]).assert_success().stdout_eq(str![[r#"
0xcde2baba0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000
"#]]);
});
// <https://github.com/foundry-rs/foundry/issues/2705>
casttest!(run_succeeds, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
cmd.args([
"run",
"-v",
"0x2d951c5c95d374263ca99ad9c20c9797fc714330a8037429a3aa4c83d456f845",
"--quick",
"--rpc-url",
rpc.as_str(),
])
.assert_success()
.stdout_eq(str![[r#"
...
Transaction successfully executed.
[GAS]
"#]]);
});
// tests that `cast --to-base` commands are working correctly.
casttest!(to_base, |_prj, cmd| {
let values = [
"1",
"100",
"100000",
"115792089237316195423570985008687907853269984665640564039457584007913129639935",
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"-1",
"-100",
"-100000",
"-57896044618658097711785492504343953926634992332820282019728792003956564819968",
];
for value in values {
for subcmd in ["--to-base", "--to-hex", "--to-dec"] {
if subcmd == "--to-base" {
for base in ["bin", "oct", "dec", "hex"] {
cmd.cast_fuse().args([subcmd, value, base]);
assert!(!cmd.assert_success().get_output().stdout_lossy().trim().is_empty());
}
} else {
cmd.cast_fuse().args([subcmd, value]);
assert!(!cmd.assert_success().get_output().stdout_lossy().trim().is_empty());
}
}
}
});
// tests that revert reason is only present if transaction has reverted.
casttest!(receipt_revert_reason, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
// <https://etherscan.io/tx/0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e>
cmd.args([
"receipt",
"0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e",
"--rpc-url",
rpc.as_str(),
])
.assert_success()
.stdout_eq(str![[r#"
blockHash 0x2cfe65be49863676b6dbc04d58176a14f39b123f1e2f4fea0383a2d82c2c50d0
blockNumber 16239315
contractAddress
cumulativeGasUsed 10743428
effectiveGasPrice 10539984136
from 0x199D5ED7F45F4eE35960cF22EAde2076e95B253F
gasUsed 21000
logs []
logsBloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root
status 1 (success)
transactionHash 0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e
transactionIndex 116
type 0
blobGasPrice
blobGasUsed
authorizationList
to 0x91da5bf3F8Eb72724E6f50Ec6C3D199C6355c59c
"#]]);
let rpc = next_http_rpc_endpoint();
// <https://etherscan.io/tx/0x0e07d8b53ed3d91314c80e53cf25bcde02084939395845cbb625b029d568135c>
cmd.cast_fuse()
.args([
"receipt",
"0x0e07d8b53ed3d91314c80e53cf25bcde02084939395845cbb625b029d568135c",
"--rpc-url",
rpc.as_str(),
])
.assert_success()
.stdout_eq(str![[r#"
blockHash 0x883f974b17ca7b28cb970798d1c80f4d4bb427473dc6d39b2a7fe24edc02902d
blockNumber 14839405
contractAddress
cumulativeGasUsed 20273649
effectiveGasPrice 21491736378
from 0x3cF412d970474804623bb4e3a42dE13F9bCa5436
gasUsed 24952
logs []
logsBloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root
status 0 (failed)
transactionHash 0x0e07d8b53ed3d91314c80e53cf25bcde02084939395845cbb625b029d568135c
transactionIndex 173
type 2
blobGasPrice
blobGasUsed
authorizationList
to 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45
revertReason Transaction too old, data: "0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000135472616e73616374696f6e20746f6f206f6c6400000000000000000000000000"
"#]]);
});
// tests that `cast --parse-bytes32-address` command is working correctly.
casttest!(parse_bytes32_address, |_prj, cmd| {
cmd.args([
"--parse-bytes32-address",
"0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045",
])
.assert_success()
.stdout_eq(str![[r#"
0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
"#]]);
});
casttest!(access_list, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
cmd.args([
"access-list",
"0xbb2b8038a1640196fbe3e38816f3e67cba72d940",
"skim(address)",
"0xbb2b8038a1640196fbe3e38816f3e67cba72d940",
"--rpc-url",
rpc.as_str(),
"--gas-limit", // need to set this for alchemy.io to avoid "intrinsic gas too low" error
"100000",
])
.assert_success()
.stdout_eq(str![[r#"
[GAS]
access list:
- address: [..]
keys:
...
- address: [..]
keys:
...
- address: [..]
keys:
...
"#]]);
});
casttest!(logs_topics, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
cmd.args([
"logs",
"--rpc-url",
rpc.as_str(),
"--from-block",
"12421181",
"--to-block",
"12421182",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000ab5801a7d398351b8be11c439e05c5b3259aec9b",
])
.assert_success()
.stdout_eq(file!["../fixtures/cast_logs.stdout"]);
});
casttest!(logs_topic_2, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
cmd.args([
"logs",
"--rpc-url",
rpc.as_str(),
"--from-block",
"12421181",
"--to-block",
"12421182",
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"",
"0x00000000000000000000000068a99f89e475a078645f4bac491360afe255dff1", /* Filter on the
* `to` address */
])
.assert_success()
.stdout_eq(file!["../fixtures/cast_logs.stdout"]);
});
casttest!(logs_sig, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
cmd.args([
"logs",
"--rpc-url",
rpc.as_str(),
"--from-block",
"12421181",
"--to-block",
"12421182",
"Transfer(address indexed from, address indexed to, uint256 value)",
"0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
])
.assert_success()
.stdout_eq(file!["../fixtures/cast_logs.stdout"]);
});
casttest!(logs_sig_2, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
cmd.args([
"logs",
"--rpc-url",
rpc.as_str(),
"--from-block",
"12421181",
"--to-block",
"12421182",
"Transfer(address indexed from, address indexed to, uint256 value)",
"",
"0x68A99f89E475a078645f4BAC491360aFe255Dff1",
])
.assert_success()
.stdout_eq(file!["../fixtures/cast_logs.stdout"]);
});
casttest!(mktx, |_prj, cmd| {
cmd.args([
"mktx",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--chain",
"1",
"--nonce",
"0",
"--value",
"100",
"--gas-limit",
"21000",
"--gas-price",
"10000000000",
"--priority-gas-price",
"1000000000",
"0x0000000000000000000000000000000000000001",
]).assert_success().stdout_eq(str![[r#"
0x02f86b0180843b9aca008502540be4008252089400000000000000000000000000000000000000016480c001a070d55e79ed3ac9fc8f51e78eb91fd054720d943d66633f2eb1bc960f0126b0eca052eda05a792680de3181e49bab4093541f75b49d1ecbe443077b3660c836016a
"#]]);
});
// ensure recipient or code is required
casttest!(mktx_requires_to, |_prj, cmd| {
cmd.args([
"mktx",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--chain",
"1",
]);
cmd.assert_failure().stderr_eq(str![[r#"
Error:
Must specify a recipient address or contract code to deploy
"#]]);
});
casttest!(mktx_signer_from_mismatch, |_prj, cmd| {
cmd.args([
"mktx",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--from",
"0x0000000000000000000000000000000000000001",
"--chain",
"1",
"0x0000000000000000000000000000000000000001",
]);
cmd.assert_failure().stderr_eq(str![[r#"
Error:
The specified sender via CLI/env vars does not match the sender configured via
the hardware wallet's HD Path.
Please use the `--hd-path <PATH>` parameter to specify the BIP32 Path which
corresponds to the sender, or let foundry automatically detect it by not specifying any sender address.
"#]]);
});
casttest!(mktx_signer_from_match, |_prj, cmd| {
cmd.args([
"mktx",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--from",
"0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf",
"--chain",
"1",
"--nonce",
"0",
"--gas-limit",
"21000",
"--gas-price",
"10000000000",
"--priority-gas-price",
"1000000000",
"0x0000000000000000000000000000000000000001",
]).assert_success().stdout_eq(str![[r#"
0x02f86b0180843b9aca008502540be4008252089400000000000000000000000000000000000000018080c001a0cce9a61187b5d18a89ecd27ec675e3b3f10d37f165627ef89a15a7fe76395ce8a07537f5bffb358ffbef22cda84b1c92f7211723f9e09ae037e81686805d3e5505
"#]]);
});
// tests that the raw encoded transaction is returned
casttest!(tx_raw, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
// <https://etherscan.io/getRawTx?tx=0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e>
cmd.args([
"tx",
"0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e",
"raw",
"--rpc-url",
rpc.as_str(),
]).assert_success().stdout_eq(str![[r#"
0xf86d824c548502743b65088275309491da5bf3f8eb72724e6f50ec6c3d199c6355c59c87a0a73f33e9e4cc8025a0428518b1748a08bbeb2392ea055b418538944d30adfc2accbbfa8362a401d3a4a07d6093ab2580efd17c11b277de7664fce56e6953cae8e925bec3313399860470
"#]]);
// <https://etherscan.io/getRawTx?tx=0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e>
cmd.cast_fuse().args([
"tx",
"0x44f2aaa351460c074f2cb1e5a9e28cbc7d83f33e425101d2de14331c7b7ec31e",
"--raw",
"--rpc-url",
rpc.as_str(),
]).assert_success().stdout_eq(str![[r#"
0xf86d824c548502743b65088275309491da5bf3f8eb72724e6f50ec6c3d199c6355c59c87a0a73f33e9e4cc8025a0428518b1748a08bbeb2392ea055b418538944d30adfc2accbbfa8362a401d3a4a07d6093ab2580efd17c11b277de7664fce56e6953cae8e925bec3313399860470
"#]]);
});
// ensure receipt or code is required
casttest!(send_requires_to, |_prj, cmd| {
cmd.args([
"send",
"--private-key",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"--chain",
"1",
]);
cmd.assert_failure().stderr_eq(str![[r#"
Error:
Must specify a recipient address or contract code to deploy
"#]]);
});
casttest!(storage, |_prj, cmd| {
let rpc = next_http_rpc_endpoint();
cmd.args(["storage", "vitalik.eth", "1", "--rpc-url", &rpc]).assert_success().stdout_eq(str![
[r#"
0x0000000000000000000000000000000000000000000000000000000000000000
"#]
]);
let rpc = next_http_rpc_endpoint();
cmd.cast_fuse()
.args(["storage", "vitalik.eth", "0x01", "--rpc-url", &rpc])
.assert_success()
.stdout_eq(str![[r#"
0x0000000000000000000000000000000000000000000000000000000000000000
"#]]);