foundry_cheatcodes/inspector/
utils.rs

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