TEMPLATES

ERC-721 NFT

Non-fungible token standard for unique digital assets.

OVERVIEW

ERC-721 defines a standard for non-fungible tokens where each token is unique. Common use cases include digital art, collectibles, gaming items, and proof of ownership.

TEMPLATE FEATURES

Auto-incrementing IDs

Token IDs increase automatically

URI Storage

Each token can have unique metadata

Burnable

Owners can destroy their NFTs

Ownable

Admin controls for minting

SOURCE CODE

NFT.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

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

contract MyNFT is ERC721, ERC721URIStorage, ERC721Burnable, Ownable {
    uint256 private _nextTokenId;

    constructor(
        string memory name,
        string memory symbol
    ) ERC721(name, symbol) Ownable(msg.sender) {}

    function safeMint(address to, string memory uri) public onlyOwner {
        uint256 tokenId = _nextTokenId++;
        _safeMint(to, tokenId);
        _setTokenURI(tokenId, uri);
    }

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

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

KEY FUNCTIONS

FunctionDescription
safeMint(to, uri)Mint new NFT with metadata URI
tokenURI(tokenId)Get metadata URI for token
ownerOf(tokenId)Get owner of a token
transferFrom(from, to, tokenId)Transfer NFT ownership
burn(tokenId)Destroy an NFT

METADATA FORMAT

The token URI should point to a JSON file following this format:

metadata.json
{
  "name": "My NFT #1",
  "description": "A unique digital collectible",
  "image": "ipfs://QmHash...",
  "attributes": [
    { "trait_type": "Rarity", "value": "Legendary" },
    { "trait_type": "Power", "value": 100 }
  ]
}

NEXT STEPS