Skip to content

Commit 36776bb

Browse files
clabbymikelodder7
authored andcommitted
Add --json to cast interface (foundry-rs#5748)
New clippy lints 🦀 Remove `cast abi` in favor of `cast interface <address> --json` Write to file if the output location is specified Improve
1 parent 7b8e51c commit 36776bb

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

crates/cast/bin/cmd/interface.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,24 @@ pub struct InterfaceArgs {
3333
)]
3434
output: Option<PathBuf>,
3535

36+
/// If specified, the interface will be output as JSON rather than Solidity.
37+
#[clap(long, short)]
38+
json: bool,
39+
3640
#[clap(flatten)]
3741
etherscan: EtherscanOpts,
3842
}
3943

4044
impl InterfaceArgs {
4145
pub async fn run(self) -> Result<()> {
42-
let InterfaceArgs { path_or_address, name, pragma, output: output_location, etherscan } =
43-
self;
46+
let InterfaceArgs {
47+
path_or_address,
48+
name,
49+
pragma,
50+
output: output_location,
51+
etherscan,
52+
json,
53+
} = self;
4454
let config = Config::from(&etherscan);
4555
let chain = config.chain_id.unwrap_or_default();
4656
let source = if Path::new(&path_or_address).exists() {
@@ -53,10 +63,17 @@ impl InterfaceArgs {
5363
let interfaces = SimpleCast::generate_interface(source).await?;
5464

5565
// put it all together
56-
let pragma = format!("pragma solidity {pragma};");
57-
let interfaces =
58-
interfaces.iter().map(|iface| iface.source.to_string()).collect::<Vec<_>>().join("\n");
59-
let res = format!("{pragma}\n\n{interfaces}");
66+
let res = if json {
67+
interfaces.into_iter().map(|iface| iface.json_abi).collect::<Vec<_>>().join("\n")
68+
} else {
69+
let pragma = format!("pragma solidity {pragma};");
70+
let interfaces = interfaces
71+
.iter()
72+
.map(|iface| iface.source.to_string())
73+
.collect::<Vec<_>>()
74+
.join("\n");
75+
format!("{pragma}\n\n{interfaces}")
76+
};
6077

6178
// print or write to file
6279
if let Some(loc) = output_location {

crates/cast/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ where
820820

821821
pub struct InterfaceSource {
822822
pub name: String,
823+
pub json_abi: String,
823824
pub source: String,
824825
}
825826

@@ -1535,7 +1536,11 @@ impl SimpleCast {
15351536
.zip(contract_names)
15361537
.map(|(contract_abi, name)| {
15371538
let source = foundry_utils::abi::abi_to_solidity(contract_abi, &name)?;
1538-
Ok(InterfaceSource { name, source })
1539+
Ok(InterfaceSource {
1540+
name,
1541+
json_abi: serde_json::to_string_pretty(contract_abi)?,
1542+
source,
1543+
})
15391544
})
15401545
.collect::<Result<Vec<InterfaceSource>>>()
15411546
}

crates/common/src/selectors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ impl SignEthClient {
224224
// filter for signatures that can be decoded
225225
Ok(sigs
226226
.iter()
227-
.cloned()
228227
.filter(|sig| abi_decode(sig, calldata, true, true).is_ok())
228+
.cloned()
229229
.collect::<Vec<String>>())
230230
}
231231

crates/fmt/src/solang_ext/ast_eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn to_num_reversed(string: &str) -> U256 {
2222
/// Helper to filter [ParameterList] to omit empty
2323
/// parameters
2424
fn filter_params(list: &ParameterList) -> ParameterList {
25-
list.iter().cloned().filter(|(_, param)| param.is_some()).collect::<Vec<_>>()
25+
list.iter().filter(|(_, param)| param.is_some()).cloned().collect::<Vec<_>>()
2626
}
2727

2828
/// Check if two ParseTrees are equal ignoring location information or ordering if ordering does

0 commit comments

Comments
 (0)