Published on 5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?

What is Polygon ?

Polygon is a protocol and a framework for building and connecting Ethereum-compatible blockchain networks. Aggregating scalable solutions on Ethereum supporting a multi-chain Ethereum ecosystem.

 

What is Hardhat ?

Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software. It helps developers manage and automate the recurring tasks that are inherent to the process of building smart contracts and dApps, as well as easily introducing more functionality around this workflow. This means compiling, running and testing smart contracts at the very core.

Hardhat comes built-in with Hardhat Network, a local Ethereum network designed for development. Its functionality focuses around Solidity debugging, featuring stack traces, console.log() and explicit error messages when transactions fail.

Hardhat Runner, the CLI command to interact with Hardhat, is an extensible task runner. It's designed around the concepts of tasks and plugins. Every time you're running Hardhat from the CLI you're running a task. E.g. npx hardhat compile is running the built-in compile task. Tasks can call other tasks, allowing complex workflows to be defined. Users and plugins can override existing tasks, making those workflows customizable and extendable.

A lot of Hardhat's functionality comes from plugins, and, as a developer, you're free to choose which ones you want to use. Hardhat is unopinionated in terms of what tools you end up using, but it does come with some built-in defaults. All of which can be overriden.

 

Installations & Configurations:

Install nodejs

It is suggested to install nodejs with nvm on ubuntu OS in order to avoid any permissions error. Install nodejs from here.

 

Install hardhat

npm install --save-dev hardhat

 

Get rpc provider

From client side the wallet itself can act as bridge to connect the particular blockchains, but if you want to connect your apppilcation to blockchain from server side, rpc provider is required. there are may providers including the block explorers itself. You can get free provider like infura. create project id and get api key. If infura does't work you can get their free rpc providers 

https://rpc-mainnet.matic.network : for mainnet

https://rpc-mumbai.matic.today : for mumbai testnet

 

Get Test matic tokens

The account from which smart contract is to be deployed should be funded with matic tokens first. You can buy matic tokens from any exchanges for mainnet , but for testnet test tokens can be accessed from faucet. Select matic token and mumbai testnet paste your account address hit submit button and confirm transaction that's it.

 

Get private key

Get the private key of the account in which you have some matic tokens are available, both for mainnet and testnet.

 

Quick setup

use following command to quickly setup hardhat environment.

npx hardhat

This will create complete enviroment with sample smart contract, script and configuration file.

The sample project will ask you to install hardhat-waffle and hardhat-ethers, which makes Hardhat compatible with tests built with Waffle. You can skip this steps , as far now our focus is to deploy smart contract on Polygon POS.

 

Project Structure

artifacts, cache, contarcts/Greeter.sol , scripts/sample-script.js, hardhat.config.js.

Replace Greeter.sol with your smart contract file, also in sample-script.js file replace Greeter with filename at-hre.ethers.getContractFactory("Greeter");

 

hardhat.config.js

replace hardhat.config.js file code with following and add private key also.

require("@nomiclabs/hardhat-ethers");
module.exports = {
  defaultNetwork: "matic",
  networks: {
    hardhat: {
    },
    matic: {
      url: "https://rpc-mumbai.maticvigil.com",
      accounts: ["private_key"]
    }
  },
  solidity: {
    version: "0.8.0",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  paths: {
    sources: "./contracts",
    tests: "./test",
    cache: "./cache",
    artifacts: "./artifacts"
  },
  mocha: {
    timeout: 20000
  }
}

 

Compile Contract

npx hardhat compile

 

Deploy

 npx hardhat run scripts/sample-script.js --network matic

The contract address will be printed on terminal.

-------------------------------------------------------------------------------------------------------------------------------------------------------------

4515
1
3