Skip to main content

foundry_cheatcodes/
lib.rs

1//! # foundry-cheatcodes
2//!
3//! Foundry cheatcodes implementations.
4
5#![cfg_attr(not(test), warn(unused_crate_dependencies))]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7#![allow(elided_lifetimes_in_paths)] // Cheats context uses 3 lifetimes
8
9#[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;
19use foundry_evm_core::{
20    backend::DatabaseExt,
21    evm::{FoundryContextFor, FoundryEvmNetwork},
22};
23use revm::context::{ContextTr, JournalTr};
24
25pub use Vm::ForgeContext;
26pub use config::CheatsConfig;
27pub use error::{Error, ErrorKind, Result};
28pub use foundry_evm_core::evm::NestedEvmClosure;
29pub use inspector::{
30    BroadcastableTransaction, BroadcastableTransactions, Cheatcodes, CheatcodesExecutor,
31};
32pub use spec::{CheatcodeDef, Vm};
33
34#[macro_use]
35mod error;
36
37mod base64;
38
39mod config;
40
41mod crypto;
42
43mod version;
44
45mod env;
46pub use env::set_execution_context;
47
48mod evm;
49
50mod fs;
51
52mod inspector;
53pub use inspector::CheatcodeAnalysis;
54
55mod json;
56
57mod script;
58pub use script::{Wallets, WalletsInner};
59
60mod string;
61
62mod test;
63pub use test::expect::ExpectedCallTracker;
64
65mod toml;
66
67mod utils;
68
69/// Cheatcode implementation.
70pub(crate) trait Cheatcode: CheatcodeDef {
71    /// Applies this cheatcode to the given state.
72    ///
73    /// Implement this function if you don't need access to the EVM data.
74    fn apply<FEN: FoundryEvmNetwork>(&self, state: &mut Cheatcodes<FEN>) -> Result {
75        let _ = state;
76        unimplemented!("{}", Self::CHEATCODE.func.id)
77    }
78
79    /// Applies this cheatcode to the given context.
80    ///
81    /// Implement this function if you need access to the EVM data.
82    #[inline(always)]
83    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
84        self.apply(ccx.state)
85    }
86
87    /// Applies this cheatcode to the given context and executor.
88    ///
89    /// Implement this function if you need access to the executor.
90    #[inline(always)]
91    fn apply_full<FEN: FoundryEvmNetwork>(
92        &self,
93        ccx: &mut CheatsCtxt<'_, '_, FEN>,
94        executor: &mut dyn CheatcodesExecutor<FEN>,
95    ) -> Result {
96        let _ = executor;
97        self.apply_stateful(ccx)
98    }
99}
100
101/// The cheatcode context.
102pub struct CheatsCtxt<'a, 'db, FEN: FoundryEvmNetwork + 'db> {
103    /// The cheatcodes inspector state.
104    pub(crate) state: &'a mut Cheatcodes<FEN>,
105    /// The EVM context.
106    pub(crate) ecx: &'a mut FoundryContextFor<'db, FEN>,
107    /// The original `msg.sender`.
108    pub(crate) caller: Address,
109    /// Gas limit of the current cheatcode call.
110    pub(crate) gas_limit: u64,
111}
112
113impl<'a, 'db, FEN: FoundryEvmNetwork> std::ops::Deref for CheatsCtxt<'a, 'db, FEN> {
114    type Target = FoundryContextFor<'db, FEN>;
115
116    #[inline(always)]
117    fn deref(&self) -> &Self::Target {
118        self.ecx
119    }
120}
121
122impl<'db, FEN: FoundryEvmNetwork> std::ops::DerefMut for CheatsCtxt<'_, 'db, FEN> {
123    #[inline(always)]
124    fn deref_mut(&mut self) -> &mut Self::Target {
125        self.ecx
126    }
127}
128
129impl<FEN: FoundryEvmNetwork> CheatsCtxt<'_, '_, FEN> {
130    pub(crate) fn ensure_not_precompile(&self, address: &Address) -> Result<()> {
131        if self.is_precompile(address) { Err(precompile_error(address)) } else { Ok(()) }
132    }
133
134    pub(crate) fn is_precompile(&self, address: &Address) -> bool {
135        self.ecx.journal().precompile_addresses().contains(address)
136    }
137}
138
139#[cold]
140fn precompile_error(address: &Address) -> Error {
141    fmt_err!("cannot use precompile {address} as an argument")
142}