The contracts in the following example consist of two main parts, one for the root chain (Sepolia in this case), and one for the child chain (Amoy). The Sepolia contract is responsible for mapping tokens from the root chain to
the child chain and sending deposits to the child chain. The Amoy contract is responsible for handling incoming messages from the Sepolia contract, minting child tokens, and processing child-to-root withdrawals.
Here is an overview of the following contracts used:
SafeToken (Sepolia): This contract deploys a simple ERC20 token that can be used to test the FxPortal functionality.
FxERC20 (Sepolia): This contract template will create new child tokens on the Amoy network. The contract is initialized with the address of the FxPortal contract and the address of the corresponding token on the root network. It contains a mint function that the root network contract to mint new child tokens can use.
FxERC20 (Amoy): This contract is a child token minted by the root network contract. It is initialized with the address of the FxPortal contract, the address of the corresponding token on the root network, and the token’s name, symbol, and decimals. It contains a burn function that can be used to burn child tokens.
FxERC20RootTunnel (Sepolia): This contract is responsible for mapping tokens from the root network to the child network and sending deposits to the child network. When a new token is mapped, the contract deploys a new child token contract using the FxERC20 template and then maps the root token to the child token. The contract sends a message to the child network containing the deposit details when a deposit is made.
FxERC20ChildTunnel (Amoy): This contract is responsible for processing messages from the root network and minting new child tokens, as well as handling child-to-root withdrawals. When a new token is mapped, the contract deploys a new child token contract using the FxERC20 template and then maps the root token to the child token. When a deposit message is received, the contract mints new child tokens and calls the onTokenTransfer function on the receiver (if it is a contract). When a withdrawal is made, the contract burns the child tokens and sends a message to the root network containing the withdrawal details.
Prerequisites:
Sepolia and Amoy testnet accounts with some testnet ETH and MATIC tokens, respectively
Metamask wallet
Steps:
Deploy the SafeToken contract on the Sepolia testnet.
Deploy an FxERC20 contract on the Sepolia network.
Deploy an FxERC20Child contract on the Amoy network.
Deploy an FxERC20RootTunnel contract on the Sepolia network.
Constructor arguments:
_checkpointManager : 0xbd07D7E1E93c8d4b2a261327F3C28a8EA7167209
_fxRoot : 0x0E13EBEdDb8cf9f5987512d5E081FdC2F5b0991e
_fxERC20Token : FxERC20 address (address of FxERC20 template created in step 2)
Deploy an FxERC20ChildTunnel contract on the Amoy network.
Constructor arguments:
_fxChild : 0xE5930336866d0388f0f745A2d9207C7781047C0f
_tokenTemplate : _fxERC20Token : FxERC20 address (address of FxERC20 template created in step 3)
On the FxERC20ChildTunnel contract (step 5), set the setFxRootTunnel function. This should be done with the FxERC20RootTunnel contract address from step 4.
On the FxERC20RootTunnel contract (step 4), set the setFxChildTunnel function. This should be done with the FxERC20ChildTunnel contract address from step 5.
On the SafeToken contract from step 1, call the mint function to mint the desired amount of tokens.
On the SafeToken contract, call the approve function with the FxERC20RootTunnel contract address from step 4 and the mint amount from step 8.
On the FxERC20RootTunnel contract from step 4, call the deposit function with the SafeToken on the Sepolia network from step 1, the user address, the mint amount from step 9, and the data parameter 0x00.
Additional steps:
On the FxERC20ChildTunnel contract from step 5, call the balanceOf function to check that the child tokens have been minted.
To withdraw the child tokens back to the Sepolia network, call the withdraw function on the FxERC20ChildTunnel contract with the amount of child tokens to be burned.
Wait for the transaction to be confirmed and the withdrawal to be processed. You can check the status of the withdrawal using the FxExplorer.
On the SafeToken contract from step 1, call the balanceOf function to check that the corresponding amount of tokens has been deposited on the Sepolia network.
Congratulations, you have successfully tested the FxPortal functionality for transferring tokens between the Sepolia and Amoy networks!
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article