0

I am reviewing a piece of code intended for the creation of an oracle and I get the following error: "Struct containing a (nested) mapping cannot be constructed"

I know it is related to the new compiler versions and I have been following some of the threads of people with similar problems in their codes but I have not just found the solution.

I hope you can help me. I leave the code snippet below.

pragma solidity ^0.8.0;

contract Oracle {
    Request[] requests; 
    uint currentId = 0; 
    uint minQuorum = 2; 
    uint totalOracleCount = 3; 

    struct Request{
        uint id;                            
        string urlToQuery;                  
        string attributeToFetch;            
        string agreedValue;                 
        mapping(uint => string) answers;    
        mapping(address => uint) quorum;    
    }

  
    function createRequest (
        string memory _urlToQuery,
        string memory _attributeToFetch
    )
    public
    {
        uint length = requests.push(Request(currentId, _urlToQuery, _attributeToFetch, ""));  **// The error is here**
        Request storage r = requests[length-1];
    }
}

2 Answers 2

3

To solve your problem, you can use push() method for create a storage slot into your struct array about new Request struct instance to insert and then you can fill it with the input values that passed into createRequest() method. I adjusted your smart code in this way:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Oracle {
    Request[] requests; 
    uint currentId = 0; 
    uint minQuorum = 2; 
    uint totalOracleCount = 3; 

    struct Request{
        uint id;                            
        string urlToQuery;                  
        string attributeToFetch;            
        string agreedValue;                 
        mapping(uint => string) answers;    
        mapping(address => uint) quorum;    
    }

  
    function createRequest (
        string memory _urlToQuery,
        string memory _attributeToFetch,
        uint _idQuestion,
        address _addressQuorum
    )
    public
    {
        Request storage r = requests.push();
        r.id = currentId;
        r.urlToQuery = _urlToQuery;
        r.attributeToFetch = _attributeToFetch; 
        r.agreedValue = "";
        r.answers[_idQuestion] = "test";
        r.quorum[_addressQuorum] = 0;
    }
}
2
  • Hey! thanks for your reply, I just modified it as you indicated and it seems to be working perfectly. Have a nice week :)
    – Alex
    May 24, 2022 at 16:46
  • Accept the answer if its correct :)
    – Bruce
    May 24, 2022 at 19:30
1

I have faced same problem and i have fixed it like that:

Solidity version i used 0.8.7;

function createRequest(string memory _description, uint256 _value, address _recipient) public  restricted{
        Request storage newRequest = requests.push();
        newRequest.description = _description;
        newRequest.value = _value;
        newRequest.recipient = _recipient;
        newRequest.complete = false;
        newRequest.approvalCount = 0;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.