1
1
#[ macro_use]
2
2
extern crate tracing;
3
3
4
+ use alloy_dyn_abi:: DynSolValue ;
4
5
use alloy_primitives:: { eip191_hash_message, hex, keccak256, Address , B256 } ;
5
6
use alloy_provider:: Provider ;
6
7
use alloy_rpc_types:: { BlockId , BlockNumberOrTag :: Latest } ;
@@ -12,7 +13,7 @@ use foundry_cli::{handler, utils};
12
13
use foundry_common:: {
13
14
abi:: get_event,
14
15
ens:: { namehash, ProviderEnsExt } ,
15
- fmt:: { format_uint_exp , print_tokens } ,
16
+ fmt:: { format_tokens , format_tokens_raw , format_uint_exp } ,
16
17
fs,
17
18
selectors:: {
18
19
decode_calldata, decode_event_topic, decode_function_selector, decode_selectors,
@@ -135,11 +136,11 @@ async fn main_args(args: CastArgs) -> Result<()> {
135
136
}
136
137
CastSubcommand :: ParseUnits { value, unit } => {
137
138
let value = stdin:: unwrap_line ( value) ?;
138
- println ! ( "{}" , SimpleCast :: parse_units( & value, unit) ?) ;
139
+ sh_println ! ( "{}" , SimpleCast :: parse_units( & value, unit) ?) ? ;
139
140
}
140
141
CastSubcommand :: FormatUnits { value, unit } => {
141
142
let value = stdin:: unwrap_line ( value) ?;
142
- println ! ( "{}" , SimpleCast :: format_units( & value, unit) ?) ;
143
+ sh_println ! ( "{}" , SimpleCast :: format_units( & value, unit) ?) ? ;
143
144
}
144
145
CastSubcommand :: FromWei { value, unit } => {
145
146
let value = stdin:: unwrap_line ( value) ?;
@@ -189,7 +190,7 @@ async fn main_args(args: CastArgs) -> Result<()> {
189
190
// ABI encoding & decoding
190
191
CastSubcommand :: AbiDecode { sig, calldata, input } => {
191
192
let tokens = SimpleCast :: abi_decode ( & sig, & calldata, input) ?;
192
- print_tokens ( & tokens, shell :: is_json ( ) )
193
+ print_tokens ( & tokens) ;
193
194
}
194
195
CastSubcommand :: AbiEncode { sig, packed, args } => {
195
196
if !packed {
@@ -200,14 +201,14 @@ async fn main_args(args: CastArgs) -> Result<()> {
200
201
}
201
202
CastSubcommand :: CalldataDecode { sig, calldata } => {
202
203
let tokens = SimpleCast :: calldata_decode ( & sig, & calldata, true ) ?;
203
- print_tokens ( & tokens, shell :: is_json ( ) )
204
+ print_tokens ( & tokens) ;
204
205
}
205
206
CastSubcommand :: CalldataEncode { sig, args } => {
206
207
sh_println ! ( "{}" , SimpleCast :: calldata_encode( sig, & args) ?) ?;
207
208
}
208
209
CastSubcommand :: StringDecode { data } => {
209
210
let tokens = SimpleCast :: calldata_decode ( "Any(string)" , & data, true ) ?;
210
- print_tokens ( & tokens, shell :: is_json ( ) )
211
+ print_tokens ( & tokens) ;
211
212
}
212
213
CastSubcommand :: Interface ( cmd) => cmd. run ( ) . await ?,
213
214
CastSubcommand :: CreationCode ( cmd) => cmd. run ( ) . await ?,
@@ -482,7 +483,7 @@ async fn main_args(args: CastArgs) -> Result<()> {
482
483
} ;
483
484
484
485
let tokens = SimpleCast :: calldata_decode ( sig, & calldata, true ) ?;
485
- print_tokens ( & tokens, shell :: is_json ( ) )
486
+ print_tokens ( & tokens) ;
486
487
}
487
488
CastSubcommand :: FourByteEvent { topic } => {
488
489
let topic = stdin:: unwrap_line ( topic) ?;
@@ -618,5 +619,22 @@ async fn main_args(args: CastArgs) -> Result<()> {
618
619
sh_println ! ( "{}" , SimpleCast :: decode_eof( & eof) ?) ?
619
620
}
620
621
} ;
622
+
623
+ /// Prints slice of tokens using [`format_tokens`] or [`format_tokens_raw`] depending whether
624
+ /// the shell is in JSON mode.
625
+ ///
626
+ /// This is included here to avoid a cyclic dependency between `fmt` and `common`.
627
+ fn print_tokens ( tokens : & [ DynSolValue ] ) {
628
+ if shell:: is_json ( ) {
629
+ let tokens: Vec < String > = format_tokens_raw ( tokens) . collect ( ) ;
630
+ let _ = sh_println ! ( "{}" , serde_json:: to_string_pretty( & tokens) . unwrap( ) ) ;
631
+ } else {
632
+ let tokens = format_tokens ( tokens) ;
633
+ tokens. for_each ( |t| {
634
+ let _ = sh_println ! ( "{t}" ) ;
635
+ } ) ;
636
+ }
637
+ }
638
+
621
639
Ok ( ( ) )
622
640
}
0 commit comments