Skip to content

Cheatcode for storage slot index #4022

Closed
Closed

Description

@tynes

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:

// 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

Activity

  1. mds1 commented on Jan 3, 2023

    @mds1
    Collaborator

    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

  2. added this to the v1.0.0 milestone on Jul 26, 2024
  3. removed this from the v1.0.0 milestone on Oct 15, 2024
  4. added theissue type on Dec 17, 2025
  5. aelmanaa commented on Nov 1, 2026

    @aelmanaa
    Contributor

    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

  6. mablr commented on Nov 2, 2026

    @mablr
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    • Status
      Next Up

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions