You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
// There is a storage slot named "bar" in the local contract "MyContract"
(uint256slot, ) = vm.storageLayout("MyContract", "bar");
vm.store(address(MyContract), slot, bytes32(uint256(0xff))));
It would be great if we can come up with a unified UX and abstract away the different ways of writing to specified storage slot(s) behind a nice interface
vm.getStorageSlots(address target, string variableName) (#11537, since v1.4.0) returns the slots a named variable occupies, which is the cheatcode asked for here. On master it also resolves ERC-7201 namespaced fields such as "example.main.x" (#17869). It needs the storage layout in the build output (--extra-output storageLayout); without it the cheatcode says so.
Checked with master a08c22857: vm.load(target, bytes32(vm.getStorageSlots(target, "example.main.x")[0])) reads the value the contract wrote.
Can this be closed, or is the unified write interface from the comment above still wanted?
Covered by vm.getStorageSlots(address target, string variableName) (#11537), which since #17869 also resolves ERC-7201 fields like "openzeppelin.storage.ERC20._totalSupply". It needs --extra-output storageLayout. Returning the byte offset of packed variables and the base slot of mappings is tracked in #18047. Closing.
Activity
mds1 commentedon Jan 3, 2023
Related to #3869 (comment)
It would be great if we can come up with a unified UX and abstract away the different ways of writing to specified storage slot(s) behind a nice interface
aelmanaa commentedon Nov 1, 2026
vm.getStorageSlots(address target, string variableName)(#11537, since v1.4.0) returns the slots a named variable occupies, which is the cheatcode asked for here. On master it also resolves ERC-7201 namespaced fields such as"example.main.x"(#17869). It needs the storage layout in the build output (--extra-output storageLayout); without it the cheatcode says so.Checked with master
a08c22857:vm.load(target, bytes32(vm.getStorageSlots(target, "example.main.x")[0]))reads the value the contract wrote.Can this be closed, or is the unified write interface from the comment above still wanted?
🤖 Generated with Claude Code
mablr commentedon Nov 2, 2026
Covered by
vm.getStorageSlots(address target, string variableName)(#11537), which since #17869 also resolves ERC-7201 fields like"openzeppelin.storage.ERC20._totalSupply". It needs--extra-output storageLayout. Returning the byte offset of packed variables and the base slot of mappings is tracked in #18047. Closing.