foundry_cheatcodes/
test.rs1use crate::{Cheatcode, Cheatcodes, CheatsCtxt, EthCheatCtx, Result, Vm::*};
4use alloy_chains::Chain as AlloyChain;
5use alloy_primitives::{Address, U256};
6use alloy_sol_types::SolValue;
7use foundry_common::version::SEMVER_VERSION;
8use foundry_evm_core::constants::MAGIC_SKIP;
9use revm::context::{ContextTr, JournalTr};
10use std::str::FromStr;
11
12pub(crate) mod assert;
13pub(crate) mod assume;
14pub(crate) mod expect;
15pub(crate) mod revert_handlers;
16
17impl Cheatcode for breakpoint_0Call {
18 fn apply_stateful<CTX>(&self, ccx: &mut CheatsCtxt<'_, CTX>) -> Result {
19 let Self { char } = self;
20 breakpoint(ccx.state, &ccx.caller, char, true)
21 }
22}
23
24impl Cheatcode for breakpoint_1Call {
25 fn apply_stateful<CTX>(&self, ccx: &mut CheatsCtxt<'_, CTX>) -> Result {
26 let Self { char, value } = self;
27 breakpoint(ccx.state, &ccx.caller, char, *value)
28 }
29}
30
31impl Cheatcode for getFoundryVersionCall {
32 fn apply(&self, _state: &mut Cheatcodes) -> Result {
33 let Self {} = self;
34 Ok(SEMVER_VERSION.abi_encode())
35 }
36}
37
38impl Cheatcode for rpcUrlCall {
39 fn apply(&self, state: &mut Cheatcodes) -> Result {
40 let Self { rpcAlias } = self;
41 let url = state.config.rpc_endpoint(rpcAlias)?.url()?.abi_encode();
42 Ok(url)
43 }
44}
45
46impl Cheatcode for rpcUrlsCall {
47 fn apply(&self, state: &mut Cheatcodes) -> Result {
48 let Self {} = self;
49 state.config.rpc_urls().map(|urls| urls.abi_encode())
50 }
51}
52
53impl Cheatcode for rpcUrlStructsCall {
54 fn apply(&self, state: &mut Cheatcodes) -> Result {
55 let Self {} = self;
56 state.config.rpc_urls().map(|urls| urls.abi_encode())
57 }
58}
59
60impl Cheatcode for sleepCall {
61 fn apply(&self, _state: &mut Cheatcodes) -> Result {
62 let Self { duration } = self;
63 let sleep_duration = std::time::Duration::from_millis(duration.saturating_to());
64 std::thread::sleep(sleep_duration);
65 Ok(Default::default())
66 }
67}
68
69impl Cheatcode for skip_0Call {
70 fn apply_stateful<CTX: EthCheatCtx>(&self, ccx: &mut CheatsCtxt<'_, CTX>) -> Result {
71 let Self { skipTest } = *self;
72 skip_1Call { skipTest, reason: String::new() }.apply_stateful(ccx)
73 }
74}
75
76impl Cheatcode for skip_1Call {
77 fn apply_stateful<CTX: ContextTr>(&self, ccx: &mut CheatsCtxt<'_, CTX>) -> Result {
78 let Self { skipTest, reason } = self;
79 if *skipTest {
80 ensure!(ccx.ecx.journal().depth() <= 1, "`skip` can only be used at test level");
83 Err([MAGIC_SKIP, reason.as_bytes()].concat().into())
84 } else {
85 Ok(Default::default())
86 }
87 }
88}
89
90impl Cheatcode for getChain_0Call {
91 fn apply(&self, state: &mut Cheatcodes) -> Result {
92 let Self { chainAlias } = self;
93 get_chain(state, chainAlias)
94 }
95}
96
97impl Cheatcode for getChain_1Call {
98 fn apply(&self, state: &mut Cheatcodes) -> Result {
99 let Self { chainId } = self;
100 let chain_id_str = chainId.to_string();
102 get_chain(state, &chain_id_str)
103 }
104}
105
106fn breakpoint(state: &mut Cheatcodes, caller: &Address, s: &str, add: bool) -> Result {
108 let mut chars = s.chars();
109 let (Some(point), None) = (chars.next(), chars.next()) else {
110 bail!("breakpoints must be exactly one character");
111 };
112 ensure!(point.is_alphabetic(), "only alphabetic characters are accepted as breakpoints");
113
114 if add {
115 state.breakpoints.insert(point, (*caller, state.pc));
116 } else {
117 state.breakpoints.remove(&point);
118 }
119
120 Ok(Default::default())
121}
122
123fn get_chain(state: &mut Cheatcodes, chain_alias: &str) -> Result {
125 let alloy_chain = AlloyChain::from_str(chain_alias)
127 .map_err(|_| fmt_err!("invalid chain alias: {chain_alias}"))?;
128 let chain_name = alloy_chain.to_string();
129 let chain_id = alloy_chain.id();
130
131 if chain_name == chain_id.to_string() {
135 return Err(fmt_err!("invalid chain alias: {chain_alias}"));
136 }
137
138 let (rpc_url, chain_alias) = if let Some(rpc_url) =
140 state.config.rpc_endpoint(&chain_name).ok().and_then(|e| e.url().ok())
141 {
142 (rpc_url, chain_name.clone())
143 } else {
144 (String::new(), chain_alias.to_string())
145 };
146
147 let chain_struct = Chain {
148 name: chain_name,
149 chainId: U256::from(chain_id),
150 chainAlias: chain_alias,
151 rpcUrl: rpc_url,
152 };
153
154 Ok(chain_struct.abi_encode())
155}