Layer2 Innovations in Defi champions and Polygon Matic Bridge
Tags: blockchain, ethereum, polygon, layer2
Published: 2021-11-09 20:49:06

Summary
During the past 30 days, the TVL across Ethereum bridges has topped $7.1 billion.In here , the Ethereum bridges have taken on L2 protocol such as Polygon,Arbitrum,Optimism and Avalanche.Those protocols are very useful to save expensive gas fee and fast transaction speed. We can resolve the many problems on L1 protocol ,which means the Ethereum mainnetwork.

Definition
What is Layer2 in blockchain? I’m going to describe my thought from this question.

Layer-2 is a third-party protocol that integrate with an underlying Layer-1 blockchain to increase transactional throughput.

Let’s see the Ethereum blockchain protocol.

The Ethereum Layer-2 is the protocal that integrate with the Ethereum Layer-1 and Layer-2 solutions stay on the Ethereum network in the form of smart contract.

Layer 1 blockchain technology is often referred to as an “On chain” solution because Layer1 is the base consensus layer of the Ethereum protocol. In other opposite, Layer 2 blockchain technology is often referred to as an “Off chain” solution. Above image is showing that each layer can process how many transactions of a block. As well, Layer2 solution resolved high gas fee issue on Layer1 solution. So many projects and platforms are migrating into Layer2 protocols and platforms.

L2 Innovation
There are various L2 protocols in Defi champions now. In this article, I am going to share about Polygon Matic ,which is powerful to incentive L2 innovation in Defi . The Polygon Matic supports the L1-L2 bridge behind on their professional Layer-2 solution.The polygon brings you a trustless two-way transaction channel between Polygon and Ethereum by introducing the cross-chain bridge with Plasma and Pos security.

Polygon Matic Bridge
A bridge is basically a set of contracts that help in moving assets from the root chain to the child chain.



Let’s see in detail how to implement L1- L2 communication correctly on Polygon SDK.

Steps to use Polygon SDK

We are going to use polygon SDK to implement our bridge successfully .

1) Npm install

Npm install @maticnetwork/maticjs : ^2.0.4
We installed this npm library on our project successfully .

2) Configuration

We will configure out the root chain and child chain at following :

root: {

RPC: 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161',
POSRootChainManager: '0xBbD7cBFA79faee899Eaf900F13C9065bF03B1A74',
DERC721: '0x839AB55B96B5137c35D7e647d4F0A675Ae27dECa',
posERC721Predicate: '0x74D83801586E9D3C4dc45FfCD30B54eA9C88cf9b',
posEtherPredicate: '0xe2B01f3978c03D6DdA5aE36b2f3Ac0d66C54a6D5',
},
child: {
RPC: 'https://rpc-mumbai.maticvigil.com',
DERC721: '0xCa9Ebc8309bafc9d88815e4b0D9B1604498eb7B5',
MaticWETH: '0x714550C2C1Ea08688607D86ed8EeF4f5E4F22323',
},
In this code, we have setup each RPC (remote procedure call) for the root chain and child chain .

We are going to prepare our project on test environment first . The polygon are supporting goerli test network on L1 and Mumbai matic test network on L2 .

First of all, we have to map our token contracts (one is the token contract on L1 and the other is the token contract on L2) by polygon support team .

In this code , We will map the DERC721 address of root and the DERC721 address of child .

Specially, there are two addressed more in root configure file .

posERC721Predicate and POSRootChainManager addresses:

These really needs to implement the bridge functionalities between polygon and mainnetwork. These addresses will not change by polygon . In here, these are test address and you can replace the addresses to mainnework ‘s address later.

Rest is optional in root and chain.

3) MaticPosClient Instance

We will get MaticPosClient instance to use the function to implement each actions (approve, deposit, burn and withdraw) with configuration .

return new MaticPOSClient({

network: 'testnet',
version: 'mumbai',
parentProvider: Parent RPC,
maticProvider: Matic PRC,
posRootChainManager: config.root.POSRootChainManager,
posERC721Predicate: config.root.posERC721Predicate,
})
}

You can replace the networks and version to main network later

1) Detailed Codes

- Approve

const tx = await maticPOSClient.approveERC721ForDeposit(config.root.DERC721, tokenId,{from:account})
approveERC721ForDeposit is function to implement the approve function , there are two parameters : DERC721 of root , tokenId

you have to set up address ( this is your wallet address) with from option .(Switch Goerli network)

- Deposit

const tx = await maticPOSClient.depositERC721ForUser(config.root.DERC721, account, tokenId,{from:account})
depositERC721ForUser is the function to implement the deposit function from your wallet into predicateERC721 contract on L1 . There are three parameters: DERC721 of root, account ( your wallet address) , tokenId

you also have to set up the address( this is your wallet address ) with from option. (switch Goerli network)

- Burn

const tx = await maticPOSClient.burnERC721(config.child.DERC721, tokenId,{from:account})
burnERC721 is the function is the function to implement the burn function . there are two parameters : DERC721 of child , tokenId.

You also have to set up the address (this is your wallet address) with from option. (Switch Polygon Mumbai testnetwork)

- Exit or Withdraw

const tx = await maticPOSClient.exitERC721(burnHash,{from:account,encodeAbi:true})
exitERC721 is the function is the function to withdraw the deposit the NFT from predicateERC721 contract successfully again .

There is one parameter: burnHash ( this is the hash after you burn your nft on polygon )

You also have to set up the your wallet address with from option (Switch Goerli network)

The important in here is that we have to confirm the checkpoint interval with polygon . Once it was completed , we can exit successfully .

Conclusion
Layer 2 tech will help us create blockchain systems that are usable, that can scale with whatever industry they’re implemented for, and that can compete with the likes of established, centralized systems like Visa. As well I have put the way how to implement Polygon Matic Bridge in this article. Hopefully ,everyone is getting closer to L2 innovations through my article.If you have any questions, please contact me.