Mint Your Own NFT Using Solidity and HardHat on Avalanche FUJI Test Network | by Yoorbit | May, 2022

Deployed our smart contracts on Avalanche Fuji

Photo by Milad Fakurian on Unsplash

In this article, you’ll learn how to create your own NFT (ERC-721 standard), and deploy it on the Avalanche FUJI C-chain testnet.

  • What are NFTs?
  • What is the ERC-721 token?
  • The ERC-721 standard
  • Creating an ERC-721 Token contract
  • Deploying Contract
  • Minting NFT on Avalanche FUJI testnet
  • Add NFT to MetaMask wallet

NFTs, which stands for Non-Fungible Tokens, are the crypto-equivalent of digital collectibles like CryptoKitties. They are usually regulated by smart contracts on top of Ethereum for security reasons. They are bought and sold using different cryptocurrencies and traded on different exchanges. NFTs may often be encoded with the same underlying code as many cryptocurrencies, although not always.

For example, CryptoKitties uses Ethereum whereas RarePepe is based on Counterparty. But all NFTs share a wide variety of features in common with cryptocurrencies. For example, all have some form of limited supply and are ‘mined’ in order to be created. Because NFTs take on so many aspects of cryptos, they are typically traded on many of the same exchanges as cryptos which makes them fairly accessible on the whole.

The ERC-721 introduces a standard for NFT, in other words, this type of Token is unique and can have different value than another Token from the same Smart Contract.

All NFTs have a uint256 variable called tokenIdso for any ERC-721 Contract, the pair contract address, uint256 tokenId must be globally unique. That said, a dApp can have a “converter” that uses the tokenId as input and outputs an image of something cool, like zombies, weapons, skills or amazing kitties!

The ERC-721 (Ethereum Request for Comments 721), proposed by William Entriken, Dieter Shirley, Jacob Evans, Nastassia Sachs in January 2018, is a Non-Fungible Token Standard that implements an API for tokens within Smart Contracts.

It provides functionalities like transferring tokens from one account to another, getting the current token balance of an account, getting the owner of a specific token, and also the total supply of the token available on the network. Besides these it also has some other functionalities like approving that an amount of token from an account can be moved by a third party account.

If a smart contracts the following methods and events it can be called an ERC-721 Non-Fungible Token Contract and, once soon, it will be responsible to keep track of the created tokens on Ethereum.

ERC-721 Methods
ERC-721 Events

Step 1: Project Setup

Okay, let’s move on to the next step. We’ll need to cd into the directory that we want to work on and run the following commands:

mkdir butterfly_token
cd butterfly_token
npm install --save-dev hardhat

Now, we should have a hardhat. Let’s get a sample project going by running the command below:

npx hardhat init

We’ll go with the option of creating a sample project. Accept all requests.

After that, we need to install a couple of dependencies. We can install these dependencies using this command:

npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts
  • @nomiclabs/hardhat-waffle We can use this plugin to build smart contract tests using Waffle in Hardhat, taking advantage of both.
  • ethereum-waffle The most advanced framework for testing smart contracts.
  • chai Chai is an assertion librarysimilar to Node’s built-in assert. It makes testing much easier by giving you lots of assertions you can run against your code.
  • @nomiclabs/hardhat-ethers This plugin brings to Hardhat the Ethereum library ethers.jswhich allows you to interact with the Ethereum blockchain in a simple way.
  • ethers A complete Ethereum wallet implementation and utilities in JavaScript (and TypeScript).
  • @openzeppelin/contracts A library for securing smart contract development. Build on a solid foundation of community-vetted code.

To make sure everything is working, run:

npx hardhat test

We will see a passed test result in our console.

Step 2: Writing Smart Contract

Next, under the contracts directory, we’ll create a file called ButterflyToken.sol. We can write an ERC-721 Token contract by inheriting OpenZepplin contracts.

_safeMint

This function takes an address and a token id as arguments. We are passing msg.sender and tokenCounter as the address and token id respectively.

  • msg.sender is the wallet calling the mint function
  • tokenCounter is the current token.

Once called, this function will safely mint a token and transfer it to the wallet calling the mint function.

_setTokenURI

We can set the metadata to token through this function. This Metadata can be — a name, description, and even an image. It takes in a token id and uri as arguments.

We’ll discuss how to get a tokenURI soon. Basically, it’s just a url that sends back a JSON object.

Sample NFT Metadata as JSON

We can compile contract writing below command:

npx hardhat compile

After compile process is finished, we are going to see a new folder named artifacts. In this folder, we have compiled ABI files.

Deploying Contract

HardHat has a sample deploy script in default. You can see that script under scripts/ folder. Now, we are going to write a script like that.

First, create a JavaScript file called deploy_butterfly_token.js and paste below code

We need to make one more configuration before deploying the contract.

Open hardhat.config.jsfile and change that with the below code:

