0
votes

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. return 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? 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. returns(mapping(address => SecretBids[]) memory nakedBids)

ps: if i change it to storage it complains that Data location must be memory.

2

2 Answers

0
votes

By the way, you should move 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.

My answer is based on solidity v0.5.x (hopefully similar to 0.7.4). Return array of struct is not supported in solidity yet. Instead you can return tuple.

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

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(...);