0

i have this contract trying to access the private mapping https://rinkeby.etherscan.io/address/0xe67a53e13e5bc8d197c97ff01a201eb73eb9eb78

bytes32(keccak256(abi.encodePacked("0x4cf6747ba3629bf85e581b60cc774724fbbe0302", 2)))

await web3.eth.getStorageAt("0xe67a53e13e5bc8d197c97ff01a201eb73eb9eb78", "0xc358a61237e9fb52fa1a3b1a52a54841166d4c7b2e1e0b7cd2c9628eb2876173")

its not returning me the proper value it returns zero, it should give me 24

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.1;

contract MyContract {

    uint public myUnit = 10;

    address public  m;

    mapping(address => uint) private p;

    function setUnit()  public {
        p[msg.sender] = 24;
    }

    function rUnit(address a) public view returns(uint){
        return p[a];
    }


    function test() public view{
        require(m == address(0));
    }

    function getMapLocation(uint slot, address key) public pure returns(bytes32){
        return bytes32(keccak256(abi.encodePacked(key, slot)));
    }
}
```

2 Answers 2

1

Since the key needs to be padded to 32 bytes as per the documentation you need to use abi.encode() instead of abi.encodePacked().

This would return the correct slot:

    return bytes32(keccak256(abi.encode(key, slot)));
4
  • Thank you.. One Question though how can i access values of such variables. mapping(address => mapping(address => uint256)) private _someV; Commented Aug 16, 2022 at 8:57
  • That would be bytes32(keccak256(abi.encode(value, keccak256(abi.encode(key, slot)))))
    – no1dea
    Commented Aug 16, 2022 at 11:23
  • Can you explain this to me a bit... Commented Aug 17, 2022 at 3:42
  • It is actually very well explained in the documentation that I have linked above. Please have a look at it and ask if there are any specific details that you do not understand.
    – no1dea
    Commented Aug 17, 2022 at 11:41
0

try calling setUnit from the same address you're calling rUnit from.

Also, the rUnit function means that it's pointless to declare p as private. If you make it public, solidity will create a 'getter function' for it which is the same as

function p(address _address) public view returns (uint) {
    return p[_address];
}

Meaning you can call p(0x123...) from web3.js

1
  • 1
    I get your point ... i just trying to understand how to get access variables defined in an smart contract.... i know that solidity created getter functions.... that rUnit function was just written for reference to check if setUnit is properly setting the value.. i'm just which it does.. set it to 24.. i'm just curious on how to access the private variable value if try to access the variable which is on slot 0 which is myUnit. it does return 10 ``` await web3.eth.getStorageAt("0xe67a53e13e5bc8d197c97ff01a201eb73eb9eb78", 0) ``` Commented Aug 16, 2022 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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