Yes.
There's a gotcha, though. Contracts can't yet pass around dynamic-length arguments, so strings won't work. I changed to bytes32
to make the simple example work.
With that detail out of the way ...
Contract B must have knowledge of contract's A ABI (the function signatures) and it's address.
The easiest method is to include both files in the same source file. Note that you can deploy B only if A is already out there somewhere and you have knowledge of its address.
B.sol
contract A {
mapping (bytes32 => uint) public someMapping;
}
contract B {
A a; // cast "a" as "contract A" defined above
function B(address addressA) public { // pass address of "A" to the constructor
a = A(addressA);
}
function getAUint(bytes32 key) public view returns(uint) {
return a.someMapping(key);
}
}
You will obviously have to extend this example to set values in A somehow so the test is a little more interesting.
If the keys really do need to be strings
, you might consider using bytes32
hashes of the strings
. This way, every unique string will create a unique fixed-size key. Stringy keys is almost always a sign of going astray in Solidity.
Hope it helps.