forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.rs
466 lines (404 loc) · 15.3 KB
/
call.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
use crate::{
traces::TraceKind,
tx::{CastTxBuilder, SenderKind},
Cast,
};
use alloy_primitives::{Address, Bytes, TxKind, U256, map::FbBuildHasher};
use alloy_rpc_types::{BlockId, BlockNumberOrTag};
use alloy_rpc_types::state::StateOverride;
use clap::Parser;
use eyre::Result;
use foundry_cli::{
opts::{EthereumOpts, TransactionOpts},
utils::{self, handle_traces, parse_ether_value, TraceResult},
};
use foundry_common::{ens::NameOrAddress, shell};
use foundry_compilers::artifacts::EvmVersion;
use foundry_config::{
figment::{
self,
value::{Dict, Map},
Figment, Metadata, Profile,
},
Config,
};
use foundry_evm::{
executors::TracingExecutor,
opts::EvmOpts,
traces::{InternalTraceMode, TraceMode},
};
use std::str::FromStr;
use std::collections::HashMap;
/// CLI arguments for `cast call`.
///
/// ## State Override Flags
///
/// The following flags can be used to override the state for the call:
///
/// * `--override-balance <address>:<balance>` - Override the balance of an account
/// * `--override-nonce <address>:<nonce>` - Override the nonce of an account
/// * `--override-code <address>:<code>` - Override the code of an account
/// * `--override-state <address>:<slot>:<value>` - Override a storage slot of an account
///
/// Multiple overrides can be specified for the same account. For example:
///
/// ```bash
/// cast call 0x... "transfer(address,uint256)" 0x... 100 \
/// --override-balance 0x123:0x1234 \
/// --override-nonce 0x123:1 \
/// --override-code 0x123:0x1234 \
/// --override-state 0x123:0x1:0x1234
/// ```
#[derive(Debug, Parser)]
pub struct CallArgs {
/// The destination of the transaction.
#[arg(value_parser = NameOrAddress::from_str)]
to: Option<NameOrAddress>,
/// The signature of the function to call.
sig: Option<String>,
/// The arguments of the function to call.
args: Vec<String>,
/// Raw hex-encoded data for the transaction. Used instead of \[SIG\] and \[ARGS\].
#[arg(
long,
conflicts_with_all = &["sig", "args"]
)]
data: Option<String>,
/// Forks the remote rpc, executes the transaction locally and prints a trace
#[arg(long, default_value_t = false)]
trace: bool,
/// Opens an interactive debugger.
/// Can only be used with `--trace`.
#[arg(long, requires = "trace")]
debug: bool,
#[arg(long, requires = "trace")]
decode_internal: bool,
/// Labels to apply to the traces; format: `address:label`.
/// Can only be used with `--trace`.
#[arg(long, requires = "trace")]
labels: Vec<String>,
/// The EVM Version to use.
/// Can only be used with `--trace`.
#[arg(long, requires = "trace")]
evm_version: Option<EvmVersion>,
/// The block height to query at.
///
/// Can also be the tags earliest, finalized, safe, latest, or pending.
#[arg(long, short)]
block: Option<BlockId>,
/// Enable Odyssey features.
#[arg(long, alias = "alphanet")]
pub odyssey: bool,
#[command(subcommand)]
command: Option<CallSubcommands>,
#[command(flatten)]
tx: TransactionOpts,
#[command(flatten)]
eth: EthereumOpts,
/// Use current project artifacts for trace decoding.
#[arg(long, visible_alias = "la")]
pub with_local_artifacts: bool,
/// Override the balance of an account.
/// Format: address:balance
#[arg(long = "override-balance", value_name = "ADDRESS:BALANCE")]
pub balance_overrides: Vec<String>,
/// Override the nonce of an account.
/// Format: address:nonce
#[arg(long = "override-nonce", value_name = "ADDRESS:NONCE")]
pub nonce_overrides: Vec<String>,
/// Override the code of an account.
/// Format: address:code
#[arg(long = "override-code", value_name = "ADDRESS:CODE")]
pub code_overrides: Vec<String>,
/// Override the state of an account.
/// Format: address:slot:value
#[arg(long = "override-state", value_name = "ADDRESS:SLOT:VALUE")]
pub state_overrides: Vec<String>,
/// Override the state diff of an account.
/// Format: address:slot:value
#[arg(long = "override-state-diff", value_name = "ADDRESS:SLOT:VALUE")]
pub state_diff_overrides: Vec<String>,
}
#[derive(Debug, Parser)]
pub enum CallSubcommands {
/// ignores the address field and simulates creating a contract
#[command(name = "--create")]
Create {
/// Bytecode of contract.
code: String,
/// The signature of the constructor.
sig: Option<String>,
/// The arguments of the constructor.
args: Vec<String>,
/// Ether to send in the transaction.
///
/// Either specified in wei, or as a string with a unit type.
///
/// Examples: 1ether, 10gwei, 0.01ether
#[arg(long, value_parser = parse_ether_value)]
value: Option<U256>,
},
}
impl CallArgs {
pub async fn run(self) -> Result<()> {
let figment = Into::<Figment>::into(&self.eth).merge(&self);
let evm_opts = figment.extract::<EvmOpts>()?;
let mut config = Config::from_provider(figment)?.sanitized();
let state_override = &self.get_state_overrides()?;
let Self {
to,
mut sig,
mut args,
mut tx,
eth,
command,
block,
trace,
evm_version,
debug,
decode_internal,
labels,
data,
with_local_artifacts,
..
} = self;
if let Some(data) = data {
sig = Some(data);
}
let provider = utils::get_provider(&config)?;
let sender = SenderKind::from_wallet_opts(eth.wallet).await?;
let from = sender.address();
let code = if let Some(CallSubcommands::Create {
code,
sig: create_sig,
args: create_args,
value,
}) = command
{
sig = create_sig;
args = create_args;
if let Some(value) = value {
tx.value = Some(value);
}
Some(code)
} else {
None
};
let (tx, func) = CastTxBuilder::new(&provider, tx, &config)
.await?
.with_to(to)
.await?
.with_code_sig_and_args(code, sig, args)
.await?
.build_raw(sender)
.await?;
if trace {
if let Some(BlockId::Number(BlockNumberOrTag::Number(block_number))) = self.block {
// Override Config `fork_block_number` (if set) with CLI value.
config.fork_block_number = Some(block_number);
}
let create2_deployer = evm_opts.create2_deployer;
let (mut env, fork, chain, odyssey) =
TracingExecutor::get_fork_material(&config, evm_opts).await?;
// modify settings that usually set in eth_call
env.cfg.disable_block_gas_limit = true;
env.block.gas_limit = U256::MAX;
let trace_mode = TraceMode::Call
.with_debug(debug)
.with_decode_internal(if decode_internal {
InternalTraceMode::Full
} else {
InternalTraceMode::None
})
.with_state_changes(shell::verbosity() > 4);
let mut executor = TracingExecutor::new(
env,
fork,
evm_version,
trace_mode,
odyssey,
create2_deployer,
)?;
let value = tx.value.unwrap_or_default();
let input = tx.inner.input.into_input().unwrap_or_default();
let tx_kind = tx.inner.to.expect("set by builder");
let trace = match tx_kind {
TxKind::Create => {
let deploy_result = executor.deploy(from, input, value, None);
TraceResult::try_from(deploy_result)?
}
TxKind::Call(to) => TraceResult::from_raw(
executor.transact_raw(from, to, input, value)?,
TraceKind::Execution,
),
};
handle_traces(
trace,
&config,
chain,
labels,
with_local_artifacts,
debug,
decode_internal,
)
.await?;
return Ok(());
}
sh_println!("{}", Cast::new(provider).call(&tx, func.as_ref(), block, state_override.clone()).await?)?;
Ok(())
}
/// Parse state overrides from command line arguments
pub fn get_state_overrides(&self) -> eyre::Result<Option<StateOverride>> {
let mut state_override = StateOverride::default();
// Store state_diff_overrides in a local variable to avoid partial move
let balance_overrides = self.balance_overrides.clone();
// Parse balance overrides
for override_str in balance_overrides {
let (addr, balance) = Self::parse_address_value(&override_str)?;
state_override.entry(addr).or_default().balance = Some(balance);
}
// Store state_diff_overrides in a local variable to avoid partial move
let nonce_overrides = self.nonce_overrides.clone();
// Parse nonce overrides
for override_str in nonce_overrides {
let (addr, nonce) = Self::parse_address_value_for_nonce(&override_str)?;
state_override.entry(addr).or_default().nonce = Some(nonce);
}
// Store state_diff_overrides in a local variable to avoid partial move
let code_overrides = self.code_overrides.clone();
// Parse code overrides
for override_str in code_overrides {
let (addr, code_str) = override_str.split_once(':').ok_or_else(|| {
eyre::eyre!("Invalid code override format. Expected <address>:<code>")
})?;
let addr = addr.parse()?;
let code = Bytes::from_str(code_str)?;
state_override.entry(addr).or_default().code = Some(code);
}
// Store state_diff_overrides in a local variable to avoid partial move
let state_overrides = self.state_overrides.clone();
// Parse state overrides
for override_str in state_overrides {
let (addr, slot, value) = Self::parse_address_slot_value(&override_str)?;
let state_map = state_override.entry(addr).or_default().state.get_or_insert_with(|| HashMap::with_hasher(FbBuildHasher::<32>::default()));
state_map.insert(slot.into(), value.into());
}
// Store state_diff_overrides in a local variable to avoid partial move
let state_diff_overrides = self.state_diff_overrides.clone();
// Parse state diff overrides
for override_str in state_diff_overrides {
let (addr, slot, value) = Self::parse_address_slot_value(&override_str)?;
let state_diff_map = state_override.entry(addr).or_default().state_diff.get_or_insert_with(|| HashMap::with_hasher(FbBuildHasher::<32>::default()));
state_diff_map.insert(slot.into(), value.into());
}
Ok(if state_override.is_empty() {
None
} else {
Some(state_override)
})
}
/// Parse an override string in the format address:value
pub fn parse_address_value(s: &str) -> eyre::Result<(Address, U256)> {
let (addr, value) = s.split_once(':').ok_or_else(|| {
eyre::eyre!("Invalid override format. Expected <address>:<value>")
})?;
Ok((addr.parse()?, value.parse()?))
}
/// Parse an override string in the format address:value
pub fn parse_address_value_for_nonce( s: &str) -> eyre::Result<(Address, u64)> {
let (addr, value) = s.split_once(':').ok_or_else(|| {
eyre::eyre!("Invalid override format. Expected <address>:<value>")
})?;
Ok((addr.parse()?, value.parse()?))
}
/// Parse an override string in the format address:slot:value
pub fn parse_address_slot_value(s: &str) -> eyre::Result<(Address, U256, U256)> {
let mut parts = s.split(':');
let addr = parts.next().ok_or_else(|| {
eyre::eyre!("Invalid override format. Expected <address>:<slot>:<value>")
})?.parse()?;
let slot = parts.next().ok_or_else(|| {
eyre::eyre!("Invalid override format. Expected <address>:<slot>:<value>")
})?.parse()?;
let value = parts.next().ok_or_else(|| {
eyre::eyre!("Invalid override format. Expected <address>:<slot>:<value>")
})?.parse()?;
if parts.next().is_some() {
return Err(eyre::eyre!("Invalid override format. Expected <address>:<slot>:<value>"));
}
Ok((addr, slot, value))
}
}
impl figment::Provider for CallArgs {
fn metadata(&self) -> Metadata {
Metadata::named("CallArgs")
}
fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
let mut map = Map::new();
if self.odyssey {
map.insert("odyssey".into(), self.odyssey.into());
}
if let Some(evm_version) = self.evm_version {
map.insert("evm_version".into(), figment::value::Value::serialize(evm_version)?);
}
Ok(Map::from([(Config::selected_profile(), map)]))
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{hex};
#[test]
fn can_parse_call_data() {
let data = hex::encode("hello");
let args = CallArgs::parse_from(["foundry-cli", "--data", data.as_str()]);
assert_eq!(args.data, Some(data));
let data = hex::encode_prefixed("hello");
let args = CallArgs::parse_from(["foundry-cli", "--data", data.as_str()]);
assert_eq!(args.data, Some(data));
}
#[test]
fn can_parse_state_overrides() {
let args = CallArgs::parse_from([
"foundry-cli",
"--override-balance",
"0x123:0x1234",
"--override-nonce",
"0x123:1",
"--override-code",
"0x123:0x1234",
"--override-state",
"0x123:0x1:0x1234",
]);
assert_eq!(args.balance_overrides, vec!["0x123:0x1234"]);
assert_eq!(args.nonce_overrides, vec!["0x123:1"]);
assert_eq!(args.code_overrides, vec!["0x123:0x1234"]);
assert_eq!(args.state_overrides, vec!["0x123:0x1:0x1234"]);
}
#[test]
fn can_parse_multiple_state_overrides() {
let args = CallArgs::parse_from([
"foundry-cli",
"--override-balance",
"0x123:0x1234",
"--override-balance",
"0x456:0x5678",
"--override-nonce",
"0x123:1",
"--override-nonce",
"0x456:2",
"--override-code",
"0x123:0x1234",
"--override-code",
"0x456:0x5678",
"--override-state",
"0x123:0x1:0x1234",
"--override-state",
"0x456:0x2:0x5678",
]);
assert_eq!(args.balance_overrides, vec!["0x123:0x1234", "0x456:0x5678"]);
assert_eq!(args.nonce_overrides, vec!["0x123:1", "0x456:2"]);
assert_eq!(args.code_overrides, vec!["0x123:0x1234", "0x456:0x5678"]);
assert_eq!(args.state_overrides, vec!["0x123:0x1:0x1234", "0x456:0x2:0x5678"]);
}
}