Solidity – Programming ERC721 Token

·

Creating non-fungible tokens (NFTs) has become a cornerstone of blockchain innovation, especially on the Ethereum network. At the heart of this revolution lies the ERC721 standard, a powerful and widely adopted protocol for building unique digital assets. Whether you're a developer exploring decentralized applications or an enthusiast diving into NFT creation, understanding how to program an ERC721 token using Solidity is essential.

This guide walks you through the structure, core functions, and step-by-step deployment of an ERC721 smart contract — all while maintaining code clarity and real-world applicability.


What Is ERC721?

ERC721 stands for Ethereum Request for Comments, with 721 being the proposal identifier. It defines a standard interface for Non-Fungible Tokens (NFTs) on the Ethereum blockchain. Unlike fungible tokens such as ETH or ERC20 tokens, each ERC721 token is unique, indivisible, and not interchangeable. This makes them ideal for representing ownership of digital art, collectibles, virtual real estate, and more.

The standard ensures compatibility across wallets, marketplaces, and platforms by defining a common set of rules and functions that every compliant contract must implement.


Core Functions of ERC721

An ERC721-compliant smart contract includes several key functions divided into categories: general token information, ownership management, and metadata handling.

Basic Token Information (ERC-20 Like)

These functions are familiar to those who have worked with ERC20 tokens:

👉 Learn how to build your first NFT collection with step-by-step guidance.

Ownership Management

These functions handle transfer permissions and ownership tracking:

Additionally:


Metadata Function

One of the most important aspects of an NFT is its metadata, which describes its attributes (like image URL, name, properties, etc.).

For example:

{
  "name": "Crypto Art #1",
  "description": "A digital masterpiece",
  "image": "https://ipfs.io/ipfs/QmXy...",
  "attributes": [ ... ]
}

This function enables platforms like OpenSea to display rich content linked to your NFT.


Events in ERC721

Smart contracts emit events to notify external applications about state changes. The two primary events in ERC721 are:

These events allow dApps and indexers to track ownership and activity in real time.


How to Create and Deploy an ERC721 Token

Let’s walk through creating a simple NFT smart contract using Solidity and deploying it via Remix IDE on the Ropsten testnet (now deprecated; we’ll use Goerli as a modern alternative).

Prerequisites


Step-by-Step Implementation

Step 1: Specify License Identifier

// SPDX-License-Identifier: MIT

This line declares the open-source license used for transparency and compliance.

Step 2: Define Solidity Version

pragma solidity ^0.8.0;

Using Solidity 0.8.x ensures built-in overflow protection and improved security.

Step 3: Import Standard Libraries

Instead of raw imports from GitHub, use OpenZeppelin’s trusted libraries:

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

These provide secure implementations of ERC721 with metadata support and ownership controls.

Step 4: Define Your Contract

contract MyNFT is ERC721URIStorage, Ownable {
    constructor() ERC721("GeeksNFT", "GFG") Ownable(msg.sender) {}

This initializes the NFT with a name ("GeeksNFT") and symbol ("GFG"), while assigning ownership to the deployer.

Step 5: Implement Minting Function

function mintNFT(address recipient, uint256 tokenId, string memory tokenURI) public onlyOwner {
    _mint(recipient, tokenId);
    _setTokenURI(tokenId, tokenURI);
}

This function allows the owner to mint new tokens and assign metadata via URI.

👉 Start minting your own NFTs with tools designed for developers and creators.


Full Smart Contract Code

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

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyNFT is ERC721URIStorage, Ownable {
    constructor() ERC721("GeeksNFT", "GFG") Ownable(msg.sender) {}

    function mintNFT(address recipient, uint256 tokenId, string memory tokenURI) public onlyOwner {
        _mint(recipient, tokenId);
        _setTokenURI(tokenId, tokenURI);
    }
}

Deployment Steps

  1. Open Remix IDE
  2. Create a new file (MyNFT.sol) and paste the code
  3. Compile using the Solidity compiler
  4. Switch environment to Injected Web3 (connects MetaMask)
  5. Deploy on Goerli testnet
  6. Interact with functions: call mintNFT with recipient address, token ID, and metadata URI

After deployment, verify the transaction on Etherscan and view your NFT on marketplaces like OpenSea (testnet mode).


Frequently Asked Questions (FAQ)

Q: Can ERC721 tokens be split or divided?
A: No. ERC721 tokens are non-fungible and indivisible — each represents a single unique asset.

Q: What’s the difference between ERC721 and ERC1155?
A: ERC721 supports only one unique token per ID, while ERC1155 allows both fungible and non-fungible tokens in a single contract.

Q: Where should I store NFT metadata?
A: Use decentralized storage like IPFS or Arweave to ensure permanence and avoid centralization risks.

Q: Do I need real ETH to deploy an ERC721 contract?
A: Not for testing. Use testnets like Goerli with free test ETH from faucets before deploying on mainnet.

Q: Can someone else mint my NFTs after deployment?
A: Only if you allow it. Using onlyOwner restricts minting to the contract owner unless explicitly changed.

Q: How do I make my NFT tradable on marketplaces?
A: Ensure your contract follows ERC721 standards and includes proper tokenURI metadata — platforms like OpenSea automatically detect compliant contracts.


Final Thoughts

Programming an ERC721 token in Solidity opens doors to innovative digital ownership models. With standardized interfaces, robust tooling from OpenZeppelin, and accessible IDEs like Remix, creating NFTs is now within reach for developers at all levels.

Whether you're launching a digital art project or building a game with rare in-game items, mastering ERC721 is your first step toward blockchain-powered uniqueness.

👉 Explore advanced NFT development tools and resources to accelerate your journey.