Skip to content

Commit 7048104

Browse files
committed
fix: fmt_error + insert_vm
1 parent e87217c commit 7048104

File tree

2 files changed

+98
-119
lines changed

2 files changed

+98
-119
lines changed

crates/cheatcodes/src/fork.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,13 @@ fn get_toml_value<'a>(
216216
key: &'a str,
217217
state: &'a crate::Cheatcodes,
218218
) -> Result<&'a toml::Value> {
219-
let config = state
220-
.config
221-
.forks
222-
.get(name)
223-
.ok_or_eyre("[fork.{name}] subsection not found in [fork] of 'foundry.toml'")?;
219+
let config = state.config.forks.get(name).ok_or_else(|| {
220+
fmt_err!("[fork.{name}] subsection not found in [fork] of 'foundry.toml'")
221+
})?;
224222
let value = config
225223
.vars
226224
.get(key)
227-
.ok_or_eyre("Variable '{key}' not found in [fork.{name}] configuration")?;
225+
.ok_or_else(|| fmt_err!("Variable '{key}' not found in [fork.{name}] configuration"))?;
228226

229227
Ok(value)
230228
}

crates/forge/tests/cli/script.rs

Lines changed: 94 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,6 +3235,10 @@ Error: script failed: call to non-contract address [..]
32353235

