2021 Fillmore Street #1128

}

24/7 solutions

Top 10 Cloud Providers

How to Mint NFTs on Polygon

Why should you learn how to mint NFTs on Polygon? Well, there is a rumor floating around that Coinbase is going to leverage Polygon for their NFT platform. Why would Coinbase use Polygon, the L2 layer that sits on top of Ethereum? The gas prices on Polygon are cheap and the protocol works.

Getting started

Open up the terminal on your Mac, and create a new project folder. We’ll call it allcode-polygon-nfts.

mkdir allcode-polygon-nft

Run the following command from the allcode-polygon-nft directory

npm install --save-dev hardhat && npx hardhat

The && signal in Linux is used to aggregate commands. The first command will install Node Package Manager. The second command will install hardhat. Hardhat, similar to Truffle.js, is a development environment to compile, deploy, test, and debug your Ethereum software.

In your terminal shell, you should see the following output. You’ll want to select the basic sample project.

        888    888                      888 888               888
	888    888                      888 888               888
	888    888                      888 888               888
	8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
	888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
	888    888 .d888888 888    888  888 888  888 .d888888 888
	888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
	888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

	👷 Welcome to Hardhat v2.7.0 👷‍

	? What do you want to do? … 
	❯ Create a basic sample project
	  Create an advanced sample project
	  Create an advanced sample project that uses TypeScript
	  Create an empty hardhat.config.js
	  Quit

After selecting the basic sample project, hit the defaults for all of the subsequent prompts: project root, gitignore, and installing the dependenies with npm.

Open the project in your favorite IDE. I prefer IntelliJ with the Solidity plugin. You should have a directory structure that looks like

MacBook-Pro:allcode-polygon-nft joelgarcia$ ls -ltr
total 1720
drwxr-xr-x    3 joelgarcia  staff      96 Dec  3 18:01 test
drwxr-xr-x    3 joelgarcia  staff      96 Dec  3 18:01 scripts
-rw-r--r--    1 joelgarcia  staff     572 Dec  3 18:01 hardhat.config.js
drwxr-xr-x    3 joelgarcia  staff      96 Dec  3 18:01 contracts
-rw-r--r--    1 joelgarcia  staff     467 Dec  3 18:01 README.md
drwxr-xr-x  385 joelgarcia  staff   12320 Dec  3 18:01 node_modules
-rw-r--r--    1 joelgarcia  staff  860814 Dec  3 18:02 package-lock.json
-rw-r--r--    1 joelgarcia  staff     220 Dec  3 18:02 package.json
-rw-r--r--    1 joelgarcia  staff     335 Dec  3 18:05 allcode-polygon-nft.iml

The test folder is for our test scripts. The scripts folder is for our deployment scripts. The hardhat.config.js is where we’ll configure the version of solidity, network, and the contracts. The contracts folder has the solidity contract entitled Greeter.sol since we created the basic sample project. The node_modules will contain the node modules that we installed, e.g. Hardhat.

Next, we need to get out Hardhat configuration to point to the Polygon Mumbai test network. Navigate over to MetaMask app on your Firefox or Chrome. Add a new Network for the Mumbai Test net. Use the configuration here. After you get the Mumbai Test network added. Copy your wallet address in MetaMask, and paste it into the Mumbai Faucet to acquire your test network Matic.

AllCode mint Nfts Polygon Mumbai test network

Wait a few minutes for the test network Matic to show up in your MetaMask wallet. When it does, you should see, .5 Matic in your MetaMask. I’ve got less than .5 because I spent some on gas for this tutorial.

Next, we’re going to return back to our IntelliJ IDE to get the Hardhat sample project to point to Matic. In the root directory of our allcode-polygon-nft, we’re going to create a file entitled .env. Into the .env file, put the following

PRIVATE_KEY=YOUR_EXPORTED_PRIVATE_KEY

Now, we’re going to head back into MetaMask to export our private key from MetaMask, so we can place the private key into the env file. In MetaMask, clicks on the three dots on the same line as your account information. Select Account Details. Export the Private Key. Paste the private key to replace the YOUR_EXPORTED_PRIVATE_KEY.

