foundry: Allow forking from different RPCs/block numbers when testing
Component
Forge
Describe the feature you would like
Currently if you want to test against mainnet state, you can pass in an RPC url and block number to fork from. However it isn’t possible to run tests on multiple networks or fork from different block numbers.
For example, you might want 1 test to run against the state at block number 10000, and another at block number 20000.
Hardhat has a method called hardhat_reset which allows you to change the RPC url/block number mainnet state is forked from: https://hardhat.org/hardhat-network/guides/mainnet-forking.html#resetting-the-fork
A cheatcode similar to that would be useful. It could look like this:
hevm.reset("https://infura.io/....", 12345);
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 18 (15 by maintainers)
Support for this has landed in Anvil PR, but has not yet been incorporated into testing,
we’d need some cheatcodes first for that @onbjerg ?
FWIW this is also relevant to https://github.com/foundry-rs/foundry/issues/939. We’ll need a block number anyway, so perhaps we can use
vm.setFork(string rpcUrl, uint256 blockNumber)to solve both of theseMaybe we can add a
vm.env(string)(string)cheatcode which loads an environment variable?Need to think about how to make the cheatcode work. Seems doable. cc @onbjerg, we’d want a way to modify the forked provider’s pinned block inside a cheatcode.
I don’t think this is being worked on actively - @mattsse might be working on something similar for #1037 that can be ported to Forge as well, but I’m not sure