Wie man ein großes Array von Strukturen füllt

Ich bin ziemlich neu bei Solidity und möchte ein Array mit 240 Strukturen füllen.

Ich habe eine Struktur:

    struct Country {
      string name;
      uint level;
      uint attackPower;
      uint defensePower;
      uint price;
   }

Und ein Array:

    Country[] public countries;

Ich möchte das Array bei der Bereitstellung mit Ländern füllen, aber wenn ich versuche, dies als solches zu tun, geht mein Benzin aus und der Vertrag kann nicht einmal bereitgestellt werden (selbst wenn die Funktion nicht aufgerufen wird). Wie erreiche ich mein Ziel richtig?

function createCountries() public{
    countries[0] = Country("AW",1, 100, 100, 1);
    countries[1] = Country("AF",1, 100, 100, 1);
    countries[2] = Country("AO",1, 100, 100, 1);
    countries[3] = Country("AI",1, 100, 100, 1);
    countries[4] = Country("AL",1, 100, 100, 1);
    countries[5] = Country("AX",1, 100, 100, 1);
    countries[6] = Country("AD",1, 100, 100, 1);
    countries[7] = Country("AE",1, 100, 100, 1);
    countries[8] = Country("AR",1, 100, 100, 1);
    countries[9] = Country("AM",1, 100, 100, 1);
    countries[10] = Country("AS",1, 100, 100, 1);
    countries[11] = Country("AQ",1, 100, 100, 1);
    countries[13] = Country("TF",1, 100, 100, 1);
    countries[14] = Country("AG",1, 100, 100, 1);
    countries[15] = Country("AU",1, 100, 100, 1);
    countries[16] = Country("AT",1, 100, 100, 1);
    countries[17] = Country("AZ",1, 100, 100, 1);
    countries[18] = Country("BI",1, 100, 100, 1);
    countries[19] = Country("BE",1, 100, 100, 1);
    countries[20] = Country("BJ",1, 100, 100, 1);
    countries[21] = Country("BF",1, 100, 100, 1);
    countries[22] = Country("BD",1, 100, 100, 1);
    countries[23] = Country("BG",1, 100, 100, 1);
    countries[24] = Country("BH",1, 100, 100, 1);
    countries[25] = Country("BS",1, 100, 100, 1);
    countries[26] = Country("BA",1, 100, 100, 1);
    countries[27] = Country("BL",1, 100, 100, 1);
    countries[28] = Country("BY",1, 100, 100, 1);
    countries[29] = Country("BZ",1, 100, 100, 1);
    countries[30] = Country("BM",1, 100, 100, 1);
    ... 210 more of these...
}

Antworten (1)

Sie sollten dies in Stapeln tun und die Tatsache nutzen, dass jede Instanziierung eines Landes fast gleich ist.

Als Beispiel habe ich diesen Vertrag erstellt:

pragma solidity 0.4.23;

contract CountryStore {
    bool public finishedCreating = false;

    mapping(bytes2 => Country) public createCountries;

    struct Country {
      string name;
      uint level;
      uint attackPower;
      uint defensePower;
      uint price;
      bool created;
    }

    Country[] public countries;

    function createCountries(string names) public onlyWhileCreating {
       for(uint256 i = 0; i < bytes(names).length; i+=2){
           // Get the next two characters and store them in `name`
           bytes memory name = new bytes(2);
           name[0] = bytes(names)[i];
           name[1] = bytes(names)[i+1];

           if(!createCountries(bytes2(name)).created)
               countries.push(Country({
                   name: string(name),
                   level: 1,
                   attackPower: 100,
                   defensePower: 100,
                   price: 1,
                   created: true
               }));
       }
    }

    function finishCreating() public {
        finishedCreating = true;
    }

    modifier onlyWhileCreating(){
       require(!finishedCreating, "This method can only be called when creating countries");
       _;
    }
}

Es verfolgt jedes erstellte Land, sodass Sie Duplikate vermeiden. Wenn Sie fertig sind, können Sie auch anrufen, setFinishedum zu deaktivieren createCountries.

Wenn Sie nun das countriesArray füllen, rufen Sie createCountriesmit den verketteten Ländercodes auf. In Web3.js:

await countryStore.createCountries("AWAFAOAIALAXADAEAR") ....
Dies ist eine Antwort für diese Situation! Aber ich möchte, dass meine anderen Struct-Attribute auch variabel sind, das würde nicht mehr funktionieren, oder?
Das stimmt, es wird nicht für jede Situation funktionieren. Das Wesentliche ist jedoch, dass Sie countriesin Stapeln instanziieren sollten, nicht alle auf einmal.