Solidity:Return 来自结构内部的映射

Solidity: Return a mapping from inside a struct

这是我的代码:

pragma solidity 0.7.4;
pragma experimental ABIEncoderV2;

mapping(uint => ProductWithBids) public internalProducts;

struct SecretBids {
        uint values;
        bool fake;
        string secret;
    }

struct ProductWithBids {
        uint id;
        string name;
        uint price;
        address payable owner;
        address payable beneficiary;
        bool purchased;

        mapping(address => Bid[]) bids;
        mapping(address => SecretBids[]) nakedBids;
        // Allowed withdrawals of previous bids
        mapping(address => uint) pendingReturns;

        uint bidsCount;

        bool ended;
        uint biddingEnd;
        uint revealEnd;

        address highestBidder;
        uint highestBid;
    }

function getNakedBids(uint _id)
    public
    view
    returns(SecretBids[] memory product) {
        ProductWithBids storage selectedProduct = internalProducts[_id];
        return selectedProduct.nakedBids;
    }

我需要 return nakedBids 但我收到以下错误: 类型错误:Return 参数类型映射(地址 => 结构 eShop.SecretBids 存储引用[] 存储引用)不能隐式转换为预期类型(第一个 return 变量的类型)结构 eShop.SecretBids 内存[] 内存。 return selectedProduct.nakedBids;

我明白错误的意思了,我想我已经尝试了所有方法并到处寻找。据我发现,solidity 根本不支持此操作。有人知道我接下来可以尝试什么吗?如果我错过了任何其他重要的代码部分,请告诉我。提前致谢。

我也试过了:

returns(mapping(address => SecretBids[]) memory nakedBids)

但随后出现以下错误: 包含(嵌套)映射的类型只能是内部函数或库函数的参数或 return 变量。 returns(mapping(address => SecretBids[]) memory nakedBids) { ^------------------------------------------------^ ,TypeError: Type mapping(address => struct eShop.SecretBids[]) 仅在存储中有效,因为它包含(嵌套)映射。 returns(mapping(address => SecretBids[]) memory nakedBids)

ps:如果我将其更改为存储,它会抱怨数据位置必须是内存。

顺便说一下,你应该把mapping(address => SecretBids[]) nakedBids;移出结构,让它自己成为一个存储变量。

nakedBids 是一个 SecretBids 结构数组。

我的回答是基于 solidity v0.5.x(希望类似于 0.7.4)。 Return solidity 尚不支持结构数组。相反,您可以 return 元组。

returns (uint[] memory, bool[] memory, string[] memory)

我几乎放弃了创建 getter 的想法。所以我以更直接的方式获得了我需要的数据。可能不是最好的方法,但它对我有用。随意分享任何其他解决方案。 这是代码:

    function reveal(
        uint _id
    )
    public
    payable
    {
        ProductWithBids storage selectedProduct = internalProducts[_id];
        //onlyAfter biddingEnd && onlyBefore revealEnd
        require(block.timestamp > selectedProduct.biddingEnd);
        require(block.timestamp < selectedProduct.revealEnd);
        uint count = selectedProduct.bidsCount;

        uint[] memory values = new uint[](count);
        bool[] memory fake = new bool[](count);
        string[] memory secret = new string[](count);

        for (uint i = 0; i < count; i++) {
            values[i] = selectedProduct.nakedBids[msg.sender][i].values;
            fake[i] = selectedProduct.nakedBids[msg.sender][i].fake;
            secret[i] = selectedProduct.nakedBids[msg.sender][i].secret;
        }

        revealInternal(
            values,
            fake,
            secret,
            _id
        );
    }

我没有从一开始就使用这个解决方案,因为我需要 react-js 从前端完成它,然后将它传递到 revealInternal(...);