Skip to content

Commit 91bbb93

Browse files
yash-atreyarplusq
authored andcommitted
refactor(cheatcodes): mv ScriptWallets into Cheatcode (foundry-rs#9106)
* refactor(`cheatcodes`): mv `ScriptWallets` into `Cheatcode` from `CheatsConfig` * nit * rename `ScriptWallets` to `Wallets` * rename cheatcode * doc nits
1 parent f06a910 commit 91bbb93

File tree

17 files changed

+72
-57
lines changed

17 files changed

+72
-57
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/spec/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ interface Vm {
19121912

19131913
/// Returns addresses of available unlocked wallets in the script environment.
19141914
#[cheatcode(group = Scripting)]
1915-
function getScriptWallets() external returns (address[] memory wallets);
1915+
function getWallets() external returns (address[] memory wallets);
19161916

19171917
// ======== Utilities ========
19181918

crates/cheatcodes/src/config.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::Result;
2-
use crate::{script::ScriptWallets, Vm::Rpc};
2+
use crate::Vm::Rpc;
33
use alloy_primitives::{map::AddressHashMap, U256};
44
use foundry_common::{fs::normalize_path, ContractsByArtifact};
55
use foundry_compilers::{utils::canonicalize, ProjectPathsConfig};
@@ -43,8 +43,6 @@ pub struct CheatsConfig {
4343
pub evm_opts: EvmOpts,
4444
/// Address labels from config
4545
pub labels: AddressHashMap<String>,
46-
/// Script wallets
47-
pub script_wallets: Option<ScriptWallets>,
4846
/// Artifacts which are guaranteed to be fresh (either recompiled or cached).
4947
/// If Some, `vm.getDeployedCode` invocations are validated to be in scope of this list.
5048
/// If None, no validation is performed.
@@ -65,7 +63,6 @@ impl CheatsConfig {
6563
config: &Config,
6664
evm_opts: EvmOpts,
6765
available_artifacts: Option<ContractsByArtifact>,
68-
script_wallets: Option<ScriptWallets>,
6966
running_contract: Option<String>,
7067
running_version: Option<Version>,
7168
) -> Self {
@@ -93,7 +90,6 @@ impl CheatsConfig {
9390
allowed_paths,
9491
evm_opts,
9592
labels: config.labels.clone(),
96-
script_wallets,
9793
available_artifacts,
9894
running_contract,
9995
running_version,
@@ -223,7 +219,6 @@ impl Default for CheatsConfig {
223219
allowed_paths: vec![],
224220
evm_opts: Default::default(),
225221
labels: Default::default(),
226-
script_wallets: None,
227222
available_artifacts: Default::default(),
228223
running_contract: Default::default(),
229224
running_version: Default::default(),
@@ -245,7 +240,6 @@ mod tests {
245240
None,
246241
None,
247242
None,
248-
None,
249243
)
250244
}
251245

crates/cheatcodes/src/crypto.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Implementations of [`Crypto`](spec::Group::Crypto) Cheatcodes.
22
3-
use crate::{Cheatcode, Cheatcodes, Result, ScriptWallets, Vm::*};
3+
use crate::{Cheatcode, Cheatcodes, Result, Vm::*, Wallets};
44
use alloy_primitives::{keccak256, Address, B256, U256};
55
use alloy_signer::{Signer, SignerSync};
66
use alloy_signer_local::{
@@ -17,7 +17,6 @@ use k256::{
1717
elliptic_curve::{bigint::ArrayEncoding, sec1::ToEncodedPoint},
1818
};
1919
use p256::ecdsa::{signature::hazmat::PrehashSigner, Signature, SigningKey as P256SigningKey};
20-
use std::sync::Arc;
2120

2221
/// The BIP32 default derivation path prefix.
2322
const DEFAULT_DERIVATION_PATH_PREFIX: &str = "m/44'/60'/0'/0/";
@@ -134,9 +133,9 @@ fn inject_wallet(state: &mut Cheatcodes, wallet: LocalSigner<SigningKey>) -> Add
134133
script_wallets.add_local_signer(wallet);
135134
} else {
136135
// This is needed in case of testing scripts, wherein script wallets are not set on setup.
137-
let script_wallets = ScriptWallets::new(MultiWallet::default(), None);
136+
let script_wallets = Wallets::new(MultiWallet::default(), None);
138137
script_wallets.add_local_signer(wallet);
139-
Arc::make_mut(&mut state.config).script_wallets = Some(script_wallets);
138+
state.set_wallets(script_wallets);
140139
}
141140
address
142141
}

crates/cheatcodes/src/inspector.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
DealRecord, GasRecord, RecordAccess,
99
},
1010
inspector::utils::CommonCreateInput,
11-
script::{Broadcast, ScriptWallets},
11+
script::{Broadcast, Wallets},
1212
test::{
1313
assume::AssumeNoRevert,
1414
expect::{
@@ -476,6 +476,8 @@ pub struct Cheatcodes {
476476

477477
/// Deprecated cheatcodes mapped to the reason. Used to report warnings on test results.
478478
pub deprecated: HashMap<&'static str, Option<&'static str>>,
479+
/// Unlocked wallets used in scripts and testing of scripts.
480+
pub wallets: Option<Wallets>,
479481
}
480482

481483
// This is not derived because calling this in `fn new` with `..Default::default()` creates a second
@@ -523,12 +525,18 @@ impl Cheatcodes {
523525
ignored_traces: Default::default(),
524526
arbitrary_storage: Default::default(),
525527
deprecated: Default::default(),
528+
wallets: Default::default(),
526529
}
527530
}
528531

529532
/// Returns the configured script wallets.
530-
pub fn script_wallets(&self) -> Option<&ScriptWallets> {
531-
self.config.script_wallets.as_ref()
533+
pub fn script_wallets(&self) -> Option<&Wallets> {
534+
self.wallets.as_ref()
535+
}
536+
537+
/// Sets the unlocked wallets.
538+
pub fn set_wallets(&mut self, wallets: Wallets) {
539+
self.wallets = Some(wallets);
532540
}
533541

534542
/// Decodes the input data and applies the cheatcode.

crates/cheatcodes/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mod inspector;
4545
mod json;
4646

4747
mod script;
48-
pub use script::{ScriptWallets, ScriptWalletsInner};
48+
pub use script::{Wallets, WalletsInner};
4949

5050
mod string;
5151

crates/cheatcodes/src/script.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Cheatcode for stopBroadcastCall {
6161
}
6262
}
6363

64-
impl Cheatcode for getScriptWalletsCall {
64+
impl Cheatcode for getWalletsCall {
6565
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
6666
let script_wallets =
6767
ccx.state.script_wallets().cloned().map(|sw| sw.signers().unwrap_or_default());
@@ -91,29 +91,29 @@ pub struct Broadcast {
9191

9292
/// Contains context for wallet management.
9393
#[derive(Debug)]
94-
pub struct ScriptWalletsInner {
94+
pub struct WalletsInner {
9595
/// All signers in scope of the script.
9696
pub multi_wallet: MultiWallet,
9797
/// Optional signer provided as `--sender` flag.
9898
pub provided_sender: Option<Address>,
9999
}
100100

101-
/// Clonable wrapper around [`ScriptWalletsInner`].
101+
/// Clonable wrapper around [`WalletsInner`].
102102
#[derive(Debug, Clone)]
103-
pub struct ScriptWallets {
103+
pub struct Wallets {
104104
/// Inner data.
105-
pub inner: Arc<Mutex<ScriptWalletsInner>>,
105+
pub inner: Arc<Mutex<WalletsInner>>,
106106
}
107107

108-
impl ScriptWallets {
108+
impl Wallets {
109109
#[allow(missing_docs)]
110110
pub fn new(multi_wallet: MultiWallet, provided_sender: Option<Address>) -> Self {
111-
Self { inner: Arc::new(Mutex::new(ScriptWalletsInner { multi_wallet, provided_sender })) }
111+
Self { inner: Arc::new(Mutex::new(WalletsInner { multi_wallet, provided_sender })) }
112112
}
113113

114-
/// Consumes [ScriptWallets] and returns [MultiWallet].
114+
/// Consumes [Wallets] and returns [MultiWallet].
115115
///
116-
/// Panics if [ScriptWallets] is still in use.
116+
/// Panics if [Wallets] is still in use.
117117
pub fn into_multi_wallet(self) -> MultiWallet {
118118
Arc::into_inner(self.inner)
119119
.map(|m| m.into_inner().multi_wallet)

crates/chisel/src/executor.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ impl SessionSource {
335335
self.config.evm_opts.clone(),
336336
None,
337337
None,
338-
None,
339338
Some(self.solc.version.clone()),
340339
)
341340
.into(),

crates/evm/evm/src/inspectors/stack.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::{
33
TracingInspector,
44
};
55
use alloy_primitives::{map::AddressHashMap, Address, Bytes, Log, TxKind, U256};
6-
use foundry_cheatcodes::CheatcodesExecutor;
6+
use foundry_cheatcodes::{CheatcodesExecutor, Wallets};
77
use foundry_evm_core::{backend::DatabaseExt, InspectorExt};
88
use foundry_evm_coverage::HitMaps;
99
use foundry_evm_traces::{SparsedTraceArena, TraceMode};
@@ -57,6 +57,8 @@ pub struct InspectorStackBuilder {
5757
pub enable_isolation: bool,
5858
/// Whether to enable Alphanet features.
5959
pub alphanet: bool,
60+
/// The wallets to set in the cheatcodes context.
61+
pub wallets: Option<Wallets>,
6062
}
6163

6264
impl InspectorStackBuilder {
@@ -87,6 +89,13 @@ impl InspectorStackBuilder {
8789
self
8890
}
8991

92+
/// Set the wallets.
93+
#[inline]
94+
pub fn wallets(mut self, wallets: Wallets) -> Self {
95+
self.wallets = Some(wallets);
96+
self
97+
}
98+
9099
/// Set the fuzzer inspector.
91100
#[inline]
92101
pub fn fuzzer(mut self, fuzzer: Fuzzer) -> Self {
@@ -161,13 +170,20 @@ impl InspectorStackBuilder {
161170
chisel_state,
162171
enable_isolation,
163172
alphanet,
173+
wallets,
164174
} = self;
165175
let mut stack = InspectorStack::new();
166176

167177
// inspectors
168178
if let Some(config) = cheatcodes {
169-
stack.set_cheatcodes(Cheatcodes::new(config));
179+
let mut cheatcodes = Cheatcodes::new(config);
180+
// Set wallets if they are provided
181+
if let Some(wallets) = wallets {
182+
cheatcodes.set_wallets(wallets);
183+
}
184+
stack.set_cheatcodes(cheatcodes);
170185
}
186+
171187
if let Some(fuzzer) = fuzzer {
172188
stack.set_fuzzer(fuzzer);
173189
}

crates/forge/src/multi_runner.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ impl MultiContractRunner {
242242
&self.config,
243243
self.evm_opts.clone(),
244244
Some(self.known_contracts.clone()),
245-
None,
246245
Some(artifact_id.name.clone()),
247246
Some(artifact_id.version.clone()),
248247
);

0 commit comments

Comments
 (0)