ERC721 Deposit and Withdraw Guide

High Level Flow

Deposit ERC721 -

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

Withdraw ERC721 -

  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, posERC721Predicate 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 ERC721 approval so that ERC721Predicate can call transferFrom function. Matic POS client exposes approveERC721ForDeposit method to make this call.

await maticPOSClient.approveERC721ForDeposit(rootToken, tokenId, { 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 depositERC721ForUser method to make this call.

await maticPOSClient.depositERC721ForUser(rootToken, from, tokenId, {
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 burnERC721 method to make this call.

await maticPOSClient.burnERC721(childToken, tokenId, { 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 exitERC721 method to make this call.

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