Call a contract on chain B from chain A
To call a contract on chain B from chain A, the user needs to call callContract
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 (opens in a new tab). - The payload
bytes
to pass to the destination contract.
function callContract(
string memory destinationChain,
string memory contractAddress,
bytes memory payload
) external;
AxelarExecutable
has an execute
function that will be triggered by the Axelar network on the destination chain after the callContract
function has been executed on the source chain. This will validate the contract call and then invoke your _execute
method, where you should write any custom logic.
function _execute(
string memory sourceChain,
string memory sourceAddress,
bytes calldata payload
) internal virtual {}
ℹ️
Ensure the payload is encoded bytes
!
The payload
passed to callContract
(and ultimately to _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):
const { ethers } = require("ethers");
// encoding a string
const payload = ethers.utils.defaultAbiCoder.encode(
["string"],
["Hello from contract A"]
);
Example of payload decoding in Solidity:
function _execute(
string memory sourceChain,
string memory sourceAddress,
bytes calldata payload
) internal override {
// decoding a string
string memory _message = abi.decode(payload, (string));
}