Deploying contracts to Polygon

So you wrote your smart contracts tested them locally and now comes the big time—deployment to a real network. This can be quite scary as a small mistake can lead to big losses. Therefore it’s always a good idea to start with test nets and then move to live. In this short tutorial, I will describe the steps you need to take to deploy your smart contracts to the Polygon network without any issues.

The process is based on my experience with the deployment of my current project FELToken—providing distributed privacy-preserving machine learning. Hopefully, this tutorial will help you speed up the process and avoid the errors that I made.

Tutorial

Starting easy, first, you will need some polygon in your wallet. In case you are using a test net, head over to the faucet for some free polygon to play with: faucet.polygon.technology. For the live network, you can find here a list of exchanges. In case you need to add Polygon to your MetaMask wallet, just follow these instructions.

Once we are locked and loaded, we can head out to Brownie’s setting. By running brownie networks list you will get a list of all networks supported by brownie. In the list, you should see Polygon as:

Polygon
  ├─Mainnet (Infura): polygon-main
  └─Mumbai Testnet (Infura): polygon-test

A few things to note here. First are the names polygon-main and polygon-test you have to use this for deployment and in settings. For example, if you want to configure Chainlink addresses for the networks you have to edit brownie-config.yaml as follows:

The second thing, to note from brownie networks list command is that Brownie is using Infura to communicate with the network. This means you will need to obtain Infura API key. I recommend using .env file in the root of you project where you store your secrets. Just add dotenv: .env to your config as above and Brownie will automatically use this file. At the end of this tutorial, your .env file should look something like this:

Getting Private Key from MetaMask

If you deployed your smart contracts to localhost, you probably already know how to do this, but just to recap.

  1. Click on MetaMask in your browser
  2. Click on next to your account name (on right)
  3. Go to Account details
  4. Export Private Key
  5. Fill in your password and copy the private key into .env

Getting Infura Project ID

Getting Infura Project ID might be a bit more complicated because you will need to register and create a project.

  1. Go to infura.io/register and create an account
  2. Click on Create New Project (once you login into your account)
  3. As product select Ethereum, fill a name, and create the project
  4. Go to Add-ons (Polygon network isn’t allowed by default)
  5. Scroll down and under Network Add-ons select Polygon PoS
  6. Finish the purchase, you will need to fill in your card details as this is only trial
  7. Go back to dashboard and project settings and copy the Project ID to the .env

I would also like to point out that you don’t need to use Infura in your web application. You can use any other RPC service. In our project, we are using rpc.maticvigil.com. However, I didn’t find any easy way how to configure Brownie to use it.

Getting PolygonScan Token

Finally, you will need a PolygonScan token. The brownie automatically verifies your contracts through PolygonScan and this token is required to do that. To obtain this token just follow the steps:

  1. Go to polygonscan.com/register and create an account
  2. Go to API-KEYs and Add a new API key
  3. You should see the new Api-Key Token in a list, just copy this string to .env

Deploy it!

And that’s it now you are ready for the big moment! You can deploy the contracts using the following command (you might need to change deploy to the name of your deployment script):

brownie run deploy --network polygon-test

Errors

Sometimes the deployment doesn’t go that smooth. I won’t deal with any compilation related issues as this is a whole different chapter. However, when I first run the above command I got the message:

Verification complete. Result: Verification - failed.

Saying that the verification on PolygonScan failed. The message itself isn’t very helpful, but there is no need to panic. First, make sure that you are using the latest version of Brownie and that should fix most of the issues.

Conclusion

I hope that you find this tutorial useful. I am trying to write a weekly tutorial based on the development of our dApp, so please follow me for more articles about web3 and how to start your own crypto project.