Simplest Messaging Demo

In the Darwinia MsgportDarwinia Msgport chapter, we discussed the design of the Msgport and ORMP messaging protocol. However, you might still be wondering how to integrate cross-chain capabilities into your application. This tutorial aims to address that by demonstrating a simple example of how to assemble these elements in your application. We will use an established message bridge between the Crab chain and Arbitrum Sepolia to send a simple message 0x123456789 from the Crab chain to Arbitrum Sepolia.

Prepare To Receiving Messages

Create Demo workspace

Let's create a demo workspace in the Remix for the contract that need to be deployed in the Arbitrum Sepolia.

Deploy MyReceiverDapp

Establish a contract in the workspace named MyReceiverDapp.sol within the contracts folder. Copy the following contract content into this file:
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; import "https://github.com/darwinia-network/darwinia-msgport/blob/main/src/Application.sol"; contract MyReceiverDapp is Application { event DappMessageRecv(uint256 fromChainId, address fromDapp, address localLine, bytes message); // local line address address public immutable LINE; // remote dapp address address public immutable DAPP; constructor(address line, address dapp) { LINE = line; DAPP = dapp; } /// @notice You could check the fromDapp address or messageLine address. function myRecv(bytes calldata message) external { uint256 fromChainId = _fromChainId(); address fromDapp = _xmsgSender(); address localLine = _msgSender(); require(localLine == LINE); require(fromDapp == DAPP); emit DappMessageRecv(fromChainId, fromDapp, localLine, message); } }
Please note that the contract constructor requires two parameters  constructor(address line, address dapp) during deployment.
  • The line parameter specifies the message line you choose for sending your message. You can refer to Supported Networks for the address. For this tutorial, we are sending the message to the Crab ORMP line, hence the address is 0x0000000000D2de3e2444926c4577b0A59F1DD8BC.
  • The dapp parameter identifies the dapp address on the Crab chain from which the cross-chain message is sent.
Image without caption
Change your Remix network to the Arbitrum Sepolia and deploy the contract. This is all that's required for receiving messages from the Crab chain. The myRecv function serves as the endpoint where the target chain can receive messages. Once the target chain is set up to receive messages, we'll guide you through how to send messages from the Crab chain.

Sending Message

Get calldata

Input the message 0x123456789 into the myRecv(bytes calldata message) function and retrieve the calldata, as demonstrated in the screenshot.
Image without caption

Get fee and messaging params

Get the cross-chain message fee and messaging service params via MsgPort API, running the following command:
curl --request GET --url 'https://msgport-api.darwinia.network/ormp/fee?from_chain_id=44&to_chain_id=421614&from_address=0x1397bb71E99D2E688Ee78a8483d280f48027BE2b&to_address=0xE669D751d2C79EA11a947aDE15eFb2720D7a6F94&payload=0x05721810000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000041245678900000000000000000000000000000000000000000000000000000000&refund_address=0x1397bb71E99D2E688Ee78a8483d280f48027BE2b'
The input params:
  • from_chain_id: 44 (Crab)
  • to_chain_id: 421614 (Arbitrum Sepolia)
  • from_address: The dapp address from which the cross-chain message is sent.
  • to_address: The deployed MyReceiverDapp address on the Arbitrum Sepolia.
  • payload: The myRecv(bytes calldata message) calldata in the last step.
The output result:
{ "code": 0, "data": { "fee": "78267965000000000000", "params": "0x0000000000000000000000000000000000000000000000000000000000044b1e0000000000000000000000001397bb71e99d2e688ee78a8483d280f48027be2b00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000" } }

Sending Message

Open the ORMP contract link in the Crab subscan and connect to your MetaMask wallet as the shown in the image below.
Image without caption
The input params:
  1. dapp address: The dapp address from which the cross-chain message is sent.
  1. message fee: Get cross-chain free from the last step, remember to adjust the decimal.
  1. to_chain_id: 421614 (Arbitrum Sepolia)
  1. to_address: The deployed MyReceiverDapp address on the Arbitrum Sepolia.
  1. payload: The myRecv(bytes calldata message) calldata.
  1. messaging params: The messaging params get from last step.
Make sure that the sender has sufficient tokens to cover the cross-chain message fee. Then, initiate the transaction and dispatch the message by clicking the Write button on the left. Once the transaction is finalized, a View Transaction link will appear, as shown.
Image without caption
Open the link and copy the tx hash:
Image without caption
Check out the Darwinia Messages Explorer or the message details and status by paste the txhash:
Image without caption
Image without caption
In less than five minutes, the message sent from the Crab chain will be delivered and dispatched in Arbitrum Sepolia.