Skip to main content

发送消息

合约可以通过调用Mailbox上的dispatch函数来发送链间消息。

IMailbox Interface
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;

import {IInterchainSecurityModule} from "./IInterchainSecurityModule.sol";
import {IPostDispatchHook} from "./hooks/IPostDispatchHook.sol";

interface IMailbox {
// ============ Events ============
/**
* @notice Emitted when a new message is dispatched via Hyperlane
* @param sender The address that dispatched the message
* @param destination The destination domain of the message
* @param recipient The message recipient address on `destination`
* @param message Raw bytes of message
*/
event Dispatch(
address indexed sender,
uint32 indexed destination,
bytes32 indexed recipient,
bytes message
);

/**
* @notice Emitted when a new message is dispatched via Hyperlane
* @param messageId The unique message identifier
*/
event DispatchId(bytes32 indexed messageId);

/**
* @notice Emitted when a Hyperlane message is processed
* @param messageId The unique message identifier
*/
event ProcessId(bytes32 indexed messageId);

/**
* @notice Emitted when a Hyperlane message is delivered
* @param origin The origin domain of the message
* @param sender The message sender address on `origin`
* @param recipient The address that handled the message
*/
event Process(
uint32 indexed origin,
bytes32 indexed sender,
address indexed recipient
);

function localDomain() external view returns (uint32);

function delivered(bytes32 messageId) external view returns (bool);

function defaultIsm() external view returns (IInterchainSecurityModule);

function defaultHook() external view returns (IPostDispatchHook);

function requiredHook() external view returns (IPostDispatchHook);

function latestDispatchedId() external view returns (bytes32);

function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external payable returns (bytes32 messageId);

function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external view returns (uint256 fee);

function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata body,
bytes calldata defaultHookMetadata
) external payable returns (bytes32 messageId);

function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody,
bytes calldata defaultHookMetadata
) external view returns (uint256 fee);

function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata body,
bytes calldata customHookMetadata,
IPostDispatchHook customHook
) external payable returns (bytes32 messageId);

function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody,
bytes calldata customHookMetadata,
IPostDispatchHook customHook
) external view returns (uint256 fee);

function process(
bytes calldata metadata,
bytes calldata message
) external payable;

function recipientIsm(
address recipient
) external view returns (IInterchainSecurityModule module);
}

Dispatch

调用此函数将消息分发到目标域和收件人。

warning

Hyperlane只能向实现handle函数的智能合约传递消息。有关更多信息,请参阅 receive a message 文档。

根据post-dispatch hook configuration,可能需要支付一些费用。请参阅quoteDispatch 小节了解更多信息。

function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external payable returns (bytes32 messageId);
info

为了与地址不同的虚拟机兼容,收件人地址被保留为bytes32。为了方便起见,在TypeCasts library 中提供了以下实用程序。

// alignment preserving cast
function addressToBytes32(address _addr) internal pure returns (bytes32) {
return bytes32(uint256(uint160(_addr)));
}

示例

// send message from alfajores to bsctestnet TestRecipient
IMailbox mailbox = IMailbox("0xEf9F292fcEBC3848bF4bB92a96a04F9ECBb78E59");
bytes32 messageId = mailbox.dispatch{value: msg.value}(
97,
"0x0000000000000000000000006489d13AcAd3B8dce4c5B31f375DE4f9451E7b38",
bytes("Hello, world")
);

Quote Dispatch

费用通常配置为支付IGP付款和协议费用。这包括目标链上的事务提交、安全配置和维护。要接收对应的dispatch调用的报价,你可以查询quoteDispatch函数。

function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external view returns (uint256 fee) {

引用的fee必须作为值传递给dispatch调用,以确保它不会回滚。

示例

// quote sending message from alfajores to bsctestnet TestRecipient
IMailbox mailbox = IMailbox("0xEf9F292fcEBC3848bF4bB92a96a04F9ECBb78E59");
uint32 destination = 97;
bytes32 recipient = "0x0000000000000000000000006489d13AcAd3B8dce4c5B31f375DE4f9451E7b38";
bytes memory body = bytes("Hello, world");
uint256 fee = mailbox.quoteDispatch(destination, recipient, body);
mailbox.dispatch{value: fee}(destination, recipient, body);
danger

dispatch的付款不足将回滚。如果你将钩子组合在一起,超额支付可能不会被退还给消息发送者。

Post-Dispatch Hook 配置

在邮箱上配置了两个钩子:

  • required: 所有dispatch都会调用,调用值为所需费用
  • default: 在required钩子之后用剩余值调用(除非被覆盖)

Required Hook

要查询所需的钩子配置,可以调用requiredHook函数。

function requiredHook() external view returns (IPostDispatchHook);

Default Hook

要查询默认的钩子配置,可以调用 defaultHook 函数。

function defaultHook() external view returns (IPostDispatchHook);

要在dispatch 调用中使用自定义钩子覆盖默认钩子,请参见Hooks Reference