Skip to content

Commit 373ad46

Browse files
feat(cheatcodes): vm.getScriptWallets() (#9052)
* feat(`cheatcodes`): vm.getScriptWallets() * feat: load default anvil accounts in script * Revert "feat: load default anvil accounts in script" This reverts commit 4d64356a51bf226482269a2af47f947c4e49e462. * clippy * test --------- Co-authored-by: grandizzy <[email protected]> --------- Co-authored-by: grandizzy <[email protected]>
1 parent a17869a commit 373ad46

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 20 additions & 0 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,10 @@ interface Vm {
18771877
#[cheatcode(group = Scripting)]
18781878
function broadcastRawTransaction(bytes calldata data) external;
18791879

1880+
/// Returns addresses of available unlocked wallets in the script environment.
1881+
#[cheatcode(group = Scripting)]
1882+
function getScriptWallets() external returns (address[] memory wallets);
1883+
18801884
// ======== Utilities ========
18811885

18821886
// -------- Strings --------

crates/cheatcodes/src/script.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::{Cheatcode, CheatsCtxt, Result, Vm::*};
44
use alloy_primitives::{Address, B256, U256};
55
use alloy_signer_local::PrivateKeySigner;
6+
use alloy_sol_types::SolValue;
67
use foundry_wallets::{multi_wallet::MultiWallet, WalletSigner};
78
use parking_lot::Mutex;
89
use std::sync::Arc;
@@ -60,6 +61,20 @@ impl Cheatcode for stopBroadcastCall {
6061
}
6162
}
6263

64+
impl Cheatcode for getScriptWalletsCall {
65+
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+
}
75+
}
76+
}
77+
6378
#[derive(Clone, Debug, Default)]
6479
pub struct Broadcast {
6580
/// Address of the transaction origin

crates/forge/tests/cli/script.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,3 +2055,43 @@ ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
20552055
20562056
"#]]);
20572057
});
2058+
2059+
forgetest_init!(can_get_script_wallets, |prj, cmd| {
2060+
let script = prj
2061+
.add_source(
2062+
"Foo",
2063+
r#"
2064+
import "forge-std/Script.sol";
2065+
2066+
interface Vm {
2067+
function getScriptWallets() external returns (address[] memory wallets);
2068+
}
2069+
2070+
contract WalletScript is Script {
2071+
function run() public {
2072+
address[] memory wallets = Vm(address(vm)).getScriptWallets();
2073+
console.log(wallets[0]);
2074+
}
2075+
}"#,
2076+
)
2077+
.unwrap();
2078+
cmd.arg("script")
2079+
.arg(script)
2080+
.args([
2081+
"--private-key",
2082+
"0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6",
2083+
"-v",
2084+
])
2085+
.assert_success()
2086+
.stdout_eq(str![[r#"
2087+
[COMPILING_FILES] with [SOLC_VERSION]
2088+
[SOLC_VERSION] [ELAPSED]
2089+
Compiler run successful!
2090+
Script ran successfully.
2091+
[GAS]
2092+
2093+
== Logs ==
2094+
0xa0Ee7A142d267C1f36714E4a8F75612F20a79720
2095+
2096+
"#]]);
2097+
});

testdata/cheats/Vm.sol

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

0 commit comments

Comments
 (0)