1

I understand that public state variables automatically creates a getter function when deployed, its value can be accessed via the getter function.

However, if a state variable is not public, how can we access the value? I am doing this for experiment purpose. Since everything is on the blockchain, even if a state variable is not public, we should still be able to find its value in the contract storage right?

I am running my contracts on Ganache, I saw one of the RPC for ganache is eth_getStorageAt(), maybe somehow it can be accomplished here?

eth_getStorageAt seems to return very raw information, if we can access the non-public state variable values, how do we locate it in the storage?

1

A contract with two storage variables, one public and one private.

pragma solidity 0.7.0;

contract Test {
    uint256 public a = 10;
    uint256 private b = 20;
}

Variable a is stored at slot 0 and can be accessed using

web3.eth.getStorageAt(
    "address of contract",
    0,
    (
        err,
        slot_content
    ) => console.log(err, slot_content)
)

Variable b is stored at slot 1 and can be accessed using

web3.eth.getStorageAt(
    "address of contract",
    1,
    (
        err,
        slot_content
    ) => console.log(err, slot_content)
)

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.