solidityethereumnftintermediate

ERC-721 NFT Contract

A simple non-fungible token (NFT) contract with minting functionality.

All snippets

Code

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

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

contract SimpleNFT is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("SimpleNFT", "SNFT") {}

    function mint(address recipient) public returns (uint256) {
        _tokenIds.increment();
        uint256 newTokenId = _tokenIds.current();
        _mint(recipient, newTokenId);
        return newTokenId;
    }
}

Explanation

This contract creates a basic NFT collection.

How it works:

  • Uses a Counters library to auto-increment token IDs.
  • Each mint() call creates a new unique token assigned to recipient.
  • Inherits all ERC-721 standard behavior from OpenZeppelin.

In production, you'd add token URI management (metadata), mint price, and supply limits.

ERC-721NFTopenzeppelinminting