I have a contract:

contract KeyValueStorage {
    mapping (address => mapping(bytes32 => uint256)) refreshTime;
    ....
}

and in another file:

import "./KeyValueStorage.sol";
contract GlobalGeneric {
    KeyValueStorage globalS;
    constructor(KeyValueStorage storage_) public {
        globalS = storage_;
    }
    function claimInterests(address _memberAddr, bytes32 _identifier) public returns (bool success) {
        require(now > globalS.refreshTime[_memberAddr][_identifier] + 90 days);
        .....
    }
....
}

But I got an error as refreshTime not found or not visible after argument-dependent lookup in contract KeyValueStorage

Your mapping is private by default. If you do not precise it's public with the keyword public before the mapping's name, you won't be able to access it unless you provide a getter.

mapping (address => mapping(bytes32 => uint256)) public refreshTime;

Once it's done, to access a value, you can only do it the following way :

require(now > globalS.refreshTime(_memberAddr, _identifier) + 90 days);

You will note that you have to pass the keys as parameters. For every public variable, Solidity is automatically creating a getter, and for mappings, the getter takes in parameters the keys needed to access a value, as you remarked.

  • That works! Wondering how can I edit the value if it is a getter function. For example, how do I modify globalS.refreshTime(_memberAddr, _identifier)? – coding_ninja Jun 26 at 18:58
  • No, a getter is a constant function and does not modify the state of the contract. You will need to use a setter for that. – Eli Drion Jun 26 at 21:32

You can Even do something this way

contract KeyValueStorage {
    mapping (address => mapping(bytes32 => uint256)) internal refreshTime;
}

import "./KeyValueStorage.sol";
contract GlobalGeneric is KeyValueStorage {

    function claimInterests(address _memberAddr, bytes32 _identifier) public view returns (bool success) {
        require(now > refreshTime[_memberAddr][_identifier] + 90 days);
        return true;
    }

}

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.