0

Im creating a smart contract where an user creates activities and then adds them in to a routine. When i compile my code i get this error:

Copying of type struct Improver.Activity memory[] memory to storage not yet supported

I tried to look at other questions similar to this one but i couldn't fix the issue.

Code:

    //SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    contract Improver {
        struct User {
            string username;
            address _address;
            string profilePic;
            uint id;
        }
    
        struct Activity {
            address owner;
            uint start;
            uint end;
            bool status;
            string activity;
            uint id;
        }
    
        struct Routine {
            Activity[] activities;
            bool areActivitiesAllDone;
            address owner;
            uint id;
        }
    
        User[] public usersArray;
        Activity[] public activityArray;
        Routine[] public routineArray;
        mapping(address => User) public addressToUser;
        mapping(string => User) public nameToUser;
        mapping(address => Activity) public addressToActivity;
        mapping(uint => Activity) public idToActivity;
        mapping(address => Routine) public addressToRoutine;
        mapping(uint => Routine) public idToRoutine;
    
        
        function createActivity(
            uint _start,
            uint _end,
            string memory _activity
        ) public {
            Activity memory newActivity = Activity({
                owner: msg.sender,
                start: _start,
                end: _end,
                status: false,
                activity: _activity,
                id: activityArray.length
            });
    
            addressToActivity[msg.sender] = newActivity;
            activityArray.push(newActivity);
        }

            function createRoutine(uint[] memory activitiesID) public {
        Activity[] memory routineActivities = new Activity[](8);
        bool[] memory activitiesStatus = new bool[](8);
        bool areAllActivitiesDone;

        // Puts activities in the array (routineActivities)
        for (uint index; index < activitiesID.length; index++) {
            Activity memory activity = idToActivity[index];
            routineActivities[routineActivities.length] = activity;
        }

        // Puts all activities status in an array (activitiesStatus)
        for (uint index; index < activitiesID.length; index++) {
            Activity memory activity = idToActivity[index];
            activitiesStatus[activitiesStatus.length] = activity.status;
        }

        // Checks if all activities are done or not and then set a variable depending on the results
        for (uint index; index < activitiesStatus.length; index++) {
            bool status = activitiesStatus[index];
            if (status == false) {
                areAllActivitiesDone = false;
            } else {
                areAllActivitiesDone = true;
            }
        }

        // Create a routine
        Routine memory newRoutine = Routine({
            activities: routineActivities,
            areActivitiesAllDone: areAllActivitiesDone,
            owner: msg.sender,
            id: routineArray.length
        });

        // Add the routine to the different mappings and arrays
        addressToRoutine[newRoutine.owner] = newRoutine;
        idToRoutine[newRoutine.id] = newRoutine;
        routineArray.push(newRoutine);
    }
}

    
   
    }

The function that's causing the problem is createRoutine(), the last one. If you have an

1 Answer 1

0

This happens because its not possible to copy a struct containing a dynamic array to storage. But you can build it. Replace the last lines with this:

    // Create a routine

    // Add the routine to the different mappings and arrays
    uint256 routineArrayIdx = routineArray.length;
    routineArray.push();
    Routine storage routineByArray = routineArray[routineArrayIdx];
    
    Routine storage routineByAddress = addressToRoutine[msg.sender];
    Routine storage routineById = idToRoutine[routineArrayIdx];

    for (uint256 i = 0; i < routineActivities.length; i++) {
        routineByAddress.activities.push(routineActivities[i]);
        routineById.activities.push(routineActivities[i]);
        routineByArray.activities.push(routineActivities[i]);
    }
    
    routineByAddress.areActivitiesAllDone = areAllActivitiesDone;
    routineByAddress.owner = msg.sender;
    routineByAddress.id = routineArrayIdx;
    
    routineById.areActivitiesAllDone = areAllActivitiesDone;
    routineById.owner = msg.sender;
    routineById.id = routineArrayIdx;

    routineByArray.areActivitiesAllDone = areAllActivitiesDone;
    routineByArray.owner = msg.sender;
    routineByArray.id = routineArrayIdx;

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.