The following example will show you how to send an ERC20 Token on C-chain to an Avalanche L1 as an ERC20 token using Interchain Messaging and Foundry. This demo is conducted on a local network run by the CLI, but can be applied to Fuji Testnet and Avalanche Mainnet directly.
All Avalanche Interchain Token Transfer contracts and interfaces implemented in this example implementation are maintained in the avalanche-interchain-token-transfer repository.
If you prefer full end-to-end testing written in Golang for bridging ERC20s, native tokens, or any combination of the two, you can view the test workflows directly in the avalanche-interchain-token-transfer repository.
Deep dives on each template interface can be found here.
Disclaimer: The avalanche-interchain-token-transfer contracts used in this tutorial are under active development and are not yet intended for production deployments. Use at your own risk.
You can run them directly on Github by clicking Code, switching to the Codespaces tab and clicking Create codespace on main. A new window will open that loads the codespace. Afterwards you will see a browser version of VS code with all the dependencies installed. Codespace time out after some time of inactivity, but can be restarted.
For convenience the private key 56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027 of the default airdrop address is stored in the environment variable $PK in .devcontainer/devcontainer.json. Furthermore, the RPC url for the C-Chain local-c and Avalanche L1 created with the name myblockchain on the local network is set in the foundry.toml file.
To get started, create an Avalanche L1 configuration named "myblockchain":
Your Avalanche L1 should have the following things:
Interchain Messaging enabled
CLI should run an AWM Relayer
Upon Avalanche L1 deployment, 100 tokens should be airdropped to the default ewoq address (0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC)
Finally, deploy your Avalanche L1:
The CLI will output addresses and information that will be important for the rest of the tutorial:
From this output, take note of the Funded Address (with 100 tokens) and set the parameter as environment variable so that we can manage them easily and also use them in the commands later.
First step is to deploy the ERC20 contract. We are using OZs example contract here and the contract is renamed to ERC20.sol for convenience. You can use any other pre deployed ERC20 contract or change the names according to your Avalanche L1 native token as well.
Now, make sure to add the contract address in the environment variables.
If you deployed the above example contract, you should see a balance of 100,000 tokens when you run the below command:
We will deploy two Interchain Token Transfer contracts. One of the source chain (which is C-chain in our case) and another on the destination chain (myblockchain in our case). This will be done by a single command with the Avalanche CLI
Go
Save the Remote contract address in the environment variables.
Now that the Avalanche Interchain Token Transfer contracts have been deployed, transfer some ERC20 tokens TOK from C-Chain to myblockchain with the following command