Skip to content

Commit eff7128

Browse files
yash-atreyarplusq
authored andcommitted
chore(cheatcodes): wallet nits (foundry-rs#9118)
1 parent d82f029 commit eff7128

File tree

3 files changed

+40
-46
lines changed

3 files changed

+40
-46
lines changed

crates/cheatcodes/src/crypto.rs

Lines changed: 8 additions & 20 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, Vm::*, Wallets};
3+
use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
44
use alloy_primitives::{keccak256, Address, B256, U256};
55
use alloy_signer::{Signer, SignerSync};
66
use alloy_signer_local::{
@@ -11,7 +11,6 @@ use alloy_signer_local::{
1111
LocalSigner, MnemonicBuilder, PrivateKeySigner,
1212
};
1313
use alloy_sol_types::SolValue;
14-
use foundry_wallets::multi_wallet::MultiWallet;
1514
use k256::{
1615
ecdsa::SigningKey,
1716
elliptic_curve::{bigint::ArrayEncoding, sec1::ToEncodedPoint},
@@ -98,11 +97,7 @@ impl Cheatcode for rememberKeyCall {
9897
impl Cheatcode for rememberKeys_0Call {
9998
fn apply(&self, state: &mut Cheatcodes) -> Result {
10099
let Self { mnemonic, derivationPath, count } = self;
101-
tracing::info!("Remembering {} keys", count);
102100
let wallets = derive_wallets::<English>(mnemonic, derivationPath, *count)?;
103-
104-
tracing::info!("Adding {} keys to script wallets", count);
105-
106101
let mut addresses = Vec::<Address>::with_capacity(wallets.len());
107102
for wallet in wallets {
108103
let addr = inject_wallet(state, wallet);
@@ -129,14 +124,7 @@ impl Cheatcode for rememberKeys_1Call {
129124

130125
fn inject_wallet(state: &mut Cheatcodes, wallet: LocalSigner<SigningKey>) -> Address {
131126
let address = wallet.address();
132-
if let Some(script_wallets) = state.script_wallets() {
133-
script_wallets.add_local_signer(wallet);
134-
} else {
135-
// This is needed in case of testing scripts, wherein script wallets are not set on setup.
136-
let script_wallets = Wallets::new(MultiWallet::default(), None);
137-
script_wallets.add_local_signer(wallet);
138-
state.set_wallets(script_wallets);
139-
}
127+
state.wallets().add_local_signer(wallet);
140128
address
141129
}
142130

@@ -256,13 +244,13 @@ fn sign_with_wallet(
256244
signer: Option<Address>,
257245
digest: &B256,
258246
) -> Result<alloy_primitives::Signature> {
259-
let Some(script_wallets) = state.script_wallets() else {
260-
bail!("no wallets are available");
261-
};
247+
if state.wallets().is_empty() {
248+
bail!("no wallets available");
249+
}
262250

263-
let mut script_wallets = script_wallets.inner.lock();
264-
let maybe_provided_sender = script_wallets.provided_sender;
265-
let signers = script_wallets.multi_wallet.signers()?;
251+
let mut wallets = state.wallets().inner.lock();
252+
let maybe_provided_sender = wallets.provided_sender;
253+
let signers = wallets.multi_wallet.signers()?;
266254

267255
let signer = if let Some(signer) = signer {
268256
signer

crates/cheatcodes/src/inspector.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use foundry_evm_core::{
3737
InspectorExt,
3838
};
3939
use foundry_evm_traces::{TracingInspector, TracingInspectorConfig};
40+
use foundry_wallets::multi_wallet::MultiWallet;
4041
use itertools::Itertools;
4142
use proptest::test_runner::{RngAlgorithm, TestRng, TestRunner};
4243
use rand::Rng;
@@ -529,9 +530,9 @@ impl Cheatcodes {
529530
}
530531
}
531532

532-
/// Returns the configured script wallets.
533-
pub fn script_wallets(&self) -> Option<&Wallets> {
534-
self.wallets.as_ref()
533+
/// Returns the configured wallets if available, else creates a new instance.
534+
pub fn wallets(&mut self) -> &Wallets {
535+
self.wallets.get_or_insert(Wallets::new(MultiWallet::default(), None))
535536
}
536537

537538
/// Sets the unlocked wallets.

crates/cheatcodes/src/script.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,8 @@ impl Cheatcode for stopBroadcastCall {
6363

6464
impl Cheatcode for getWalletsCall {
6565
fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result {
66-
let script_wallets =
67-
ccx.state.script_wallets().cloned().map(|sw| sw.signers().unwrap_or_default());
68-
69-
if let Some(script_wallets) = script_wallets {
70-
let script_wallets: Vec<Address> = script_wallets.into_iter().collect();
71-
Ok(script_wallets.abi_encode())
72-
} else {
73-
Ok(Default::default())
74-
}
66+
let wallets = ccx.state.wallets().signers().unwrap_or_default();
67+
Ok(wallets.abi_encode())
7568
}
7669
}
7770

@@ -135,6 +128,21 @@ impl Wallets {
135128
pub fn signers(&self) -> Result<Vec<Address>> {
136129
Ok(self.inner.lock().multi_wallet.signers()?.keys().cloned().collect())
137130
}
131+
132+
/// Number of signers in the [MultiWallet].
133+
pub fn len(&self) -> usize {
134+
let mut inner = self.inner.lock();
135+
let signers = inner.multi_wallet.signers();
136+
if signers.is_err() {
137+
return 0;
138+
}
139+
signers.unwrap().len()
140+
}
141+
142+
/// Whether the [MultiWallet] is empty.
143+
pub fn is_empty(&self) -> bool {
144+
self.len() == 0
145+
}
138146
}
139147

140148
/// Sets up broadcasting from a script using `new_origin` as the sender.
@@ -148,16 +156,14 @@ fn broadcast(ccx: &mut CheatsCtxt, new_origin: Option<&Address>, single_call: bo
148156
let mut new_origin = new_origin.cloned();
149157

150158
if new_origin.is_none() {
151-
if let Some(script_wallets) = ccx.state.script_wallets() {
152-
let mut script_wallets = script_wallets.inner.lock();
153-
if let Some(provided_sender) = script_wallets.provided_sender {
154-
new_origin = Some(provided_sender);
155-
} else {
156-
let signers = script_wallets.multi_wallet.signers()?;
157-
if signers.len() == 1 {
158-
let address = signers.keys().next().unwrap();
159-
new_origin = Some(*address);
160-
}
159+
let mut wallets = ccx.state.wallets().inner.lock();
160+
if let Some(provided_sender) = wallets.provided_sender {
161+
new_origin = Some(provided_sender);
162+
} else {
163+
let signers = wallets.multi_wallet.signers()?;
164+
if signers.len() == 1 {
165+
let address = signers.keys().next().unwrap();
166+
new_origin = Some(*address);
161167
}
162168
}
163169
}
@@ -175,17 +181,16 @@ fn broadcast(ccx: &mut CheatsCtxt, new_origin: Option<&Address>, single_call: bo
175181
}
176182

177183
/// Sets up broadcasting from a script with the sender derived from `private_key`.
178-
/// Adds this private key to `state`'s `script_wallets` vector to later be used for signing
184+
/// Adds this private key to `state`'s `wallets` vector to later be used for signing
179185
/// if broadcast is successful.
180186
fn broadcast_key(ccx: &mut CheatsCtxt, private_key: &U256, single_call: bool) -> Result {
181187
let wallet = super::crypto::parse_wallet(private_key)?;
182188
let new_origin = wallet.address();
183189

184190
let result = broadcast(ccx, Some(&new_origin), single_call);
185191
if result.is_ok() {
186-
if let Some(script_wallets) = ccx.state.script_wallets() {
187-
script_wallets.add_local_signer(wallet);
188-
}
192+
let wallets = ccx.state.wallets();
193+
wallets.add_local_signer(wallet);
189194
}
190195
result
191196
}

0 commit comments

Comments
 (0)