Smart contract built with hardhat on Polygon network

Soyeon·2021년 12월 8일


목록 보기

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.
Polygon solves pain points associated with Blockchains, like high gas fees and slow speeds, without sacrificing security.

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 overridden.

Setting up Metamask for Polygon (Matic Network)

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

This is the site for setting testnet and mainet

👉 RPC Infomation page
👉 How to set up metamask

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.

👉 mainet faucet link
👉 mumbai testnet faucet link

👉 Get private key

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

Install hardhat-ethers

Hardhat plugin for integration with ethers.js.

$ npm install --save-dev @nomiclabs/hardhat-ethers 'ethers@^5.0.0'

Setup hardhat

$ npx hardhat
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.0.0

? What do you want to do? …
  Create a sample project
❯ Create an empty hardhat.config.js

Project Structure




module.exports = {
  solidity: {
    version: '0.8.2',
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
  paths: {
    sources: './contracts',
  defaultNetwork: 'matic',
  networks: {
    hardhat: {},
    matic: {
      url: '',
      accounts: ['METAMASK_PRIVATE_KEY`],


async function main() {
  const [deployer] = await ethers.getSigners();
  const TOR = await ethers.getContractFactory('SmartContracts');

  // Start deployment, returning a promise that resolves to a contract object
  const tor = await TOR.deploy();
  console.log('Contract deployed to address:', tor.address);

  .then(() => process.exit(0))
  .catch((error) => {


pragma solidity >=0.7.0 <0.9.0;

contract SmartContracts {
    struct SmartContract {
        address Address;
        bytes32 MappingHash;
        uint blockNumber;
        bool exists;
    mapping(bytes32 => SmartContract) public SmartContracts;

    function mintHashFile(address Address, bytes32 MappingHash) external {
        SmartContracts[MappingHash] = SmartContract(Address, MappingHash, block.number, true);

In my case, I needed to mint some hash value in the smart contract
This mintHashFile function is not used in the stage of issuing the smart contract now and will be called from Node.js later when we need

👉 here is another cases of solidity code bt example

Compile Contract

npx hardhat compile


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

The contract address will be printed on terminal.

front-end developer

3개의 댓글

2022년 5월 8일

In the deploy.js, there's no value assigned to the var "ethers". Is it a typo?

1개의 답글
2024년 3월 19일

Not all smart contract specialists provide a quality service, which is obvious. Finding a team that will take responsibility for you, taking into account the goals and needs of your business specifically, can be time-consuming rather than difficult. Learn more about Pessimistic's smart contract auditing services. The duration of an audit can take anywhere from one week to a month, but it's definitely worth it.

답글 달기