In this tutorial, we'll learn how to create and mint our own Non-Fungible Tokens (NFTs) on the Ethereum blockchain. NFTs are unique digital assets that are stored on a blockchain. They have gained popularity in various fields, including arts, gaming, and real estate.
By the end of this tutorial, you will be able to create your very own NFT and mint it to the Ethereum blockchain.
Prerequisites:
- Basic understanding of Ethereum blockchain
- Basic knowledge of Solidity (the language used to write smart contracts on Ethereum)
- Metamask wallet setup
- Node.js installed on your system
We will be using the Ethereum's ERC721 standard for creating our NFT. The ERC721 standard has a set of predefined functions which we will use to create our own NFT.
First, install the necessary packages. Open your terminal and type:
npm install -g truffle
mkdir NFT
cd NFT
truffle unbox metacoin
npm install @openzeppelin/contracts
Now, let's write our smart contract. In the contracts directory, create a new file MyNFT.sol
and write the following code:
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/token/ERC721/ERC721Full.sol";
import "@openzeppelin/contracts/drafts/Counters.sol";
contract MyNFT is ERC721Full {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721Full("MyNFT", "MNFT") public {
}
function mintNFT(address recipient, string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}
This code creates a new NFT with a unique ID and a URI pointing to its metadata.
Next, we need to deploy our contract to the Ethereum network. For this tutorial, we will deploy it to the local network.
Create a new file in the migrations directory, 2_deploy_contract.js
, and write:
const MyNFT = artifacts.require("MyNFT");
module.exports = function(deployer) {
deployer.deploy(MyNFT);
};
Now open your terminal and type:
truffle develop
migrate --reset
Now that our contract is deployed, let's mint our first NFT!
MyNFT.deployed().then(function(instance) { return instance.mintNFT("0x...", "https://my-nft.com/1") });
Replace "0x..." with your Metamask address and "https://my-nft.com/1" with your own metadata URI.
Let's break down our smart contract code:
...
contract MyNFT is ERC721Full {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721Full("MyNFT", "MNFT") public {
}
...
Here, we declare our contract MyNFT
which inherits from ERC721Full
, a standard implementation for an ERC721 token. We are using the Counters
library to handle unique token IDs.
...
function mintNFT(address recipient, string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
...
This function allows us to mint a new NFT. It increments the token ID, mints the new token, and sets the token's URI.
This tutorial covered the basics of creating and minting NFTs on the Ethereum blockchain. We learned how to write a smart contract using the ERC721 standard, how to deploy it, and how to interact with it to mint NFTs.
As a next step, you can explore different metadata standards for NFTs, how to sell NFTs on a marketplace, or how to create NFTs with more complex properties.
Modify the smart contract to add a function that returns the total number of NFTs minted.
Create a new NFT with a unique property, like a color or a name.
Create and deploy a smart contract that mints a series of NFTs with consecutive IDs.
Remember, the best way to learn is by doing. Keep practicing and exploring new concepts!