This table summarizes the core interfaces that may be used in the application's Msgport integration. These interfaces are straightforward and easy to understand. Let's dive in!
The IMessagePort interface within the Msgport is crafted to simplify the complexities of underlying cross-chain messaging protocols for dApp developers. It offers a standardized interface to send cross-chain messages across various messaging protocols. The interface includes two critical functions:
send(): Serves as the gateway for users or applications to send a cross-chain message. It returns a msgId, which is the identifier of the message and can be used to track the message status.
fee(): Retrieves the necessary fee information for utilizing the send() function.
The Msgport layer accommodates a variety of messaging protocols that adhere to this interface, including ORMP, ICMP, and XCMP, among others. The code for the interface furnishes detailed explanations of each function's use.
pragma solidity^0.8.0;interfaceIMessagePort{eventMessageSent(bytes32indexedmsgId,addressfromDapp,uint256toChainId,addresstoDapp,bytesmessage,bytesparams);eventMessageRecv(bytes32indexedmsgId,boolresult,bytesreturnData);/// @dev Send a cross-chain message over the MessagePort./// @notice Send a cross-chain message over the MessagePort./// @param toChainId The message destination chain id. <https://eips.ethereum.org/EIPS/eip-155>/// @param toDapp The user application contract address which receive the message./// @param message The calldata which encoded by ABI Encoding./// @param params Extend parameters to adapt to different message protocols./// @return msgId Return the ID of message.functionsend(uint256toChainId,addresstoDapp,bytescalldatamessage,bytescalldataparams)externalpayablereturns(bytes32msgId);/// @notice Get a quote in source native gas, for the amount that send() requires to pay for message delivery./// It should be noted that not all ports will implement this interface./// @dev If the messaging protocol does not support on-chain fetch fee, then revert with "Unimplemented!"./// @param toChainId The message destination chain id. <https://eips.ethereum.org/EIPS/eip-155>/// @param fromDapp The user application contract address which send the message./// @param toDapp The user application contract address which receive the message./// @param message The calldata which encoded by ABI Encoding./// @param params Extend parameters to adapt to different message protocols.functionfee(uint256toChainId,addressfromDapp,addresstoDapp,bytescalldatamessage,bytescalldataparams)externalview
returns(uint256);}
The Application contract is typically extended by the receiving application on the destination chain. This extension allows the receiving application to incorporate additional validation checks for the information contained in cross-chain messages. To see this implementation in action, you can examine the provided runnable examples.
pragma solidity^0.8.17;abstractcontractApplication{function_msgPort()internalviewreturns(address_port){_port=msg.sender;}function_messageId()internalpurereturns(bytes32_msgDataMessageId){require(msg.data.length>=84,"!messageId");assembly{_msgDataMessageId:=calldataload(sub(calldatasize(),84))}}/// @notice The cross-chain message source chainIdfunction_fromChainId()internalpurereturns(uint256_msgDataFromChainId){require(msg.data.length>=52,"!fromChainId");assembly{_msgDataFromChainId:=calldataload(sub(calldatasize(),52))}}/// @notice Get the source chain fromDapp address.function_xmsgSender()internalpurereturns(addresspayable_from){require(msg.data.length>=20,"!fromDapp");assembly{_from:=shr(96,calldataload(sub(calldatasize(),20)))}}}
The IPortMetadata interface in the Msgport framework is intended to handle essential information regarding each port. It mandates the inclusion of two specific types of data:
name: Acts as a globally unique identifier for the port.
uri: Serves as a reference to the location where detailed information about the port is stored, typically pointing to an IPFS link by default.
This interface enables the Msgport to maintain vital information that applications may need in order to utilize the ports effectively.
pragma solidity^0.8.0;interfaceIPortMetadata{eventURI(stringuri);/// @notice Get the port name, it's globally unique and immutable./// @return The MessagePort name.functionname()externalviewreturns(stringmemory);/// @return The port metadata uri.functionuri()externalviewreturns(stringmemory);}