Telepathy
  • Introduction
  • Why Telepathy?
  • Getting Started
  • Build with Telepathy
    • Cross-Chain Messaging
      • Example: Cross-Chain Counter
      • Example: Cross-Chain Messaging Demo
      • Unit Testing
    • Ethereum Data Oracle
      • Example: Cross-Chain Airdrop
      • Example: Cross-Chain ENS Resolution
    • Ethereum Consensus Oracle
      • Example: Validator Balance Data
  • Telepathy Protocol
    • Overview
    • Sync Committee Protocol
    • Proof of Consensus
    • Smart Contracts
    • Off-chain Actors
    • Circuits
    • Guardrails
  • Resources
    • Telepathy Explorer
    • Contract Addresses
    • Brand Assets
  • Demo
  • Explorer
  • Github
  • Website
  • Discord
Powered by GitBook
On this page
  1. Build with Telepathy
  2. Cross-Chain Messaging

Example: Cross-Chain Messaging Demo

Sending a simple string across chains.

PreviousExample: Cross-Chain CounterNextUnit Testing

Last updated 2 years ago

In the , we demonstrate how to concatenate together a string message that includes:

  • arbitrary text

  • the sender's balance

  • the sender's name (if applicable)

Contracts

CrossChainMailer (Source Contract)

constructor(address _telepathyRouter) {
    telepathyRouter = ITelepathyRouter(_telepathyRouter);
}

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

function sendMail(
    uint32 _destinationChainId, 
    address _destinationMailbox, 
    bytes memory _message
) external payable
{
    if (msg.value < fee) {
        revert InsufficientFee(msg.value, fee);
    }
    string memory data = StringHelper.formatMessage(
        _message, msg.sender.balance, ENSHelper.getName(msg.sender)
    );
    telepathyRouter.send(_destinationChainId, _destinationMailbox, bytes(data));
}

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)

contract CrossChainMailbox is TelepathyHandler
constructor(address _telepathyRouter) TelepathyHandler(_telepathyRouter) {}
function handleTelepathyImpl(uint32 _sourceChainId, address _sourceAddress, bytes memory _message)
    internal
    override
{
    messages.push(string(_message));
    emit MessageReceived(_sourceChainId, _sourceAddress, string(_message));
}

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

The full source code for the contracts used in this demo can be found at the in . This contains the source contract CrossChainMailer and destination contract CrossChainMailbox.

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

Destination contracts should inherit from the contract:

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

Then override the function with custom logic:

Messenger Demo Github
contracts/src/CrossChainMailbox.sol
Router
TelepathyHandler
handleTelepathyImpl
Telepathy Messager Demo
ENS
TelepathyHandler
Play with the demo .
here
Router