logoAcademy

Use ERC-20 as Native Token (DIY)

Learn how to transfer an ERC-20 token to a new Avalanche L1 and use it as a native token via ICTT.

In this section, you will learn how to transfer an ERC-20 token from Avalanche’s C-Chain to a new Avalanche L1 using Interchain Token Transfers (ICTT) and set it up to act as the native token on the new L1. This guide will take you through the steps of configuring a local network environment, deploying the necessary contracts, and transferring tokens.

Create a new blockchain and Deploy on Local Network

Use the Avalanche CLI to create a new blockchain where you will deploy the ERC-20 as the native token.

avalanche blockchain create myblockchain  

For this exercise, create a Proof of Authority blockchain that has Native Minter Precompile activated. This precompile is not activated NOT with the default test values, so make sure to go through the manual configuration of your L1 with the Avalanche-CLI. Add the deployer address as the adminfor this, for convinience we will use the ewoq key that is already loaded as part of the Starter-Kit environment variables and which public key is 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC. Go with the default values for all other steps in the configuration process. Note: Having the Native Minter Precompile active and the correct admin rights assigned to the deployer is crucial for the rest of this lesson.

avalanche blockchain deploy myblockchain

Add all the environment variables. You can retrieve all your L1 detailes with avalanche blockchain describe myblockchain and avalanche primary describe for C-chain details.

export TELEPORTER_REGISTRY_C_CHAIN=0x...
export FUNDED_ADDRESS=0x...
export TELEPORTER_REGISTRY_L1=0x...
export L1_BLOCKCHAIN_ID_HEX=0x...
export C_CHAIN_BLOCKCHAIN_ID_HEX=0x...

Deploy an ERC-20 Contract on C-Chain

Use the Avalanche CLI to create a new blockchain where you will deploy the ERC-20 as the native token.

You will deploy an ERC-20 token on the Avalanche C-Chain, which will later be transferred and used as the native token on the new L1.

forge create --rpc-url local-c --private-key $PK lib/avalanche-interchain-token-transfer/contracts/src/mocks/ExampleERC20Decimals.sol:ExampleERC20Decimals --constructor-args 18

Save the newly created ERC20 address to the environment variables

export $ERC20_HOME_C_CHAIN=0x...

Deploy Interchain Token Transfer Contracts

Set up the home and remote transferer contracts for transferring tokens between the C-Chain and the newly created L1.

  • ERC20TokenHome Contract on C-Chain
forge create --rpc-url local-c --private-key $PK lib/avalanche-interchain-token-transfer/contracts/src/TokenHome/ERC20TokenHome.sol:ERC20TokenHome --constructor-args $TELEPORTER_REGISTRY_C_CHAIN $FUNDED_ADDRESS $ERC20_HOME_C_CHAIN 18

Export to environment variables:

export ERC20_HOME_TRANSFERER_C_CHAIN=0x...
  • NativeTokenRemote Contract on myblockchain
forge create --rpc-url myblockchain --private-key $PK lib/avalanche-interchain-token-transfer/contracts/src/TokenRemote/NativeTokenRemote.sol:NativeTokenRemote --constructor-args "($TELEPORTER_REGISTRY_L1, $FUNDED_ADDRESS, $C_CHAIN_BLOCKCHAIN_ID_HEX, $ERC20_HOME_TRANSFERER_C_CHAIN, 18)" "EXMP.b" 100000000000000000000 0 false 0

Add to the enviroment variables

export NATIVE_TOKEN_REMOTE_L1=0x...

Note: When deploying the NativeTokenRemote contract on the L1, ensure that the initial amount matches the native token amount that was minted when the blockchain was created. This ensures consistency between the native token supply and the remote token counterpart.

Granting Native Minting Rights to NativeTokenRemote Contract

To ensure that the NativeTokenRemote contract can mint native tokens on the L1 when ERC-20 tokens are transferred from the C-Chain, the contract must be granted minting rights. This is done by adding the NativeTokenRemote contract address to the Native Minter Precompile.

  1. You will need to interact with the Native Minter Precompile, which resides at a fixed address on all Avalanche L1s:
    Native Minter Precompile Address: 0x0200000000000000000000000000000000000001

  2. Use the following command to grant the NativeTokenRemote contract minting rights by setting it as an enabled address on the Native Minter Precompile:

cast send --rpc-url myblockchain --private-key $PK 0x0200000000000000000000000000000000000001 "setEnabled(address)" $NATIVE_TOKEN_REMOTE_L1

Once this step is completed, the NativeTokenRemote contract will have the necessary permissions to mint native tokens when ERC-20 tokens are transferred from the C-Chain.

Register Remote Token with Home Transferer

Register the remote token on the home chain so that it recognizes the transferer contracts.

cast send --rpc-url myblockchain --private-key $PK $NATIVE_TOKEN_REMOTE_L1 "registerWithHome((address, uint256))" "(0x0000000000000000000000000000000000000000, 0)"

Collateralize and Transfer Tokens

Add collateral to the transferer contract on the home chain, and then send the ERC-20 tokens across chains.

  • Approve Tokens for Transfer
    Approve a certain number of tokens to be used by the Home Transferer.
cast send --rpc-url local-c --private-key $PK $ERC20_HOME_C_CHAIN "approve(address, uint256)" $ERC20_HOME_TRANSFERER_C_CHAIN 2000000000000000000000
  • Add Collateral and Send Tokens
    Add collateral to the transferer contract.
cast send --rpc-url local-c --private-key $PK $ERC20_HOME_TRANSFERER_C_CHAIN "addCollateral(bytes32, address, uint256)" $L1_BLOCKCHAIN_ID_HEX $NATIVE_TOKEN_REMOTE_L1 100000000000000000000

Send tokens to the L1

cast send --rpc-url local-c --private-key $PK $ERC20_HOME_TRANSFERER_C_CHAIN "send((bytes32, address, address, address, uint256, uint256, uint256, address), uint256)" "(${L1_BLOCKCHAIN_ID_HEX}, ${NATIVE_TOKEN_REMOTE_L1}, ${FUNDED_ADDRESS}, ${ERC20_HOME_C_CHAIN}, 0, 0, 250000, 0x0000000000000000000000000000000000000000)" 1000000000000000000000

Conclusion

Follow the steps above to transfer an ERC-20 token from the C-Chain to your custom Avalanche L1 and use it as the native token. This exercise will demonstrate how Avalanche’s Interchain Token Transfer (ICTT) system works, ensuring that tokens are properly locked, transferred, and minted across multiple chains.

For more detailed information, refer to the official Avalanche ICTT documentation.

On this page