Ethereum 有没有一种方法可以在不分解字段结构的情况下返回一个结构数组?
我一直在围绕这个问题做一些研究,但我找不到确切的答案。我用的是0.4.24 我有这样一份合同:Ethereum 有没有一种方法可以在不分解字段结构的情况下返回一个结构数组?,ethereum,solidity,Ethereum,Solidity,我一直在围绕这个问题做一些研究,但我找不到确切的答案。我用的是0.4.24 我有这样一份合同: contract { struct FutureOperation is Ownable { uint256 date; uint256 price; uint256 amount; string name; } FutureOperation[] futureOperations; // ...
contract {
struct FutureOperation is Ownable {
uint256 date;
uint256 price;
uint256 amount;
string name;
}
FutureOperation[] futureOperations;
// ...
function getAllFutureOperations() public onlyOwner returns (FutureOperation[]) {
return futureOperations;
}
}
function getAllFutureOperations() public onlyOwner returns (uint256[] dates, uint256[] prices, uint256[] amounts, string[] names) {
return futureOperations;
}
当我在混音中编译这个时,我得到以下错误:
browser/myfuturetoken.sol:53:64: TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.
我发现一些博客帖子说我应该对结构中的字段进行解构,以将它们作为基元类型的数组返回。所以,在这种情况下,它看起来像这样:
contract {
struct FutureOperation is Ownable {
uint256 date;
uint256 price;
uint256 amount;
string name;
}
FutureOperation[] futureOperations;
// ...
function getAllFutureOperations() public onlyOwner returns (FutureOperation[]) {
return futureOperations;
}
}
function getAllFutureOperations() public onlyOwner returns (uint256[] dates, uint256[] prices, uint256[] amounts, string[] names) {
return futureOperations;
}
有没有其他办法?较新的编译器是否能够返回结构数组
谢谢。正如错误所述,编译器还不支持返回动态数组。然而,实验特性支持它。要使用实验编译器,您需要进行如下更改:
pragma experimental ABIEncoderV2;
contract myContract{
struct FutureOperation {
uint256 date;
uint256 price;
uint256 amount;
string name;
}
string[] futureOperations;
function getAllFutureOperations() public view returns (string[] memory) {
return futureOperations;
}
}
注意:确保不要在生产版本中使用实验性的东西