How to Deploy a Smart Contract to the Polygon Blockchain

mbvissers.eth
BLOCK6
Published in
4 min readApr 19, 2022

Deploying a smart contract using HardHat.

Smart contracts are becoming an essential part of the crypto space. Whether it’s an NFT, a new ERC20 token, or a complex DeFi project, they still need to be deployed when they’re done.

Deploying your first smart contract may be a daunting task, but it is easier than you might think. It only requires some libraries to be installed and some code to be copy-pasted. And I will help you get through it all.

Setting up What We Need

Deploying smart contracts is often done with NPM libraries. The most popular libraries for smart contract development are Truffle and HardHat. Alternatively, you could also use the Remix IDE. To use Truffle or HardHat (we will use HardHat) you need to install NodeJS. This should also install NPM, the package manager for Node.

To install HardHat using NPM you can run the following commands in your console. Be sure to run them from an empty folder because they’ll initialize a new project.

npm init -y
npm install --save-dev hardhat
npx hardhat

The final command uses npx instead of npm . This will run some code automatically and promo for the type of project to start. Choose to create an empty hardhat.config.js file. This is the most minimal way.

You can run npx hardhat again in the same directory to get a basic overview of what HardHat can do.

Adding the smart contract

HardHat uses a few specific folders to get their files from. Your smart contract should be placed in ./contracts/ . Of course, the smart contract should be a .sol file.

You can then compile the smart contract by running npx hardhat compile in the console from the project directory. This will create a few more extra files that might be useful in later development, like the contract’s ABI. These can be found in the build folder, but we don’t use them directly while deploying.

Deploying the Smart Contract

The first thing you need to do to deploy a smart contract is to choose the network you want to deploy on. Since this article is written for deploying on Polygon, you can also deploy on Ethereum or any testnet with these same steps.

Finding the network information

Most information we need comes from a quick Google search. For example “Polygon testnet” will point you towards the Polygon Mumbai testnet. This will take you to this page from Polygon. Select the Mumbai testnet tab and take a look at all the info.

These and some of the other fields are useful for adding the network to MetaMask (you can Google how to do this as well). They also let us know which token we need to find a faucet for. So look up a faucet for Mumbai and get some testnet MATIC.

Faucets change all the time, some work, some don’t so it’s a bit of a search until you gained some testnet MATIC that you can view from MetaMask.

Setting the network in the config file

My config file, hardhat.config.js , looks like this after adding the network information.

We only have to take a look at the networks object. Everything else is boilerplate code automatically generated by HardHat.

I use dotenv to securely insert my deployment account’s private key (don’t share this key with anyone). We use one of Polygon’s RPCs that we can find in the table we looked at before. We also set a gasPrice which should be optimal, but it might fix some issues if you ever get errors about gas running out.

Creating a deploy script

Creating a deploy script simply requires you to copy and paste the following code.

We use hre , or HardHat’s runtime environment to create an instance of the contract. We then deploy that and print its address to the console.

It will deploy the smart contract from the accounts array provided in the config within the network configuration.

Deploying the contract

To deploy the smart contract, we only have to run a simple command in the command line.

npx hardhat run scripts/deploy.js --network matic_testnet

This will run our deploy script using HardHat. We also set the network to be the matic_testnet that we added in the config file. It should compile the smart contract a final time and deploy it to the testnet.

Conclusion

Congratulations, your smart contract should now be deployed and you can get on with development. You can read some of my other articles about connecting the smart contract with web3, or how to test it locally with HardHat or Truffle.

Thank you so much for reading and have an excellent day.

Consider supporting me by getting a Medium membership. It helps me out a lot, it won’t cost you anything extra, and you can read as many Medium articles as you like!

Follow me on Twitter and gm.xyz to keep up with my projects.

Check out Pixel Pizzas on Polygon.

Contents distributed by Learn.Block6.tech

👉 Telegram — Fresh ideas

👉 Twitter — Latest articles

👉 LinkTr.ee

--

--

mbvissers.eth
BLOCK6
Writer for

I occasionally write about programming. Follow me on Twitter @0xmbvissers