Skip to main content

ER1155 Deposit and Withdraw Guide

High Level Flow

Depositing ERC1155 -

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

Withdrawing ERC1155 -

  1. Burn tokens on Polygon 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

Instantiate the contracts

const mainWeb3 = new Web3(mainProvider)
const maticWeb3 = new Web3(maticProvider)
const rootTokenContract = new mainWeb3.eth.Contract(rootTokenABI, rootTokenAddress)
const rootChainManagerContract = new mainWeb3.eth.Contract(rootChainManagerABI, rootChainManagerAddress)
const childTokenContract = new maticWeb3(childTokenABI, childTokenAddress)


Provide approval for the ERC1155Predicate to spend tokens by calling the setApprovalForAll method of token contract. This function takes two arguments operator and approved. Operator is the address that is being approved to spend user's tokens. Approved is a boolean indicating if tokens can be spent.

This is what the setApprovalForAll method looks like

await rootTokenContract.methods
.setApprovalForAll(erc1155Predicate, true)
.send({ from: userAddress })


Note that the 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: userAddress, rootToken, and depositData. userAddress is the address of the user that will receive the deposit on the Polygon chain. rootToken is the address of the token on the main chain. depositData is the ABI-encoded list of ids, list of tokens and bytes data.

const depositData = mainWeb3.eth.abi.encodeParameters(
['uint256[]', 'uint256[]', 'bytes'],
idList, amountList, data
await rootChainManagerContract.methods
.depositFor(userAddress, rootToken, depositData)
.send({ from: userAddress })


Tokens can be burned on Polygon 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.

// For single burn
const burnTx = await childTokenContract.methods
.withdrawSingle(id, amount)
.send({ from: userAddress })
const burnTxHash = burnTx.transactionHash
// For batch burn
const burnTx = await childTokenContract.methods
.withdrawBatch(idList, amountList)
.send({ from: userAddress })
const burnTxHash = burnTx.transactionHash


The 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 the burn transaction to be submitted before calling this function. The Proof is generated by RLP encoding the following fields -

  1. headerNumber - Checkpoint header block number containing the burn tx
  2. blockProof - Proof that the block header (in the child chain) is a leaf in the submitted merkle root
  3. blockNumber - Block number containing the burn tx on child chain
  4. blockTime - Burn tx block time
  5. txRoot - Transactions root of block
  6. receiptRoot - Receipts root of block
  7. receipt - Receipt of the burn transaction
  8. receiptProof - Merkle proof of the burn receipt
  9. branchMask - 32 bits denoting the path of receipt in merkle patricia tree
  10. receiptLogIndex - Log Index to read from the receipt

Generating proof manually can be tricky so it is advisable to use Polygon Edge. If you want to send the transaction manually, you can pass encodeAbi as true in the options object to get raw calldata.

const exitCalldata = await maticPOSClient
.exitSingleERC1155(burnTxHash, { from, encodeAbi: true })

Send this calldata to RootChainManager.

await mainWeb3.eth.sendTransaction({
from: userAddress,
to: rootChainManagerAddress,