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;
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 tempo;
63
64mod test;
65pub use test::expect::ExpectedCallTracker;
66
67mod toml;
68
69mod utils;
70
71pub(crate) trait Cheatcode: CheatcodeDef {
73 fn apply<FEN: FoundryEvmNetwork>(&self, state: &mut Cheatcodes<FEN>) -> Result {
77 let _ = state;
78 unimplemented!("{}", Self::CHEATCODE.func.id)
79 }
80
81 #[inline(always)]
85 fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
86 self.apply(ccx.state)
87 }
88
89 #[inline(always)]
93 fn apply_full<FEN: FoundryEvmNetwork>(
94 &self,
95 ccx: &mut CheatsCtxt<'_, '_, FEN>,
96 executor: &mut dyn CheatcodesExecutor<FEN>,
97 ) -> Result {
98 let _ = executor;
99 self.apply_stateful(ccx)
100 }
101}
102
103pub struct CheatsCtxt<'a, 'db, FEN: FoundryEvmNetwork + 'db> {
105 pub(crate) state: &'a mut Cheatcodes<FEN>,
107 pub(crate) ecx: &'a mut FoundryContextFor<'db, FEN>,
109 pub(crate) caller: Address,
111 pub(crate) gas_limit: u64,
113}
114
115impl<'a, 'db, FEN: FoundryEvmNetwork> std::ops::Deref for CheatsCtxt<'a, 'db, FEN> {
116 type Target = FoundryContextFor<'db, FEN>;
117
118 #[inline(always)]
119 fn deref(&self) -> &Self::Target {
120 self.ecx
121 }
122}
123
124impl<'db, FEN: FoundryEvmNetwork> std::ops::DerefMut for CheatsCtxt<'_, 'db, FEN> {
125 #[inline(always)]
126 fn deref_mut(&mut self) -> &mut Self::Target {
127 self.ecx
128 }
129}
130
131impl<FEN: FoundryEvmNetwork> CheatsCtxt<'_, '_, FEN> {
132 pub(crate) fn ensure_not_precompile(&self, address: &Address) -> Result<()> {
133 if self.is_precompile(address) { Err(precompile_error(address)) } else { Ok(()) }
134 }
135
136 pub(crate) fn is_precompile(&self, address: &Address) -> bool {
137 self.ecx.journal().precompile_addresses().contains(address)
138 }
139}
140
141#[cold]
142fn precompile_error(address: &Address) -> Error {
143 fmt_err!("cannot use precompile {address} as an argument")
144}