发送消息
合约可以通过调用Mailbox上的dispatch
函数来发送链间消息。
IMailbox
Interface
- Solidity
// 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
小节了解更多信息。
- Solidity
- CosmWasm
- Sealevel
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)));
}
🚧 即将到来! 🚧
🚧 即将到来! 🚧
示例
origin:
destination:
body:
- Solidity
- CosmWasm
- Sealevel
// send message from alfajores to fuji TestRecipient
IMailbox mailbox = IMailbox("0xEf9F292fcEBC3848bF4bB92a96a04F9ECBb78E59");
bytes32 messageId = mailbox.dispatch{value: msg.value}(
43113,
"0x00000000000000000000000044a7e1d76fD8AfA244AdE7278336E3D5C658D398",
bytes("Hello, world")
);
🚧 Coming soon! 🚧
🚧 Coming soon! 🚧
Quote Dispatch
费用通常配置为支付IGP付款和协议费用。这包括目标链上的事务提交、安全配置和维护。要接收对应的dispatch
调用的报价,你可以查询quoteDispatch
函数。
- Solidity
- CosmWasm
- Sealevel
function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external view returns (uint256 fee) {
引用的fee
必须作为值传递给dispatch
调用,以确保它不会回滚。
🚧 即将到来! 🚧
🚧 即将到来! 🚧
示例
origin:
destination:
body:
- Solidity
- CosmWasm
- Sealevel
// quote sending message from alfajores to fuji TestRecipient
IMailbox mailbox = IMailbox("0xEf9F292fcEBC3848bF4bB92a96a04F9ECBb78E59");
uint32 destination = 43113;
bytes32 recipient = "0x00000000000000000000000044a7e1d76fD8AfA244AdE7278336E3D5C658D398";
bytes memory body = bytes("Hello, world");
uint256 fee = mailbox.quoteDispatch(destination, recipient, body);
mailbox.dispatch{value: fee}(destination, recipient, body);
🚧 Coming soon! 🚧
🚧 Coming soon! 🚧
tip
对dispatch
少付的金额将回滚,但多付的金额将退还给消息发送者。这样,开发人员就可以跳过显式的quoteDispatch
调用,转而使用具有超额值的quoteDispatch
调用。
Post-Dispatch Hook 配置
在邮箱上配置了两个钩子:
required
: 所有dispatch
都会调用,调用值为所需费用default
: 在required
钩子之后用剩余值调用(除非被覆盖)
Required Hook
要查询所需的钩子配置,可以调用requiredHook
函数。
- Solidity
function requiredHook() external view returns (IPostDispatchHook);
Default Hook
要查询默认的钩子配置,可以调用 defaultHook
函数。
- Solidity
function defaultHook() external view returns (IPostDispatchHook);
要在dispatch
调用中使用自定义钩子覆盖默认钩子,请参见Hooks Reference。