Mapping Assets using POS
Introduction
Assets can be transferred in between root chain & child chain. Let's be first clear regarding nomenclature
- Root chain/ Base chain/ Parent chain/ Layer 1 :: all are same, referring to either Goerli or Ethereum Mainnet
- Child chain/ Layer 2 :: refers to either Matic Mumbai or Matic Matic Mainnet
For assets i.e. ERC20, ERC721, ERC1155 to be transferrable in between chains, we need to be following certain guidelines
- Assets must have required predicate contracts deployed
- Asset contract need to deployed on root chain
- Modified version of asset contract needs to be deployed on child chain
- Then they need to be mapped by calling
RootChainManager.mapToken(...)
, which can only be performed by certain accounts
For mapping i.e. the final step, make sure you check below
Walk through
Here we're going to modify child smart contract, given root smart contract, for making it mapping eligible.
Root Token Contract
Let's moidfy this smart contract & use it as our root token contract.
Lets say we've just deployed this on Goerli Testnet at 0x...
.
Child Token Contract
Now we need to add two functions in above defined smart contract i.e. {deposit
, withdraw
}.
Why ?
For transferring assets from root chain to child chain, we need to call RootChainManager.depositFor(...)
, which will eventually ask StateSender.syncState(...)
, to transfer this asset from root chain to child chain, by emitting StateSynced
event.
But before that make sure you've approved RootChainManagerProxy
to spend equal amount of token(s), so that it can call RootERC20.transferFrom
& start deposit.
Once this event is emitted, our Heimdal Nodes, which keep monitoring root chain periodically, will pick up StateSynced
event & perform call to onStateReceive
function of target smart contract. Here our target smart contract is nothing but ChildChainManager.onStateReceive
.
deposit
method which we're going to add in our smart contract, is going to be called by ChildChainManagerProxy
& can only be called by this one.
withdraw
method to be called on child smart contract, which will be check pointed & published on root chain as Merkel Root Proof, which then can be finally exitted by calling RootChainManager.exit
, while submitting proof.
- Token minting happens in
deposit
method. - Tokens to be burnt in
withdraw
method.
These rules need to followed to keep balance of assets between two chains, otherwise it'll be assets created from thin air.
Note: No token minting in constructor of child token contract.
Implementation
As we now know, why we need to implement deposit
& withdraw
methods in child token contract, we can proceed for implementing it.
But one thing you might notice, deposit
function, in our implementation can be called by anyone, which must not happen. So we're going to make sure it can only be called by ChildChainManagerProxy
.
This updated implementation can be used for mapping.
Steps :
- First deploy root token on root chain i.e. {Goerli, Ethereum Mainnet}
- Modify root token by adding
deposit
&withdraw
functions & deploy corresponding child token on child chain i.e. {Matic Mumbai, Matic Mainnet} - Then submit a mapping request, to be resolved by team.
Request Submission
Please go through this.