Is it possible as the creater of the smart contract to access a not public variable? E.g.:

contract MyContract {

    uint256 someLevel;

    function changeLevel(uint _newLevel) public {
        someLevel = _newLevel;
    }
}

How would I access in this case someLevel via web3 or at all?

All variables on Ethereum are accessible and readable by everyone in the contract storage, even if they are marked private. In the situation where you want to access the contract storage, you can do so using web3.eth.getStorageAt():

web3.eth.getStorageAt(contractAddress, 0)
.then(console.log);

If your contract only stores this one variable, it should be the first index in the storage as noted above.

That being said, in your sample, the variable is not marked private, so then it is made public by default, and there should be a getter function automatically generated for it. So you can simply call that function to read the variable.

It is very much possible to see values of even private variables stored in the smart contract.

Refer the following blog for more details -

https://hackernoon.com/your-private-solidity-variable-is-not-private-save-it-before-it-becomes-public-52a723f29f5e

  • True, though as mentioned in the other answer, this specific variable is not private. – goodvibration Sep 25 at 9:10

The other answers are right in the fact that is possible to access the storage of any contract deployed in ethereum, including private variables.

However, I do not think this answer the question. The questions if this is possible form another contract. Reading the storage of a contract from another contract is not possible (as far as I know). From the point of view of a contract, it can only do what the code in it allows to.

Hope this helps

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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