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