Loading...
Polygon brings you a trustless two-way transaction channel between Polygon and Ethereum by introducing the cross-chain bridge with Plasma and PoS security. With this users can transfer tokens across Polygon without incurring third-party risks and market liquidity limitations.
You can tranfer your assets from Polygon to Matic and vice versa. This Smartbook will show you transferring assets from Polygon network to Ethereum network i.e from child chain to parent chain or we can say Withdrawing assets.
In this SmartBook we will be using Matic Mumbai test network as our child network and Goeril test network as our parent network and tranfer already mapped Dummy ERC20 token.
The SmartBook for Depositing assets i.e transfering from Ethereum network to Polygon network is also available here
RPC urls of both the networks are needed in order to communicate with respective blockchain networks. There are many rpc providers such as Infura, Alchemy, Chainstack, MaticVigil, QuickNode and Ankr but for this toutorial we will be using,
Visit Infura and MaticVigil , log in and create your project to get rpc urls.
You can get test tokens from matic's faucet.
There you need to select (POS) Test ERC20 from select token section and from select network section , select Mumbai. Then enter your address, click on submit and confirm.
In order to interact with Polygon POS blockchain, Polygon network must be configured on Metamask. You can refer this article to configure Metamask. You need to add test tokens mentioned in previous step.
Address for both tokens are as follows :
First initialise nodejs project and create working file with following commands,
> npm init
> touch index.js
Dependencies required and their commands for installation are as follows :
> npm install web3
> npm install @maticnetwork/maticjs
> npm install @truffle/hdwallet-provider
Now that installation process is complete lets look at the code.
Let's first initialise the installed dependencies
var Web3 = require('web3');
const MaticPOSClient = require('@maticnetwork/maticjs').MaticPOSClient;
const HDWalletProvider = require('@truffle/hdwallet-provider');
To create a MaticPOSClient instance its object require parent provider and child provider i.e matic provider . We can create those providers with the rpc url and private key of the account from which we are going to do transactions with the help of HDWalletProvider that we initialised before from truffle's hdwallet provider library.
const privateKey = "<Private Key>";
const currentaddress = "<Public Key>";
const parentProvider = new HDWalletProvider(privateKey, 'https://goerli.infura.io/v3/<API key>');
const maticProvider = new HDWalletProvider(privateKey, 'https://rpc-mumbai.maticvigil.com/v1/<API key>');
With this we can now initialise our MaticPOSClient as follows,
const maticPOSClient = new MaticPOSClient({
network: "testnet",
version: "mumbai",
parentProvider: parentProvider,
maticProvider: maticProvider
});
According to Polygon's guidelines for withdrawing assets, token have to be burnt with burnERC20 method. Then user have to Store the transaction hash for this call and use it while generating burn proof.
Once the checkpoint has been submitted for the block containing burn transaction, user should call the exit function of RootChainManager contract and submit the proof of burn.
Upon submitting valid proof, tokens are transferred to the user. Matic POS client exposes exitERC20 method to make this call. This function can be called only after the checkpoint is included in the main chain.
We implement this guidelines for withdrawing as follows,
const amount = Web3.utils.toWei('0.1', 'ether');
var proof = await maticPOSClient.burnERC20(
"0xfe4F5145f6e09952a5ba9e956ED0C25e3Fa4c7F1", amount, { from: currentaddress }
);
await maticPOSClient.exitERC20(proof.transactionHash, { from: currentaddress }).catch((err)=>{
console.log(err)
});
This is how we can transfer (withdraw) assets from child chain to parent chain.
var Web3 = require('web3');
const MaticPOSClient = require('@maticnetwork/maticjs').MaticPOSClient;
const HDWalletProvider = require('@truffle/hdwallet-provider');
async function withdraw_token() {
const privateKey = "<Private Key>";
const currentaddress = "<Public Key>";
const parentProvider = new HDWalletProvider(privateKey, 'https://goerli.infura.io/v3/<API>');
const maticProvider = new HDWalletProvider(privateKey, 'https://rpc-mumbai.maticvigil.com/v1/<API>');
const maticPOSClient = new MaticPOSClient({
network: "testnet",
version: "mumbai",
parentProvider: parentProvider,
maticProvider: maticProvider
});
const amount = Web3.utils.toWei('0.1', 'ether');
console.log('Burning . . .');
var proof = await maticPOSClient.burnERC20("0xfe4F5145f6e09952a5ba9e956ED0C25e3Fa4c7F1", amount, { from: currentaddress });
console.log('Hash generated');
await maticPOSClient.exitERC20(proof.transactionHash, { from: currentaddress }).catch((err) => {
console.log(err)
});
console.log('Exit completed');
}
withdraw_token();