How to Change Storage Values on a Fork
Forks will be deprecated on March 31, 2025.
Please migrate to Tenderly Virtual TestNets or contact our support for assistance with automatic migration.
New projects on Tenderly should use Virtual TestNets.
New projects on Tenderly should use Virtual TestNets.
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.
example.tsx
...
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.