Skip to content

Commit 77d9bf5

Browse files
committed
fix(cast): --json support for erc20 cmds
1 parent ced313e commit 77d9bf5

File tree

2 files changed

+135
-5
lines changed

2 files changed

+135
-5
lines changed

crates/cast/src/cmd/erc20.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use foundry_cli::{
1414
opts::RpcOpts,
1515
utils::{LoadConfig, get_provider},
1616
};
17+
use foundry_common::shell;
1718
#[doc(hidden)]
1819
pub use foundry_config::utils::*;
1920

@@ -241,7 +242,11 @@ impl Erc20Subcommand {
241242
.call()
242243
.await?;
243244

244-
sh_println!("{}", format_uint_exp(allowance))?
245+
if shell::is_json() {
246+
sh_println!("{}", serde_json::to_string(&allowance.to_string())?)?
247+
} else {
248+
sh_println!("{}", format_uint_exp(allowance))?
249+
}
245250
}
246251
Self::Balance { token, owner, block, .. } => {
247252
let provider = get_provider(&config)?;
@@ -253,7 +258,12 @@ impl Erc20Subcommand {
253258
.block(block.unwrap_or_default())
254259
.call()
255260
.await?;
256-
sh_println!("{}", format_uint_exp(balance))?
261+
262+
if shell::is_json() {
263+
sh_println!("{}", serde_json::to_string(&balance.to_string())?)?
264+
} else {
265+
sh_println!("{}", format_uint_exp(balance))?
266+
}
257267
}
258268
Self::Name { token, block, .. } => {
259269
let provider = get_provider(&config)?;
@@ -264,7 +274,12 @@ impl Erc20Subcommand {
264274
.block(block.unwrap_or_default())
265275
.call()
266276
.await?;
267-
sh_println!("{}", name)?
277+
278+
if shell::is_json() {
279+
sh_println!("{}", serde_json::to_string(&name)?)?
280+
} else {
281+
sh_println!("{}", name)?
282+
}
268283
}
269284
Self::Symbol { token, block, .. } => {
270285
let provider = get_provider(&config)?;
@@ -275,7 +290,12 @@ impl Erc20Subcommand {
275290
.block(block.unwrap_or_default())
276291
.call()
277292
.await?;
278-
sh_println!("{}", symbol)?
293+
294+
if shell::is_json() {
295+
sh_println!("{}", serde_json::to_string(&symbol)?)?
296+
} else {
297+
sh_println!("{}", symbol)?
298+
}
279299
}
280300
Self::Decimals { token, block, .. } => {
281301
let provider = get_provider(&config)?;
@@ -297,7 +317,12 @@ impl Erc20Subcommand {
297317
.block(block.unwrap_or_default())
298318
.call()
299319
.await?;
300-
sh_println!("{}", format_uint_exp(total_supply))?
320+
321+
if shell::is_json() {
322+
sh_println!("{}", serde_json::to_string(&total_supply.to_string())?)?
323+
} else {
324+
sh_println!("{}", format_uint_exp(total_supply))?
325+
}
301326
}
302327
// State-changing
303328
Self::Transfer { token, to, amount, send_tx, .. } => {

crates/cast/tests/cli/erc20.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,108 @@ forgetest_async!(erc20_burn_success, |prj, cmd| {
263263
let total_supply: U256 = output.split_whitespace().next().unwrap().parse().unwrap();
264264
assert_eq!(total_supply, initial_supply - burn_amount);
265265
});
266+
267+
// tests that `balance` command works correctly with --json flag
268+
forgetest_async!(erc20_balance_json, |prj, cmd| {
269+
let (rpc, token) = setup_token_test(&prj, &mut cmd).await;
270+
271+
let output = cmd
272+
.cast_fuse()
273+
.args(["--json", "erc20", "balance", &token, anvil_const::ADDR1, "--rpc-url", &rpc])
274+
.assert_success()
275+
.get_output()
276+
.stdout_lossy();
277+
278+
let balance_str: String = serde_json::from_str(&output).expect("valid json string");
279+
let balance: U256 = balance_str.parse().unwrap();
280+
assert_eq!(balance, U256::from(1_000_000_000_000_000_000_000u128));
281+
});
282+
283+
// tests that `allowance` command works correctly with --json flag
284+
forgetest_async!(erc20_allowance_json, |prj, cmd| {
285+
let (rpc, token) = setup_token_test(&prj, &mut cmd).await;
286+
287+
// First approve some tokens
288+
let approve_amount = U256::from(50_000_000_000_000_000_000u128);
289+
cmd.cast_fuse()
290+
.args([
291+
"erc20",
292+
"approve",
293+
&token,
294+
anvil_const::ADDR2,
295+
&approve_amount.to_string(),
296+
"--rpc-url",
297+
&rpc,
298+
"--private-key",
299+
anvil_const::PK1,
300+
])
301+
.assert_success();
302+
303+
// Check allowance with JSON flag
304+
let output = cmd
305+
.cast_fuse()
306+
.args([
307+
"--json",
308+
"erc20",
309+
"allowance",
310+
&token,
311+
anvil_const::ADDR1,
312+
anvil_const::ADDR2,
313+
"--rpc-url",
314+
&rpc,
315+
])
316+
.assert_success()
317+
.get_output()
318+
.stdout_lossy();
319+
320+
let allowance_str: String = serde_json::from_str(&output).expect("valid json string");
321+
let allowance: U256 = allowance_str.parse().unwrap();
322+
assert_eq!(allowance, approve_amount);
323+
});
324+
325+
// tests that `name`, `symbol`, `decimals`, and `totalSupply` commands work correctly with --json
326+
// flag
327+
forgetest_async!(erc20_metadata_json, |prj, cmd| {
328+
let (rpc, token) = setup_token_test(&prj, &mut cmd).await;
329+
330+
// Test name with --json
331+
let output = cmd
332+
.cast_fuse()
333+
.args(["--json", "erc20", "name", &token, "--rpc-url", &rpc])
334+
.assert_success()
335+
.get_output()
336+
.stdout_lossy();
337+
let name: String = serde_json::from_str(&output).expect("valid json string");
338+
assert_eq!(name, "Test Token");
339+
340+
// Test symbol with --json
341+
let output = cmd
342+
.cast_fuse()
343+
.args(["--json", "erc20", "symbol", &token, "--rpc-url", &rpc])
344+
.assert_success()
345+
.get_output()
346+
.stdout_lossy();
347+
let symbol: String = serde_json::from_str(&output).expect("valid json string");
348+
assert_eq!(symbol, "TEST");
349+
350+
// Test decimals with --json
351+
let output = cmd
352+
.cast_fuse()
353+
.args(["--json", "erc20", "decimals", &token, "--rpc-url", &rpc])
354+
.assert_success()
355+
.get_output()
356+
.stdout_lossy();
357+
let decimals: u8 = output.trim().parse().expect("valid number");
358+
assert_eq!(decimals, 18);
359+
360+
// Test totalSupply with --json
361+
let output = cmd
362+
.cast_fuse()
363+
.args(["--json", "erc20", "total-supply", &token, "--rpc-url", &rpc])
364+
.assert_success()
365+
.get_output()
366+
.stdout_lossy();
367+
let total_supply_str: String = serde_json::from_str(&output).expect("valid json string");
368+
let total_supply: U256 = total_supply_str.parse().unwrap();
369+
assert_eq!(total_supply, U256::from(1_000_000_000_000_000_000_000u128));
370+
});

0 commit comments

Comments
 (0)