Create NFT tokens with OpenZeppelin

Create ERC721 tokens without coing much smart contract

ethereum
erc721

ganesh deshpande

@theblockmatrix
1067 3 3

What is OpenZeppelin

OpenZeppelin is open source smart contracts community-diven library to build secure smart contract very efficient and in effective way.

 

Now to develop NFT tokens we can use their predefined smart contract to reduce our coding complexity

ERC721 Contract


pragma solidity ^0.8.0;

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

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

    constructor() ERC721("Mynft", "ABD") {}

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

        uint256 newid = _tokenIds.current();
        _mint(owner, newid);
        _setTokenURI(newid, tokenURI);

        return newid;
    }
}

 

Explanation

Step 1:

declare solidity version and import libraries

pragma solidity ^0.8.0;

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

 

ERC721 - Main NFT tokens contract

ERC721URIStorage - used to store the url for token Id

Counter - To increment the teoken ID safely 

 

Generally tokenId is used with counter but you can customixe it according to your own need. for example you can use cahinlink randomness , generate random number in chainlink and use it as token id .

 

Step 2:

Declare contract and constructor

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

    constructor() ERC721("Mynft", "ABD") {}

this will be our NFT smart contract which will hold all the tokens generated in it.

 

Step 3:

Generate new token 

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

        uint256 newid = _tokenIds.current();
        _mint(owner, newid);
        _setTokenURI(newid, tokenURI);

        return newid;
    }

generate nft , this function accepts 2 arguments , the address which will be the owner of this newly generate token and token url this will point to the json file which describes this token

If you don't want to use tokenURL you can skip including the file in step 1 , also delete the _setTokenURI function called in CreateNFT, and also we don't need to take that tokenURI argument in CreateNFT , the customized contarct will look like this

pragma solidity ^0.8.0;

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

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

    constructor() ERC721("Mynft", "ABD") {}

    function CreateNFT(address owner)
        public
        returns (uint256)
    {
        _tokenIds.increment();

        uint256 newid = _tokenIds.current();
        _mint(owner, newid);

        return newid;
    }
}

Happy NFT Building !


More SmartBooks like this