ERC1155 Deposit and Withdraw Guide
Check the latest Matic.js documentation for ERC1155.
This tutorial uses the Polygon Testnet (Mumbai) which is mapped to the Goerli Network to demonstrate the asset transfer to and fro the two blockchains. An important thing to be noted while following this tutorial is that you should always use a Proxy address whenever it is available. For example, the RootChainManagerProxy
address has to be used for interaction instead of the RootChainManager
address. The PoS contract addresses, ABI, Test Token Addresses, and other deployment details of the PoS bridge contracts can be found here.
Mapping your assets is necessary to integrate the PoS bridge on your application. You can submit a mapping request here. But for the purpose of this tutorial, we have already deployed the Test tokens and mapped them on the PoS bridge. You may need it for trying out the tutorial on your own. You can request the desired Asset from the faucet. If the test tokens are unavailable on the faucet, do reach us on discord.
In the upcoming tutorial, every step will be explained in detail along with a few code snippets. However, you can always refer to this repository which will have all the example source code that can help you to integrate and understand the working of PoS bridge.
High Level Flow
Deposit ERC1155 -
- Approve ERC1155Predicate contract to spend the tokens that have to be deposited.
- Make depositFor call on RootChainManager.
Withdraw ERC1155 -
- Burn tokens on Polygon chain.
- Call the
exit()
function onRootChainManager
to submit proof of the burn transaction. This call can be made after the checkpoint is submitted for the block containing the burn transaction.
Step Details
approveALL
This is a normal ERC1155 approval so that ERC1155Predicate
can call transferFrom
function. The Polygon POSClient
exposes the approveERC1155ForDeposit
method to make this call.
const erc1155RootToken = posClient.erc1155(<root token address>,true);
const approveResult = await erc1155RootToken.approveAll();
const txHash = await approveResult.getTransactionHash();
const txReceipt = await approveResult.getReceipt();
Deposit
Deposit method can be done by calling depositFor
on the RootChainManager
contract. Note that the token needs to be mapped and approved for transfer beforehand. Once the tokens are transferred, the deposit proceeds using the StateSync
mechanism. The Polygon POSClient
exposes the depositSingleERC1155ForUser
& depositBatchERC1155ForUser
methods to make this call.
const erc1155RootToken = posClient.erc1155(<root token address>, true);
const result = await erc1155RootToken.deposit({
amount: 1,
tokenId: '123',
userAddress: <from address>,
data: '0x5465737445524331313535', // data is optional
});
const txHash = await result.getTransactionHash();
const txReceipt = await result.getReceipt();
The deposit
function of ChildToken
is called by the ChildChainManager
. Tokens should be minted when this call is made.
Deposits from Ethereum to Polygon happen using a State Sync mechanism and take about 22-30 minutes. After waiting for this time interval, it is recommended to check the balance using the 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.
Burn
User can call the withdraw
function of the ChildToken
contract. This function should burn the tokens. The Polygon POSClient
exposes the burnSingleERC1155
& burnBatchERC1155
methods to make this call.
const erc1155Token = posClient.erc1155(<child token address>);
const result = await erc1155Token.withdrawStart(<token id>,<amount>);
const txHash = await result.getTransactionHash();
const txReceipt = await result.getReceipt();
Store the transaction hash for this call and use it while generating burn proof.
Exit
WithdrawExit method
Once the checkpoint
has been submitted for the block containing the burn transaction, a user should call the exit
function of the RootChainManager
contract and submit the proof of burn. Upon submitting valid proof, tokens are transferred to the user. Polygon PoS client exposes the exitBatchERC1155
& exitSingleERC1155
methods to make this call. These functions can be called only after the checkpoint is included in the main chain. The checkpoint inclusion can be tracked by following this guide.
const erc1155RootToken = posClient.erc1155(<root token address>, true);
const result = await erc1155RootToken.withdrawExit(<burn tx hash>);
const txHash = await result.getTransactionHash();
const txReceipt = await result.getReceipt();