DEV Community

Cover image for Building an ERC721 NFT Contract with Solidity
Sumana
Sumana

Posted on

Building an ERC721 NFT Contract with Solidity

Understanding the Basics of an NFT Contract

Non-Fungible Tokens (NFTs) are unique digital assets that represent ownership of a specific item, like digital art, music, or even virtual real estate. In this blog, I'll walk you through an ERC721 NFT contract built using Solidity and OpenZeppelin libraries.

What is ERC721?

ERC721 is the standard for creating NFTs on the Ethereum blockchain. Unlike fungible tokens (e.g., ERC20), which are identical and interchangeable, ERC721 tokens are unique and non-interchangeable, making them perfect for representing distinct assets.


Breakdown of the Contract

Let's dive into the code to see how this NFT contract works.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6;

// Importing the necessary libraries and ERC721 standards
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract NFT is ERC721URIStorage {
    using Counters for Counters.Counter;  // Using the Counters library for tracking token IDs
    Counters.Counter private _tokenIds;   // Counter to keep track of unique token IDs
    address contractAddress;  // Address of the marketplace contract

    // Constructor to set the marketplace address and name our NFT collection
    constructor(address marketplaceAddress) ERC721("Metaverse Tokens","METT") {
        contractAddress = marketplaceAddress;
    }

    // Function to mint a new NFT
    function createToken(string memory tokenURI) public returns (uint) {
        _tokenIds.increment();  // Increment the token ID counter
        uint256 newItemId = _tokenIds.current();  // Get the current token ID

        // Mint the new NFT to the caller's address (msg.sender)
        _mint(msg.sender, newItemId);

        // Set the token's metadata URI, pointing to details like image or description
        _setTokenURI(newItemId, tokenURI);

        // Allow the marketplace to manage the token on behalf of the owner
        setApprovalForAll(contractAddress, true);

        return newItemId;  // Return the newly minted token's ID
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Concepts in the Code

  1. Inheriting OpenZeppelin Libraries:
    We're using OpenZeppelin's ERC721 and ERC721URIStorage libraries to ensure our contract adheres to the ERC721 standard while also storing token URIs (metadata).

  2. Counters Library:
    Solidity doesn't support auto-incrementing IDs natively, so we use the Counters library to manage our NFT IDs.

  3. Minting NFTs:
    The createToken() function handles the minting process. It assigns a new token ID, mints the token, and sets the URI, which links to the token’s metadata (such as an image or description).

  4. Granting Marketplace Permissions:
    After minting, the contract gives the NFT marketplace permission to transfer the NFT on behalf of the owner using the setApprovalForAll() function.


How It Works in Practice

  • When you call createToken(), a new NFT is minted with a unique ID.
  • The metadata (token URI) associated with the NFT can be anything, typically pointing to an image or data on IPFS.
  • The marketplace (identified by contractAddress) gets the authority to facilitate transactions of these tokens between users.

This contract can be used to mint and manage NFTs, which can then be bought, sold, or traded on a marketplace platform. It’s a simple yet powerful foundation for any NFT-based project!

Top comments (0)