Skip to main content

foundry_cheatcodes/inspector/
utils.rs

1use super::Ecx;
2use crate::inspector::Cheatcodes;
3use alloy_primitives::{Address, Bytes, U256};
4use revm::{
5    context::ContextTr,
6    inspector::JournalExt,
7    interpreter::{CreateInputs, CreateScheme},
8};
9
10/// Common behaviour of legacy and EOF create inputs.
11pub(crate) trait CommonCreateInput {
12    fn caller(&self) -> Address;
13    fn gas_limit(&self) -> u64;
14    fn value(&self) -> U256;
15    fn init_code(&self) -> Bytes;
16    fn scheme(&self) -> Option<CreateScheme>;
17    fn set_caller(&mut self, caller: Address);
18    fn log_debug(&self, cheatcode: &mut Cheatcodes, scheme: &CreateScheme);
19    fn allow_cheatcodes(&self, cheatcodes: &mut Cheatcodes, ecx: Ecx) -> Address;
20}
21
22impl CommonCreateInput for &mut CreateInputs {
23    fn caller(&self) -> Address {
24        CreateInputs::caller(self)
25    }
26    fn gas_limit(&self) -> u64 {
27        CreateInputs::gas_limit(self)
28    }
29    fn value(&self) -> U256 {
30        CreateInputs::value(self)
31    }
32    fn init_code(&self) -> Bytes {
33        CreateInputs::init_code(self).clone()
34    }
35    fn scheme(&self) -> Option<CreateScheme> {
36        Some(CreateInputs::scheme(self))
37    }
38    fn set_caller(&mut self, caller: Address) {
39        CreateInputs::set_call(self, caller);
40    }
41    fn log_debug(&self, cheatcode: &mut Cheatcodes, scheme: &CreateScheme) {
42        let kind = match scheme {
43            CreateScheme::Create => "create",
44            CreateScheme::Create2 { .. } => "create2",
45            CreateScheme::Custom { .. } => "custom",
46        };
47        debug!(target: "cheatcodes", tx=?cheatcode.broadcastable_transactions.back().unwrap(), "broadcastable {kind}");
48    }
49    fn allow_cheatcodes(&self, cheatcodes: &mut Cheatcodes, ecx: Ecx) -> Address {
50        let caller = CreateInputs::caller(self);
51        let old_nonce =
52            ecx.journal().evm_state().get(&caller).map(|acc| acc.info.nonce).unwrap_or_default();
53        let created_address = self.created_address(old_nonce);
54        cheatcodes.allow_cheatcodes_on_create(ecx, caller, created_address);
55        created_address
56    }
57}