High Level Flow
Deposit ERC721 (1 step process)
- The deposit function is to be invoked where the tokens get deposited to the Polygon contract, and are available for use in the Polygon network.
Withdraw erc721 (3 step process)
- Withdrawal of funds is initiated from Polygon. A checkpoint interval of 30 mins(For testnets wait for ~10 minutes) is set, where all the blocks on the Polygon block layer are validated since the last checkpoint.
- Once the checkpoint is submitted to the mainchain ERC20 contract, an NFT Exit (ERC721) token is created of equivalent value. Users need to wait for about 2 seconds challenge period (For both testnet and mainnet )
- Once the challenge period is complete, the withdrawn funds can be claimed back to your ERC20 acccount from the mainchain contract using a process-exit procedure.
For now, just go with the fact that the challenge period for withdrawals is an important part of the Plasma framework to ensure security of your transactions. Later, once you get to know the system better, the reason for the 7-day withdrawal window will become clear to you.
Configuring Matic SDK
Install Matic SDK (2.0.2)
Initiating Maticjs client
Create a new file in root directory name it process.env
Approve: This is a normal ERC721 approval so that depositManagerContract can call transferFrom function. Polygon Plasma client exposes approveERC20TokensForDeposit method to make this call.
deposit: Deposit can be done by calling safeTransferFrom on getERC721TokenContract contract.
Note that token needs to be mapped and approved for deposit beforehand.
safeDepositERC721Tokens method to make this call.
NOTE: Deposits from Ethereum to Polygon happen using a state sync mechanism and takes about ~5-7 minutes. After waiting for this time interval, it is recommended to check the balance using web3.js/matic.js library or using Metamask. The explorer will show the balance only if at least one asset transfer has happened on the child chain. This link explains how to track the deposit events.
User can call withdraw function of getERC721TokenContract child token contract. This function should burn the tokens. Polygon Plasma client exposes startWithdrawForNFT method to make this call.
User can call startExitWithBurntTokens function of erc721Predicate contract. This function should burn the tokens. Polygon Plasma client exposes withdrawNFT method to make this call. This function can be called only after the checkpoint is included in the main chain. The checkpoint inclusion can be tracked by following this guide.
3. Process Exit
Once the Challenge Period has been passed for the transaction present in checkpoint, user should call the processExits function of withdrawManager contract and submit the proof of burn. Upon submitting valid proof tokens are transferred to the user. Polygon Plasma client exposes processExits method to make this call.
Note: A checkpoint, which is a representation of all transactions happening on Polygon to the erc721 chain every ~30 minutes, is submitted to the mainchain erc721 contract.
Note: Once the checkpoint is submitted, user needs to wait for a challenge period of 2 seconds.