0

How can I retrieve the storage value with solidity? I'm looking for something equivalent to web3.eth.getStorageAt in web3js.

contract WhatEver {
    uint public prize; <-- I want to read this value from another contract
}

EDIT

The target contract is not owned by me, I'm trying to hack it (one of the Ethernaut levels)

Does anyone know?

2

1 Answer 1

1

You don't need to get into the mess of accessing raw storage to answer your question. Since the uint you're trying to access is public, that means there is an externally-facing function called prize which returns it, so all your contract would need to do is something like:

interface WhatEver {
  function prize() external view returns(uint);
}

contract YourContract {
  function getPrize(address whateverAddress) public view returns(uint) {
    return WhatEver(whateverAddress).prize()
  }
}

That being said, this helps you do what you're trying to do, but doesn't answer the question of how to replicate getStorageAt in Solidity, so I'd call this only a partial answer.

I do think that you should be able to replicate getStorageAt using Yul and maybe a delegatecall so that you can SLOAD off of the target contract and then read it or return it from memory, but do not know how to do so. (A regular call or even staticcall may also be sufficient.) This seems to have some of the steps lined up, and makes me believe that the answer may be contingent on the datatype being returned, but unfortunately does not have any answers as of this writing. If you are interested in pursuing this path, this page in the Solidity docs explains how storage is used, and this page has the details for Yul.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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