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.
A bridge is basically a set of contracts that help in moving assets from the root chain to the child chain. There are primarily two bridges to move assets between Ethereum and Matic. First one is the Plasma bridge and the second one is called the PoS Bridge or Proof of Stake bridge. Plasma bridge provides an increased security guarantees due to the Plasma exit mechanism.
This Smartbook will show you, transferring of assets from Polygon network to Ethereum network i.e from Parent chain to Child chain or we can say depositing assets using Plasma Bridge. You can refer this Smartbook for the same transfer but with implementing POS bridge .
In this SmartBook we will be using Goeril test network as our parent network and Matic Mumbai test network as our child network and transfer already mapped ERC20 Test Token.
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 (Plasma) Test ERC20 from Select Token section and from Select Network section , select Goerli. Then enter your address, click on submit and confirm.
In order to interact with Polygon blockchain, Polygon network must be configured on Metamask. You can refer this article to configure Metamask. You need to add test tokens mentioned in the previous step.
Addresses for both of these 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 @maticnetwork/maticjs
> npm install @truffle/hdwallet-provider
> npm install web3
Now that installation process is complete lets look at the code.
Let's first initialise the installed dependencies
const web3 = require('web3');
const Matic = require("@maticnetwork/maticjs").default;
const HDWalletProvider = require("@truffle/hdwallet-provider");
const Network = require("@maticnetwork/meta/network");
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 network = new Network("testnet", "mumbai");
const from = "<Public Key>";
const private_key = "<Private Key>";
const matic = new Matic({
network: "testnet",
version: "mumbai",
parentProvider: new HDWalletProvider(
private_key,
"https://goerli.infura.io/v3/<API>"
),
maticProvider: new HDWalletProvider(
private_key,
"https://rpc-mumbai.maticvigil.com/v1/<API>"
),
parentDefaultOptions: from,
maticDefaultOptions: from,
});
According to Polygon's guidelines, Deposit ERC20 is a 2 step process :
So let's create a function execute and put both, approve and deposit function, in it.
async function execute() {
const token = network.Main.Contracts.Tokens.TestToken;
const amount = matic.web3Client.web3.utils.toWei('0.01');
// approve
console.log("Approve Initiated");
await matic.approveERC20TokensForDeposit(token, amount, {
from: from
}).then((res) => {
console.log("approve hash: ", res.transactionHash)
});
// deposit
console.log("Deposit Initiated");
await matic.depositERC20ForUser(token, from, amount, {
from: from
}).then((res) => {
console.log("deposit hash: ", res.transactionHash)
});
}
execute().then(_ => process.exit(0));
Replace Public Address, Private Key and API of both providers before executing. Use the following command to run the code and you will see assets getting deposited.
> node index.js
Deposits from Ethereum to Matic happen using a state sync mechanism and takes about ~5-7 minutes.
const web3 = require('web3');
const Matic = require("@maticnetwork/maticjs").default;
const HDWalletProvider = require("@truffle/hdwallet-provider");
const Network = require("@maticnetwork/meta/network");
const network = new Network("testnet", "mumbai");
const from = "<Public Key>";
const private_key = "<Private Key>";
const matic = new Matic({
network: "testnet",
version: "mumbai",
parentProvider: new HDWalletProvider(
private_key,
"https://goerli.infura.io/v3/<API>"
),
maticProvider: new HDWalletProvider(
private_key,
"https://rpc-mumbai.maticvigil.com/v1/<API>"
),
parentDefaultOptions: from,
maticDefaultOptions: from,
});
async function execute() {
const token = network.Main.Contracts.Tokens.TestToken;
const amount = matic.web3Client.web3.utils.toWei('0.01');
// approve
console.log("Approve Initiated");
await matic.approveERC20TokensForDeposit(token, amount, {
from: from
}).then((res) => {
console.log("approve hash: ", res.transactionHash)
});
// deposit
console.log("Deposit Initiated");
await matic.depositERC20ForUser(token, from, amount, {
from: from
}).then((res) => {
console.log("deposit hash: ", res.transactionHash)
});
}
execute().then(_ => process.exit(0));