Can one view (externally) the contents of a private variable defined in a contract? Or, the only way one can view (externally) is by declaring the variable public or having an external function returning its value?
Asking because I know that even if a variable is declared private it still resides on the public blockchain so wanted to confirm if there are any other ways to read the variable like via eth.getStorageAt() API or something.
If the answer to this question is 'Yes' then please also tell me how can I do it.
I have seen elsewhere on this site people mentioning it's possible but nowhere it's described how. It's also mentioned that it can be seen outside the contract in a blockchain explorer like etherscan.io etc. but I haven't been successful so far.
For example, I have the below solidity contract:
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.4;
contract Test {
uint private balance;
string public name;
constructor() payable{
balance = 22;
name = "Ross";
}
function updateBalance(uint _balance) external {
balance = _balance;
}
function updateName(string calldata _name) external {
name = _name;
}
}
deployed on Kovan test network here: https://kovan.etherscan.io/tx/0x44084a920eb995ba66c91b9f54b03096a9403c7519b35619a4d9650c64d2533b but I am not able to figure how to read the value of balance
on etherscan.
0x6a6f657900000000000000000000000000000000000000000000000000000008
when I doweb3.eth.getStorageAt('0xC05B6465914841FcFf642b6f9aDE6982b8B92175', 1);
but I do not know how to decode it. Any tips? I tried hex decode but doesn't work. Maybe I am missing something here.web3.utils.hexToAscii()
on the returned result - >web3.eth .getStorageAt('0xC05B6465914841FcFf642b6f9aDE6982b8B92175', 0) .then(result => { console.log(web3.utils.hexToAscii(result)); });