2

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);
}
  • Providing more context will help to answer your question. Could you clarify the structure of both contracts, how they should interact? – Thomas Vanderstraeten Oct 29 '18 at 12:31
1

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

As of right now, it's not possible to return structs in solidity. The most obvious workaround would be to make functions that return individual elements of a specific struct, like, assuming a cup has a title:

function cup_title(bytes32 cup) external returns (string){
   return cups[cup].title
}

And repeat that for every property.

Or, have just one function that returns multiple values from it (ie: all the attributes of the struct).

Or, use the Seriality library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.