If I deploy a smart contract that stores a variable A, but I dont create any function to retrieve that variable out. Is there a way to read that variable using web3?
I think you can read it by calling eth.getStorageAt(address, index)
, for which you will have to inspect the contractâs storage layout in order to calculate the index
you want.
Bear in mind that this can only be performed offchain, and you canât do this onchain (from another contract) as far as I know.
I am going to give it a try. Thanks!
Is this what The Graph is trying to achieve? Faster access to indexed data from the blockchain?
Yes, but Iâm not sure it can be used to read random storage slots of a contract.
Hi @ismahelio,
I assume you are referring to a private state variable.
If the state variable is public, then the compiler creates a getter for you.
https://docs.soliditylang.org/en/v0.8.1/contracts.html?highlight=public%20getter#getter-functions
Yes im referring to private variables. I havent yet tried the function matriay suggested in this post. It will be good if u know of any examples available
Hi @ismahelio,
I donât have any examples of using getStorageAt
. Feel free to share an example when you do it.
I found some examples:
Both examples work when I try to look for a uint. Since the result of web.eth.getStorageAt() is a hex string (I think). However Im not sure how to decode it if I get something else like a string for exampleâŚ
Like this:
â0x00000000000000000000000000000000000000000000000000000000000004d2â is 1234
but what is this?
â0x000000000000000000000000366cee9148511ada8f6862c0ef561055403f1da9â