Skip to main content

foundry_cheatcodes/inspector/
utils.rs

1use crate::inspector::Cheatcodes;
2use alloy_primitives::{Address, Bytes, U256};
3use foundry_evm_core::evm::{FoundryContextFor, FoundryEvmNetwork};
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<FEN: FoundryEvmNetwork>(
19        &self,
20        cheatcode: &mut Cheatcodes<FEN>,
21        scheme: &CreateScheme,
22    );
23    fn allow_cheatcodes<FEN: FoundryEvmNetwork>(
24        &self,
25        cheatcodes: &mut Cheatcodes<FEN>,
26        ecx: &mut FoundryContextFor<'_, FEN>,
27    ) -> Address;
28}
29
30impl CommonCreateInput for &mut CreateInputs {
31    fn caller(&self) -> Address {
32        CreateInputs::caller(self)
33    }
34    fn gas_limit(&self) -> u64 {
35        CreateInputs::gas_limit(self)
36    }
37    fn value(&self) -> U256 {
38        CreateInputs::value(self)
39    }
40    fn init_code(&self) -> Bytes {
41        CreateInputs::init_code(self).clone()
42    }
43    fn scheme(&self) -> Option<CreateScheme> {
44        Some(CreateInputs::scheme(self))
45    }
46    fn set_caller(&mut self, caller: Address) {
47        CreateInputs::set_call(self, caller);
48    }
49    fn log_debug<FEN: FoundryEvmNetwork>(
50        &self,
51        cheatcode: &mut Cheatcodes<FEN>,
52        scheme: &CreateScheme,
53    ) {
54        let kind = match scheme {
55            CreateScheme::Create => "create",
56            CreateScheme::Create2 { .. } => "create2",
57            CreateScheme::Custom { .. } => "custom",
58        };
59        debug!(target: "cheatcodes", tx=?cheatcode.broadcastable_transactions.back().unwrap(), "broadcastable {kind}");
60    }
61    fn allow_cheatcodes<FEN: FoundryEvmNetwork>(
62        &self,
63        cheatcodes: &mut Cheatcodes<FEN>,
64        ecx: &mut FoundryContextFor<'_, FEN>,
65    ) -> Address {
66        let caller = CreateInputs::caller(self);
67        let old_nonce =
68            ecx.journal().evm_state().get(&caller).map(|acc| acc.info.nonce).unwrap_or_default();
69        let created_address = self.created_address(old_nonce);
70        cheatcodes.allow_cheatcodes_on_create(ecx, caller, created_address);
71        created_address
72    }
73}