On line 2 you need to write your own private key.

For getting a private key you need to have the MetaMask extension on your browser. Check this tutorial if you don’t have MetaMask.

You can get your private key following these steps.

Before deploying your contract you need to add some AVAX to your wallet. You can do this for free on every Test Network. For FUJI Testnet you can use this site.

Paste your Wallet address and Request 10 AVAX.

After this you can deploy your contract by writing the below code:

npx hardhat run scripts/deploy_butterfly_token.js --network avalanche_fuji

After the deploy process finishes, you are going to see config.js file created on your filesystem. This is your token address. If you want to check you can use SnowTrace. Copy that address and paste it to the search bar. You are going to information about your contract.

Minting NFT on Avalanche FUJI testnet

After the deploy process finish now time to mint NFT. Before start writing code you need to add one more dependency. Copy the below command and hit enter.

npm install axios

We are going to use this for uploading metadata to Pinata.

Create a new file named mint_nft.js under scripts/ folder and add the below imports to the top of this file.

  • butterflyToken is our token address on Avalanche.
  • uploadImage this is the file that uploads helper. We are coming this next.
  • ButterflyToken is our contracts artifact file as JSON.

After this, we need to add a couple of functions.

This function’s call hierarchically is:

  • tokenCounter is current token counter e.g. 0, 1, 2…
  • ipfsHash hash string that pointing to our uploaded metadata.
  • getTokenCounter() reads current token counter and returns it.
  • We use ethers.js for interacting with contract.
Replace YOUR_PRIVATE_KEY wtih your private key. It needs to look like 0x5g4sb.....
  • This function takes one parameteripfsUrl that metadata file URL and returns the current token counter after minting a new NFT.

After all process finish mint_nft.js file is going to look like this:

Now create new file named PinataConnection.js under scripts/ folder and copy, paste these codes to this file.

uploadImage function takes 4 parameters:

  • file_name is our NFT image file name from file systeme.g. butterfly.jpg. You need to add your image to your top project folder.
  • nft_title is the title of NFT.
  • nft_desc is a description of NFT.
  • tokenId is the current token counter.

uploadToPinata function takes 5 parameters and uploads selected image to Pinata.

  • IPFSHash is hash string of our uploaded image file.
  • nft_title is the title of NFT.
  • nft_desc is description of NFT.
  • tokenId is current token counter.
You need to add your Pinata API_KEY and API_SECRET. For get this key and secret follow this steps:1- Create new account on Pinata.
2- Login your account.
3- Click profile icon on top right and select API Keys option.
4- Then click Nex Key button.

sendMetadata function takes 4 parameters, generates metadata, uploads to Pinata, and returns IPFSHash of this metadata.

  • image is our NFT image file.
  • name is our NFT image file name from file systeme.g. butterfly.jpg. You need to add your image to your top project folder.
  • nft_title is the title of NFT.
  • nft_desc is a description of NFT.
  • tokenId is the current token counter.

Don’t forget to add your image to the root of the project file. And update nft_desc, nft_title variables. After this you can write this command:

npx hardhat run scripts/mint_nft.js --network avalanche_fuji

When the mint process finished you are going to see this on the console:

Image Uploading to Pinata...
QmYFr3b4xSkPRybwhKx7......vysLCKRX7FyCv
Minting NFT...
Contract Address: 0x37A03F828A40f06eE7......E9df73a3Fbc44 Token Counter: 0

Congratulations you just minted your first NFT!

Add NFT to MetaMask wallet

Seeing Tesnet NFTs on Avalanche is a little bit tricky. You need to install MetaMask to your Android or IOS Phone. After installation adds your wallet to MetaMask. You must add the account that owns this NFT. This means who belongs YOUR_PRIVATE_KEY.

After this add Avalanche FUJI Testnet to MetaMask if you don’t have it. Follow the below steps.

1. Click hamburger (≡) menu on top left.
2. Click Settings.
3. Select Networks.
4. Click Add Network button, input below informations and click Add.
FUJI Testnet Settings:​Network Name: Avalanche FUJI C-Chain
New RPC URL: https://api.avax-test.network/ext/bc/C/rpc
ChainID: 43113
Symbol: AVAX
Explorer: https://testnet.snowtrace.io/

After this switch networks by clicking Wallet button.

For adding NFT to your MetaMask Wallet — switch to the NFT tab.

Then click the Import NFT button and type Contract Address and Token Counter. After waiting 30 seconds you are going to see metadata. If you don’t see long click to NFT and click refresh metadata.

You can get codes from GitHub.

This is what we have learned and done so far:

  • Learned NFTs and ERC-721 tokens.
  • Create an ERC-721 contract.
  • Deployed our contract on Avalanche Fuji.
  • Create an NFT file and upload it to Pinata.
  • Mint that NFT.
  • Add MetaMask to this NFT.

Useful Links

Leave a Comment