Solidity

如何填充大量結構

  • May 11, 2018

我對 Solidity 很陌生,想用 240 個結構填充一個數組。

我有一個結構:

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

還有一個數組:

   Country[] public countries;

我想在部署時用 Country 填充數組,但是當我嘗試這樣做時,我的 gas 用完了,甚至無法部署契約(即使沒有呼叫該函式)。實現我的目標的正確方法是什麼?

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

您應該分批進行,並利用 Country 的每個實例化幾乎相同的事實。

我以創建此契約為例:

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");
      _;
   }
}

它跟踪創建的每個國家/地區,因此您可以避免重複。此外,完成後,您可以呼叫setFinisheddisable createCountries

現在,當您填充countries數組時,呼叫createCountries連接的國家程式碼。在 Web3.js 中:

await countryStore.createCountries("AWAFAOAIALAXADAEAR") ....

引用自:https://ethereum.stackexchange.com/questions/48241