How to Create your Own ERC-721 Token that runs on Local Testnet
Published: 2021-10-13 13:20:33

In a very simple term ERC721 token is one of the token standard used in NFT space which can be found on the Ethereum blockchain. 

Blockchain space as a whole is a fast-moving space with new token standards proposed all the time. New [ERC-4337] (https://medium.com/infinitism/erc-4337-account-abstraction without-ethereum-protocol-changes-d75c9d94dc4a) update proposal has been announced recently by Vitalik Buterin. The Ethereum developer has discussed the new features that will be introduced with the update and the possibilities that the new ERC brings to the network.

In this article, you will learn how to create erc721 token.

•	Setting up Development Environment 
•	Use OpenZeppelin Contracts
•	Creating the ERC721 Token
•	Adding Migrations to Truffle
•	Deploying the Token to the Truffle Development Network
•	Mint Token
•	Transfer Token from one Another to Another Account


##### Setting up Development Environment

There are just three basic steps for creating any smart contract:
Write, Compile and Deploy.

We have multiple options to choose from for the environment. The most popular ones are Truffle and HardHat. They both help with writing, compiling, and deploying our smart contracts to the blockchain. We are going to be using Truffle since it is very easy to get started with. 

#####Prerequisites

NodeJS + npm installed. I am using [v14.17.6 LTS](put-link-here).

Let's setup Truffle and import the OpenZeppelin Token Library. We are using [Truffle 5.3.7](put-link-here) and [OpenZeppelin 4.1.0](put-link-here) for this project.

For our token, we will be using Truffle and the OpenZeppelin Contracts. The first step is therefore to initialize a new Truffle Project and add in the OpenZeppelin contracts.

If you haven't installed truffle globally yet, then install truffle first that is very important before you can move forward:

      npm install -g truffle
 
Then create a new folder, open VSCode (Terminal), initialise a new Truffle project and install OpenZeppelin Contracts:

      truffle init npm 

      install @openzeppelin/contracts 

#####use OpenZeppelin Contracts

From the [OpenZeppelin Github Page](https://github.com/OpenZeppelin/openzeppelin-contracts): A library for secure smart contract development. Build on a solid foundation of community-vetted code.

They come with a preset, so we can easily do an ERC721 Contract that is mintable, pausable, burnable and supports all functions we possibly need to be ERC721 compliant.

We are going to using this for our implementation [https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol)


#####Creating the ERC721 Token

Create a new file in /contracts/ FemiToken.sol with the following content:


      // SPDX-License-Identifier: MIT
       pragma solidity 0.8.3;

      import "@openzeppelin/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol";


      contract FemiToken is ERC721PresetMinterPauserAutoId {

    constructor() ERC721PresetMinterPauserAutoId("FemiToken", "AIS", "https://femi.art/metadata/") {}

      function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
       return string(abi.encodePacked(super.tokenURI(tokenId),".json"));
        }
      }

 This is a normal ERC721 Contract based on the preset that OpenZeppelin gives us. If we open the [ERC721PresetMinterPauserAutoId](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol) Contract then you see that it actually does a few things in the constructor: 

1.	It sets up a new ERC721 Token Contract with the name and the symbol, in this case "FemiToken" and "AIS".

2.	It sets the tokenURI. 

3.	It will grant the deployer pausing rights and minting rights.

4.	Additionally, and completely optional, but we decided to add ".json" to the url for the metadata file automatically, hence modified the tokenURI function.


#####Adding Migrations to Truffle

Create a migrations file to migrations/2_token_migration.js with the following this content.



      const FemiToken = artifacts.require("FemiToken");

      module.exports = function (deployer) {
 
           deployer.deploy(FemiToken);

      };


Next: In the truffle-config.js file, edit the compiler settings from 0.5.1 to 0.8.3 as you can see on the screen:



      compilers: {
    solc: {

      version: "0.8.3", 
      // docker: true,
      // settings: {          
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
      }
    },



#####Deploying the Token to the Truffle Development Network.

Then simply run the truffle development console

      truffle development

This should open the Truffle Development console, just like what we have below:
  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1c3q6ezrhgfpq12usm12.PNG)



Now lets just migrate the token and then mint one token:

      migrate

will deploy the tokens to this test-chain.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxwvo510rgxd3ptcgdb2.PNG)

Awesome! Our smart contract has just be migrated, we can play around with the smart contract by sending token from one account to another, sounds interesting right.

Now let get our token instance using: 

      let token = await FemiToken.deployed();
  
These will give us the instance of FemiToken, which will display the whole token contract, include all the functions that our token has.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vq3s940ooco10k0binn7.PNG)


Mint token

      token.mint(accounts[0]);  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihl4whypyqqc8rxmaf31.PNG)

From this image as you can see the status is true, that means our transaction went through, that is cool right



#####Transfer token from one another to another account
Transfer from first account to the second account

      token.transferFrom(accounts[0], accounts[1], 0);  

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g5mu0i98wec97xfc635.PNG)

####Congratulations! 

You have just created your own ERC-721 Token. I hope this was a quick guide to get you up to speed on token development in the NFT space.  The new [ERC-4337](https://medium.com/infinitism/erc-4337-account-abstraction-without-ethereum-protocol-changes-d75c9d94dc4a) update proposal has been announced recently by Vitalik Buterin. I will want you to read through it if have not done that click on the link above. Change is coming let change it together.

Learning to learn, 100% ready to learn, code for real impact in blockchain space

Watch-out for the next upcoming article.