Read data from blockchain with no public function

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? :thinking:

2 Likes

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.

1 Like

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?

1 Like

Yes, but I’m not sure it can be used to read random storage slots of a contract.

1 Like

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 :ok_hand:t2:

1 Like

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:

stack overflow

Infura. Look for getStorageAt

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’

1 Like

Hi @ismahelio,

You may want to share the contract that you are trying to get the value from.