Erc-721

ERC721 Mint Stack 太深錯誤

  • February 19, 2022

所以我讓這個堆棧陷入深度錯誤

在此處輸入圖像描述

function mint() public payable{
       require(commonImages.length > 0);
       require(uncommonImages.length > 0);
       require(rareImages.length > 0);
       require(legendaryImages.length > 0);
       require(commonWeapons.length > 0);
       require(uncommonWeapons.length > 0);
       require(rareWeapons.length > 0);
       require(legendaryWeapons.length > 0);
       require(commonSpecials.length > 0);
       require(uncommonSpecials.length > 0);
       require(rareSpecials.length > 0);
       require(legendarySpecials.length > 0);
       //require(msg.value >= minPrice);
       //require(owner.send(msg.value));
       require(GXGToken.allowance(msg.sender, address(this)) >= minPrice, "Not enough of GXG token");//To ensure they will deposit the right amount
       GXGToken.transferFrom(msg.sender, owner(), minPrice);

       _tokenIds.increment();
       uint256 _tokenId = _tokenIds.current();
       tokenOwner[_tokenId] = msg.sender;
       uint _number = _tokenId;

       //rarity check
       uint _rarityRand = rand(0, 100);
       uint8 _levelRarity = 0;
       if(presale){
           if(_rarityRand<presaleRarityPercentages[3]){
               _levelRarity = 4;
           }else{
               _rarityRand = rand(0, 100);
               if(_rarityRand<presaleRarityPercentages[2]){
                   _levelRarity = 3;
               }else{
                   _rarityRand = rand(0, 100);
                   if(_rarityRand<presaleRarityPercentages[1]){
                       _levelRarity = 2;
                   }else{
                       _levelRarity = 1;
                   }
               }
           }
       }else{
           if(_rarityRand<normalRarityPercentages[1]){
               _levelRarity = 2;
           }else{
               _levelRarity = 1;
           }
       }
       
       string memory _image;
       string memory _special;
       string memory _weapon1;
       string memory _weapon2;
       string memory _weapon3;  
       string[] memory _weaponsTemp;
       string[] memory _specialsTemp; 
       //delete _weaponsTemp; 
       //delete _specialsTemp;  
       uint _weaponRand; 
       uint i; 
       uint index;
       uint allWeaponCount;
       uint allSpecialCount;

       if(_levelRarity == 1){
           _image = commonImages[rand(0, commonImages.length-1)];
           _special = commonSpecials[rand(0, commonSpecials.length-1)];
           _weapon1 = commonWeapons[rand(0, commonWeapons.length-1)];  
       }else if(_levelRarity == 2){
           allWeaponCount = (commonWeapons.length-1)+(uncommonWeapons.length-1);
           _weaponsTemp = new string[] (allWeaponCount);
           allSpecialCount = (commonSpecials.length-1)+(uncommonSpecials.length-1);
           _specialsTemp = new string[] (allSpecialCount);
           index = 0;
           for(i=0;i<commonWeapons.length;i++){
               _weaponsTemp[index] = commonWeapons[i];
               index++;
           }
           for(i=0;i<uncommonWeapons.length;i++){
               _weaponsTemp[index] = uncommonWeapons[i];
               index++;
           }
           index = 0;
           for(i=0;i<commonSpecials.length;i++){
               _specialsTemp[index] = commonSpecials[i];
               index++;
           }
           for(i=0;i<uncommonSpecials.length;i++){
               _specialsTemp[index] = uncommonSpecials[i];
               index++;
           }

           _image = uncommonImages[rand(0, uncommonImages.length-1)];
           _special = _specialsTemp[rand(0, _specialsTemp.length-1)];

           _weaponRand = rand(0, allWeaponCount-1);
           _weapon1 = _weaponsTemp[_weaponRand];  
           _weaponsTemp[_weaponRand] = _weaponsTemp[_weaponsTemp.length - 1];            
           allWeaponCount--;

           _weaponRand = rand(0, allWeaponCount-1);
           _weapon2 = _weaponsTemp[_weaponRand];  
       }else if(_levelRarity == 3){
           allWeaponCount = (commonWeapons.length-1)+(uncommonWeapons.length-1)+(rareWeapons.length-1);
           _weaponsTemp = new string[] (allWeaponCount);
           allSpecialCount = (commonSpecials.length-1)+(uncommonSpecials.length-1)+(rareSpecials.length-1);
           _specialsTemp = new string[] (allSpecialCount);
           index = 0;
           for(i=0;i<commonWeapons.length;i++){
               _weaponsTemp[index] = commonWeapons[i];
               index++;
           }
           for(i=0;i<uncommonWeapons.length;i++){
               _weaponsTemp[index] = uncommonWeapons[i];
               index++;
           }
           for(i=0;i<rareWeapons.length;i++){
               _weaponsTemp[index] = rareWeapons[i];
               index++;
           }
           index = 0;
           for(i=0;i<commonSpecials.length;i++){
               _specialsTemp[index] = commonSpecials[i];
               index++;
           }
           for(i=0;i<uncommonSpecials.length;i++){
               _specialsTemp[index] = uncommonSpecials[i];
               index++;
           }
           for(i=0;i<rareSpecials.length;i++){
               _specialsTemp[index] = rareSpecials[i];
               index++;
           }
           _image = rareImages[rand(0, rareImages.length-1)];
           _special = _specialsTemp[rand(0, _specialsTemp.length-1)];

           _weaponRand = rand(0, allWeaponCount-1);
           _weapon1 = _weaponsTemp[_weaponRand];  
           _weaponsTemp[_weaponRand] = _weaponsTemp[_weaponsTemp.length - 1];
           allWeaponCount--;

           _weaponRand = rand(0, _weaponsTemp.length-1);
           _weapon2 = _weaponsTemp[_weaponRand];  
           _weaponsTemp[_weaponRand] = _weaponsTemp[_weaponsTemp.length - 1];
           allWeaponCount--;

           _weaponRand = rand(0, _weaponsTemp.length-1);
           _weapon3 = _weaponsTemp[_weaponRand];  
       }else if(_levelRarity == 4){
           allWeaponCount = (uncommonWeapons.length-1)+(rareWeapons.length-1)+(legendaryWeapons.length-1);
           _weaponsTemp = new string[] (allWeaponCount);
           index = 0;
           for(i=0;i<uncommonWeapons.length;i++){
               _weaponsTemp[index] = uncommonWeapons[i];
               index++;
           }
           for(i=0;i<rareWeapons.length;i++){
               _weaponsTemp[index] = rareWeapons[i];
               index++;
           }
           for(i=0;i<legendaryWeapons.length;i++){
               _weaponsTemp[index] = legendaryWeapons[i];
               index++;
           }

           _image = legendaryImages[rand(0, legendaryImages.length-1)];
           _special = legendarySpecials[rand(0, legendarySpecials.length-1)];

           _weaponRand = rand(0, allWeaponCount-1);
           _weapon1 = _weaponsTemp[_weaponRand];  
           _weaponsTemp[_weaponRand] = _weaponsTemp[_weaponsTemp.length - 1];
           allWeaponCount--;

           _weaponRand = rand(0, allWeaponCount-1);
           _weapon2 = _weaponsTemp[_weaponRand];  
           _weaponsTemp[_weaponRand] = _weaponsTemp[_weaponsTemp.length - 1];
           allWeaponCount--;

           _weaponRand = rand(0, allWeaponCount-1);
           _weapon3 = _weaponsTemp[_weaponRand];  
       }

       Spaceship memory s;
       s.number = _number;
       s.image = _image;
       s.special = _special;
       s.weapon1 = _weapon1;
       s.weapon2 = _weapon2;
       s.weapon3 = _weapon3;
       s.rarity = _levelRarity;
       s.level = 1;
       s.durability = 100;
       s.health = 3;
       s.speed = 150;
       
       spaceships.push(s);
       super._mint(msg.sender, _tokenId);
   }

在我的 NFT 中使用許多屬性進行鑄幣的最佳實踐是什麼?

堆棧太深意味著你在一個函式中聲明了太多的局部變數。一個函式中只能有 16 個局部變數。

您需要根據您要執行的操作將您的功能分解為多個功能或契約。

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