How to Create an NFT

ยท

This tutorial will guide you through writing and deploying an ERC-721 (Non-Fungible Token) smart contract on the Ethereum blockchain using tools like Hardhat, Alchemy, and Metamask. By the end, you'll understand how to mint your own NFT without needing prior blockchain development experience.

Key Tools and Technologies

Step-by-Step Guide

1. Setting Up Your Development Environment

  1. Create an Alchemy Account
    Sign up for a free account at Alchemy to access Ethereum node infrastructure without running your own node.
  2. Install Metamask
    Download the browser extension and create an account. Switch to the Ropsten Test Network for this tutorial.
  3. Get Test ETH
    Use the Ropsten faucet to obtain fake ETH for testing.

2. Initializing Your Project

mkdir my-nft
cd my-nft
npm init -y
npm install --save-dev hardhat
npx hardhat

Select "Create an empty hardhat.config.js" when prompted.

3. Project Structure

Organize your folders:

mkdir contracts scripts

4. Writing the Smart Contract

Create contracts/MyNFT.sol with this ERC-721 implementation:

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

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

contract MyNFT is ERC721, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("MyNFT", "NFT") {}

    function mintNFT(address recipient, string memory tokenURI)
        public onlyOwner
        returns (uint256)
    {
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        _mint(recipient, newItemId);
        _setTokenURI(newItemId, tokenURI);
        return newItemId;
    }
}

5. Configuring the Project

Install required dependencies:

npm install @openzeppelin/contracts @nomiclabs/hardhat-ethers ethers dotenv

Update hardhat.config.js:

require('dotenv').config();
require("@nomiclabs/hardhat-ethers");

module.exports = {
  solidity: "0.7.3",
  networks: {
    ropsten: {
      url: process.env.API_URL,
      accounts: [process.env.PRIVATE_KEY]
    }
  }
};

6. Deploying the Contract

Create scripts/deploy.js:

async function main() {
  const MyNFT = await ethers.getContractFactory("MyNFT");
  const myNFT = await MyNFT.deploy();
  console.log("Contract deployed to:", myNFT.address);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

Run deployment:

npx hardhat run scripts/deploy.js --network ropsten

FAQ Section

What is an ERC-721 token?

ERC-721 is the Ethereum standard for non-fungible tokens (NFTs), allowing creation of unique digital assets with individual properties.

Why use the Ropsten test network?

Ropsten provides a safe environment to test smart contracts without spending real ETH on gas fees.

How do I view my deployed contract?

Check your contract address on Ropsten Etherscan. ๐Ÿ‘‰ Learn more about blockchain explorers

What's next after deployment?

In Part II, we'll cover minting NFTs with your contract, and Part III will explain how to view NFTs in Metamask.

Key Takeaways

๐Ÿ‘‰ Explore advanced NFT development techniques to enhance your project further.