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.
I assume you are referring to a private state variable.
If the state variable is public, then the compiler creates a getter for you.
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
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…
“0x00000000000000000000000000000000000000000000000000000000000004d2” is 1234
but what is this?
You may want to share the contract that you are trying to get the value from.