ERC20 Deposit and Withdraw Guide

High Level Flow

Deposit ERC20 -

  1. Approve ERC20Predicate contract to spend the tokens that have to be deposited.
  2. Make depositFor call on RootChainManager.

Withdraw ERC20 -

  1. Burn tokens on matic chain.
  2. Call exit function on RootChainManager to submit proof of burn transaction. This call can be made after checkpoint is submitted for the block containing burn transaction.

Step Details


Configuring Matic SDK

Install Matic SDK (2.0.2)

npm install --save @maticnetwork/maticjs
"dependencies": {
"@maticnetwork/maticjs": "2.0.2"
}

While creating MaticPOSClient object network,version,maticProvider, parentProvider, posERC20Predicate and posRootChainManager need to be provided.

const MaticPOSClient = require("@maticnetwork/maticjs").MaticPOSClient;
const maticPOSClient = new MaticPOSClient({
network: "testnet", // optional, default is testnet
version: "mumbai", // optional, default is mumbai
parentProvider: new HDWalletProvider(config.user.privateKey, config.root.RPC),
maticProvider: new HDWalletProvider(config.user.privateKey, config.child.RPC),
posRootChainManager: config.root.POSRootChainManager,
posERC20Predicate: config.root.posERC20Predicate, // optional, required only if working with ERC20 tokens
posERC721Predicate: config.root.posERC721Predicate, // optional, required only if working with ERC721 tokens
posERC1155Predicate: config.root.posERC1155Predicate, // optional, required only if working with ERC71155 tokens
parentDefaultOptions: { from: config.user.address }, // optional, can also be sent as last param while sending tx
maticDefaultOptions: { from: config.user.address }, // optional, can also be sent as last param while sending tx
});

Approve

This is a normal ERC20 approval so that ERC20Predicate can call transferFrom function. Matic POS client exposes approveERC20ForDeposit method to make this call.

await maticPOSClient.approveERC20ForDeposit(rootToken, amount, { from });

Deposit

Deposit can be done by calling depositFor on RootChainManager contract. Note that token needs to be mapped and approved for transfer beforehand. Once tokens are transferred deposit proceeds using StateSync mechanism. Matic POS client exposes depositERC20ForUser method to make this call.

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

deposit function of ChildToken is called by the ChildChainManager. Tokens should be minted when this call is made.

Burn

User can call withdraw function of ChildToken contract. This function should burn the tokens. Matic POS client exposes burnERC20 method to make this call.

await maticPOSClient.burnERC20(childToken, amount, { from });

Store the transaction hash for this call and use it while generating burn proof.

Exit

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.

await maticPOSClient.exitERC20(burnTxHash, { from });