description |
---|
Send a message to any Hyperlane supported network |
Developers can send interchain messages by calling Mailbox.dispatch()
.
{% hint style="warning" %}
Hyperlane can only deliver messages to smart contracts that implement the IMessageRecipient
interface. See the receive.md documentation for more information.
{% endhint %}
/**
* @notice Dispatches a message to the destination domain & recipient.
* @param _destinationDomain Domain of destination chain
* @param _recipientAddress Address of recipient on destination chain as bytes32
* @param _body Raw bytes content of message body
* @return The message ID inserted into the Mailbox's merkle tree
*/
function dispatch(
uint32 _destination,
bytes32 _recipient,
bytes calldata _body
) external returns (bytes32) {
See addressesand domains for Mailbox
contract addresses, and chain domain IDs, respectively.
{% hint style="info" %}
Recipient addresses are left-padded to bytes32
for compatibility with virtual machines that are addressed differently.
{% endhint %}
The following utility is provided in the TypeCasts
library for convenience.
// alignment preserving cast
function addressToBytes32(address _addr) internal pure returns (bytes32) {
return bytes32(uint256(uint160(_addr)));
}
Delivering a message to its destination requires submitting a transaction on the destination chain. If you want to have relayer.md deliver the message on your behalf, you can pay for the gas for this transaction on the origin chain.
Learn more about paying-for-interchain-gas.md.
The code snippet below shows an example of sending a message from Ethereum to Avalanche.
{% hint style="warning" %} Note this example does not pay for interchain gas, and therefore would not be delivered automatically by a relayer. {% endhint %}
uint32 constant avalancheDomain = 43114;
address constant avalancheRecipient = 0x36FdA966CfffF8a9Cdc814f546db0e6378bFef35;
address constant ethereumMailbox = 0x2f9db5616fa3fad1ab06cb2c906830ba63d135e3;
IMailbox(ethereumMailbox).dispatch(
avalancheDomain,
addressToBytes32(avalancheRecipient),
bytes("hello avalanche from ethereum")
);