1use crate::{
2 Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt, Result, Vm::*,
3 json::json_value_to_token,
4};
5use alloy_dyn_abi::DynSolValue;
6use alloy_evm::EvmEnv;
7use alloy_network::AnyNetwork;
8use alloy_primitives::{B256, U256};
9use alloy_provider::Provider;
10use alloy_rpc_types::Filter;
11use alloy_sol_types::SolValue;
12use foundry_common::provider::ProviderBuilder;
13use foundry_evm_core::{
14 FoundryContextExt, backend::JournaledState, evm::FoundryEvmNetwork, fork::CreateFork,
15};
16use revm::context::ContextTr;
17
18impl Cheatcode for activeForkCall {
19 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
20 let Self {} = self;
21 ccx.ecx
22 .db()
23 .active_fork_id()
24 .map(|id| id.abi_encode())
25 .ok_or_else(|| fmt_err!("no active fork"))
26 }
27}
28
29impl Cheatcode for createFork_0Call {
30 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
31 let Self { urlOrAlias } = self;
32 create_fork(ccx, urlOrAlias, None)
33 }
34}
35
36impl Cheatcode for createFork_1Call {
37 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
38 let Self { urlOrAlias, blockNumber } = self;
39 create_fork(ccx, urlOrAlias, Some(blockNumber.saturating_to()))
40 }
41}
42
43impl Cheatcode for createFork_2Call {
44 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
45 let Self { urlOrAlias, txHash } = self;
46 create_fork_at_transaction(ccx, urlOrAlias, txHash)
47 }
48}
49
50impl Cheatcode for createSelectFork_0Call {
51 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
52 let Self { urlOrAlias } = self;
53 create_select_fork(ccx, urlOrAlias, None)
54 }
55}
56
57impl Cheatcode for createSelectFork_1Call {
58 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
59 let Self { urlOrAlias, blockNumber } = self;
60 create_select_fork(ccx, urlOrAlias, Some(blockNumber.saturating_to()))
61 }
62}
63
64impl Cheatcode for createSelectFork_2Call {
65 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
66 let Self { urlOrAlias, txHash } = self;
67 create_select_fork_at_transaction(ccx, urlOrAlias, txHash)
68 }
69}
70
71impl Cheatcode for rollFork_0Call {
72 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
73 let Self { blockNumber } = self;
74 persist_caller(ccx);
75 fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
76 db.roll_fork(None, (*blockNumber).to(), evm_env, inner)
77 })
78 }
79}
80
81impl Cheatcode for rollFork_1Call {
82 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
83 let Self { txHash } = self;
84 persist_caller(ccx);
85 fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
86 db.roll_fork_to_transaction(None, *txHash, evm_env, inner)
87 })
88 }
89}
90
91impl Cheatcode for rollFork_2Call {
92 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
93 let Self { forkId, blockNumber } = self;
94 persist_caller(ccx);
95 fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
96 db.roll_fork(Some(*forkId), (*blockNumber).to(), evm_env, inner)
97 })
98 }
99}
100
101impl Cheatcode for rollFork_3Call {
102 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
103 let Self { forkId, txHash } = self;
104 persist_caller(ccx);
105 fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
106 db.roll_fork_to_transaction(Some(*forkId), *txHash, evm_env, inner)
107 })
108 }
109}
110
111impl Cheatcode for selectForkCall {
112 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
113 let Self { forkId } = self;
114 persist_caller(ccx);
115 check_broadcast(ccx.state)?;
116 fork_env_op(ccx.ecx, |db, evm_env, tx_env, inner| {
117 db.select_fork(*forkId, evm_env, tx_env, inner)
118 })
119 }
120}
121
122impl Cheatcode for transact_0Call {
123 fn apply_full<FEN: FoundryEvmNetwork>(
124 &self,
125 ccx: &mut CheatsCtxt<'_, '_, FEN>,
126 executor: &mut dyn CheatcodesExecutor<FEN>,
127 ) -> Result {
128 let Self { txHash } = *self;
129 transact(ccx, executor, txHash, None)
130 }
131}
132
133impl Cheatcode for transact_1Call {
134 fn apply_full<FEN: FoundryEvmNetwork>(
135 &self,
136 ccx: &mut CheatsCtxt<'_, '_, FEN>,
137 executor: &mut dyn CheatcodesExecutor<FEN>,
138 ) -> Result {
139 let Self { forkId, txHash } = *self;
140 transact(ccx, executor, txHash, Some(forkId))
141 }
142}
143
144impl Cheatcode for allowCheatcodesCall {
145 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
146 let Self { account } = self;
147 ccx.ecx.db_mut().allow_cheatcode_access(*account);
148 Ok(Default::default())
149 }
150}
151
152impl Cheatcode for makePersistent_0Call {
153 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
154 let Self { account } = self;
155 ccx.ecx.db_mut().add_persistent_account(*account);
156 Ok(Default::default())
157 }
158}
159
160impl Cheatcode for makePersistent_1Call {
161 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
162 let Self { account0, account1 } = self;
163 ccx.ecx.db_mut().add_persistent_account(*account0);
164 ccx.ecx.db_mut().add_persistent_account(*account1);
165 Ok(Default::default())
166 }
167}
168
169impl Cheatcode for makePersistent_2Call {
170 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
171 let Self { account0, account1, account2 } = self;
172 ccx.ecx.db_mut().add_persistent_account(*account0);
173 ccx.ecx.db_mut().add_persistent_account(*account1);
174 ccx.ecx.db_mut().add_persistent_account(*account2);
175 Ok(Default::default())
176 }
177}
178
179impl Cheatcode for makePersistent_3Call {
180 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
181 let Self { accounts } = self;
182 for account in accounts {
183 ccx.ecx.db_mut().add_persistent_account(*account);
184 }
185 Ok(Default::default())
186 }
187}
188
189impl Cheatcode for revokePersistent_0Call {
190 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
191 let Self { account } = self;
192 ccx.ecx.db_mut().remove_persistent_account(account);
193 Ok(Default::default())
194 }
195}
196
197impl Cheatcode for revokePersistent_1Call {
198 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
199 let Self { accounts } = self;
200 for account in accounts {
201 ccx.ecx.db_mut().remove_persistent_account(account);
202 }
203 Ok(Default::default())
204 }
205}
206
207impl Cheatcode for isPersistentCall {
208 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
209 let Self { account } = self;
210 Ok(ccx.ecx.db().is_persistent(account).abi_encode())
211 }
212}
213
214impl Cheatcode for rpc_0Call {
215 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
216 let Self { method, params } = self;
217 let url =
218 ccx.ecx.db().active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?;
219 rpc_call(&url, method, params)
220 }
221}
222
223impl Cheatcode for rpc_1Call {
224 fn apply<FEN: FoundryEvmNetwork>(&self, state: &mut Cheatcodes<FEN>) -> Result {
225 let Self { urlOrAlias, method, params } = self;
226 let url = state.config.rpc_endpoint(urlOrAlias)?.url()?;
227 rpc_call(&url, method, params)
228 }
229}
230
231impl Cheatcode for rpcJson_0Call {
232 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
233 let Self { method, params } = self;
234 let url =
235 ccx.ecx.db().active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?;
236 rpc_json_call(&url, method, params)
237 }
238}
239
240impl Cheatcode for rpcJson_1Call {
241 fn apply<FEN: FoundryEvmNetwork>(&self, state: &mut Cheatcodes<FEN>) -> Result {
242 let Self { urlOrAlias, method, params } = self;
243 let url = state.config.rpc_endpoint(urlOrAlias)?.url()?;
244 rpc_json_call(&url, method, params)
245 }
246}
247
248impl Cheatcode for eth_getLogsCall {
249 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
250 let Self { fromBlock, toBlock, target, topics } = self;
251 let (Ok(from_block), Ok(to_block)) = (u64::try_from(fromBlock), u64::try_from(toBlock))
252 else {
253 bail!("blocks in block range must be less than 2^64")
254 };
255
256 if topics.len() > 4 {
257 bail!("topics array must contain at most 4 elements")
258 }
259
260 let url =
261 ccx.ecx.db().active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?;
262 let provider = ProviderBuilder::<AnyNetwork>::new(&url).build()?;
263 let mut filter = Filter::new().address(*target).from_block(from_block).to_block(to_block);
264 for (i, &topic) in topics.iter().enumerate() {
265 filter.topics[i] = topic.into();
266 }
267
268 let logs = foundry_common::block_on(provider.get_logs(&filter))
269 .map_err(|e| fmt_err!("failed to get logs: {e}"))?;
270
271 let eth_logs = logs
272 .into_iter()
273 .map(|log| EthGetLogs {
274 emitter: log.address(),
275 topics: log.topics().to_vec(),
276 data: log.inner.data.data,
277 blockHash: log.block_hash.unwrap_or_default(),
278 blockNumber: log.block_number.unwrap_or_default(),
279 transactionHash: log.transaction_hash.unwrap_or_default(),
280 transactionIndex: log.transaction_index.unwrap_or_default(),
281 logIndex: U256::from(log.log_index.unwrap_or_default()),
282 removed: log.removed,
283 })
284 .collect::<Vec<_>>();
285
286 Ok(eth_logs.abi_encode())
287 }
288}
289
290impl Cheatcode for getRawBlockHeaderCall {
291 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
292 let Self { blockNumber } = self;
293 let url = ccx.ecx.db().active_fork_url().ok_or_else(|| fmt_err!("no active fork"))?;
294 let provider = ProviderBuilder::<AnyNetwork>::new(&url).build()?;
295 let block_number = u64::try_from(blockNumber)
296 .map_err(|_| fmt_err!("block number must be less than 2^64"))?;
297 let block =
298 foundry_common::block_on(async move { provider.get_block(block_number.into()).await })
299 .map_err(|e| fmt_err!("failed to get block: {e}"))?
300 .ok_or_else(|| fmt_err!("block {block_number} not found"))?;
301
302 let header: alloy_consensus::Header = block
303 .into_inner()
304 .header
305 .inner
306 .try_into_header()
307 .map_err(|e| fmt_err!("failed to convert to header: {e}"))?;
308 Ok(alloy_rlp::encode(&header).abi_encode())
309 }
310}
311
312fn create_select_fork<FEN: FoundryEvmNetwork>(
314 ccx: &mut CheatsCtxt<'_, '_, FEN>,
315 url_or_alias: &str,
316 block: Option<u64>,
317) -> Result {
318 check_broadcast(ccx.state)?;
319
320 let fork = create_fork_request(ccx, url_or_alias, block)?;
321 fork_env_op(ccx.ecx, |db, evm_env, tx_env, inner| {
322 db.create_select_fork(fork, evm_env, tx_env, inner)
323 })
324}
325
326fn create_fork<FEN: FoundryEvmNetwork>(
328 ccx: &mut CheatsCtxt<'_, '_, FEN>,
329 url_or_alias: &str,
330 block: Option<u64>,
331) -> Result {
332 let fork = create_fork_request(ccx, url_or_alias, block)?;
333 let id = ccx.ecx.db_mut().create_fork(fork)?;
334 Ok(id.abi_encode())
335}
336
337fn create_select_fork_at_transaction<FEN: FoundryEvmNetwork>(
339 ccx: &mut CheatsCtxt<'_, '_, FEN>,
340 url_or_alias: &str,
341 transaction: &B256,
342) -> Result {
343 check_broadcast(ccx.state)?;
344
345 let fork = create_fork_request(ccx, url_or_alias, None)?;
346 fork_env_op(ccx.ecx, |db, evm_env, tx_env, inner| {
347 db.create_select_fork_at_transaction(fork, evm_env, tx_env, inner, *transaction)
348 })
349}
350
351fn create_fork_at_transaction<FEN: FoundryEvmNetwork>(
353 ccx: &mut CheatsCtxt<'_, '_, FEN>,
354 url_or_alias: &str,
355 transaction: &B256,
356) -> Result {
357 let fork = create_fork_request(ccx, url_or_alias, None)?;
358 let id = ccx.ecx.db_mut().create_fork_at_transaction(fork, *transaction)?;
359 Ok(id.abi_encode())
360}
361
362fn create_fork_request<FEN: FoundryEvmNetwork>(
364 ccx: &mut CheatsCtxt<'_, '_, FEN>,
365 url_or_alias: &str,
366 block: Option<u64>,
367) -> Result<CreateFork> {
368 persist_caller(ccx);
369
370 let rpc_endpoint = ccx.state.config.rpc_endpoint(url_or_alias)?;
371 let url = rpc_endpoint.url()?;
372 let mut evm_opts = ccx.state.config.evm_opts.clone();
373 evm_opts.fork_block_number = block;
374 evm_opts.fork_retries = rpc_endpoint.config.retries;
375 evm_opts.fork_retry_backoff = rpc_endpoint.config.retry_backoff;
376 if let Some(Ok(auth)) = rpc_endpoint.auth {
377 evm_opts.fork_headers = Some(vec![format!("Authorization: {auth}")]);
378 }
379 let fork = CreateFork {
380 enable_caching: !ccx.state.config.no_storage_caching
381 && ccx.state.config.rpc_storage_caching.enable_for_endpoint(&url),
382 url,
383 evm_opts,
384 };
385 Ok(fork)
386}
387
388fn fork_env_op<CTX: FoundryContextExt, T: SolValue>(
392 ecx: &mut CTX,
393 f: impl FnOnce(
394 &mut CTX::Db,
395 &mut EvmEnv<CTX::Spec, CTX::Block>,
396 &mut CTX::Tx,
397 &mut JournaledState,
398 ) -> eyre::Result<T>,
399) -> Result {
400 let mut evm_env = ecx.evm_clone();
401 let mut tx_env = ecx.tx_clone();
402 let (db, inner) = ecx.db_journal_inner_mut();
403 let result = f(db, &mut evm_env, &mut tx_env, inner)?;
404 ecx.set_evm(evm_env);
405 ecx.set_tx(tx_env);
406 Ok(result.abi_encode())
407}
408
409fn check_broadcast<FEN: FoundryEvmNetwork>(state: &Cheatcodes<FEN>) -> Result<()> {
410 if state.broadcast.is_none() {
411 Ok(())
412 } else {
413 Err(fmt_err!("cannot select forks during a broadcast"))
414 }
415}
416
417fn transact<FEN: FoundryEvmNetwork>(
418 ccx: &mut CheatsCtxt<'_, '_, FEN>,
419 executor: &mut dyn CheatcodesExecutor<FEN>,
420 transaction: B256,
421 fork_id: Option<U256>,
422) -> Result {
423 executor.transact_on_db(ccx.state, ccx.ecx, fork_id, transaction)?;
424 Ok(Default::default())
425}
426
427fn persist_caller<FEN: FoundryEvmNetwork>(ccx: &mut CheatsCtxt<'_, '_, FEN>) {
432 ccx.ecx.db_mut().add_persistent_account(ccx.caller);
433}
434
435fn rpc_call(url: &str, method: &str, params: &str) -> Result {
437 let result = rpc_result(url, method, params)?;
438 let result_as_tokens = convert_to_bytes(
439 &json_value_to_token(&result, None)
440 .map_err(|err| fmt_err!("failed to parse result: {err}"))?,
441 );
442
443 let payload = match &result_as_tokens {
444 DynSolValue::Bytes(b) => b.clone(),
445 _ => result_as_tokens.abi_encode(),
446 };
447 Ok(DynSolValue::Bytes(payload).abi_encode())
448}
449
450fn rpc_json_call(url: &str, method: &str, params: &str) -> Result {
452 let result = rpc_result(url, method, params)?;
453 Ok(serde_json::to_string(&result)?.abi_encode())
454}
455
456fn rpc_result(url: &str, method: &str, params: &str) -> Result<serde_json::Value> {
457 let provider = ProviderBuilder::<AnyNetwork>::new(url).build()?;
458 let params_json: serde_json::Value = serde_json::from_str(params)?;
459 foundry_common::block_on(provider.raw_request(method.to_string().into(), params_json))
460 .map_err(|err| fmt_err!("{method:?}: {err}"))
461}
462
463fn convert_to_bytes(token: &DynSolValue) -> DynSolValue {
465 match token {
466 DynSolValue::FixedBytes(bytes, size) => {
469 DynSolValue::Bytes(bytes.as_slice()[..*size].to_vec())
470 }
471 DynSolValue::Address(addr) => DynSolValue::Bytes(addr.to_vec()),
472 val => val.clone(),
473 }
474}