Skip to main content
edited title
Link

Is it possible to set the value of a mapping from another contract without a setter function?

Source Link

Is it possible to set the value of a mapping from another contract without a setter function?

I have a mapping in contractA marked as public. I know a default getter function is created that will allow me to get the value from contractB, but can I set the value from contractB too? The compile error I'm getting isn't clear "TypeError: Expression has to be an lvalue." so I'm just wondering if I have the syntax wrong or if it's simply not possible even though it's marked as public.

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.7.0 <0.9.0;

contract contractAInterface {
    mapping(string => uint256) public testMapping;
}

contract contractB {

    address contractAddress = 0xd9145CCE52D386f254917e481eB44e9943F39138;
    contractAInterface contractA = contractAInterface(contractAddress);

    function setValueOfMapping(string memory _key, uint256 _value) public {
        contractA.testMapping(_key) = _value;
    }
}