This can be done using the v2 ABI encoder. Let's talk about how to use this encoder, and then how to return an array of structs.
How to use abicoderv2
source: Solidity docs
abicoderv2
is the ABI encoder by default starting in Soldity 0.8. It was originally considered experimental, but as of Solidity 0.6 was given non-experimental status. As such, you're using it by default in 0.8 and up.
To use it from 0.6 to 0.8 (non-inlusive), put the following at the top of the contract (I've generally seen it put directly beneath the pragma
declaring the version of Solidity):
pragma abicoderv2;
For Solidity <0.6, use:
pragma experimental ABIEncoderV2
How to return an array of structs
Example code:
struct myStruct {
address foo;
uint256 bar;
}
myStruct[] private myStructs;
function getMyStruct() public view returns(myStruct[] memory) {
return myStruct;
}
The memory
was necessary if there was an interface to the contract implementing a skeleton of this function, it may be able to be taken out if there isn't an interface behind it, though I'm not sure you'd want to even if you could. If I get more clarity on this last point, I'll try to update the answer.