2

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.

7
  • thanks for commenting, however, that answer still doesn't fully describe the 'how' part of it.
    – Ram Patra
    Commented Apr 12, 2021 at 16:44
  • 2
    How about this one -> ethereum.stackexchange.com/questions/44893/… Commented Apr 12, 2021 at 16:51
  • Yes, this is it. I was thinking getStorageAt would be the way but wasn't sure of it. One last question though, I am getting 0x6a6f657900000000000000000000000000000000000000000000000000000008 when I do web3.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.
    – Ram Patra
    Commented Apr 12, 2021 at 17:29
  • Another question would be, is web3 api the only way then? Are there other ways/apis with which I can access? Thanks again for the help 🙏
    – Ram Patra
    Commented Apr 12, 2021 at 17:36
  • 2
    I think you'll need to run web3.utils.hexToAscii() on the returned result - > web3.eth .getStorageAt('0xC05B6465914841FcFf642b6f9aDE6982b8B92175', 0) .then(result => { console.log(web3.utils.hexToAscii(result)); }); Commented Apr 12, 2021 at 21:54

1 Answer 1

0

I think one can view the content of the private variable. Declaring the private variable private does not mean someone cannot have access to it. It just means that if another contract inherit this contract the other contract won't be able to modify the private variable value but that does not prevent anyone who wants to retrieve the value of the variable from reading it. I think the issue you have is that you have not yet set up a get function for the variable. Try adding this to your code:

function getbalance() public view returns (uint){ return balance; }

1
  • 1
    This question is about how to read/access a private variable in a smart contract in ethereum.
    – Ram Patra
    Commented Apr 12, 2021 at 23:15

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