Next, we’ll want to ensure that the .env file is not checked into your source control. Open your .gitignore file in the root directory in IntelliJ. If the .env isn’t in the file, then add it.

Going back to the terminal app, from the root directory of your project, type

npm i dotenv

This will enable our hardhat scripts to work directly with environment variables.

Going back to IntelliJ, let’s update the hardhat.config.js to let it know that it’s using Polygon. At the top of the file, let’s add the following couple of lines:

require('dotenv').config();
const PRIVATE_KEY = process.env.PRIVATE_KEY;

This will tell our code to make use of the dotenv configuration. We’ll import the private key from the .env file.

Next, we’ll need to find the module.exports object in the hardhat.config.js file. We’ll want to replace the contents with the following:

AllCode Mint NFTs Polygon

In the module.exports, we’re telling hardhat that we’re making use of the matic test-network, specifying the latest version of solidity, and providing the location of the appropriate directories.

Okay, we’ve done a lot. We’ve provided our exported private key, introduced the environment variable, and specified the paths for the our contracts test scripts, and artifacts. Let’s make sure we didn’t screw up by deploying the Greeter.sol to the Mumbai test network.

npx hardhat run scripts/sample-script.js --network matic

This command will tell hardhat to run the sample-script.js, which will deploy the Greeter.sol. The network parameter will specify the matic network that is on module.exports in our hardhat.config.js.

Your output should look like

Downloading compiler 0.8.0 
Compiling 2 files with 0.8.0
Compilation finished successfully
Greeter deployed to: 0xd3305a49CC09D2011951Af81473E7ab4eF540E37

Congratulations! You’ve just deployed your first contract to the Polygon Mumbai network. In our next tutorial, we’ll write some ERC721 Non-fungible tokens that make use of OpenZeppelin

Joel Garcia
Joel Garcia

Joel Garcia has been building AllCode since 2015. He’s an innovative, hands-on executive with a proven record of designing, developing, and operating Software-as-a-Service (SaaS), mobile, and desktop solutions. Joel has expertise in HealthTech, VoIP, and cloud-based solutions. Joel has experience scaling multiple start-ups for successful exits to IMS Health and Golden Gate Capital, as well as working at mature, industry-leading software companies. He’s held executive engineering positions in San Francisco at TidalWave, LittleCast, Self Health Network, LiveVox acquired by Golden Gate Capital, and Med-Vantage acquired by IMS Health.

Related Articles

What is Tigera?

What is Tigera?

An AWS Advanced Technology Partner, Tigera delivers Calico and Calico Enterprise for security and networking on EKS, both of which are AWS Containers Competency certified.

Vezt

Vezt

Blockchain technology has the potential to be a windfall for musicians, filmmakers, and video game developers. With the advent of new technology, the way we consume entertainment is changing. Vezt assists artists in distributing their tracks on digital channels and in promoting their work.

Top Platforms for NFTs: Polygon and Flow

Top Platforms for NFTs: Polygon and Flow

As blockchain technology continues to develop, we are seeing an increase in the number of platforms, languages, and applications. Your works of art and collectables can now be presented and traded in the form of non-fungible tokens (NFTs), which are a relatively new development.

Free AWS Services List

Download this FREE list of all 200+ AWS services and ensure that you're using the optimal services for your use case to enhance efficiency and save money!

Free AWS Business Continuity Plan Template

Make sure you have the proper business continuity plan explicitly for your AWS infrastructure. Our team of experts built this template using AWS Best Practices so you can ensure it's built to scale! 

Free Cloud Migration Checklist

Without the proper cloud migration strategy, you risk losing time and money. Ensure that your migration process is running smoothly with our FREE cloud migration checklist.

Free AWS Services List

You might be optimizing with the wrong AWS services. Download this FREE list of all 200+ AWS services and ensure that you're using the optimal services for your use case to enhance efficiency and save money!

Download your FREE AWS Business Continuity Plan Template
Download Free 200+ AWS Services Checklist
Download our 10-Step Cloud Migration ChecklistYou'll get direct access to our full-length guide on Google Docs. From here, you will be able to make a copy, download the content, and share it with your team.