Developing for Polygon and Sidechains
Tags: blockchain, solidity, javascript, tutorial
Published: 2021-11-25 10:23:26

## Introduction

In our last tutorial, we covered how to develop and deploy an [NFT Collectible Smart Contract](https://dev.to/rounakbanik/writing-an-nft-collectible-smart-contract-2nh8) from scratch. Last week, we published another article showing you how to [estimate your costs](https://medium.com/scrappy-squirrels/estimating-smart-contract-costs-f65acf818c26) while operating on the Ethereum mainnet and why it would be a good idea to consider a scalability solution like Polygon.

In this article, we will show you how to deploy your project to the Polygon network and in the process, end up saving potentially thousands of dollars.

## Overview of developing on sidechains

![Polygon, Binance, and Fantom](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ghj3xbyqu75tu82x1shp.png)

If you followed our tutorial on how to create an NFT Collectible Smart Contract, then congratulations! You are already a Polygon developer. You’re also a developer on the Binance Smart Chain, the Fantom Opera Network, and any sidechain or L2 scalability solutions that are EVM compatible.

This tutorial will demonstrate deployment to Polygon but the steps are almost identical for any other Ethereum sidechain and (to an extent) L2 chains like Arbitrum.

### Steps

1. Write a smart contract like you would for the Ethereum mainnet.
2. Recalibrate payable currency to reflect the chain’s token value.
3. Add the sidechain network to Metamask and Hardhat configuration file.
4. Acquire the chain’s token directly or by bridging from the Ethereum mainnet.
5. Deploy to the sidechain by paying fees using the chain’s token.

## Writing a Smart Contract

### Note

You can skip this section if you completed our smart contract tutorial.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g44d1wee1ntqs9ram9f.png)

We have already covered how to develop a smart contract for Ethereum in detail (and I have a feeling I’m mentioning this a little too often). I hope you already have your custom project ready to go. If not, you can clone a starter repository that we created.

