diff --git a/crates/cheatcodes/assets/cheatcodes.json b/crates/cheatcodes/assets/cheatcodes.json index 554cae92a890..4dbdd29b5da7 100644 --- a/crates/cheatcodes/assets/cheatcodes.json +++ b/crates/cheatcodes/assets/cheatcodes.json @@ -5453,18 +5453,18 @@ }, { "func": { - "id": "getScriptWallets", + "id": "getWallets", "description": "Returns addresses of available unlocked wallets in the script environment.", - "declaration": "function getScriptWallets() external returns (address[] memory wallets);", + "declaration": "function getWallets() external returns (address[] memory wallets);", "visibility": "external", "mutability": "", - "signature": "getScriptWallets()", - "selector": "0x7c49aa1f", + "signature": "getWallets()", + "selector": "0xdb7a4605", "selectorBytes": [ - 124, - 73, - 170, - 31 + 219, + 122, + 70, + 5 ] }, "group": "scripting", diff --git a/crates/cheatcodes/spec/src/vm.rs b/crates/cheatcodes/spec/src/vm.rs index 0fd8a62a7edc..21d41b373e01 100644 --- a/crates/cheatcodes/spec/src/vm.rs +++ b/crates/cheatcodes/spec/src/vm.rs @@ -1912,7 +1912,7 @@ interface Vm { /// Returns addresses of available unlocked wallets in the script environment. #[cheatcode(group = Scripting)] - function getScriptWallets() external returns (address[] memory wallets); + function getWallets() external returns (address[] memory wallets); // ======== Utilities ======== diff --git a/crates/cheatcodes/src/config.rs b/crates/cheatcodes/src/config.rs index c6a15f45dfd3..cfd6e9452df6 100644 --- a/crates/cheatcodes/src/config.rs +++ b/crates/cheatcodes/src/config.rs @@ -1,5 +1,5 @@ use super::Result; -use crate::{script::ScriptWallets, Vm::Rpc}; +use crate::Vm::Rpc; use alloy_primitives::{map::AddressHashMap, U256}; use foundry_common::{fs::normalize_path, ContractsByArtifact}; use foundry_compilers::{utils::canonicalize, ProjectPathsConfig}; @@ -43,8 +43,6 @@ pub struct CheatsConfig { pub evm_opts: EvmOpts, /// Address labels from config pub labels: AddressHashMap, - /// Script wallets - pub script_wallets: Option, /// Artifacts which are guaranteed to be fresh (either recompiled or cached). /// If Some, `vm.getDeployedCode` invocations are validated to be in scope of this list. /// If None, no validation is performed. @@ -65,7 +63,6 @@ impl CheatsConfig { config: &Config, evm_opts: EvmOpts, available_artifacts: Option, - script_wallets: Option, running_contract: Option, running_version: Option, ) -> Self { @@ -93,7 +90,6 @@ impl CheatsConfig { allowed_paths, evm_opts, labels: config.labels.clone(), - script_wallets, available_artifacts, running_contract, running_version, @@ -223,7 +219,6 @@ impl Default for CheatsConfig { allowed_paths: vec![], evm_opts: Default::default(), labels: Default::default(), - script_wallets: None, available_artifacts: Default::default(), running_contract: Default::default(), running_version: Default::default(), @@ -245,7 +240,6 @@ mod tests { None, None, None, - None, ) } diff --git a/crates/cheatcodes/src/crypto.rs b/crates/cheatcodes/src/crypto.rs index e5e6f56e3660..f8f24e70b565 100644 --- a/crates/cheatcodes/src/crypto.rs +++ b/crates/cheatcodes/src/crypto.rs @@ -1,6 +1,6 @@ //! Implementations of [`Crypto`](spec::Group::Crypto) Cheatcodes. -use crate::{Cheatcode, Cheatcodes, Result, ScriptWallets, Vm::*}; +use crate::{Cheatcode, Cheatcodes, Result, Vm::*, Wallets}; use alloy_primitives::{keccak256, Address, B256, U256}; use alloy_signer::{Signer, SignerSync}; use alloy_signer_local::{ @@ -17,7 +17,6 @@ use k256::{ elliptic_curve::{bigint::ArrayEncoding, sec1::ToEncodedPoint}, }; use p256::ecdsa::{signature::hazmat::PrehashSigner, Signature, SigningKey as P256SigningKey}; -use std::sync::Arc; /// The BIP32 default derivation path prefix. const DEFAULT_DERIVATION_PATH_PREFIX: &str = "m/44'/60'/0'/0/"; @@ -134,9 +133,9 @@ fn inject_wallet(state: &mut Cheatcodes, wallet: LocalSigner) -> Add script_wallets.add_local_signer(wallet); } else { // This is needed in case of testing scripts, wherein script wallets are not set on setup. - let script_wallets = ScriptWallets::new(MultiWallet::default(), None); + let script_wallets = Wallets::new(MultiWallet::default(), None); script_wallets.add_local_signer(wallet); - Arc::make_mut(&mut state.config).script_wallets = Some(script_wallets); + state.set_wallets(script_wallets); } address } diff --git a/crates/cheatcodes/src/inspector.rs b/crates/cheatcodes/src/inspector.rs index acb86681f848..a5a2a474f7ff 100644 --- a/crates/cheatcodes/src/inspector.rs +++ b/crates/cheatcodes/src/inspector.rs @@ -8,7 +8,7 @@ use crate::{ DealRecord, GasRecord, RecordAccess, }, inspector::utils::CommonCreateInput, - script::{Broadcast, ScriptWallets}, + script::{Broadcast, Wallets}, test::{ assume::AssumeNoRevert, expect::{ @@ -476,6 +476,8 @@ pub struct Cheatcodes { /// Deprecated cheatcodes mapped to the reason. Used to report warnings on test results. pub deprecated: HashMap<&'static str, Option<&'static str>>, + /// Unlocked wallets used in scripts and testing of scripts. + pub wallets: Option, } // This is not derived because calling this in `fn new` with `..Default::default()` creates a second @@ -523,12 +525,18 @@ impl Cheatcodes { ignored_traces: Default::default(), arbitrary_storage: Default::default(), deprecated: Default::default(), + wallets: Default::default(), } } /// Returns the configured script wallets. - pub fn script_wallets(&self) -> Option<&ScriptWallets> { - self.config.script_wallets.as_ref() + pub fn script_wallets(&self) -> Option<&Wallets> { + self.wallets.as_ref() + } + + /// Sets the unlocked wallets. + pub fn set_wallets(&mut self, wallets: Wallets) { + self.wallets = Some(wallets); } /// Decodes the input data and applies the cheatcode. diff --git a/crates/cheatcodes/src/lib.rs b/crates/cheatcodes/src/lib.rs index 106b4c96121d..ff93b8336236 100644 --- a/crates/cheatcodes/src/lib.rs +++ b/crates/cheatcodes/src/lib.rs @@ -45,7 +45,7 @@ mod inspector; mod json; mod script; -pub use script::{ScriptWallets, ScriptWalletsInner}; +pub use script::{Wallets, WalletsInner}; mod string; diff --git a/crates/cheatcodes/src/script.rs b/crates/cheatcodes/src/script.rs index f9535844c7c4..960d6a5508f4 100644 --- a/crates/cheatcodes/src/script.rs +++ b/crates/cheatcodes/src/script.rs @@ -61,7 +61,7 @@ impl Cheatcode for stopBroadcastCall { } } -impl Cheatcode for getScriptWalletsCall { +impl Cheatcode for getWalletsCall { fn apply_stateful(&self, ccx: &mut CheatsCtxt) -> Result { let script_wallets = ccx.state.script_wallets().cloned().map(|sw| sw.signers().unwrap_or_default()); @@ -91,29 +91,29 @@ pub struct Broadcast { /// Contains context for wallet management. #[derive(Debug)] -pub struct ScriptWalletsInner { +pub struct WalletsInner { /// All signers in scope of the script. pub multi_wallet: MultiWallet, /// Optional signer provided as `--sender` flag. pub provided_sender: Option
, } -/// Clonable wrapper around [`ScriptWalletsInner`]. +/// Clonable wrapper around [`WalletsInner`]. #[derive(Debug, Clone)] -pub struct ScriptWallets { +pub struct Wallets { /// Inner data. - pub inner: Arc>, + pub inner: Arc>, } -impl ScriptWallets { +impl Wallets { #[allow(missing_docs)] pub fn new(multi_wallet: MultiWallet, provided_sender: Option
) -> Self { - Self { inner: Arc::new(Mutex::new(ScriptWalletsInner { multi_wallet, provided_sender })) } + Self { inner: Arc::new(Mutex::new(WalletsInner { multi_wallet, provided_sender })) } } - /// Consumes [ScriptWallets] and returns [MultiWallet]. + /// Consumes [Wallets] and returns [MultiWallet]. /// - /// Panics if [ScriptWallets] is still in use. + /// Panics if [Wallets] is still in use. pub fn into_multi_wallet(self) -> MultiWallet { Arc::into_inner(self.inner) .map(|m| m.into_inner().multi_wallet) diff --git a/crates/chisel/src/executor.rs b/crates/chisel/src/executor.rs index b63ebe3e4113..1a2267719c56 100644 --- a/crates/chisel/src/executor.rs +++ b/crates/chisel/src/executor.rs @@ -335,7 +335,6 @@ impl SessionSource { self.config.evm_opts.clone(), None, None, - None, Some(self.solc.version.clone()), ) .into(), diff --git a/crates/evm/evm/src/inspectors/stack.rs b/crates/evm/evm/src/inspectors/stack.rs index 1e3b36181e0d..403e906f6323 100644 --- a/crates/evm/evm/src/inspectors/stack.rs +++ b/crates/evm/evm/src/inspectors/stack.rs @@ -3,7 +3,7 @@ use super::{ TracingInspector, }; use alloy_primitives::{map::AddressHashMap, Address, Bytes, Log, TxKind, U256}; -use foundry_cheatcodes::CheatcodesExecutor; +use foundry_cheatcodes::{CheatcodesExecutor, Wallets}; use foundry_evm_core::{backend::DatabaseExt, InspectorExt}; use foundry_evm_coverage::HitMaps; use foundry_evm_traces::{SparsedTraceArena, TraceMode}; @@ -57,6 +57,8 @@ pub struct InspectorStackBuilder { pub enable_isolation: bool, /// Whether to enable Alphanet features. pub alphanet: bool, + /// The wallets to set in the cheatcodes context. + pub wallets: Option, } impl InspectorStackBuilder { @@ -87,6 +89,13 @@ impl InspectorStackBuilder { self } + /// Set the wallets. + #[inline] + pub fn wallets(mut self, wallets: Wallets) -> Self { + self.wallets = Some(wallets); + self + } + /// Set the fuzzer inspector. #[inline] pub fn fuzzer(mut self, fuzzer: Fuzzer) -> Self { @@ -161,13 +170,20 @@ impl InspectorStackBuilder { chisel_state, enable_isolation, alphanet, + wallets, } = self; let mut stack = InspectorStack::new(); // inspectors if let Some(config) = cheatcodes { - stack.set_cheatcodes(Cheatcodes::new(config)); + let mut cheatcodes = Cheatcodes::new(config); + // Set wallets if they are provided + if let Some(wallets) = wallets { + cheatcodes.set_wallets(wallets); + } + stack.set_cheatcodes(cheatcodes); } + if let Some(fuzzer) = fuzzer { stack.set_fuzzer(fuzzer); } diff --git a/crates/forge/src/multi_runner.rs b/crates/forge/src/multi_runner.rs index 802ad3884cc6..e7361db0004f 100644 --- a/crates/forge/src/multi_runner.rs +++ b/crates/forge/src/multi_runner.rs @@ -242,7 +242,6 @@ impl MultiContractRunner { &self.config, self.evm_opts.clone(), Some(self.known_contracts.clone()), - None, Some(artifact_id.name.clone()), Some(artifact_id.version.clone()), ); diff --git a/crates/forge/tests/cli/script.rs b/crates/forge/tests/cli/script.rs index 81b7461205fe..558580728b00 100644 --- a/crates/forge/tests/cli/script.rs +++ b/crates/forge/tests/cli/script.rs @@ -2077,12 +2077,12 @@ forgetest_init!(can_get_script_wallets, |prj, cmd| { import "forge-std/Script.sol"; interface Vm { - function getScriptWallets() external returns (address[] memory wallets); + function getWallets() external returns (address[] memory wallets); } contract WalletScript is Script { function run() public { - address[] memory wallets = Vm(address(vm)).getScriptWallets(); + address[] memory wallets = Vm(address(vm)).getWallets(); console.log(wallets[0]); } }"#, diff --git a/crates/script/src/broadcast.rs b/crates/script/src/broadcast.rs index 3faaa441e24c..b0551de57dfa 100644 --- a/crates/script/src/broadcast.rs +++ b/crates/script/src/broadcast.rs @@ -17,7 +17,7 @@ use alloy_serde::WithOtherFields; use alloy_transport::Transport; use eyre::{bail, Context, Result}; use forge_verify::provider::VerificationProviderType; -use foundry_cheatcodes::ScriptWallets; +use foundry_cheatcodes::Wallets; use foundry_cli::utils::{has_batch_support, has_different_gas_calc}; use foundry_common::{ provider::{get_http_provider, try_get_http_provider, RetryProvider}, @@ -154,7 +154,7 @@ impl SendTransactionsKind { pub struct BundledState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, pub build_data: LinkedBuildData, pub sequence: ScriptSequenceKind, } diff --git a/crates/script/src/build.rs b/crates/script/src/build.rs index b0c5a2947a4d..3f5ffae81c8d 100644 --- a/crates/script/src/build.rs +++ b/crates/script/src/build.rs @@ -8,7 +8,7 @@ use crate::{ use alloy_primitives::{Bytes, B256}; use alloy_provider::Provider; use eyre::{OptionExt, Result}; -use foundry_cheatcodes::ScriptWallets; +use foundry_cheatcodes::Wallets; use foundry_common::{ compile::ProjectCompiler, provider::try_get_http_provider, ContractData, ContractsByArtifact, }; @@ -156,7 +156,7 @@ impl LinkedBuildData { pub struct PreprocessedState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, } impl PreprocessedState { @@ -242,7 +242,7 @@ impl PreprocessedState { pub struct CompiledState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, pub build_data: BuildData, } diff --git a/crates/script/src/execute.rs b/crates/script/src/execute.rs index a1b3cf6133fb..4c2e893686ce 100644 --- a/crates/script/src/execute.rs +++ b/crates/script/src/execute.rs @@ -14,7 +14,7 @@ use alloy_provider::Provider; use alloy_rpc_types::TransactionInput; use async_recursion::async_recursion; use eyre::{OptionExt, Result}; -use foundry_cheatcodes::ScriptWallets; +use foundry_cheatcodes::Wallets; use foundry_cli::utils::{ensure_clean_constructor, needs_setup}; use foundry_common::{ fmt::{format_token, format_token_raw}, @@ -41,7 +41,7 @@ use yansi::Paint; pub struct LinkedState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, pub build_data: LinkedBuildData, } @@ -92,7 +92,7 @@ impl LinkedState { pub struct PreExecutionState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, pub build_data: LinkedBuildData, pub execution_data: ExecutionData, } @@ -274,7 +274,7 @@ pub struct ExecutionArtifacts { pub struct ExecutedState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, pub build_data: LinkedBuildData, pub execution_data: ExecutionData, pub execution_result: ScriptResult, diff --git a/crates/script/src/lib.rs b/crates/script/src/lib.rs index 94c028bb9b47..de06fa0c19de 100644 --- a/crates/script/src/lib.rs +++ b/crates/script/src/lib.rs @@ -43,7 +43,7 @@ use foundry_evm::{ constants::DEFAULT_CREATE2_DEPLOYER, executors::ExecutorBuilder, inspectors::{ - cheatcodes::{BroadcastableTransactions, ScriptWallets}, + cheatcodes::{BroadcastableTransactions, Wallets}, CheatsConfig, }, opts::EvmOpts, @@ -207,7 +207,7 @@ pub struct ScriptArgs { impl ScriptArgs { pub async fn preprocess(self) -> Result { let script_wallets = - ScriptWallets::new(self.wallets.get_multi_wallet().await?, self.evm_opts.sender); + Wallets::new(self.wallets.get_multi_wallet().await?, self.evm_opts.sender); let (config, mut evm_opts) = self.load_config_and_evm_opts_emit_warnings()?; @@ -560,7 +560,7 @@ impl ScriptConfig { async fn get_runner_with_cheatcodes( &mut self, known_contracts: ContractsByArtifact, - script_wallets: ScriptWallets, + script_wallets: Wallets, debug: bool, target: ArtifactId, ) -> Result { @@ -569,7 +569,7 @@ impl ScriptConfig { async fn _get_runner( &mut self, - cheats_data: Option<(ContractsByArtifact, ScriptWallets, ArtifactId)>, + cheats_data: Option<(ContractsByArtifact, Wallets, ArtifactId)>, debug: bool, ) -> Result { trace!("preparing script runner"); @@ -611,12 +611,12 @@ impl ScriptConfig { &self.config, self.evm_opts.clone(), Some(known_contracts), - Some(script_wallets), Some(target.name), Some(target.version), ) .into(), ) + .wallets(script_wallets) .enable_isolation(self.evm_opts.isolate) }); } diff --git a/crates/script/src/simulate.rs b/crates/script/src/simulate.rs index 432a8ccd544e..2fbfbcffafed 100644 --- a/crates/script/src/simulate.rs +++ b/crates/script/src/simulate.rs @@ -16,7 +16,7 @@ use alloy_network::TransactionBuilder; use alloy_primitives::{map::HashMap, utils::format_units, Address, Bytes, TxKind, U256}; use dialoguer::Confirm; use eyre::{Context, Result}; -use foundry_cheatcodes::ScriptWallets; +use foundry_cheatcodes::Wallets; use foundry_cli::utils::{has_different_gas_calc, now}; use foundry_common::{get_contract_name, shell, ContractData}; use foundry_evm::traces::{decode_trace_arena, render_trace_arena}; @@ -36,7 +36,7 @@ use yansi::Paint; pub struct PreSimulationState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, pub build_data: LinkedBuildData, pub execution_data: ExecutionData, pub execution_result: ScriptResult, @@ -234,7 +234,7 @@ impl PreSimulationState { pub struct FilledTransactionsState { pub args: ScriptArgs, pub script_config: ScriptConfig, - pub script_wallets: ScriptWallets, + pub script_wallets: Wallets, pub build_data: LinkedBuildData, pub execution_artifacts: ExecutionArtifacts, pub transactions: VecDeque, diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index bb9b07a6595c..2d4030d30e1a 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -267,7 +267,7 @@ interface Vm { function getNonce(address account) external view returns (uint64 nonce); function getNonce(Wallet calldata wallet) external returns (uint64 nonce); function getRecordedLogs() external returns (Log[] memory logs); - function getScriptWallets() external returns (address[] memory wallets); + function getWallets() external returns (address[] memory wallets); function indexOf(string calldata input, string calldata key) external pure returns (uint256); function isContext(ForgeContext context) external view returns (bool result); function isDir(string calldata path) external returns (bool result);