a
image

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

Related Articles

3 Ways Gen AI and AWS can Enhance Your Business

3 Ways Gen AI and AWS can Enhance Your Business

Amazon is on the cutting edge of new technologies. They have been increasingly experimenting with AI and learning algorithms, culminating in their most recent breakthroughs in Generative AI. Developers and technology enthusiasts have access to their innovations through the tools available on AWS.

Business Owner’s Guide to DevOps Essentials

Business Owner’s Guide to DevOps Essentials

As a business owner, it’s essential to maximize workplace efficiency. DevOps is a methodology that unites various departments to achieve business goals swiftly. Maintaining a DevOps loop is essential for the health and upkeep of deployed applications.

AWS Graviton and Arm-architecture Processors

AWS Graviton and Arm-architecture Processors

AWS launched its new batch of Arm-based processors in 2018 with AWS Graviton. It is a series of server processors designed for Amazon EC2 virtual machines. The EC2 AI instances support web servers, caching fleets, distributed data centers, and containerized microservices. Arm architecture is gradually being rolled out to handle enterprise-grade utilities at scale. Graviton instances are popular for handling intense workloads in the cloud.