Make sure you have [Git](https://git-scm.com/downloads) and run the following commands: 

```
git clone https://github.com/rounakbanik/nft-collectible-contract polygon-nft

cd polygon-nft

npm install
```

Create a new file called `.env` and input the following details.

```
API_URL = "<--Rinkeby RPC URL-->"

PRIVATE_KEY = "<-- Metamask wallet private key -->"

ETHERSCAN_API = ""

POLYGON_URL = ""
```

You won’t need `API_URL` for this tutorial so feel free to set this to a blank string (don’t delete it though, the configuration file will break).

You should already know how to get your Metamask wallet’s private key. Let `ETHERSCAN_API` and `POLYGON_URL` stay blank for the time being.

Now, go to the `hardhat.config.js` file and remove line 25 (the one with the `defaultNetwork` configuration. We won’t be needing this either.)

Finally, run the following command:

```
npx hardhat run scripts/run.js
```

If this runs without any errors, congratulations! You are up to speed, and we can finally concentrate on the Polygon aspects of the project.

## Recalibrating price

![Polygon Matic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/adpemyggerdb8b8kp6m0.png)

We set the base price of our NFT at 0.01 ETH. In other words, users would have to pay 0.01 ETH for each NFT that they minted (plus gas, of course). We encapsulate this information in line 16 of `NFTCollectible.sol` in the `contracts` folder of our project.

![VS Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3z4ca1wpxm8xm66qvho.png)

Transactions on the Polygon sidechain aren’t conducted in ETH though. The Polygon chain has its own ERC20 token called MATIC. We, therefore, need to set our price in MATIC.

At the time of writing, ETH is touching $5000 whereas MATIC is touching $2. Therefore, if we wanted our NFT to be priced the same (in terms of USD), we would price it at 25 MATIC.

Let’s make a change in our contract to reflect this change.

```
uint public constant PRICE = 25 ether;
```

Wait, what? Why does this say `25 ether` and not something like `25 matic`?

Solidity does not really understand what ETH is. In Solidity, the keyword ether is just a shorthand for 10¹⁸. To Solidity, the line above is the same as this:

```
uint public constant PRICE = 25000000000000000000;
```

To put it another way, you can specify payable amounts in Solidity in terms of Wei. On the mainnet, 1 ETH is 10¹⁸ Wei. On Polygon, 10¹⁸ Wei is 1 MATIC. This is a huge difference considering the difference in the price of ETH and MATIC. Always make sure you calibrate your prices correctly if you are moving to a different network!

In this tutorial, we are going to be working with the Polygon Mumbai testnet and I’m going to price the NFT at 0.01 MATIC (for reasons you’ll see soon). So, I’m going to reset the line back to what it originally was.

```
uint public constant PRICE = 0.01 ether;
```

Please remember. On Polygon, this is 0.01 MATIC. **Not 0.01 ETH.**

## Add Polygon Network to Metamask and Hardhat

Let’s add the Polygon and Polygon MUMBAI networks to our Metamask wallet. This is really simple to do and [Polygon has a short, excellent tutorial](https://docs.polygon.technology/docs/develop/metamask/config-polygon-on-metamask) on this. Here is a snapshot of my wallet connected to the Mumbai network.

![Metamask](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6w12ifn8jk8dx9iqoi8s.png)

For Hardhat, we will use a custom RPC URL from Alchemy. Go ahead and [create an Alchemy account](https://alchemy.com/?a=d7d81950c0) if you haven’t already. Next, create an App by setting the chain to Polygon and the network to Mumbai.

![Alchemy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5t0mfhx4l06t8pmlk08n.png)

Finally, click on **VIEW KEY** for your app and get the HTTP URL. Go back to your .env file and fill in the value for `POLYGON_URL`.

```
POLYGON_URL = "<---Alchemy Polygon URL -->"
```

Finally, our `hardhat.config.js` file should add Mumbai as one of our deployment networks. I have done this already for you in lines 30–33.

## Get fake MATIC

![Fake Matic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6d3ld191b71ckdm2cfl.png)

Now that we have configured our network on both Metamask and Hardhat, let’s proceed to get some fake MATIC.

Go to [https://faucet.polygon.technology/](https://faucet.polygon.technology/) and request for test tokens for the Mumbai network. Unlike Rinkeby, you shouldn’t face too many issues acquiring these tokens. You will almost always get 1 MATIC almost near instantaneously.

### Note about Polygon and sidechain main networks

When you’re ready to deploy to the main Polygon network (or the sidechain of your choice), you will need to acquire real MATIC.

There are two ways to do this:

1. Buy MATIC on the Ethereum mainnet and bridge it to the Polygon network.
2. Buy MATIC on a centralized exchange (like Wazirx or Coinbase) and transfer it directly to Metamask.

In the case of sidechains, it is almost always easier and cheaper to do (2).

## Deploy to the Polygon Mumbai network

We’re ready to go! Run the following command on your terminal.

```
npx hardhat run scripts/run.js --network mumbai
```

![Terminal output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5q5ajuqf56nn2k01udk2.png)

We can confirm that our contract was deployed and our NFTs were minted by visiting [https://mumbai.polygonscan.com/](https://mumbai.polygonscan.com/) and searching for our contract address. As you can see above, our contract was deployed to 0xe4ad3e1d2553eCbe4Ab64cd717564dbD36d520cc.

![Polygonscan](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/if5vjc1wd40ri1yn8ndg.png)

One of the biggest advantages that Polygon has over other sidechains is that it is supported by OpenSea, the largest NFT marketplace in the world and the defacto platform for secondary sales for almost every popular NFT project.

Visit [https://testnets.opensea.io/](https://testnets.opensea.io/) and search for your contract address. You will see that your collection has already been uploaded to OpenSea almost magically.

Check out our collection [here](https://testnets.opensea.io/collection/nft-collectible-2mnw3epg2h).

![Scrappy Squirrels on Opensea](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecphe7svoex8ygyy0n2c.png)

## Verifying our contract

As a bonus, let’s verify our contract on Polygonscan so our users are able to mint from Polygonscan directly.

To do this, you will need to sign up for a [Polygonscan account](https://polygonscan.com/). Next, proceed to [create an API key](https://polygonscan.com/myapikey).

Go back to the `.env` file one last time and fill in the value for `ETHERSCAN_API`.

```
ETHERSCAN_API = "<--Polygonscan API key-->"
```

I have kept the name `ETHERSCAN_API` from the previous tutorial because Polygonscan is powered by Etherscan and we still use the `hardhat-etherscan` library to verify our contract. Feel free to change the naming if you wish.

Now, run the following command on your Terminal.

```
npx hardhat verify --network mumbai DEPLOYED_CONTRACT_ADDRESS "ipfs://QmZbWNKJPAjxXuNFSEaksCJVd1M6DaKQViJBYPK2BdpDEP/"
```

In my case, this was the exact command I ran.

```
npx hardhat verify --network mumbai 0xe4ad3e1d2553eCbe4Ab64cd717564dbD36d520cc "ipfs://QmZbWNKJPAjxXuNFSEaksCJVd1M6DaKQViJBYPK2BdpDEP/"
```

You should now see a small green checkmark on your contract’s Polygonscan page. More importantly, your users will be able to read your contract and call functions from it.

![Polygonscan contract page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqbq94647pevn2zyiu94.png)

## Conclusion

Congratulations! You have a good understanding of how to build for Polygon or migrate existing projects into Polygon. The great news, as I’ve stated already, is that this knowledge converts really well to any EVM-compatible network (Binance, Fantom, Arbitrum, Optimism, etc.)

If you have any questions, please feel free to drop them on the [#suggestions-and-qna channel of our Discord](https://discord.gg/8UqJXTX7Kd).

If you don’t have questions, come say hi to us on our [Discord](If you don’t have questions, come say hi to us on our Discord anyway! Also, if you liked our content, we would be super grateful if you tweet about us, follow us(@ScrappyNFTs and @Rounak_Banik), and invite your circle to our Discord. Thank you for your support!) anyway! Also, if you liked our content, we would be super grateful if you tweet about us, follow us(@ScrappyNFTs and [@Rounak_Banik](https://twitter.com/Rounak_Banik)), and invite your circle to our Discord. Thank you for your support!

### *About Scrappy Squirrels*

[Scrappy Squirrels](https://www.scrappysquirrels.co/) is a collection of 10,000+ randomly generated NFTs on the Ethereum Blockchain. Scrappy Squirrels are meant for buyers, creators, and developers who are completely new to the NFT ecosystem.

The community is built around learning about the NFT revolution, exploring its current use cases, discovering new applications, and finding members to collaborate on exciting projects with.

Join our community here: [https://discord.gg/8UqJXTX7Kd](https://discord.gg/8UqJXTX7Kd)