Giter VIP home page Giter VIP logo

foundry-storage-locations's Introduction

Demo repository for accessing storage slots in Solidity

Reference docs:

Foundry Inspect

forge inspect StorageSlots storageLayout

Foundry Inspect - Example Output

...
  "storage": [
    {
      "astId": 40940,
      "contract": "src/StorageSlots.sol:StorageSlots",
      "label": "favoriteNumber",
      "offset": 0,
      "slot": "0",
      "type": "t_uint256"
    },
    {
      "astId": 40942,
      "contract": "src/StorageSlots.sol:StorageSlots",
      "label": "someBool",
      "offset": 0,
      "slot": "1",
      "type": "t_bool"
    },
    {
      "astId": 40945,
      "contract": "src/StorageSlots.sol:StorageSlots",
      "label": "myArray",
      "offset": 0,
      "slot": "2",
      "type": "t_array(t_uint256)dyn_storage"
    },
    {
      "astId": 40949,
      "contract": "src/StorageSlots.sol:StorageSlots",
      "label": "myMap",
      "offset": 0,
      "slot": "3",
      "type": "t_mapping(t_uint256,t_bool)"
    }
  ],
...

Cast Storage Slots

Run Anvil

anvil

In a different terminal deploy the script and use cast storage <CONTRACT_ADDRESS> <STORAGE_SLOT> to read the storage slot.

forge script script/DeployStorageSlots.s.sol --rpc-url http://127.0.0.1:8545 --private-key <ANVIL_PRIVATE_KEY_0> --broadcast

cast storage 0x5FbDB2315678afecb367f032d93F642f64180aa3 2

Run Script

forge build
forge script script/DeployStorageSlots.s.sol

Script - Example Output

== Return ==
0: contract StorageSlots 0x90193C961A926261B756D1E5bb255e67ff9498A1

== Logs ==
  Value at location 0:
  favoriteNumber = 25
  0x0000000000000000000000000000000000000000000000000000000000000019

  Value at location 1:
  someBool = true
  0x0000000000000000000000000000000000000000000000000000000000000001

  Value at location 2:
  Length of myArray = 3
  0x0000000000000000000000000000000000000000000000000000000000000003

  Value at location 3:
  Empty slot for myMap
  0x0000000000000000000000000000000000000000000000000000000000000000

  Value at location 4:
  Empty slot for NOT_IN_STORAGE as it's a constant and not in storage
  0x0000000000000000000000000000000000000000000000000000000000000000

  Value at location 5:
  Empty slot for i_not_in_storage as it's immutable and not in storage
  0x0000000000000000000000000000000000000000000000000000000000000000

  Array storage slot length:
  0x0000000000000000000000000000000000000000000000000000000000000002
  First element storage slot:
  0x405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace
  First element in array:
  0x000000000000000000000000000000000000000000000000000000000000006f

  Storage slot for myMap[0]:
  0x3617319a054d772f909f7c479a2cebe5066e836a939412e32403c99029b92eff
  Value of myMap[0]:
  0x0000000000000000000000000000000000000000000000000000000000000001

License

MIT

foundry-storage-locations's People

Contributors

eridianalpha avatar

Watchers

 avatar

Forkers

dashbaord202401

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.