A basic Nft – ERC721

Everyone loves an NFT and they all want one, so that’s why I’m using this contract type for my example. An ERC721 Nft token ( Non-Fungible-Token) needs a token id. You can’t live without that. We’ll just use the Counter class supplied to us by the awesome people at Open Zeppelin.

Import the library and declare our counter with the ‘using’ keyword so everytime we call Counter we’re actually using Counters.Counter — we’re using the struct defined in the Counter library.

I changed the name of the uri member variable to baseUri because this will be the base uri of every single NFT minted with the contract and the token id will be concatenated to the end of that uri ( i.e. https://some-place.xyz/tokens/<token id>).

We also used the Ownable contract but we’ll get into that later.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;

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

contract BasicERC721 is ERC721, Ownable {
    
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIds;

    string private baseUri;

    constructor(
        string memory _name,
        string memory _symbol,
        string memory _uri) 
        ERC721(_name,_symbol) {
            baseUri = _uri;
        }

    function mint(address to) public virtual {
        _mint(to, _tokenIds.current());
        _tokenIds.increment();
    }
    
}

Next we add the mint function so we can start incrementing our token count. The mint function that we’re inheriting from in the OZ ERC721 contract has used the ‘virtual override’ keywords which enables us to use the ‘virtual’ designation and override the functionality ; write our own mint function.

repo branch : https://github.com/bws9000/smart-contract-tests/tree/upgrade-erc721-token

Previous Article

By Burt Snyder

Writing about interesting things and sharing ideas.

1 comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.