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 {...}

Jeremy Then
  • 4,599
  • 3
  • 5
  • 28
0TTT0
  • 672
  • 2
  • 10
  • 20

1 Answers1

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.

Jeremy Then
  • 4,599
  • 3
  • 5
  • 28
Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145