0

I get an error when trying to push the newRound struct to storage. How can it be fixed?

Error:

UnimplementedFeatureError: Copying of type struct Lottery.Ticket memory[] memory to storage not yet supported.

pragma solidity >=0.4.25 <0.6.0;

contract Lottery {
    struct Round {
        uint deployBlock;
        uint endBlock;        
        uint drawBlock;
        Ticket[] tickets;        
        uint checkpot;        
        address winner;
    }

    struct Ticket{
        address owner;
        uint luckyNumber;
    }

    Round[] rounds;
    uint public roundIndex;


    function drawWinner() public returns (address _winner){

        Ticket[] memory newTickets;

        // How to push this new struct to storage?
        Round memory newRound = Round({
                deployBlock: block.number, 
                endBlock: block.number + ROUND_DURATION,
                drawBlock: block.number + ROUND_DURATION + 5,
                tickets: newTickets,
                checkpot: 0,
                winner:address(0)
            });

        rounds.push(newRound);

        roundIndex++;
        return rounds[roundIndex].tickets[0].owner;
    }
}

2

In the available features of solidity yet, You can't really do what you're trying to do with dynamic arrays. You'll need to change your approach slightly to get it to work. There are two ways to do this kind of thing, let me give you hint to workaround.

Add Ticket into Round as mapping instead of array and keep the index as ID to travers. Like so:

struct Round {
        uint deployBlock;
        uint endBlock;        
        uint drawBlock;
        mapping (uint=>Ticket) tickets;
        uint TicketIndex;
        uint checkpot;        
        address winner;
}

You can user TicketIndex to add tickets and travers tickets in the round struct.

Alternatively, you can increase the size of the rounds array and use Solidity's default zero values. Something like that:

function drawWinner() public returns (address _winner){
        rounds.length++;
        Ticket memory t;
        Round storage lastRound = rounds[rounds.length-1];
        lastRound.deployBlock = block.number;
        lastRound.endBlock= block.number + ROUND_DURATION;
        lastRound.drawBlock= block.number + ROUND_DURATION + 5;

You can add tickets to this object like so:

lastRound.tickets.push(t);

Currently I came to know about these two work arounds as solidity does not support this feature yet. Give it a try and you will find out the clear solution. I hope it helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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