0

So i'm getting this stack to deep error

enter image description here

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

What best practice here to do minting with lot of attributes in my NFT?

5
  • Can you share the complete function.
    – Prosperity
    Feb 19, 2022 at 15:51
  • @Prosperity i edited the post Feb 19, 2022 at 15:57
  • Move some of your implementations into an internal function then you can call the internal function from minting function, this will help you to reduce number of variables that you declare in a function. Clear ?
    – Prosperity
    Feb 19, 2022 at 16:05
  • ok, i will try it Feb 19, 2022 at 16:07
  • @Prosperity thanks, got it working now Feb 19, 2022 at 16:13

1 Answer 1

1

Stack too deep means that you have declared too many local variables in a function. You can only have 16 local variables in a function.

You'll need to break your function up into multiple functions or contracts depending on what you are trying to do.

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.