Call a contract on chain B from chain A and attach some tokens
To call chain B from chain A and send some tokens along the way, the user needs to call callContractWithToken
on the gateway of chain A, specifying:
- The destination chain, which must be an EVM chain from Chain names.
- The destination contract address, which must inherit from
AxelarExecutable
defined in AxelarExecutable.sol. - The payload
bytes
to pass to the destination contract. - The symbol of the token to transfer, which must be a supported asset (Mainnet | Testnet).
- The amount of the token to transfer.
NOTE: The security of your contracts, and the tokens passed between them, is limited to the security of the chains they integrate with. Since blockchains can have different security practices, we recommend doing due diligence on all chains your contract will be deployed to.
As per the snippet below:
AxelarExecutable
has an executeWithToken
function that will be triggered by the Axelar network on the destination chain after the callContractWithToken
function has been executed on the source chain. This will validate the contract call and then invoke your _executeWithToken
method, where you should write any custom logic.
The destination contract will be authorized to transfer the ERC-20 identified by the tokenSymbol
.
Example
Suppose our destination contract wants to forward the token it received to a recipient provided in the payload. It could be done this way.
Ensure the payload is encoded bytes
!
The payload
passed to callContract
(and ultimately to the _execute
and _executeWithToken
) has type bytes
. Use the ABI encoder/decoder convert your data to bytes
.
Example of payload encoding in JavaScript (using ethers.js):
Example of payload decoding in Solidity:
Learn More: Explore our tutorial on building a decentralized application (dApp) for cross-chain airdrops using Solidity, Next.js, and Axelar’s General Message Passing with callContractWithToken
. This tutorial will guide you through the process of distributing tokens across multiple chains: How to Build a Cross-Chain Airdrop DApp With Solidity, Next.js, and Axelar.