0

How do I find the slot of a private variable?

I know that Foundry has the method stdstorage.sig(), but that only works for variables with public getters, so private ones are out of scope.

Thanks!

2

1 Answer 1

1

Forge provides the inspect command to get specialized information about a smart contract.

Printing a contract's storage layout is achieved via:

forge inspect <Contract> storage --pretty

Note that <Contract> is the path to the contract's file plus the contract's name, eg src/Counter.sol:Counter.

The --pretty flag prints the storage layout in a more readable format. Without it, the command outputs the storage layout in json format.

5
  • How do you feed the address of an already deployed contract to the inspect command? Don't see the flag for it in the docs
    – dNyrM
    Nov 13 at 10:40
  • If the contract is verified on etherscan, you can use cast etherscan-source <address> --chain <chainId>. See here for more info. Nov 13 at 10:51
  • That worked! Thanks.
    – dNyrM
    Nov 13 at 11:02
  • 1
    also you can use services like evm.storage for deployed contracts
    – crystalbit
    Nov 13 at 11:11
  • While external services like evm.storage can be used, keep in mind that they only support a subset of EVM chains. Foundry itself is chain-independent and the above proposed solution works for every EVM chain. However, the etherscan trick also only works for EVM chains with an Etherscan instance. Nov 14 at 7:18

Your Answer

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

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