This article explores Avalanche ICTT. The diagrams walk through each stage of the process so that readers without an engineering background can understand the overall system. For developers, the article also examines the underlying source code in detail.
For a more detailed treatment of Avalanche ICTT, see the following book.
Avalanche Interchain Token Transfer (ICTT) is an application for transferring tokens between different L1 blockchains. A set of smart contracts deployed across multiple L1s implements the system, using ICM (Interchain Messaging) for cross-chain communication.
For more on ICM (Interchain Messaging), see the following resources.
ICTT consists of one TokenHome contract and one or more TokenRemote contracts.
The Home contract is deployed on the L1 where the original token exists (the Home chain) and manages that token. The TokenRemote contract is deployed on the destination L1 (the Remote chain) and represents a wrapped version of the token being transferred.
The original diagrams are retained below; their labels may be in Japanese.
Contract architecture
The inheritance relationships between the contracts are shown below.
The TokenHome contract contains the core implementation, the functionality shared by ERC20 and native tokens, and the definitions of the required functions. The corresponding UpgradeableContract implementations that inherit TokenHome handle ERC20-specific and native-token-specific operations. The ERC20TokenHome and NativeTokenHome contracts inherit those implementations without further changes. Custom functionality can be added at that level.
Token transfers
A transfer locks tokens on the Home chain and issues new wrapped tokens on the Remote chain.
The user requests that the TokenHome contract be granted permission to transfer the tokens they hold.
The permission to transfer the tokens is granted.
The user requests a token transfer to the Remote chain.
The tokens are locked in the TokenHome contract and transferred to the Remote chain.
The TokenHome contract tracks the token balances transferred to each TokenRemote contract across the L1s. When tokens are returned to TokenHome, the original tokens locked in that contract are released to the user.
The user requests that the tokens transferred to the Remote chain be returned.
The tokens are transferred from the Remote chain to the Home chain.
The locked tokens are returned to the user.
A TokenHome contract can handle either an ERC20 token or a native token and transfer it to any previously registered TokenRemote contract. Tokens on the Remote chain may also be ERC20 or native tokens, allowing the combinations of token types between Home and Remote chains illustrated below.
Multi-hop transfers are also supported. For example, to transfer between two Remote chains whose TokenRemote contracts are connected to the same TokenHome contract, the first Remote chain's TokenRemote sends a request to TokenHome. Upon receiving the request, TokenHome transfers the tokens to the second Remote chain's TokenRemote contract.
In addition to basic token transfers, ICTT provides a sendAndCall interface that combines token transfer and use of the tokens by a smart contract within a single ICM message. If the destination smart contract call fails, the transferred tokens are sent to a fallback address on the destination chain. The sendAndCall interface makes it possible to use transferred tokens directly in dApps on other chains, for example to execute swaps or pay service fees.
Transfer workflow
ICTT uses ICM. As a prerequisite, cross-chain communication passes through Teleporter, the Warp Precompile, and a Relayer, as described below. For the full ICM workflow, see the following article.
This abstracts native-token handling by converting native tokens into wrapped tokens so they can be processed through the same workflow as ERC20 tokens.
TokenRemote contract
The ERC20 implementation of the TokenRemote contract inherits ERC20, so the ERC20 token name and symbol must be supplied at deployment.
For native tokens, the native token itself is minted.
The TokenRemoteSettings also contains the Teleporter contract information, the Home chain ID, and the TokenHome contract address, as shown below. ICM (Interchain Messaging) requires this information.
The implementation branches according to the received message type (in this case, REGISTER_REMOTE). When registering a TokenRemote contract with TokenHome, the _registerRemote function runs. Its arguments include the source L1 chain ID and the source contract address (the TokenRemote contract).
After performing several validation checks, _registerRemote registers the TokenRemote contract. It also accounts for differences in the number of decimals used by the ERC20 tokens.
contracts/ictt/TokenHome/TokenHome.sol#L170-L225
bytes32 remoteBlockchainID, address remoteTokenTransferrerAddress, RegisterRemoteMessage memory message ) internal { TokenHomeStorage storage $ = _getTokenHomeStorage(); require(remoteBlockchainID != bytes32(0), "TokenHome: zero remote blockchain ID"); require( remoteBlockchainID != $._blockchainID, "TokenHome: cannot register remote on same chain" ); require( remoteTokenTransferrerAddress != address(0), "TokenHome: zero remote token transferrer address" ); require( !$._registeredRemotes[remoteBlockchainID][remoteTokenTransferrerAddress].registered, "TokenHome: remote already registered" ); require( message.remoteTokenDecimals <= TokenScalingUtils.MAX_TOKEN_DECIMALS, "TokenHome: remote token decimals too high" ); require( message.homeTokenDecimals == $._tokenDecimals, "TokenHome: invalid home token decimals" ); (uint256 tokenMultiplier, bool multiplyOnRemote) = TokenScalingUtils .deriveTokenMultiplierValues($._tokenDecimals, message.remoteTokenDecimals); // Calculate the collateral needed in home token denomination. uint256 collateralNeeded = TokenScalingUtils.removeTokenScale( tokenMultiplier, multiplyOnRemote, message.initialReserveImbalance ); // Round up the collateral needed by 1 in the case that {multiplyOnRemote} is true and // {initialReserveImbalance} is not divisible by the {tokenMultiplier} to // ensure that the full amount is accounted for. if (multiplyOnRemote && message.initialReserveImbalance % tokenMultiplier != 0) { collateralNeeded += 1; } $._registeredRemotes[remoteBlockchainID][remoteTokenTransferrerAddress] = RemoteTokenTransferrerSettings({ registered: true, collateralNeeded: collateralNeeded, tokenMultiplier: tokenMultiplier, multiplyOnRemote: multiplyOnRemote }); emit RemoteRegistered( remoteBlockchainID, remoteTokenTransferrerAddress, collateralNeeded, message.remoteTokenDecimals ); }
When deploying a TokenRemote contract, you can specify a value called initialReserveImbalance_. This value specifies the initial amount of tokens issued.
On the TokenHome side, _addCollateral is executed. Depositing the specified amount of the ERC20 token associated with TokenHome as collateral satisfies the initial issuance requirement.
contracts/ictt/TokenHome/TokenHome.sol#L449-L486
bytes32 remoteBlockchainID, address remoteTokenTransferrerAddress, uint256 amount ) internal sendNonReentrant { TokenHomeStorage storage $ = _getTokenHomeStorage(); RemoteTokenTransferrerSettings memory remoteSettings = $._registeredRemotes[remoteBlockchainID][remoteTokenTransferrerAddress]; require(remoteSettings.registered, "TokenHome: remote not registered"); require(remoteSettings.collateralNeeded > 0, "TokenHome: zero collateral needed"); // Deposit the full amount, and withdraw back to the sender if there is excess. amount = _deposit(amount); // Calculate the remaining collateral needed, any excess amount, and adjust // {amount} to represent the amount of tokens added as collateral. uint256 remainingCollateralNeeded; uint256 excessAmount; if (amount >= remoteSettings.collateralNeeded) { remainingCollateralNeeded = 0; excessAmount = amount - remoteSettings.collateralNeeded; amount = remoteSettings.collateralNeeded; } else { remainingCollateralNeeded = remoteSettings.collateralNeeded - amount; } // Update the remaining collateral needed. $._registeredRemotes[remoteBlockchainID][remoteTokenTransferrerAddress].collateralNeeded = remainingCollateralNeeded; emit CollateralAdded( remoteBlockchainID, remoteTokenTransferrerAddress, amount, remainingCollateralNeeded ); // If there is excess amount, send it back to the sender. if (excessAmount > 0) { _withdraw(_msgSender(), excessAmount); } }
The original article describes collateralNeeded being true as indicating that the initial issuance has been collateralized. See the linked implementation for the actual condition.
When the TokenHome contract sends a request to issue tokens on the Remote chain (through the TokenRemote contract), it checks collateralNeeded as shown below.
Users therefore cannot transfer tokens to the relevant Remote chain until the initial issuance has been covered.
You can also specify 0, which indicates that there is no initial issuance. In this case, users can transfer tokens to the Remote chain immediately after the TokenRemote contract is deployed.
Approval
To transfer ERC20 tokens from the Home chain to the Remote chain, the user must first approve the TokenHome contract to spend the transfer amount of its associated ERC20 token. This approve allows the TokenHome contract to transfer (transferFrom) the user's funds on their behalf.
Native-token transfers do not require approve.
Executing a transfer
The user submits a request to transfer ERC20 tokens from the Home chain to the Remote chain.
Native-token transfers follow almost the same workflow.
After checking the execution conditions, _prepareSend transfers the ERC20 tokens to the TokenHome contract and locks them there.
contracts/ictt/TokenHome/TokenHome.sol#L755-L788
bytes32 remoteBlockchainID, address remoteTokenTransferrerAddress, uint256 amount, address primaryFeeTokenAddress, uint256 feeAmount ) private returns (uint256, uint256) { TokenHomeStorage storage $ = _getTokenHomeStorage(); RemoteTokenTransferrerSettings memory remoteSettings = $._registeredRemotes[remoteBlockchainID][remoteTokenTransferrerAddress]; require(remoteSettings.registered, "TokenHome: remote not registered"); require(remoteSettings.collateralNeeded == 0, "TokenHome: collateral needed for remote"); // Deposit the funds sent from the user to the token transferrer, // and set to adjusted amount after deposit. amount = _deposit(amount); if (feeAmount > 0) { feeAmount = SafeERC20TransferFrom.safeTransferFrom( IERC20(primaryFeeTokenAddress), _msgSender(), feeAmount ); } // Scale the amount based on the token multiplier for the given TokenRemote instance. uint256 scaledAmount = TokenScalingUtils.applyTokenScale( remoteSettings.tokenMultiplier, remoteSettings.multiplyOnRemote, amount ); require(scaledAmount > 0, "TokenHome: zero scaled amount"); // Increase the balance of the TokenRemote instance by the scaled amount. $._transferredBalances[remoteBlockchainID][remoteTokenTransferrerAddress] += scaledAmount; return (scaledAmount, feeAmount); }
The safeTransferFrom function used here is implemented in a library called SafeERC20TransferFrom. It transfers the token amount from the address specified by from (the user's address) to address(this) (the TokenHome contract), using the amount specified by amount.
bytes32 sourceBlockchainID, address originSenderAddress, bytes memory message ) internal override { TokenRemoteStorage storage $ = _getTokenRemoteStorage(); require( sourceBlockchainID == $._tokenHomeBlockchainID, "TokenRemote: invalid source blockchain ID" ); require( originSenderAddress == $._tokenHomeAddress, "TokenRemote: invalid origin sender address" ); TransferrerMessage memory transferrerMessage = abi.decode(message, (TransferrerMessage)); // If the contract was not previously known to be registered or collateralized, it is now given that // the home has sent a message to mint funds. if (!$._isRegistered || !$._isCollateralized) { $._isRegistered = true; $._isCollateralized = true; } // Remote contracts should only ever receive single-hop messages because // multi-hop messages are always routed through the home contract. if (transferrerMessage.messageType == TransferrerMessageType.SINGLE_HOP_SEND) { SingleHopSendMessage memory payload = abi.decode(transferrerMessage.payload, (SingleHopSendMessage)); _withdraw(payload.recipient, payload.amount); } else if (transferrerMessage.messageType == TransferrerMessageType.SINGLE_HOP_CALL) { // The {sourceBlockchainID}, and {originSenderAddress} specified in the message // payload will not match the sender of this Teleporter message in the case of a // multi-hop message. Since Teleporter messages are only received from the specified // token TokenHome instance, no additional authentication is needed on the payload values. SingleHopCallMessage memory payload = abi.decode(transferrerMessage.payload, (SingleHopCallMessage)); _handleSendAndCall(payload, payload.amount); } else { revert("TokenRemote: invalid message type"); } }
The _withdraw function calls _mint to deliver the specified amount of ERC20 tokens to the designated address (recipient), using the amount specified by amount.
On TokenHome, the Teleporter contract calls _receiveTeleporterMessage. Because the message type is SINGLE_HOP_SEND, the following processing takes place.
Internally, the _withdraw function delivers the specified ERC20 token amount to the requesting user's address (recipient), using the amount specified by amount.
This is the reverse of issuing tokens through the TokenHome contract.
Executing additional logic
A token transfer can also execute arbitrary additional logic. To do this, call the sendAndCall function below. Pass the information about the operation you want to execute, such as calling a specific function on a specific Remote-chain contract, through the input argument.
The input argument stores the following SendAndCallInput information. Supply the target contract address as recipientContract and the byte-encoded execution information as recipientPayload.
On the Remote chain, the TokenRemote contract's _receiveTeleporterMessage function invokes the handler for the SINGLE_HOP_CALL message type (the _handleSendAndCall function).
// The {sourceBlockchainID}, and {originSenderAddress} specified in the message // payload will not match the sender of this Teleporter message in the case of a // multi-hop message. Since Teleporter messages are only received from the specified // token TokenHome instance, no additional authentication is needed on the payload values. SingleHopCallMessage memory payload = abi.decode(transferrerMessage.payload, (SingleHopCallMessage)); _handleSendAndCall(payload, payload.amount); } else { revert("TokenRemote: invalid message type"); } }
In the input argument, set destinationBlockchainID to the chain ID of the other Remote chain. Set destinationTokenTransferrerAddress to the address of its TokenRemote contract.
Inside _send, the previously registered Home chain ID differs from the value of input.destinationBlockchainID supplied in the argument. The contract therefore identifies the request as a multi-hop transfer and invokes _processSendMultiHop.
The ICM message is addressed to the previously registered TokenHome contract on the Home chain. TokenHome acts as a hub, forwarding transfer requests between Remote chains.
On the Home chain, the Teleporter contract calls the _receiveTeleporterMessage function on TokenHome. In this example, the message type is MULTI_HOP_CALL, so the following processing takes place.
contracts/ictt/TokenHome/TokenHome.sol#L558-L590
MultiHopCallMessage memory payload = abi.decode(transferrerMessage.payload, (MultiHopCallMessage)); (uint256 homeAmount, uint256 fee) = _processMultiHopTransfer( sourceBlockchainID, originSenderAddress, payload.amount, payload.secondaryFee ); // For a multi-hop send, the fee token address has to be {tokenAddress}, // because the fee is taken from the amount that has already been deposited. // For ERC20 tokens, the token address of the contract is directly passed. // For native assets, the contract address is the wrapped token contract. _routeMultiHopSendAndCall({ sourceBlockchainID: sourceBlockchainID, originTokenTransferrerAddress: originSenderAddress, originSenderAddress: payload.originSenderAddress, input: SendAndCallInput({ destinationBlockchainID: payload.destinationBlockchainID, destinationTokenTransferrerAddress: payload.destinationTokenTransferrerAddress, recipientContract: payload.recipientContract, recipientPayload: payload.recipientPayload, requiredGasLimit: payload.secondaryRequiredGasLimit, recipientGasLimit: payload.recipientGasLimit, multiHopFallback: payload.multiHopFallback, fallbackRecipient: payload.fallbackRecipient, primaryFeeTokenAddress: $._tokenAddress, primaryFee: fee, secondaryFee: 0 }), amount: homeAmount }); return; } else if (transferrerMessage.messageType == TransferrerMessageType.REGISTER_REMOTE) { RegisterRemoteMessage memory payload =
From this point, the workflow is the same as a normal request from TokenHome to TokenRemote. Routing transfers through TokenHome allows each TokenRemote contract to issue tokens based on TokenHome.
The workflow is illustrated below.
Conclusion
This article covered Avalanche ICTT.
I also write about other technical topics, which you can explore through the following resources.