Programmatically Create a Canonical Interchain Token Using the Interchain Token Service
If you have an ERC-20 token on one or more blockchains and you want to make the token interoperable across chains, the Interchain Token Service provides a solution. You can transform an ERC-20 token into an Interchain Token by deploying a token manager.
If you would like to create a wrapped, bridgeable version of your ERC-20 token on other chains, you can register it as a Canonical Interchain Token using the InterchainTokenFactory
contract.
Each token can only be registered once as a Canonical Interchain Token. This ensures unique and streamlined token management across different blockchains. Though you can register your Canonical Interchain Token directly through the Interchain Token Portal, there are times where you may want to do so programmatically, such as when you have already deployed a token on one chain and wish to deploy a wrapped version of that token on another chain.
In this tutorial, you will learn how to:
- Programmatically create a Canonical Interchain Token from scratch using Axelar’s Interchain Token Service
- Register a Canonical Interchain Token on the Fantom chain
- Deploy remote Canonical Interchain Token on the Polygon chain
- Transfer your token between Fantom and Polygon
Prerequisites
You will need:
- A basic understanding of Solidity and JavaScript
- A wallet with FTM and MATIC funds for testing. If you don’t have these funds, you can get FTM from the Fantom faucet and MATIC from the Mumbai faucets (1, 2).
Deploy an ERC-20 token on the Fantom testnet
Create a SimpleERC20 token and give it a name and symbol. You can skip this step if you already have an ERC-20 token deployed on the Fantom testnet.
Set up your development environment
Create and initialize a project
Open up your terminal and navigate to any directory of your choice. Run the following commands to create and initiate a project:
Install Hardhat and the AxelarJS SDK
Install Hardhat and the AxelarJS SDK with the following commands:
Set up project ABIs
Next, set up the ABIs for the Interchain Token Service, Interchain Token Factory, and the contract from the token you deployed.
Create a folder named utils
. Inside the folder, create the following new files and add the respective ABIs:
- Add the Interchain Token Service ABI to
interchainTokenServiceABI.json
. - Add the Interchain Token Factory ABI to
interchainTokenFactoryABI.json
. - Add your custom token ABI to
customTokenABI.json
. You can get this from FTMScan with the address of your deployed token.
Set up an RPC for the local chain
Back in the root directory, set up an RPC for the Fantom testnet. You will use this as your local (source) chain.
Create an .env
file
To make sure you’re not accidentally publishing your private key, create an .env
file to store it in:
Add your private key to .env
and hardhat.config.js
Export your private key and add it to the .env
file you just created:
If you will push this project on GitHub, create a .gitignore
file and include .env
.
Then, create a file with the name hardhat.config.js
and add the following code snippet:
Register a Canonical Interchain Token on a local chain
Now that you have set up an RPC for the Fantom testnet, you can register a Canonical Interchain Token.
Create a canonicalInterchainToken.js
script
Create a new file named canonicalInterchainToken.js
and import the required dependencies:
Ethers.js
- The AxelarJS SDK
- The custom token contract ABI
- The address of the
InterchainTokenService
contract - The address of the
InterchainTokenFactory
contract
Get the signer
Next, create a getSigner()
function in canonicalInterchainToken.js
. This will obtain a signer for a secure transaction:
Get the contract instance
Then, create a getContractInstance()
function in canonicalInterchainToken.js
. This will get the contract instance based on the contract’s address, ABI, and signer:
Register Canonical Interchain Token
Now you’re ready to register your token as a Canonical Interchain Token! Create a registerCanonicalInterchainToken()
function for the Fantom testnet. This will register a Canonical Interchain Token with your custom token address:
Add a main()
function
Add a main()
function to execute the canonicalInterchainToken.js
script. It will handle any errors that may arise:
Run the canonicalInterchainToken.js
script to deploy to Fantom
Run the script in your terminal to register and deploy the token, specifying the fantom
testnet:
If you see something similar to the following on your console, you have successfully registered your token as a Canonical Interchain Token.
Store the token ID
Copy the token ID and store it somewhere safe. You will need it to initiate a remote token transfer in a later step.
Check the transaction on the Fantom testnet scanner
Check the Fantom testnet scanner to see if you have successfully registered your token as a Canonical Interchain Token.
Deploy Remote Canonical Interchain Token
You’ve just successfully a Canonical Interchain Token to Fantom, which you are using as your local chain. Now, deploy the token remotely to Polygon, which will be the remote chain in this tutorial. Remember that you can specify any two chains to be your local and remote chains.
Estimate gas fees
In canonicalInterchainToken.js
, call estimateGasFee()
from the AxelarJS SDK to estimate the actual cost of deploying your remote Canonical Interchain Token on a remote chain:
Perform remote token deployment
Create a deployRemoteCanonicalInterchainToken()
function that will perform token remote canonical deployment on the Polygon Mumbai testnet.
Update main()
to deploy to remote chains
Update main()
to execute deployRemoteCanonicalInterchainToken()
:
Run the canonicalInterchainToken.js
script to deploy to Polygon
Run the script in your terminal to to deploy remote Canonical Interchain Token, once again specifying the fantom
testnet (the source chain where all transactions are taking place):
You should see something similar to the following on your console:
Check the transaction on the Axelar testnet scanner
Check the Axelarscan testnet scanner to see if you have successfully deployed the remote Canonical Interchain Token “MAT” on the Polygon Mumbai testnet. It should look something like this. Make sure that Axelar shows a successful transaction before continuing on to the next step.
Transfer your token between chains
Now that you have registered and deployed a Canonical Interchain Token both locally to Fantom and remotely to Polygon, you can transfer between those two chains via the interchainTransfer()
method.
Initiate a remote token transfer
In canonicalInterchainToken.js
, create a transferTokens()
function that will facilitate remote token transfers between chains. Change the token ID to the tokenId
that you saved from an earlier step, and change the address in transfer
to your own wallet address:
Update main()
to execute token transfer
Update the main()
to execute transferTokens()
:
Run the canonicalInterchainToken.js
script to transfer tokens
Run the script in your terminal, specifying the fantom
testnet:
You should see something similar to the following on your console:
If you see this, it means that your interchain transfer has been successful! 🎉
Note: If you get the following nonce too low
error, wait a few minutes
and run canonicalInterchainToken.js
again. Some chains have a longer
transaction time than others.
Check the transaction on the Axelar testnet scanner
Check the Axelarscan testnet scanner to see if you have successfully transferred MAT from the Fantom testnet to the Polygon testnet. It should look something like this.
Import the new token into your wallet
You can also import the new token into your own wallet with its contract address that you saved from FTMScan.
Congratulations!
You have now programmatically created a Canonical Interchain Token using Axelar’s Interchain Token Service and transferred it between two chains. You should now be able to confidently create and manage your own Interchain Tokens, opening up a wide range of possibilities for token transfers and asset bridges.
Great job making it this far! To show your support to the author of this tutorial, please post about your experience and tag @axelarnetwork on Twitter (X).
What’s next
You can also explore other functionalities of the Interchain Token Service, such as:
- Transforming an existing token into an Interchain token
- Turn deployed tokens on multiple chains into Interchain Tokens