Are you getting the most out of your AWS investment? Get your free AWS Well-Architected Assessment.

2021 Fillmore Street #1128


24/7 solutions


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

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
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


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:

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

Traditional IT vs. AWS – How Small Businesses can Benefit

Traditional IT vs. AWS – How Small Businesses can Benefit

AWS solutions can accomplish a variety of problems and tasks including IT needs. Even smaller businesses that have a more limited scope that their businesses cover can look to find some way to upgrade their business operations through what Amazon has to offer. Though it may be intimidating and difficult to adapt to, there is more than enough reason to adopt AWS.

AWS Think Big with Small Business Program’s Competitive Edge

AWS Think Big with Small Business Program’s Competitive Edge

The Amazon Cloud can help alleviate most issues involving transitioning the cloud. Businesses of any scope can hope to build solutions that are scalable and adaptable to their industry of work. Smaller or minority-owned businesses may still struggle to stand out among bigger companies or make an AWS environment as efficient as possible with fewer funds to spend. To alleviate these burdens, Amazon has the Think Big With Small Business program available through their Public Sector partnership program.

AWS Think Big for Small Business, Data Analytics, and Business Intelligence

AWS Think Big for Small Business, Data Analytics, and Business Intelligence

The AWS Think Big for Small Business Program is an outreach program designed to provide small and/or minority-owned public sector organizations support in the form of business intelligence, technical expertise, and marketing strategies. With cloud-based solutions and experience, various public institutions globally have seen continued success in government, educational, and nonprofit sectors. While the funding provided can help significantly to meet business objectives, the expertise on navigating the cloud and how to extend outwards towards customers is just as critical.