How to Change Storage Values on a Fork
If you want to simulate a transaction in a very specific state of the involved contract(s), there are two ways to do it.
- You can execute several transactions to get them in the desired state.
- Alternatively, you can plant a specific value into one or more storage slots of your smart contract(s).
If you decide to go with the second option, you need to use
tenderly_setStorageAt JSON-RPC call.
This is a custom JSON-RPC call that is applicable only for Tenderly Forks.
Here’s an example. You can play around with it using this Tenderly Sandbox.
... const forkRpcUrl = ...; const provider = new ethers.providers.JsonRpcProvider(forkRpcUrl); const two32BHexStr = ethers.utils.hexZeroPad(ethers.utils.hexValue(2), 32); const fiftyFive32BHexStr = ethers.utils.hexZeroPad(ethers.utils.hexValue(55), 32); await provider.send("tenderly_setStorageAt", [ // the contract address contractAddress, two32BHexStr, fiftyFive32BHexStr, ]); const newValue = await fork.provider.getStorageAt(greeter.address, 2);
The first parameter is the contract address, the second is the storage slot, and the third is the value we want to set. The last two parameters have to be a string representation of a 32-byte number.