Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: activeFork cheatcode implementation #220

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crates/era-cheatcodes/src/cheatcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,23 @@ impl CheatcodeTracer {
use Vm::{VmCalls::*, *};

match call {
activeFork(activeForkCall {}) => {
tracing::info!("πŸ‘· Getting active fork");
let handle: &ForkStorage<RevmDatabaseForEra<S>> =
&storage.borrow_mut().storage_handle;
let fork_storage = handle.inner.read().unwrap();
let fork_id = fork_storage
.fork
.as_ref()
.unwrap()
.fork_source
.db
.lock()
.unwrap()
.active_fork_id();
assert!(fork_id.is_some(), "No active fork found. Please create a fork first.");
self.return_data = Some(vec![fork_id.unwrap().to_u256()]);
}
addr(addrCall { privateKey: private_key }) => {
tracing::info!("πŸ‘· Getting address for private key");
let Ok(address) = zksync_types::PackedEthSignature::address_from_private_key(
Expand Down
26 changes: 25 additions & 1 deletion crates/era-cheatcodes/tests/src/cheatcodes/Fork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract ForkTest is Test {
console.log("decimals_after", decimals_after);
require(
decimals_after == TOKEN_DECIMALS,
"Contract dosent exists in fork"
"Contract doesn't exists in fork"
);
require(
block.number == FORK_BLOCK + 1,
Expand Down Expand Up @@ -83,4 +83,28 @@ contract ForkTest is Test {
"ENV for blocks is not set correctly"
);
}

function testActiveFork() public {
(bool success, bytes memory data) = Constants.CHEATCODE_ADDRESS.call(
abi.encodeWithSignature(
"createFork(string,uint256)",
"mainnet",
FORK_BLOCK + 100
)
);
require(success, "fork failed");

uint256 forkId = uint256(bytes32(data));
(bool success1, ) = Constants.CHEATCODE_ADDRESS.call(
abi.encodeWithSignature("selectFork(uint256)", forkId)
);
require(success1, "select fork failed");

(bool success3, bytes memory activeFork) = Constants
.CHEATCODE_ADDRESS
.call(abi.encodeWithSignature("activeFork()"));

console.log("activeFork", uint256(bytes32(activeFork)));
require(success3, "active fork failed");
}
}
Loading