[Ethereum] How to create Interface to read Struct in mapping

interfacessoliditystruct

There are 2 contracts A and B.

A has a mapping which returns Struct:

mapping (bytes32 => Cup) public cups;

How can we read the cups mapping in contract B?

Following code doesn't work for sure:

interface CupsInteraface {
    function cups(bytes32 cup) external returns (Struct);
}

Best Answer

There is no way to do this in a stable version of solidity, but possible in experimental

pragma experimental ABIEncoderV2;

interface CupInteraface {
    struct S {
        bytes32 s;
    }
    function get(bytes32 cup) external returns (S memory);
}

contract Cup is CupInteraface {
    mapping(bytes32 => S) private _cups;

    constructor(bytes32 cup) public {
        _cups[cup] = S(cup);
    }

    function get(bytes32 cup) public returns (S memory) {
        return _cups[cup];
    }
}
Related Topic