foundry_cheatcodes/
lib.rs1#![cfg_attr(not(test), warn(unused_crate_dependencies))]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7#![allow(elided_lifetimes_in_paths)] #[macro_use]
10extern crate foundry_common;
11
12#[macro_use]
13pub extern crate foundry_cheatcodes_spec as spec;
14
15#[macro_use]
16extern crate tracing;
17
18use alloy_primitives::{Address, B256, U256};
19use evm2::interpreter::GasTracker;
20use foundry_evm_core::{
21 backend::{Backend, CowBackend, LocalForkId},
22 evm::{
23 BlockEnvFor, EvmEnvFor, EvmFactoryFor, FoundryEvmFactory, FoundryEvmFor, FoundryEvmNetwork,
24 SpecFor, evm_env,
25 },
26 fork::CreateFork,
27};
28
29pub use Vm::ForgeContext;
30pub use config::CheatsConfig;
31pub use error::{Error, ErrorKind, Result};
32pub use inspector::{
33 BroadcastableTransaction, BroadcastableTransactions, Cheatcodes, CheatcodesExecutor,
34 StorageHook, decode_cheatcode_call, message_result, revert_frame,
35};
36pub use spec::{CheatcodeDef, Vm};
37
38#[macro_use]
39mod error;
40
41mod base64;
42
43mod config;
44
45mod crypto;
46
47mod version;
48
49mod env;
50pub use env::{current_execution_context, set_execution_context};
51
52mod evm;
53pub use evm::{RecordAccess, prank::Prank};
54
55mod expected_emit;
56
57mod external_storage;
58
59mod fs;
60
61mod inspector;
62pub use inspector::CheatcodeAnalysis;
63
64mod json;
65
66#[cfg(feature = "monad")]
67mod monad;
68
69mod script;
70pub use script::{Wallets, WalletsInner};
71
72mod state;
73pub use state::CheatcodesCore;
74
75mod string;
76pub use string::parse_value;
77
78mod tempo;
79
80mod test;
81pub use test::{
82 expect::{ExpectedCallTracker, ExpectedCallType},
83 revert_handlers::{DUMMY_CALL_OUTPUT, DUMMY_CREATE_ADDRESS, expect_revert_mismatch},
84};
85
86mod toml;
87
88mod utils;
89
90pub(crate) trait Cheatcode: CheatcodeDef {
92 fn apply_context(&self, _state: &mut CheatcodesCore) -> Result {
96 self.apply_context_free()
97 }
98
99 #[inline(always)]
103 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
104 self.apply_context(&mut ccx.state.core)
105 }
106
107 #[inline(always)]
111 fn apply_full<FEN: FoundryEvmNetwork>(
112 &self,
113 ccx: &mut CheatsCtxt<'_, '_, FEN>,
114 executor: &mut dyn CheatcodesExecutor<FEN>,
115 ) -> Result {
116 let _ = executor;
117 self.apply_stateful(ccx)
118 }
119
120 fn apply_context_free(&self) -> Result {
124 unimplemented!("{}", Self::CHEATCODE.func.id)
125 }
126}
127
128pub trait CheatcodesBackend<FEN: FoundryEvmNetwork> {
133 fn backend(&self) -> &Backend<FEN>;
135
136 fn backend_mut(&mut self) -> &mut Backend<FEN>;
138}
139
140impl<FEN: FoundryEvmNetwork> CheatcodesBackend<FEN> for Backend<FEN> {
141 fn backend(&self) -> &Self {
142 self
143 }
144
145 fn backend_mut(&mut self) -> &mut Self {
146 self
147 }
148}
149
150impl<FEN: FoundryEvmNetwork> CheatcodesBackend<FEN> for CowBackend<'_, FEN> {
151 fn backend(&self) -> &Backend<FEN> {
152 self
153 }
154
155 fn backend_mut(&mut self) -> &mut Backend<FEN> {
156 self
157 }
158}
159
160pub struct CheatsCtxt<'a, 'evm, FEN: FoundryEvmNetwork> {
162 pub(crate) state: &'a mut Cheatcodes<FEN>,
164 pub(crate) evm: &'a mut FoundryEvmFor<'evm, FEN>,
166 pub(crate) backend: &'a mut dyn CheatcodesBackend<FEN>,
168 pub(crate) caller: Address,
170 pub(crate) gas: &'a mut GasTracker,
172 pub(crate) is_static: bool,
174 pub(crate) depth: usize,
176}
177
178impl<'evm, FEN: FoundryEvmNetwork> CheatsCtxt<'_, 'evm, FEN> {
179 pub(crate) fn ensure_not_precompile(&self, address: &Address) -> Result<()> {
180 if self.is_precompile(address) { Err(precompile_error(address)) } else { Ok(()) }
181 }
182
183 pub(crate) fn is_precompile(&self, address: &Address) -> bool {
189 self.evm.precompiles().addresses().contains(address)
190 }
191
192 #[inline]
194 pub(crate) fn db(&self) -> &Backend<FEN> {
195 self.backend.backend()
196 }
197
198 #[inline]
200 pub(crate) fn db_mut(&mut self) -> &mut Backend<FEN> {
201 self.backend.backend_mut()
202 }
203
204 #[inline]
206 pub(crate) fn evm_db_mut(&mut self) -> (&mut FoundryEvmFor<'evm, FEN>, &mut Backend<FEN>) {
207 (&mut *self.evm, self.backend.backend_mut())
208 }
209
210 #[inline]
212 pub(crate) const fn depth(&self) -> usize {
213 self.depth
214 }
215
216 #[inline]
218 pub(crate) const fn caller_depth(&self) -> usize {
219 self.depth.saturating_sub(1)
220 }
221
222 #[inline]
227 pub(crate) fn spec(&self) -> SpecFor<FEN> {
228 self.state
229 .execution_evm_version
230 .or_else(|| self.db().lifecycle().pending_env().map(|env| env.spec))
231 .unwrap_or_else(|| self.evm.config_spec_id())
232 }
233
234 pub(crate) fn evm_env(&self) -> EvmEnvFor<FEN> {
237 let env = evm_env(self.evm);
238 let spec = self.spec();
239 if spec == env.spec {
240 return env;
241 }
242 EvmFactoryFor::<FEN>::default().env_with_spec(env, spec)
243 }
244
245 #[inline]
247 pub(crate) fn chain_id(&self) -> u64 {
248 self.state
249 .env_overrides
250 .get(self.active_fork_id())
251 .and_then(|overrides| overrides.chain_id)
252 .unwrap_or(self.evm.version().chain_id)
253 }
254
255 #[inline]
257 pub(crate) const fn block_number(&self) -> U256 {
258 self.evm.block().number
259 }
260
261 #[inline]
263 pub(crate) const fn timestamp(&self) -> U256 {
264 self.evm.block().timestamp
265 }
266
267 #[inline]
269 pub(crate) fn basefee(&self) -> u64 {
270 self.evm.block().basefee.saturating_to()
271 }
272
273 #[inline]
275 pub(crate) fn slot_num(&self) -> u64 {
276 self.evm.block().slot_num.saturating_to()
277 }
278
279 #[inline]
281 pub(crate) fn update_block(&mut self, f: impl FnOnce(&mut BlockEnvFor<FEN>)) {
282 let mut block = *self.evm.block();
283 f(&mut block);
284 self.evm.set_block(block);
285 }
286
287 #[inline]
289 pub(crate) fn tx_caller(&self) -> Address {
290 self.state.origin_override.unwrap_or(self.evm.tx_env().origin)
291 }
292
293 #[inline]
295 pub(crate) fn tx_blob_hashes(&self) -> Vec<B256> {
296 self.evm.tx_env().blob_hashes.iter().map(|hash| B256::from(*hash)).collect()
297 }
298
299 #[inline]
301 pub(crate) fn active_fork_id(&self) -> Option<LocalForkId> {
302 self.db().active_fork_id()
303 }
304
305 #[inline]
307 pub(crate) fn active_fork_url(&self) -> Option<String> {
308 self.db().active_fork_url()
309 }
310
311 #[inline]
313 pub(crate) fn active_fork_block_number(&self) -> Option<u64> {
314 self.db().active_fork_block_number()
315 }
316
317 #[inline]
319 pub(crate) fn active_fork_options(&self) -> Option<CreateFork> {
320 self.db().active_fork_options()
321 }
322
323 #[inline]
325 pub(crate) fn is_forked_mode(&self) -> bool {
326 self.db().is_forked_mode()
327 }
328
329 #[inline]
331 pub(crate) fn is_persistent(&self, account: &Address) -> bool {
332 self.db().is_persistent(account)
333 }
334}
335
336#[cold]
337fn precompile_error(address: &Address) -> Error {
338 fmt_err!("cannot use precompile {address} as an argument")
339}