32363236
// Tests that can access the fork config for each chain from `foundry.toml`
32373237
forgetest_init!(can_access_fork_config_chain_ids, |prj, cmd| {
3238+
prj.insert_vm();
3239+
prj.insert_console();
3240+
prj.insert_ds_test();
3241+
32383242
prj.update_config(|config| {
32393243
config.forks = vec![
32403244
(
@@ -3291,67 +3295,54 @@ forgetest_init!(can_access_fork_config_chain_ids, |prj, cmd| {
32913295
)]);
32923296
});
32933297

3294-
prj.add_script(
3298+
prj.add_source(
32953299
"ForkScript.s.sol",
32963300
r#"
3297-
import {console} from "forge-std/Script.sol";
3298-
3299-
interface Vm {
3300-
function forkChains() external view returns (string[] memory);
3301-
function forkChainIds() external view returns (uint256[] memory);
3302-
function forkChainId(uint256 chain) external view returns (uint256);
3303-
function forkChainRpcUrl(uint256 chain) external view returns (string memory);
3304-
function forkChainInt(uint256 chain, string memory key) external view returns (int256);
3305-
function forkChainUint(uint256 chain, string memory key) external view returns (uint256);
3306-
function forkChainBool(uint256 chain, string memory key) external view returns (bool);
3307-
function forkChainAddress(uint256 chain, string memory key) external view returns (address);
3308-
function forkChainBytes32(uint256 chain, string memory key) external view returns (bytes32);
3309-
function forkChainString(uint256 chain, string memory key) external view returns (string memory);
3310-
function forkChainBytes(uint256 chain, string memory key) external view returns (bytes memory);
3311-
}
3301+
import {Vm} from "./Vm.sol";
3302+
import {DSTest} from "./test.sol";
3303+
import {console} from "./Console.sol";
3304+
3305+
contract ForkScript is DSTest {
3306+
Vm vm = Vm(HEVM_ADDRESS);
3307+
3308+
function run() public view {
3309+
(uint256[2] memory chainIds, string[2] memory chains) = ([uint256(1), uint256(10)], ["mainnet", "optimism"]);
3310+
(uint256[] memory cheatChainIds, string[] memory cheatChains) = (vm.forkChainIds(), vm.forkChains());
3311+
3312+
for (uint256 i = 0; i < chains.length; i++) {
3313+
assert(chainIds[i] == cheatChainIds[0] || chainIds[i] == cheatChainIds[1]);
3314+
assert(eqString(chains[i], cheatChains[0]) || eqString(chains[i], cheatChains[1]));
3315+
console.log("chain:", chains[i]);
3316+
console.log("id:", chainIds[i]);
3317+
3318+
string memory rpc = vm.forkChainRpcUrl(chainIds[i]);
3319+
int256 i256 = vm.forkChainInt(chainIds[i], "i256");
3320+
uint256 u256 = vm.forkChainUint(chainIds[i], "u256");
3321+
bool boolean = vm.forkChainBool(chainIds[i], "bool");
3322+
address addr = vm.forkChainAddress(chainIds[i], "addr");
3323+
bytes32 b256 = vm.forkChainBytes32(chainIds[i], "b256");
3324+
bytes memory byytes = vm.forkChainBytes(chainIds[i], "bytes");
3325+
string memory str = vm.forkChainString(chainIds[i], "str");
3326+
3327+
console.log(" > rpc:", rpc);
3328+
console.log(" > vars:");
3329+
console.log(" > i256:", i256);
3330+
console.log(" > u256:", u256);
3331+
console.log(" > bool:", boolean);
3332+
console.log(" > addr:", addr);
3333+
console.log(" > string:", str);
33123334
3313-
contract ForkScript {
3314-
address internal constant HEVM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code"))));
3315-
Vm constant vm = Vm(HEVM_ADDRESS);
3316-
3317-
function run() public view {
3318-
(uint256[2] memory chainIds, string[2] memory chains) = ([uint256(1), uint256(10)], ["mainnet", "optimism"]);
3319-
(uint256[] memory cheatChainIds, string[] memory cheatChains) = (vm.forkChainIds(), vm.forkChains());
3320-
3321-
for (uint256 i = 0; i < chains.length; i++) {
3322-
assert(chainIds[i] == cheatChainIds[0] || chainIds[i] == cheatChainIds[1]);
3323-
assert(eqString(chains[i], cheatChains[0]) || eqString(chains[i], cheatChains[1]));
3324-
console.log("chain:", chains[i]);
3325-
console.log("id:", chainIds[i]);
3326-
3327-
string memory rpc = vm.forkChainRpcUrl(chainIds[i]);
3328-
int256 i256 = vm.forkChainInt(chainIds[i], "i256");
3329-
uint256 u256 = vm.forkChainUint(chainIds[i], "u256");
3330-
bool boolean = vm.forkChainBool(chainIds[i], "bool");
3331-
address addr = vm.forkChainAddress(chainIds[i], "addr");
3332-
bytes32 b256 = vm.forkChainBytes32(chainIds[i], "b256");
3333-
bytes memory byytes = vm.forkChainBytes(chainIds[i], "bytes");
3334-
string memory str = vm.forkChainString(chainIds[i], "str");
3335-
3336-
console.log(" > rpc:", rpc);
3337-
console.log(" > vars:");
3338-
console.log(" > i256:", i256);
3339-
console.log(" > u256:", u256);
3340-
console.log(" > bool:", boolean);
3341-
console.log(" > addr:", addr);
3342-
console.log(" > string:", str);
3343-
3344-
assert(
3345-
b256 == 0xdeadbeaf00000000000000000000000000000000000000000000000000000000
3346-
|| b256 == 0x000000000000000000000000000000000000000000000000000000deadc0ffee
3347-
);
3348-
}
3349-
}
3350-
3351-
function eqString(string memory s1, string memory s2) public pure returns(bool) {
3352-
return keccak256(bytes(s1)) == keccak256(bytes(s2));
3353-
}
3335+
assert(
3336+
b256 == 0xdeadbeaf00000000000000000000000000000000000000000000000000000000
3337+
|| b256 == 0x000000000000000000000000000000000000000000000000000000deadc0ffee
3338+
);
33543339
}
3340+
}
3341+
3342+
function eqString(string memory s1, string memory s2) public pure returns(bool) {
3343+
return keccak256(bytes(s1)) == keccak256(bytes(s2));
3344+
}
3345+
}
33553346
"#,
33563347
)
33573348
.unwrap();
@@ -3382,6 +3373,9 @@ forgetest_init!(can_access_fork_config_chain_ids, |prj, cmd| {
33823373

33833374
// Tests that can derive chain id of the active fork + get the config from `foundry.toml`
33843375
forgetest_init!(can_derive_chain_id_access_fork_config, |prj, cmd| {
3376+
prj.insert_vm();
3377+
prj.insert_console();
3378+
prj.insert_ds_test();
33853379
let mainnet_endpoint = rpc::next_http_rpc_endpoint();
33863380

33873381
prj.update_config(|config| {
@@ -3440,69 +3434,56 @@ forgetest_init!(can_derive_chain_id_access_fork_config, |prj, cmd| {
34403434
)]);
34413435
});
34423436

3443-
prj.add_test(
3444-
"ForkTest.t.sol",
3445-
&r#"
3446-
import {console} from "forge-std/Test.sol";
3447-
3448-
interface Vm {
3449-
function createSelectFork(string memory) external view;
3450-
function forkChain() external view returns (string memory);
3451-
function forkChainId() external view returns (uint256);
3452-
function forkRpcUrl() external view returns (string memory);
3453-
function forkInt(string memory key) external view returns (int256);
3454-
function forkUint(string memory key) external view returns (uint256);
3455-
function forkBool(string memory key) external view returns (bool);
3456-
function forkAddress(string memory key) external view returns (address);
3457-
function forkBytes32(string memory key) external view returns (bytes32);
3458-
function forkString(string memory key) external view returns (string memory);
3459-
function forkBytes(string memory key) external view returns (bytes memory);
3460-
}
3461-
3462-
contract ForkTest {
3463-
address internal constant HEVM_ADDRESS = address(uint160(uint256(keccak256("hevm cheat code"))));
3464-
Vm constant vm = Vm(HEVM_ADDRESS);
3465-
3466-
function test_panicsWhithoutSelectedFork() public {
3467-
vm.forkChain();
3468-
}
3469-
3470-
function test_forkVars() public {
3471-
vm.createSelectFork("<url>");
3437+
prj.add_source(
3438+
"ForkTest.t.sol",
3439+
&r#"
3440+
import {Vm} from "./Vm.sol";
3441+
import {DSTest} from "./test.sol";
3442+
import {console} from "./Console.sol";
34723443
3473-
console.log("chain:", vm.forkChain());
3474-
console.log("id:", vm.forkChainId());
3475-
assert(eqString(vm.forkRpcUrl(), "<url>"));
3444+
contract ForkTest is DSTest {
3445+
Vm vm = Vm(HEVM_ADDRESS);
34763446
3477-
int256 i256 = vm.forkInt("i256");
3478-
uint256 u256 = vm.forkUint("u256");
3479-
bool boolean = vm.forkBool("bool");
3480-
address addr = vm.forkAddress("addr");
3481-
bytes32 b256 = vm.forkBytes32("b256");
3482-
bytes memory byytes = vm.forkBytes("bytes");
3483-
string memory str = vm.forkString("str");
3447+
function test_panicsWhithoutSelectedFork() public {
3448+
vm.forkChain();
3449+
}
34843450
3485-
console.log(" > vars:");
3486-
console.log(" > i256:", i256);
3487-
console.log(" > u256:", u256);
3488-
console.log(" > bool:", boolean);
3489-
console.log(" > addr:", addr);
3490-
console.log(" > string:", str);
3451+
function test_forkVars() public {
3452+
vm.createSelectFork("<url>");
34913453
3492-
assert(
3493-
b256 == 0xdeadbeaf00000000000000000000000000000000000000000000000000000000
3494-
|| b256 == 0x000000000000000000000000000000000000000000000000000000deadc0ffee
3495-
);
3496-
}
3454+
console.log("chain:", vm.forkChain());
3455+
console.log("id:", vm.forkChainId());
3456+
assert(eqString(vm.forkRpcUrl(), "<url>"));
3457+
3458+
int256 i256 = vm.forkInt("i256");
3459+
uint256 u256 = vm.forkUint("u256");
3460+
bool boolean = vm.forkBool("bool");
3461+
address addr = vm.forkAddress("addr");
3462+
bytes32 b256 = vm.forkBytes32("b256");
3463+
bytes memory byytes = vm.forkBytes("bytes");
3464+
string memory str = vm.forkString("str");
3465+
3466+
console.log(" > vars:");
3467+
console.log(" > i256:", i256);
3468+
console.log(" > u256:", u256);
3469+
console.log(" > bool:", boolean);
3470+
console.log(" > addr:", addr);
3471+
console.log(" > string:", str);
3472+
3473+
assert(
3474+
b256 == 0xdeadbeaf00000000000000000000000000000000000000000000000000000000
3475+
|| b256 == 0x000000000000000000000000000000000000000000000000000000deadc0ffee
3476+
);
3477+
}
34973478
3498-
function eqString(string memory s1, string memory s2) public pure returns(bool) {
3499-
return keccak256(bytes(s1)) == keccak256(bytes(s2));
3500-
}
3479+
function eqString(string memory s1, string memory s2) public pure returns(bool) {
3480+
return keccak256(bytes(s1)) == keccak256(bytes(s2));
35013481
}
3482+
}
35023483
"#
3503-
.replace("<url>", &mainnet_endpoint),
3504-
)
3505-
.unwrap();
3484+
.replace("<url>", &mainnet_endpoint),
3485+
)
3486+
.unwrap();
35063487

35073488
cmd.args(["test", "-vvv", "ForkTest"]).assert_failure().stdout_eq(str![[r#"
35083489
...

0 commit comments

Comments
 (0)