ETH Deposit and Withdraw Guide

High Level Flow

Deposit ETH -

  1. Make depositEtherFor call on RootChainManager and send the required ether.

Withdraw ETH -

  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 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
});

Deposit

ETH can be deposited to matic chain by calling depositEtherFor on RootChainManager contract. Matic POS client exposes depositEtherForUser method to make this call.

ETH is deposited as ERC20 token on Matic chain. For withdrawing it follow the same process as withdrawing ERC20 tokens.

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

Burn

User can call withdraw function of MaticWETH contract. This function should burn the tokens. Since Ether is an ERC20 token on matic chain, use burneth method of Matic POS client exposes to make this call.

await maticPOSClient.burneth(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.exiteth(burnTxHash, { from });