1

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

2 Answers 2

3

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.

2
  • 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)? Commented Jun 26, 2018 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. Commented Jun 26, 2018 at 21:32
-1

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;
    }

}
1
  • Inheriting KeyValueStorage — as opposed to referencing an instance of it — means that refreshTime will be a different mapping from the one that OP wants to get.
    – slider
    Commented Dec 23, 2021 at 16:50

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.