<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Masood Ur Rehman</title>
    <description>The latest articles on DEV Community by Masood Ur Rehman (@masudrehman).</description>
    <link>https://dev.to/masudrehman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F961926%2F6aa2c870-4198-41b6-9ab6-dc1ca0cdb3c6.jpeg</url>
      <title>DEV Community: Masood Ur Rehman</title>
      <link>https://dev.to/masudrehman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/masudrehman"/>
    <language>en</language>
    <item>
      <title>Solidity NFT bird feeding(Level) + Staking</title>
      <dc:creator>Masood Ur Rehman</dc:creator>
      <pubDate>Tue, 01 Nov 2022 12:31:48 +0000</pubDate>
      <link>https://dev.to/masudrehman/solidity-nft-bird-feeding-staking-15f7</link>
      <guid>https://dev.to/masudrehman/solidity-nft-bird-feeding-staking-15f7</guid>
      <description>&lt;p&gt;&lt;em&gt;Advance Solidity experince required&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I will get straight to the point.&lt;br&gt;
This is an nft project, in which, birds(nft's) are upgraded by feeding them ERC20 tokens. Also, each bird level will have his own reward APY.&lt;/p&gt;
&lt;h2&gt;
  
  
  PROCESS:-
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Egg mint &amp;gt; egg in incubator &amp;gt; egg hatched + random attributes + random uniqueness(common, uncommon, rare, legendary) &amp;gt; bird upgrade to mature &amp;gt; mature to max mature.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
reward will depend on bird level and uniqueness.&lt;/p&gt;

&lt;p&gt;So let's start.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./base64.sol";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;they are libraries which we will need. Most of them are self explanatory. The base64 library is needed for text encoding. you will see it later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Astrobirdz is ERC721, Ownable {
    using SafeMath for uint256;
    using Counters for Counters.Counter;
    Counters.Counter public tokenIds;

    address private _burnAddress = 0x000000000000000000000000000000000000dEaD;
    address private _marketPlaceAddress;
    address private _tokenAddress;
    string private _eggUri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/video_2022-04-15_14-40-52.mp4";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The contract name is Astrobirdz. &lt;code&gt;Counters&lt;/code&gt; library is used with the &lt;code&gt;tokenIds&lt;/code&gt; variable, so we will be able to increment or decrement &lt;code&gt;tokenIds&lt;/code&gt; value safely, like, &lt;code&gt;tokenIds.increment()&lt;/code&gt; etc.&lt;br&gt;
&lt;code&gt;_burnAddress&lt;/code&gt; is a dead address, where some percent of tokens will be burned.&lt;br&gt;
&lt;code&gt;_marketPlaceAddress&lt;/code&gt; is the market place address where birds(Eggs) can be sold by the contract owner.&lt;br&gt;
&lt;code&gt;_tokenAddress&lt;/code&gt; is the erc20 address which will be used to feed(upgrade the birds).&lt;br&gt;
&lt;code&gt;_eggUri&lt;/code&gt; is the url for the egg nft, which will be minted to the owner, which he will be able to sell in marketplace.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // Rarity Classes
    enum Class {
    Common,
    Uncommon,
    Rare,
    Legendary
  }
    uint private _seed;
    uint8 private _burnPercent = 25;
    uint8 constant NUM_CLASSES = 4;
    // Starts From 0
    uint8 private constant UNIQUENFTS = 6;
   uint8 public _commonMatureAPY = 10;
   uint8 public _commonMaxMatureAPY = 15;
   uint8 public _unCommonMatureAPY = 15;
   uint8 public _unCommonMaxMatureAPY = 20;
   uint8 public _rareMatureAPY = 25;
   uint8 public _rareMaxMatureAPY = 30;
   uint8 public _legendaryMatureAPY = 50;
   uint8 public _legendaryMaxMatureAPY = 70;
   uint public commonMatureCost = 30000 * 10**18;
   uint public commonMaxMatureCost = 50000 * 10**18;
   uint public unCommonMatureCost = 50000 * 10**18;
   uint public unCommonMaxMatureCost = 70000 * 10**18;
   uint public rareMatureCost = 100000 * 10**18;
   uint public rareMaxMatureCost = 150000 * 10**18;
   uint public legendaryMatureCost = 150000 * 10**18;
   uint public legendaryMaxMatureCost = 200000 * 10**18;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There will be 4 classes of birds rarity. there will be 6 unique nft birds, like eagles, vultures etc. the Apy is the reward and cost are the level upgrading erc20 tokens price.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Attributes {
        string uniqueAttribute;
        uint8 speice;
        uint8 rarity;
        uint8 cannon;
        uint8 laser;
        uint8 bomb;
        uint8 shields;
        uint8 armour;
        uint8 health;
        //check if attributes are setted
        bool set;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will have the following numbers of attributes(powers etc).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct EggHatch {
        uint hatchTime;
        bool hasAlreadyHatched;
        bool isHatching;
    }

    mapping(uint=&amp;gt;Attributes) private _tokenIdToAttributes;
    mapping(uint=&amp;gt;EggHatch) private _eggHatch;
    mapping(uint=&amp;gt;string) private _nftToUniqueAttr;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is self explanatory. &lt;code&gt;EggHatch&lt;/code&gt; is to keep track of egg hatching when it will be placed in incubator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mapping(uint=&amp;gt;uint) public level;
mapping(uint=&amp;gt;uint) private _rewardTime;

    event EggMinted(address indexed, uint indexed);
    event EggLocked(uint indexed, uint indexed);
    event EggRarity(uint indexed, uint indexed);
    event UpgradeMature(uint indexed, uint indexed);
    event UpgradeMaxMature(uint indexed, uint indexed);
    event Reward(uint indexed, uint indexed, uint indexed);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;level&lt;/code&gt; is used to keep track of bird level, whether it's mature or max mature etc. &lt;code&gt;_rewardTime&lt;/code&gt; is for how much time has been passed since the reward was claimed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; constructor(address tokenAddress, address _marketAddress) ERC721("Astrobirdz", "ABZ") {
        _tokenAddress = tokenAddress;
        _marketPlaceAddress = _marketAddress;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set the constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function mintEgg(uint tNumber)
        public
        onlyOwner
    {
        for(uint i = 0; i&amp;lt;tNumber; i++) {
             tokenIds.increment();

             uint256 newItemId = tokenIds.current();
            _mint(msg.sender, newItemId);
            setApprovalForAll(_marketPlaceAddress, true);

            level[newItemId] = 0;
        }
       emit EggMinted(msg.sender, tNumber);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eggs are minted only by the owner, approval is also given to the &lt;code&gt;marketplaceAddress&lt;/code&gt;, so afterwards owner doesn't have to approve every egg manually for selling it on marketplace.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function lockInIncubator(uint _tokenId) public {
        require(ownerOf(_tokenId) == msg.sender, "Not Owner");
        EggHatch memory eggHatch = _eggHatch[_tokenId];
        require(eggHatch.hasAlreadyHatched == false, "already hatched");

        eggHatch.isHatching = true;
        eggHatch.hasAlreadyHatched = true;
        eggHatch.hatchTime = block.timestamp + 7 days;
         _eggHatch[_tokenId] = eggHatch;
        emit EggLocked(_tokenId, eggHatch.hatchTime);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this function will lock the egg in incubator for 7 days, after that it can be hatched.&lt;br&gt;
first it will check if the owner of the egg is the &lt;code&gt;msg.sender&lt;/code&gt;, then it will check if it's already been hatched or not.&lt;br&gt;
it will then set &lt;code&gt;isHatching&lt;/code&gt; and &lt;code&gt;hasAlreadyHatched&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
it will also set 7 days for it to be in incubator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function hatchEgg(uint _tokenId) public {
    require(ownerOf(_tokenId) == msg.sender, "Not Owner");
    EggHatch memory eggHatch = _eggHatch[_tokenId];
    require(eggHatch.isHatching == true, "Not Hatching");
    require(eggHatch.hatchTime &amp;lt;= block.timestamp,"Hatch Time Hasn't Passed Yet");

    eggHatch.isHatching = false;
    _eggHatch[_tokenId] = eggHatch;

    level[_tokenId] = 1;

    Attributes memory _attr = selectRandomNftWithAttributes(_tokenId);
    _attr = selectAttrbiutes(_attr);
    _tokenIdToAttributes[_tokenId] = _attr;
    emit EggRarity(_tokenId, _attr.rarity);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this function is for hatching the egg after 7 days has passed.&lt;br&gt;
the require statements are self explanatory.&lt;br&gt;
&lt;code&gt;level&lt;/code&gt; is set to 1, &lt;code&gt;selectRandomNftWithAttributes&lt;/code&gt; will select random bird from 6 unique birds, and assign one unique attribute like Speed, Camoflauge, Strength etc.&lt;br&gt;
&lt;code&gt;selectAttrbiutes&lt;/code&gt; will give rarity to the bird whether it will be common, uncommon, rare, legendary according to probability of each rarity, and also set the attributes randomly, we will see this function a little later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function selectRandomNftWithAttributes(uint _tokenId) internal returns(Attributes memory) {
        uint _rand = randomUniqueNft();
        Attributes memory _attr = _tokenIdToAttributes[_tokenId];
        if(_rand == 0) {
            _attr.uniqueAttribute = "Powerful Sharp Feet";
            _attr.speice = 0;
        } else if(_rand == 1) {
            _attr.uniqueAttribute = "Powerful Beak";
            _attr.speice = 1;
        } else if(_rand == 2) {
            _attr.uniqueAttribute = "Speed";
            _attr.speice = 2;
        } else if(_rand == 3) {
            _attr.uniqueAttribute = "Camoflauge";
            _attr.speice = 3;
        } else if(_rand == 4) {
            _attr.uniqueAttribute = "Strength";
            _attr.speice = 4;
        } else if(_rand == 5) {
            _attr.uniqueAttribute = "Intelligence";
            _attr.speice = 5;
        } 

        return _attr;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as it was previously mentioned that this will select a random unique bird from six birds. and it will assign one unique attribute according to which bird was selected by the &lt;code&gt;randomUniqueNft&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function randomUniqueNft() internal view returns (uint) {
        uint rand =  uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, _seed)));
        return rand % UNIQUENFTS;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it's a simple function to return one unique bird from six birds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function selectAttrbiutes(Attributes memory attr) internal view returns(Attributes memory){
        Class _class = randomNumProb();


        if(_class == Class.Common) {

            attr.rarity = 0;
            attr.cannon = randRarity(230, 34);
            attr.laser = randRarity(10230, 34);
            attr.bomb = randRarity(12200, 34);
            attr.shields = randRarity(10560, 34);
            attr.armour = randRarity(10740, 34);
            attr.health = randRarity(10450, 34);
            attr.set = true;
            return attr;

        } else if(_class == Class.Uncommon) {

            attr.rarity = 1;
           attr.cannon = randRarity(230, 15) + 35;
            attr.laser = randRarity(10230, 15) + 35;
            attr.bomb = randRarity(12200, 15) + 35;
            attr.shields = randRarity(10560, 15) + 35;
            attr.armour = randRarity(10740, 15) + 35;
            attr.health = randRarity(10450, 15) + 35;
            attr.set = true;
            return attr;

        } else if(_class == Class.Rare) {

            attr.rarity = 2;
            attr.cannon = randRarity(230, 25) + 50;
            attr.laser = randRarity(10230, 25) + 50;
            attr.bomb = randRarity(12200, 25) + 50;
            attr.shields = randRarity(10560, 25) + 50;
            attr.armour = randRarity(10740, 25) + 50;
            attr.health = randRarity(10450, 25) + 50;
            attr.set = true;
            return attr;

        } else if(_class == Class.Legendary) {

            attr.rarity = 3;
            attr.cannon = randRarity(230, 25) + 75;
            attr.laser = randRarity(10230, 25) + 75;
            attr.bomb = randRarity(12200, 25) + 75;
            attr.shields = randRarity(10560, 25) + 75;
            attr.armour = randRarity(10740, 25) + 75;
            attr.health = randRarity(10450, 25) + 75;
            attr.set = true;
            return attr;

        }

    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it might look overwhelming but it's simple logic.&lt;br&gt;
it will first select random class, like it's common, uncommon etc.&lt;br&gt;
the attributes are set according the rarity of class, common will have low attributes, and legendary will have max attributes then others.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function randomNumProb() internal view returns(Class) {
        uint rand =  uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, _seed))) % 100;
        uint[] memory _classProbabilities = new uint[](4);
        _classProbabilities[0] = 68;
        _classProbabilities[1] = 20;
        _classProbabilities[2] = 10;
        _classProbabilities[3] = 2;

         // Start at top class (length - 1)
        // skip common (0), we default to it
        for (uint i = _classProbabilities.length - 1; i &amp;gt; 0; i--) {
            uint probability = _classProbabilities[i];
            if(rand &amp;lt; probability) {
                return Class(i);
            } else {
                rand = rand - probability;
            }
        }

        return Class.Common; 
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it will return a random class rarity according to the probability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function upgradeToMatureBird(uint _tokenId) external {
        require(ownerOf(_tokenId) == msg.sender, "not owner");
        require(level[_tokenId] == 1, "not baby bird, only baby bird can be upgraded");
        IERC20 token = IERC20(_tokenAddress);
        Attributes memory attr = _tokenIdToAttributes[_tokenId];
        uint8 rar = attr.rarity;
        uint cost;
        if(rar == 0) {
            cost = commonMatureCost;
        } else  if(rar == 1) {
            cost = unCommonMatureCost;
        } else  if(rar == 2) {
            cost = rareMatureCost;
        } else  if(rar == 3) {
            cost = legendaryMatureCost;
        } 
        uint balance = token.balanceOf(msg.sender);
        require(balance &amp;gt;= cost, "low balance");
        uint256 allowance = token.allowance(msg.sender, address(this));
        require(allowance &amp;gt;= cost, "Check the token allowance");
        uint burnAmount = cost.mul(_burnPercent).div(100);
        token.transferFrom(msg.sender, address(this), cost);
        token.transfer(_burnAddress, burnAmount);
        level[_tokenId] = 2;
        _rewardTime[_tokenId] = block.timestamp;
        emit UpgradeMature(_tokenId, cost);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;birds are upgraded to &lt;code&gt;level 2&lt;/code&gt; using this function. 25% are burned, the rest are sent to the contract. the &lt;code&gt;if else statement&lt;/code&gt; checks whether the rarity is common, uncommon, rare, legendary. the cost to upgrade will depend on the rarity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function upgradeToMaxMatureBird(uint _tokenId) external {
         require(ownerOf(_tokenId) == msg.sender, "not owner");
        require(level[_tokenId] == 2, "not mature bird, only mature bird can be upgraded");
        IERC20 token = IERC20(_tokenAddress);
        Attributes memory attr = _tokenIdToAttributes[_tokenId];
        uint8 rar = attr.rarity;
        uint cost;
        if(rar == 0) {
            cost = commonMaxMatureCost;
        } else  if(rar == 1) {
            cost = unCommonMaxMatureCost;
        } else  if(rar == 2) {
            cost = rareMaxMatureCost;
        } else  if(rar == 3) {
            cost = legendaryMaxMatureCost;
        } 
        uint balance = token.balanceOf(msg.sender);
        require(balance &amp;gt;= cost, "low balance");
        uint256 allowance = token.allowance(msg.sender, address(this));
        require(allowance &amp;gt;= cost, "Check the token allowance");
        uint burnAmount = cost.mul(_burnPercent).div(100);
        uint remainingTokens = cost - burnAmount;
        token.transferFrom(msg.sender, address(this), remainingTokens);
        token.transfer(_burnAddress, burnAmount);
        level[_tokenId] = 3;
        withdrawReward(_tokenId);
        emit UpgradeMaxMature(_tokenId, cost);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;level of bird is upgraded to 3. same logic as the other &lt;code&gt;upgradeMature&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// this need to be fixed, withdrawReward function contains some bugs
    function withdrawReward(uint _tokenId) public returns(uint) {
        require(ownerOf(_tokenId) == msg.sender, "not Owner");
        require(level[_tokenId] &amp;gt; 1, "only mature and max mature bird can withdraw");
        Attributes memory attr = _tokenIdToAttributes[_tokenId];
        uint per;
        if(attr.rarity == 0) {
            if(level[_tokenId] == 2) {
                per = _commonMatureAPY;
            } else if(level[_tokenId] == 3) {
                per = _commonMaxMatureAPY;
            }
        } else if(attr.rarity == 1) {
           if(level[_tokenId] == 2) {
                per = _unCommonMatureAPY;
            } else if(level[_tokenId] == 3) {
                per = _unCommonMaxMatureAPY;
            }
        } else if(attr.rarity == 2) {
             if(level[_tokenId] == 2) {
                per = _rareMatureAPY;
            } else if(level[_tokenId] == 3) {
                per = _rareMaxMatureAPY;
            }
        } else if(attr.rarity == 3) {
            if(level[_tokenId] == 2) {
                per = _legendaryMatureAPY;
            } else if(level[_tokenId] == 3) {
                per = _legendaryMaxMatureAPY;
            }
        }
        per = per * 1000000000;
        uint perInSec = per / 31536000;
        uint bal = IERC20(_tokenAddress).balanceOf(address(this));
        bal = bal.div(1000000000);
        uint r =  bal.mul(perInSec).div(100);
        uint t = (block.timestamp).sub(_rewardTime[_tokenId]);
        r = r.mul(t);
        IERC20(_tokenAddress).transfer(msg.sender, r);
        _rewardTime[_tokenId] = block.timestamp;
        emit Reward(_tokenId, t, r);
        return r;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I am not so good in math, and for sure this function may contain bugs. so you might want to change the reward system to weeks rather seconds. the seconds wihdraw system is confusing for me. if somebody fix this, then do a PR.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
        if (_i == 0) {
            return "0";
        }
        uint j = _i;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len;
        while (_i != 0) {
            k = k-1;
            uint8 temp = (48 + uint8(_i - _i / 10 * 10));
            bytes1 b1 = bytes1(temp);
            bstr[k] = b1;
            _i /= 10;
        }
        return string(bstr);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;uint to string conversion function. Will need it during &lt;code&gt;tokenUri&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function tokenURI(uint256 tokenId) override(ERC721) public view returns (string memory) {
         if(_tokenIdToAttributes[tokenId].set == false) {
             string memory json = Base64.encode(
            bytes(string(
                abi.encodePacked(
                    '{"name": "', uint2str(tokenId), '",',
                    '"image_data": "', _eggUri, '",',
                    '"description": "', 'An Egg"',
                    '}'   
                )
            ))
        );
        return string(abi.encodePacked('data:application/json;base64,', json));
        }

         string memory uri = "";

         if(level[tokenId] == 1) {
             if(_tokenIdToAttributes[tokenId].speice == 0) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/baby-eagle-complete.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 1) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Baby%20-%20Cockatiel.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 2) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Baby%20-%20Sparrow.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 3) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Baby%20-%20Cardinal.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 4) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Baby%20-%20Vulture.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 5) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Baby%20-%20Swan.mp4";
             }
         } else if(level[tokenId] == 2 || level[tokenId] == 3) {
              if(_tokenIdToAttributes[tokenId].speice == 0) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Adult%20-%20Golden%20Eagle.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 1) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Adult%20-%20Cockateil.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 2) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Adult%20-%20Sparrow.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 3) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Adult%20-%20Cardinal.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 4) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Adult%20-%20Vulture.mp4";
             } else if(_tokenIdToAttributes[tokenId].speice == 5) {
                 uri = "https://gateway.pinata.cloud/ipfs/QmVWCtAxaRVktazv4JddXMhMZYAUNRWrvZoDGQhmuy64Hp/Adult%20-%20Swan.mp4";
             }
         }

         string memory json = Base64.encode(
            bytes(string(
                abi.encodePacked(
                    '{"name": "', uint2str(tokenId), '",',
                    '"image_data": "', uri, '",',
                    // '"description": "', 'Bird"', ',',
                    '"attributes": [{"trait_type": "Cannon", "value": "', uint2str(_tokenIdToAttributes[tokenId].cannon), '"},',
                    '{"trait_type": "Attribute", "value": "', _tokenIdToAttributes[tokenId].uniqueAttribute, '"},',
                    '{"trait_type": "Laser", "value": "', uint2str(_tokenIdToAttributes[tokenId].laser), '"},',
                    '{"trait_type": "Bomb", "value": "', uint2str(_tokenIdToAttributes[tokenId].bomb), '"},',
                    '{"trait_type": "Shields", "value": "', uint2str(_tokenIdToAttributes[tokenId].shields), '"},',
                    '{"trait_type": "Armour", "value": "', uint2str(_tokenIdToAttributes[tokenId].armour), '"},',
                    '{"trait_type": "Health", "value": "', uint2str(_tokenIdToAttributes[tokenId].health), '"}',
                    ']}'

                )
            ))
        );
        return string(abi.encodePacked('data:application/json;base64,', json));
     }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this function just sets the attributes to json format, standard for the erc721 tokeUri. nothing completed here, only conditions are used to correctly display the egg or bird according to the level.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/masudrehman/NEST-NFT-Reward"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>blockchain</category>
      <category>web3</category>
      <category>nft</category>
    </item>
  </channel>
</rss>
