foundry-rs / foundry-rs/foundry
Cheatcode for storage slot index
- Dominant language
- Rust
- Stars
- 10.6k
- Forks
- 2.6k
- Avg merge
- 16h 38m
- Merged PRs (30d)
- 511
Description
### Component
Forge
### Describe the feature you would like
There are times when it is desired to set a specific named storage slot. The solidity compiler `storageLayout` contains each named storage slot as well as the type and offset. In tests, it is desirable to set named storage slots when there is no public setter and the easiest way to do this is to look up the corresponding storage slot and hardcode the value as an input to `vm.store`. This is brittle because if the storage layout changes, then it will cause the test to fail. It would be nice to have a cheatcode along the lines of `vm.storageLayout(string contractName, string storageLabel)(uint256 slot, uint256 offset)`. This could then be used in tandem with `vm.store` to decouple the exact storage layout from the test implementation.
The following pattern can then be used:
```solidity
// There is a storage slot named "bar" in the local contract "MyContract"
(uint256 slot, ) = vm.storageLayout("MyContract", "bar");
vm.store(address(MyContract), slot, bytes32(uint256(0xff))));
```
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.