Estimated time to complete this guide: ~15 minutes
Even if you've been living under a rock, you'll have noticed that every major news outlet has been profiling the rise of NFTs. With NFTs bringing blockchain into the public eye, now is an excellent opportunity to understand the hype yourself by publishing your own NFT (ERC-721 Token) on the Ethereum blockchain!
In this tutorial, we will walk through creating and deploying an ERC-721 smart contract on the Sepolia test network using Metamask, Solidity, Hardhat, Pinata, and Alchemy. In Part II of this tutorial, we'll cover how to mint an NFT from code, and in Part III we'll explain how to view your NFT in your mobile wallet.
Creating an NFT
Prerequisites
Before you begin the steps in this tutorial, ensure you complete the following steps:
Install both Node.js (> 14) and npm on your local machine. To check your Node version, run the following command in your terminal:
node -v
Step 1: Create an Alchemy App
To create an Alchemy app:
- From Alchemy's dashboard, hover over the Apps drop-down menu and choose Create App.
- Provide a Name and Description for your app.
- For Chain, select Ethereum and for Network select Sepolia.
- Click the Create App button.
Once you have created your app, click on your app's View Key button in the dashboard and save the API KEY. We will use this later.
Step 2: Create a Metamask Wallet
We need an Ethereum wallet to send and receive transactions. For this tutorial, we’ll use Metamask, a virtual wallet in the browser.
You can download and create a Metamask account for free. Once you have an account, make sure to switch to the "Sepolia Test Network" in the upper right (so that we're not dealing with real money).
Step 3: Add SepoliaETH from a Faucet
In order to deploy our smart contract to the test network, we’ll need some fake SepoliaETH. The easiest way to acquire this is by using Alchemy's Sepolia faucet.
If all goes well, you should see your SepoliaETH balance updated on Metamask.
Step 4: Create a Node Project
Let’s create an empty node project. Navigate to your command line and type:
mkdir my-nft-project
cd my-nft-project
npm init -yWe are now in a good position to set up and install Hardhat, the industry standard Ethereum development environment.
Step 5: Create a Hardhat Project
Hardhat is a development environment to compile, deploy, test, and debug smart contracts. It helps developers create dApps locally before deploying them to a live chain.
In your terminal, run the following commands:
npm install --save-dev hardhat
npx hardhatSelect Create a JavaScript project and agree to all the defaults.
To check if everything works properly, run:
npx hardhat testWe now have our Hardhat development environment successfully configured. Let us now install the OpenZeppelin contracts package. This will give us access to ERC721 implementations (the standard for NFTs) on top of which we will build our contract.
Step 6: Install dotenv
Install the dotenv package to manage environment variables:
npm install dotenv --saveStep 7: Uninstall the current ethers library
Uninstall any existing ethers library to avoid conflicts:
npm uninstall ethersStep 8: Install ethers version 5 and Hardhat Ethers plugin
Install ethers version 5 along with the correct Hardhat Ethers plugin:
npm install ethers@^5.0.0 @nomicfoundation/hardhat-ethers@^3.0.0 --save-devStep 9: Write the Smart Contract
Open the project in your favorite editor. Navigate to the contracts folder and create a new file called MyNFT.sol. Add the following code to the file:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721URIStorage, Ownable {
uint256 private _currentTokenId;
constructor() ERC721("MyNFT", "NFT") {}
function mintNFT(address recipient, string memory tokenURI)
public
onlyOwner
returns (uint256)
{
_currentTokenId++;
uint256 newItemId = _currentTokenId;
_safeMint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}Make sure that the Solidity version defined above (^0.8.20) is the same as the version defined in the hardhat.config.js file.
Step 10: Connect Metamask & Alchemy to Your Project
Every transaction sent from your virtual wallet requires a signature using your unique private key. To provide our program with this permission, we can safely store our private key (and Alchemy API key) in an environment file.
Create a .env file in the root directory of our project, and add your Metamask private key and HTTP Alchemy API Key (from Step 1) to it.
Your .env should look like this:
API_URL="https://eth-sepolia.g.alchemy.com/v2/your-api-key"
PRIVATE_KEY="your-metamask-private-key"Step 11: Update hardhat.config.js
We've added several dependencies and plugins so far, now we need to update hardhat.config.js so that our project knows about all of them.
Replace the contents of hardhat.config.js with the following:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
solidity: "0.8.20",
defaultNetwork: "sepolia",
networks: {
sepolia: {
url: API_URL,
accounts: [PRIVATE_KEY]
}
}
};Step 12: Write the Deployment Script
Create a directory called scripts/ at the root of the project and add a deploy.js file to it with the following:
const { ethers } = require("hardhat");
async function main() {
const MyNFT = await ethers.getContractFactory("MyNFT");
const myNFT = await MyNFT.deploy();
await myNFT.deployed();
console.log("Contract deployed to address:", myNFT.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});Step 13: Deploy the Contract
We're finally ready to deploy our smart contract! Navigate back to the root of your project directory, and in the command line run:
npx hardhat run scripts/deploy.js --network sepoliaYou should then see something like:
Contract deployed to address: 0x1234567890123456789012345678901234567890If we go to Sepolia Etherscan and search for our contract address we should be able to see that it has been deployed successfully.
FAQ
What is an NFT?
An NFT (Non-Fungible Token) is a unique digital asset stored on a blockchain that represents ownership of a specific item or piece of content.
Why use the Sepolia testnet?
The Sepolia testnet allows developers to test their smart contracts without using real ETH or risking real funds.
How much does it cost to deploy an NFT smart contract?
Deploying to a testnet like Sepolia is free (you use test ETH), but deploying to mainnet requires gas fees paid in real ETH.
👉 Learn more about Ethereum development
That’s all for Part I of this tutorial. In Part II, we’ll actually interact with our smart contract by minting an NFT, and in Part III we’ll explain how to view your NFT in Metamask! 🤑