Transfer ERC20 token from Ethereum to Polygon

How to transfer assets from ethereum chain to polygon/matic

erc20
ethereum
polygon

ganesh deshpande

@theblockmatrix
1074 0 0

In this tutorial we will discuss about how to transfer token assets from Ethereum chain to Polygon Matic. We will discuss both how to do this on test network as well as on mainnetwork, the procedure remains the same

Prerequisites:

  • NodeJS
  • Truffle HDWalletprovider
  • MaticJS/MaticPOSClient
  • Metamask account public/private key pairs
  • Goerli RPC provider + Mumbai Matic RPC provider (for testnet)/ Ethereum + Matic Mainnet RPC providers (for mainnet)
  • Web3

Create NodeJS Project

you can create nodejs project by npm init. the complete index.js file code is provided below :

index.js

var express = require('express');
var Web3 = require('web3');
const HDWalletProvider = require('@truffle/hdwallet-provider')
const MaticPOSClient = require('@maticnetwork/maticjs').MaticPOSClient

var app = express();
var port = process.env.PORT || 3000;


async function approveandtransfer() {
    
    const privateKey = 'your_private_key';
    
    const from = "your_public_key";
    const rootToken = "0x655F2166b0709cd575202630952D71E2bB0d61Af"; // this is taken from matic docs and discussed below
    
    // the following RPC urls will change for mainnet.
    const parentProvider = new HDWalletProvider(privateKey, 'https://goerli.infura.io/v3/your_project_ID');
    const maticProvider = new HDWalletProvider(privateKey, 'https://rpc-mumbai.maticvigil.com/v1/your_project_ID');
    
    
    
    //test ERC20 token address parent goerli - 0x655F2166b0709cd575202630952D71E2bB0d61Af
    //test ERC20 token address child mumbai - 0xfe4F5145f6e09952a5ba9e956ED0C25e3Fa4c7F1
   // Above addresses are taken from matic docs and discussed below
    
    // for mumbai testnet
    const maticPOSClient = new MaticPOSClient({
        network: "testnet",
        version: "mumbai",
        parentProvider: parentProvider,
        maticProvider: maticProvider
    });
    
    //for mainnet
    const maticPOSClient = new MaticPOSClient({
       network: "mainnet",
      version: "v1",
      parentProvider: <ethereum-provider>,
      maticProvider: <matic-provider>
    });

    console.log("approve initiated");

    var amount = Web3.utils.toWei('0.1', 'ether');

    await maticPOSClient.approveERC20ForDeposit(rootToken, amount, { from: 'your_public_key' });
    
    console.log("approve completed and transfer initiated");

    await maticPOSClient.depositERC20ForUser(rootToken, from, amount, {
        from: 'your_public_key',
        gasPrice: "10000000000",
    });

    console.log("transfer complete");
}

approveandtransfer();

app.listen(port);
console.log('listening on', port);

The above code is defined for testing purpose. You can do the same on mainnet by changing token addresses and maticPOSClient configuration.

package.json

{
  "name": "matic",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "dependencies": {
    "@maticnetwork/maticjs": "^2.0.44",
    "@truffle/hdwallet-provider": "^1.4.1",
    "express": "^4.17.1",
    "web3": "^1.4.0"
  }
}

 

Description:

Step 1:

Get public/private key from metamask. Once you got the public and private key you should deposite some ethers in goerli testnet. you can get test etheres from goerli faucet here.

 

Step 2:

How to get rootToken address ?

In order to transfer assets from Ethereum to Matic and vice versa the token need to be mapped first. If your token is not mapped with polygon, send mapping request to get it done. If the token is already mapped, we are good to go.

For this tutorial we are using polygon's dummy ERC20 token , which we will be transferring from goerli network to matic mumbai testnet. So to get your proper token address you can check the address for Mainnet and Testnet here.

 

How to get test ERC20 token ?

We will first neet to have some ERC20 tokens deposited in our goerli testnet in order to transfer it to the matic mumbai testnet. so head over to the matic faucet. Matic Faucet.

Select token as (POS) test ERC20 & Goerli testnet. Paste your public key or address and submit it and confirm. Then you will have to import the same token in metamask wallet just to confirm if it get's transferred. So in metamask in assets click on add token and paste the toekn address and import it. this will be the same address of dummy ERC20 token on goerli testnet.

 

How to get goerli RPC provider ?

You can get the RPC provider form infura. head over to infura.io and create project. you will get the RPC urls for different networks. copy url for goerli testnet.

 

How to get mumbai matic testnet RPC ?

You can get it from the same project you created for goerli rpc , but for some reason when I was testing it didn't work out. so I used the alternet provider i.e maticvigil. the process is same.

 

Step 3:

Declare matic POS client and configure it as given in the above code.

 

Step 4:

Use two functions to approve and transfer the tokens from goerli testnet to matic mumbai testnet.

generally the waiting period is 5-7 min. wait for the transfer to complete.

How to run the files ?

Open terminal and go to the project directory

then use the following commands one by one:

npm install
---------------------------------
node index.js

 

How to confirm the transfer ?

there are two ways to confirm the transfer , either you can set a checkpoint or you can directly configure metamask for Matic Mumbai testnet and import the dummy ERC20 token by adding tokens in assets (the same way we added the dummy token in goerli testnet). You can check if the token amount is increased or not.


More SmartBooks like this