6

It's easy enough to grab the data at any storage location of a contract, but we need to know the state variable declarations/storage layout of the contract to know what we're doing and how to interpret the data.

How can I programmatically find out the state variable declarations/storage layout for a contract?

I'd like to make a storage explorer type tool and being able to determine the storage layout of a contract is a game changer.

5
  • Well, any contact published on the network should have it's code public, and from there (its code) you should be able to understand it's storage layout. Right?
    – Sky
    Commented Jul 16, 2022 at 21:40
  • that is a manual process, not determining the storage layout programmatically. A tool would be even more useful if it could figure out the storage layout for a contract didn't have the contacts source code available
    – sola24
    Commented Jul 17, 2022 at 8:12
  • Hmm, it should be possible (after all , the code published on Blockchain is publicly available). But I had heard of no tool that can do that. Good question.
    – Sky
    Commented Jul 17, 2022 at 8:47
  • It does not satisfy all the requirements (needs the contract source), but it may be helpful nonetheless: github.com/tintinweb/smart-contract-inspector
    – shamisen
    Commented Jul 20, 2022 at 22:30
  • You could also pretty easily write a script to read all the transactions made to a contract and play all the storage events. Commented Jul 24, 2022 at 23:13

1 Answer 1

4

If a contract is written in Solidity entirely, then it should follow the storage layout rules. We can implement the logic ourselves, or use the output from solc:

% solc --storage-layout /tmp/A.sol | tail -1 | jq .
{
  "storage": [
    {
      "astId": 4,
      "contract": "/tmp/A.sol:A",
      "label": "x",
      "offset": 0,
      "slot": "0",
      "type": "t_uint64"
    },
    {
      "astId": 7,
      "contract": "/tmp/A.sol:A",
      "label": "y",
      "offset": 8,
      "slot": "0",
      "type": "t_uint128"
    },

A decompiler might be helpful if the source is not available.

If the author uses assembly / inline assembly, then the contract may not follow any layout rules. For example,

PUSH1 42
TIMESTAMP
SSTORE

this code will write 42 to the storage slot pointed by the timestamp.

1
  • I tried to run this command and it gives me an error unrecognised option '--storage-layout' . I ran it without the tail command as a I am on windows. Is there any other way you can suggest?? for windows Commented May 17, 2023 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.