DEV Community

Cover image for How to Deploy Smart Contracts on Polygon (Matic) Network? How to Handle Different Errors?
Akhilesh Thite
Akhilesh Thite

Posted on • Updated on

How to Deploy Smart Contracts on Polygon (Matic) Network? How to Handle Different Errors?

In this tutorial, we will cover how to deploy smart contracts to the Polygon (Matic) Mumbai test network. We'll cover some of the possible errors which might occur during the deployment. So, grab a cup of coffee ☕️ and follow the steps.

After this tutorial you will be able to:

  • Deploy the smart contracts on polygon (Matic) Mumbai Test Network.
  • Tackle the errors while deploying the smart contracts on polygon (Matic) Mumbai Test Network.

Author

Akhilesh Thite

1. MetaMask setup.

To deploy the smart contracts on Matic you first have to create a Matic network in MetaMask wallet.
Settings -> Networks -> Add network -> Save
MetaMask Setup
To get test Matic for deployment and testing,
go to -> Matic Faucet -> Select Mumbai -> Paste wallet address -> Submit
Done! check your wallet, you'll see some Matic there.

2. truffle-config

  • truffle-config.js for Mac users
  • truffle.js for Windows users The truffle-config file is an important file to understand. In this file, we must configure the path to the DTube Solidity file (smart contract), the contract ABI, and define the available networks.
const HDWalletProvider = require("@truffle/hdwallet-provider")
require('dotenv').config(); // Load .env file

module.exports = {
  networks: {
   // For Ganache, your personal blockchain
   development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard Ethereum port 
      network_id: "*",       // Any network (default: none)
    },
  },
  contracts_directory: './src/contracts/',
  contracts_build_directory: './src/abis/',
  compilers: {
    solc: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Ensure you create an .env file in the project root directory (~/DTube/.env) and paste into it the Secret Recovery Phrase (12 words) of your preferably newly generated and testnet-only MetaMask wallet with the variable name MNEMONIC. This will be loaded by truffle at runtime, and the environment variable can then be accessed with process.env.MNEMONIC.

MNEMONIC= 12 secret words here..
Enter fullscreen mode Exit fullscreen mode

Now, let's add matic network in our truffle-config file which will contain our environment variable MNEMONIC and RPC URL.

    matic: {
      provider: () => new HDWalletProvider(process.env.MNEMONIC, 
      `https://rpc-mumbai.matic.today`),
      network_id: 80001,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true,
      gas: 6000000,
      gasPrice: 10000000000,
    },
Enter fullscreen mode Exit fullscreen mode

You can set the gas price and gas limits for faster transactions as shown in the above code block.

3. Deploy Smart Contracts

Command: truffle migrate --network matic
If you're deploying it second time then deploy with this command just to reset and avoid JSON errors.
Command: truffle migrate --network matic --reset
If everything worked fine, you'll see something like this:

2_deploy_contracts.js
=====================

   Replacing 'MyContract'
   ------------------
   > transaction hash:    0x1c94d095a2f629521344885910e6a01076188fa815a310765679b05abc09a250
   > Blocks: 5            Seconds: 5
   > contract address:    0xbFa33D565Fcb81a9CE8e7a35B61b12B04220A8EB
   > block number:        2371252
   > block timestamp:     1578238698
   > account:             0x9fB29AAc15b9A4B7F17c3385939b007540f4d791
   > balance:             79.409358061899298312
   > gas used:            1896986
   > gas price:           0 gwei
   > value sent:          0 ETH
   > total cost:          0 ETH

   Pausing for 2 confirmations...
   ------------------------------
   > confirmation number: 5 (block: 2371262)
initialised!

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:                   0 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0 ETH
Enter fullscreen mode Exit fullscreen mode

Code snippet from matic truffle docs.

4. Dealing with different errors

If you get any of these errors then follow these steps

Error:

Error: PollingBlockTracker - encountered an error while attempting to update latest block:
Enter fullscreen mode Exit fullscreen mode

Fix_1:

Change https://rpc-mumbai.matic.today by using Infura custom RPC
infura -> Create new project -> Settings -> Endpoints -> Polygon Mumbai

    matic: {
      provider: () => new HDWalletProvider(process.env.MNEMONIC, 
      `https://polygon-mumbai.infura.io/v3/process.env.PROJECT_ID`),
      network_id: 80001,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true,
    },
  },
Enter fullscreen mode Exit fullscreen mode

Paste your PROJECT_ID there from .env file.
truffle migrate --network matic --reset

If still dealing with error, try this 2nd fix.

Fix_2:

Change https://rpc-mumbai.matic.today by using Matic custom RPC

    matic: {
      provider: () => new HDWalletProvider(process.env.MNEMONIC, 
      `https://rpc-mumbai.maticvigil.com/v1/process.env.PROJECT_ID`),
      network_id: 80001,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true,
    },
  },
Enter fullscreen mode Exit fullscreen mode

Paste your PROJECT_ID there from .env file.
truffle migrate --network matic --reset

Error:

*** Deployment Failed ***

"Migrations" -- only replay-protected (EIP-155) transactions allowed over RPC.
Enter fullscreen mode Exit fullscreen mode

Fix:

npm install @truffle/hdwallet-provider@1.4.0
Truffle hdwallet-provider version 1.4.0 will fix this error.

Error:

Error:  *** Deployment Failed ***

"Migrations" -- Transaction was not mined within 750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined!.
Enter fullscreen mode Exit fullscreen mode

Fix:

    matic: {
      provider: () => new HDWalletProvider(process.env.MNEMONIC, 
      `https://rpc-mumbai.maticvigil.com/v1/process.env.PROJECT_ID`),
      network_id: 80001,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true,
      networkCheckTimeout: 100000,
    },
  },
Enter fullscreen mode Exit fullscreen mode

Just add networkCheckTimeout: 100000

If you discover any new errors and If you know the solution for it, then feel free to comment down below, I'll add that Error-Fix here

References

Truffle docs: https://www.trufflesuite.com/docs/truffle/overview
Polygon (Matic) docs: https://docs.matic.network/docs/develop/getting-started
GitHub repo: https://github.com/AkhileshThite/DTube

Thank you!

Top comments (5)

Collapse
 
stevemasch profile image
stevemasch

Hi, thank you for this tutorial, I was incredibly frustrating getting past these issues.
Now I have been able to deploy only 3 of my 4 contracts and once the deployment script get to the last contract it still gives me:
Error: PollingBlockTracker - encountered an error while attempting to update latest block:
Error: Too Many Requests

any idea why? My wallet is funded with matics.

Collapse
 
akhileshthite profile image
Akhilesh Thite

Hey!
if you are using Infura custom RPC or the default one, then change that by creating a new custom RPC from this website rpc.maticvigil.com/
Make sure you are on "Truffle hdwallet-provider version 1.4.0" by this command:
npm install @truffle/hdwallet-provider@1.4.0
This should solve all the issues.

Collapse
 
ssalanitri profile image
Sergio Salanitri

Try to deploy all the contract in one file. Using flatten option to generate one file with al the contracts.

Collapse
 
abhayra31630427 profile image
Abhay rana

This smart contracts should be in solidity only ?

Collapse
 
akhileshthite profile image
Akhilesh Thite

Yes.