Skip to content

Commit 9b46351

Browse files
fix(cast): cast call --json formatting (#12060)
--------- Co-authored-by: 0xrusowsky <[email protected]>
1 parent afe7d98 commit 9b46351

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

crates/cast/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ impl<P: Provider<AnyNetwork>> Cast<P> {
200200
Ok(if decoded.is_empty() {
201201
res.to_string()
202202
} else if shell::is_json() {
203-
let tokens = decoded.iter().map(format_token_raw).collect::<Vec<_>>();
203+
let tokens = decoded
204+
.into_iter()
205+
.map(|value| serialize_value_as_json(value, None))
206+
.collect::<eyre::Result<Vec<_>>>()?;
204207
serde_json::to_string_pretty(&tokens).unwrap()
205208
} else {
206209
// seth compatible user-friendly return type conversions

crates/cast/tests/cli/main.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use foundry_test_utils::{
1616
str,
1717
util::OutputExt,
1818
};
19+
use serde_json::json;
1920
use std::{fs, path::Path, str::FromStr};
2021

2122
#[macro_use]
@@ -56,7 +57,7 @@ Options:
5657
5758
-j, --threads <THREADS>
5859
Number of threads to use. Specifying 0 defaults to the number of logical cores
59-
60+
...
6061
[aliases: --jobs]
6162
6263
-V, --version
@@ -82,11 +83,11 @@ Display options:
8283
8384
-v, --verbosity...
8485
Verbosity level of the log messages.
85-
86+
...
8687
Pass multiple times to increase the verbosity (e.g. -v, -vv, -vvv).
87-
88+
...
8889
Depending on the context the verbosity levels have different meanings.
89-
90+
...
9091
For example, the verbosity levels of the EVM are:
9192
- 2 (-vv): Print logs for all tests.
9293
- 3 (-vvv): Print execution traces for failing tests.
@@ -1686,7 +1687,7 @@ casttest!(mktx_raw_unsigned_no_from_missing_nonce, |_prj, cmd| {
16861687
"--chain",
16871688
"1",
16881689
"--gas-limit",
1689-
"21000",
1690+
"21000",
16901691
"--gas-price",
16911692
"20000000000",
16921693
"0x742d35Cc6634C0532925a3b8D6Ac6F67C9c2b7FD",
@@ -3586,12 +3587,12 @@ forgetest_async!(cast_send_create_with_constructor_args, |prj, cmd| {
35863587
contract ConstructorContract {
35873588
uint256 public value;
35883589
string public name;
3589-
3590+
35903591
constructor(uint256 _value, string memory _name) {
35913592
value = _value;
35923593
name = _name;
35933594
}
3594-
3595+
35953596
function getValue() public view returns (uint256) {
35963597
return value;
35973598
}
@@ -3665,7 +3666,7 @@ casttest!(cast_estimate_create_with_constructor_args, |prj, cmd| {
36653666
contract EstimateContract {
36663667
uint256 public value;
36673668
string public name;
3668-
3669+
36693670
constructor(uint256 _value, string memory _name) {
36703671
value = _value;
36713672
name = _name;
@@ -3767,13 +3768,13 @@ contract ComplexContract {
37673768
address public owner;
37683769
uint256[] public values;
37693770
bool public active;
3770-
3771+
37713772
constructor(address _owner, uint256[] memory _values, bool _active) {
37723773
owner = _owner;
37733774
values = _values;
37743775
active = _active;
37753776
}
3776-
3777+
37773778
function getValuesLength() public view returns (uint256) {
37783779
return values.length;
37793780
}
@@ -4080,6 +4081,43 @@ casttest!(cast_call_can_override_several_state_diff, |_prj, cmd| {
40804081
"#]]);
40814082
});
40824083

4084+
casttest!(correct_json_serialization, |_prj, cmd| {
4085+
let rpc = next_http_archive_rpc_url();
4086+
// cast calldata "decimals()"
4087+
let calldata = "0x313ce567";
4088+
let tokens = [
4089+
"0xdac17f958d2ee523a2206206994597c13d831ec7", // USDT
4090+
"0x6b175474e89094c44da98b954eedeac495271d0f", // DAI
4091+
"0x6b175474e89094c44da98b954eedeac495271d0f", // WETH
4092+
];
4093+
let calldata_args = format!(
4094+
"[{}]",
4095+
tokens
4096+
.iter()
4097+
.map(|token| format!("({token},false,{calldata})"))
4098+
.collect::<Vec<_>>()
4099+
.join(",")
4100+
);
4101+
let args = vec![
4102+
"call",
4103+
"--json",
4104+
"--rpc-url",
4105+
rpc.as_str(),
4106+
"0xcA11bde05977b3631167028862bE2a173976CA11",
4107+
"aggregate3((address,bool,bytes)[])((bool,bytes)[])",
4108+
&calldata_args,
4109+
];
4110+
let expected_output = json!([[
4111+
[true, "0x0000000000000000000000000000000000000000000000000000000000000006"],
4112+
[true, "0x0000000000000000000000000000000000000000000000000000000000000012"],
4113+
[true, "0x0000000000000000000000000000000000000000000000000000000000000012"]
4114+
]]);
4115+
let decoded: serde_json::Value =
4116+
serde_json::from_slice(&cmd.args(args).assert_success().get_output().stdout)
4117+
.expect("not valid json");
4118+
assert_eq!(decoded, expected_output);
4119+
});
4120+
40834121
// Test cast abi-encode-event with indexed parameters
40844122
casttest!(abi_encode_event_indexed, |_prj, cmd| {
40854123
cmd.args([

0 commit comments

Comments
 (0)