Example: Cross-Chain Messaging Demo

Sending a simple string across chains.

In the Telepathy Messager Demoarrow-up-right, we demonstrate how to concatenate together a string message that includes:

  • arbitrary text

  • the sender's balance

  • the sender's ENSarrow-up-right name (if applicable)

Play with the demo herearrow-up-right.

Contracts

The full source code for the contracts used in this demo can be found at the Messenger Demo Githubarrow-up-right in contracts/src/CrossChainMailbox.solarrow-up-right. This contains the source contract CrossChainMailer and destination contract CrossChainMailbox.

CrossChainMailer (Source Contract)

This contract needs a reference to the Telepathy Router, so we pass in that information to the constructor:

The entrypoint for this contract is the sendMail function, which a user (EOA) calls to send a message across chains.

The user calling this function specifies which destination chain they would like to send their message to (with the parameter _destinationChainId) as well as the _destinationMailbox contract address (a CrossChainMailbox deployed on the destination chain).

The body of sendMail uses the StringHelper library to piece together the information of message + balance + ENS name in one formatted string. Then we use send to send that actual message to our CrossChainMailbox through Telepathy.

CrossChainMailbox (Destination Contract)

Destination contracts should inherit from the TelepathyHandlerarrow-up-right contract:

This gives convenient functionality for ensuring that ONLY the Telepathy Router can call this function, and gives a function to override to receive messages. To set up this contract a TelepathyHandlerarrow-up-right, pass in the Router's address in the constructor:

Then override the handleTelepathyImplarrow-up-right function with custom logic:

In this case, we store the message and emit an event.

Last updated