How to develop and deploy your NFT using Hardhat and Solidity? [Beginner]

By akohad Apr9,2024

[ad_1]

NFTs (Non-Fungible Tokens) are gaining popularity, and many people want to create their own unique tokens. In this guide, we will explore how to create and test NFTs on any network based on the Ethereum Virtual Machine (EVM) using the Hardhat framework for contract development and testing. So let’s start 😉

The first step is to install Hardhat and configure it. Run the following commands in your terminal:

npm install --save-dev hardhat
npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
npx hardhat

After installing Hardhat, configure the hardhat.config.js file with the network where you want to deploy your contract.

Create a new file with the extension .sol and write the contract for your NFT. Here’s an example of a simple contract:solidityCopy code

// contracts/MyNFT.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

contract MyNFT is ERC721, Ownable {
mapping(uint256 => string) private _tokenURIs;

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

function mint(address to, uint256 tokenId, string memory tokenURI) external onlyOwner {
_safeMint(to, tokenId);
_setTokenURI(tokenId, tokenURI);
}

function _setTokenURI(uint256 tokenId, string memory tokenURI) internal {
require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
_tokenURIs[tokenId] = tokenURI;
}

function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
return _tokenURIs[tokenId];
}
}

Create a new directory test and add a file with the extension .js for writing tests. Here’s an example test:

// test/MyNFT.j
const { expect } = require("chai");

describe("MyNFT", function () {
it("Should mint a new NFT", async function () {
const MyNFT = await ethers.getContractFactory("MyNFT");
const myNFT = await MyNFT.deploy();
await myNFT.deployed();

const owner = await myNFT.owner();
expect(owner).to.equal(owner);
await myNFT.mint(owner.address, 1, "https://example.com/token_metadata.json");
const balance = await myNFT.balanceOf(owner.address);
expect(balance).to.equal(1);
});
});

npx hardhat test

This will run your test, which will verify the functionality of your NFT contract.

Step 5: Deploying the Contract: To deploy your contract on the desired network, use the command:

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

Where YOUR_NETWORK is the target network for deploying your contract.

Why Use Token URI: tokenURI is the metadata URL for each NFT. It contains information about the token, such as name, description, image, and other details. You can create and use metadata for your NFTs by storing them in JSON format or any other data format. Typically, metadata is stored on IPFS or any other distributed data storage to ensure availability and immutability. After creating metadata, you can use its URL in the mint function to create NFTs with the corresponding metadata.

Now you have all the necessary tools and knowledge to create and test your own NFT on any EVM-based network using Hardhat. I hope this guide will assist you in this development process!

LinkedIn | X | BuyMeACoffe



[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *