My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 8 Jul 2021
Create NFT tokens with OpenZeppelin
Create ERC721 tokens without coing much smart contract
img
Ganesh Deshpande
0
Like
1827

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 !

Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11494
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
8068
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6215
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5533
3