3

I am wondering what pattern I am supposed to follow in Solidity to take in an array of structs as a parameter. I am translating code from CPP and mostly everything translates directly besides this (and variable scoping in functions).

TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.

    struct HorseRaceData {
            HorseData data;
            uint64 currentSpeed;
            uint64 currentStamina;
            uint64 distanceTraveled;
            uint64 time;
            bool done;
    }

    function placementSimulation (HorseRaceData[] memory horses) public {...}

1 Answer 1

0

As a general rule, try to reduce large operations to a more granular style and rely on clients to iterate as needed. There are exceptions, but you could do worse than addressing concerns like lists one row at a time.

The elements of the list are structs which are also beyond the capabilities of the current ABI encoder. What you can do is pass in members. Something like:

    pragma solidity 0.5.1;
    
    contract Horses {
        
        struct HorseStruct {
            uint64 currentSpeed;
            uint64 currentStamina;
            uint64 distanceTraveled;
            uint64 time;
            bool done;
        }
        
        HorseStruct[] public horseStructs;
        
        function placementSimulation (uint64 currentSpeed, uint64 currentStamina, uint64 distanceTraveled,  uint64 time, bool done) public {
            HorseStruct memory horseData = HorseStruct({
                currentSpeed: currentSpeed,
                currentStamina: currentStamina,
                distanceTraveled: distanceTraveled,
                time: time,
                done: done});
            horseStructs.push(horseData);
        }
            
    }

Have a look over here for some general patterns: Are there well-solved and simple storage patterns for Solidity?

Hope it helps.

Your Answer

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

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