简体   繁体   English

Solidity:从结构内部返回映射

[英]Solidity: Return a mapping from inside a struct

here is my code:这是我的代码:

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

i need to return the nakedBids but i get the following error: TypeError: Return argument type mapping(address => struct eShop.SecretBids storage ref[] storage ref) is not implicitly convertible to expected type (type of first return variable) struct eShop.SecretBids memory[] memory.我需要返回裸投标,但出现以下错误:TypeError: Return argument type mapping(address => struct eShop.SecretBids storage ref[] storage ref) is not implicitly convertible to expected type (type of first return variable) struct eShop .SecretBids 内存[] memory。 return selectedProduct.nakedBids;返回 selectedProduct.nakedBids;

I get what the error means, i think i have tried everything and looked everywhere.我明白错误的含义,我想我已经尝试了一切并到处寻找。 From what i have found is that solidity doesn't support at all this action.Does anyone has any idea what i can try next?根据我的发现,solidity 根本不支持这个动作。有谁知道我接下来可以尝试什么? If i missed any other important code parts let me know.如果我错过了任何其他重要的代码部分,请告诉我。 Thanks in advance.提前致谢。

I also tried:我也试过:

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

but then i get the following error: Types containing (nested) mappings can only be parameters or return variables of internal or library functions.但后来我收到以下错误:包含(嵌套)映射的类型只能是内部或库函数的参数或返回变量。 returns(mapping(address => SecretBids[]) memory nakedBids) { ^-----------------------------------------------^,TypeError: Type mapping(address => struct eShop.SecretBids[]) is only valid in storage because it contains a (nested) mapping.返回(映射(地址 => SecretBids[])memory 裸出价){ ^--------------------------------- -------------^,TypeError: Type mapping(address => struct eShop.SecretBids[]) 仅在存储中有效,因为它包含(嵌套)映射。 returns(mapping(address => SecretBids[]) memory nakedBids)返回(映射(地址 => SecretBids[])memory 裸投标)

ps: if i change it to storage it complains that Data location must be memory. ps:如果我将其更改为存储,它会抱怨数据位置必须是 memory。

By the way, you should move mapping(address => SecretBids[]) nakedBids;顺便说一句,你应该移动mapping(address => SecretBids[]) nakedBids; out of the struct, and let it be a storage variable on its own.从结构中取出,让它自己成为一个存储变量。

nakedBids is an array of SecretBids struct. nakedBidsSecretBids结构的数组。

My answer is based on solidity v0.5.x (hopefully similar to 0.7.4).我的答案基于solidity v0.5.x(希望类似于0.7.4)。 Return array of struct is not supported in solidity yet. Solidity 尚不支持返回结构数组。 Instead you can return tuple.相反,您可以返回元组。

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

I pretty much gave up on the idea of creating a getter.我几乎放弃了创建吸气剂的想法。 So i got the data i needed in a more direct way.所以我以更直接的方式获得了我需要的数据。 Probably is not the best way but it worked for me.可能不是最好的方法,但它对我有用。 Feel free to share any other solutions.随意分享任何其他解决方案。 Here is the code:这是代码:

    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
        );
    }

I did not go from the start with this solution because i needed the react-js to do it from the front-end and then just pass it in revealInternal(...);我从一开始就没有 go 使用此解决方案,因为我需要 react-js 从前端执行此操作,然后将其传递给revealInternal(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM