High Level Flow
Depositing ERC1155 -
- Approve ERC1155Predicate contract to spend the tokens that have to be deposited.
- Make depositFor call on RootChainManager.
Withdrawing ERC1155 -
- Burn tokens on matic chain.
- 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.
Instantiate the contracts
Approve ERC1155Predicate to spend tokens by calling the setApprovalForAll function of token contract. This function takes two arguments operator and approved. operator is the address that is being approval to spend user's tokens. approved is a boolean indiating if tokens can be spent.
Note that token needs to be mapped and approved for deposit before making this call.
Call the depositFor function of RootChainManager contract. This function takes 3 arguments user, rootToken and depositData. user is the address of user that will receive the deposit on matic chain. rootToken is the address of token on main chain. depositData is abi encoded list of ids, list of tokens and bytes data.
Tokens can be burned on matic chain by calling the withdrawSingle or withdrawBatch function on child token contract. withdrawSingle takes two arguments, id and amount indicating the tokens to be burned. withdrawBatch takes 2 arguments, list of ids and list of amounts. Proof of this burn needs to be submitted in the exit step. So store the transaction hash.
Exit function on RootChainManager contract has to be called to unlock and receive the tokens back from ERC1155Predicate. This function takes a single bytes argument that proves the burn transaction. Wait for the checkpoint containing burn tenasaction to be submitted before calling this function. The Proof is generated by RLP ecoding following fields -
- headerNumber - Checkpoint header block number containing the burn tx
- blockProof - Proof that the block header (in the child chain) is a leaf in the submitted merkle root
- blockNumber - Block number containing the burn tx on child chain
- blockTime - Burn tx block time
- txRoot - Transactions root of block
- receiptRoot - Receipts root of block
- receipt - Receipt of the burn transaction
- receiptProof - Merkle proof of the burn receipt
- branchMask - 32 bits denoting the path of receipt in merkle patricia tree
- receiptLogIndex - Log Index to read from the receipt
Generating proof manually can be tricky so it is advisable to use the matic SDK. If you want to send the transaction manually, you can pass encodeAbi as true in the options object to get raw calldata.
Send this calldata to RootChainManager.