How to create and deploy NFT (Non-fungible-Token) using Solidity via Remix

How to create and deploy NFT (Non-fungible-Token) using Solidity

How to create and deploy NFT (Non-fungible-Token) using Solidity via Remix

There are two token standards which are very popular in Ethereum World, ERC20 & ERC721. ERC20 is most commonly used for ICO and many projects are implementing this token contract for crowd funding in crypto world. Where as ERC721 is getting popular in last 2 years and it is so hot at this moment. 

What is special about ERC721? 

ERC20 is fungible token, which means it is look-alike. For instance, if you have Fiat(currency) in your hand nd you can exchange that currency to buy-in good. So which means, you can equally balance the ERC20 token with some other asset and exchange the same. 

ERC721 is Non-fungible token, i.e, UNIQUE. It has unique meta-data and you cannot exchange it for same value. Let's say you have beautiful photograph and you had put so much efforts in capturing it. If you feel, this is unique and no one could have it, then that can be considreed as NFT. All you have to do is, just tag this picture with right meta-data and add this as NFT in ethereum network.  So you can own this asset for long time and you can even sell it in AUCTION for more money. Sounds interesting?, yes this is something that have more attention in business world now.

What are the pre-requisites to create ERC721?

- Solidity Programming Knowledge

- ERC721 Standards and it's functions

- http://remix.ethereum.org/ - How to use

- Metamask in your browser - It is available as chrome extension

- Truffle or Brownie for program compilation / migration.

Note: you can deploy contract from remix itself, in such case truffle / brownie is not required. ONly if you want to do it programmtically from CLI, then go with Truffle / Brownie

In this article, we will review how to handle this NFT smart contract deployment via Remix

pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract MyNFT is ERC721Enumerable, ERC721URIStorage {
    
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("SampleNFTName", "CATY") {}


    function _beforeTokenTransfer(address from, address to, uint256 tokenId)
        internal
        override(ERC721, ERC721Enumerable)
    {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    function _burn(uint256 tokenId) internal override(ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }

    function tokenURI(uint256 tokenId)
        public
        view
        override(ERC721, ERC721URIStorage)
        returns (string memory)
    {
        return super.tokenURI(tokenId);
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC721, ERC721Enumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }

    function createNFT(address owner, string memory _tokenURI)
        public
        returns (uint256)
    {
        _tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(owner, newItemId);
        _setTokenURI(newItemId, _tokenURI);
        return newItemId;
    }
    
    
}

Just copy paste the above program in remix and make sure, there is no compilation errors. Right after that, you will be able to deploy in localhost by pointing the Environment to Javascript VM

Deploy your contract(Storage.sol is my contract file name) Afrer you hit deploy button, you will see success message in log window like below.

Yay!, your contract is successfull & you will be able to see the methods in left window where you can call "createNFT" method by passing the parameters such as owner address and tokenURI - this can be a json formatted values with any image metadata.

Overall, this is simple method to create your NFT in matter of minutes, but there are many advance methods to setup fool proof NFT by including VRF from chainlink to get verified random number and so on.. So choose as per your use case!.

Thanks for taking time to read this article..

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
1