Skip to content

Conversation

yash-atreya
Copy link
Contributor

@yash-atreya yash-atreya commented Sep 3, 2025

Motivation

Closes #3869

Solution

  • New cheatcode: vm.getStorageSlots(address target, string variableName) returns (uint256[] slots)
  • Leverages the storage layout to pinpoint the base slot of the variable and fetches rest of the slots according to the endoing type
  • Test

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@yash-atreya yash-atreya marked this pull request as ready for review September 3, 2025 14:00
@yash-atreya yash-atreya added A-cheatcodes Area: cheatcodes C-forge Command: forge labels Sep 3, 2025
@yash-atreya
Copy link
Contributor Author

Are we planning to deprecate StdStorage is forge-std? The cheatcode seems preferable.

This approach leverages the StorageLayout, whereas the StdStorage approach doesn't and works well for simple variables but NOT for variables that span multiple slots.

I'm okay with deprecating StdStorage but users would have to compile with --extra-output storageLayout for this to work

@zerosnacks
Copy link
Member

I see, deprecating StdStorage likely causes issues in existing scripting suites and forcing a rewrite for something that is working for a user is not preferable. We do need to make sure we document this new cheatcode in the book to help guide new users towards it.

zerosnacks
zerosnacks previously approved these changes Sep 4, 2025
grandizzy
grandizzy previously approved these changes Sep 8, 2025
Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Base automatically changed from yash/decode-bytes-strings-state-diffs to master September 8, 2025 11:12
@yash-atreya yash-atreya dismissed stale reviews from grandizzy and zerosnacks September 8, 2025 11:12

The base branch was changed.

@yash-atreya yash-atreya enabled auto-merge (squash) September 8, 2025 12:16
@grandizzy grandizzy self-requested a review September 8, 2025 12:17
@yash-atreya yash-atreya merged commit 5dc5891 into master Sep 8, 2025
23 checks passed
@yash-atreya yash-atreya deleted the yash/get-storage-slots branch September 8, 2025 12:17
@github-project-automation github-project-automation bot moved this to Done in Foundry Sep 8, 2025
@ZeroEkkusu
Copy link
Contributor

Would be nice to include storage layout flag in foundry.toml.

MerkleBoy pushed a commit to MerkleBoy/foundry that referenced this pull request Sep 17, 2025
* feat(common): identify bytes and string slots using storageLayout

* fmt

* clippy

* aggregate and decode bytes and strings

* cleanup

* nits

* clippy

* fmt

* nit

* cleanup

* identify the slots using the length of data in base slot value

* nit

* fix

* fix mapping identification

* feat(cheatcodes): vm.getStorageSlots

* fix tests

* nit

* fmt

* nit

* nit

* fmt

* Update crates/common/src/slot_identifier.rs

* Update crates/common/src/slot_identifier.rs

* add mapping in test

* fmt

* error mapping and dyn arrays

* nit

* nit

---------

Co-authored-by: zerosnacks <95942363+zerosnacks@users.noreply.github.com>
zerosnacks pushed a commit to foundry-rs/forge-std that referenced this pull request Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cheatcodes Area: cheatcodes C-forge Command: forge

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Cannot find the storage slot for a public string variable

4 participants