Transfer arbitrary messages


Let's embark on creating our first contract that enables cross-chain interaction via the Rayls Protocol. To accomplish this, we must inherit from the **RaylsApp** abstract contract, which provides a diverse set of methods to facilitate communication.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {RaylsApp} from "@rayls/contracts/RaylsApp.sol";

contract HelloWorldContract is RaylsApp {
    string public message;

    constructor(
        address _endpoint,
        address _raylsNodeEndpoint,
        address _userGovernance
    ) RaylsApp(_endpoint, _raylsNodeEndpoint, _userGovernance) {}

    function sendMessage(
        address to,
        uint256 toChainId,
        string memory _message
    ) public {
        _raylsSend(
            toChainId,
            to,
            abi.encodeWithSignature("receiveGreetings(string)", _message)
        );
    }

    function receiveGreetings(string memory _message) public {
        message = _message;
    }

    function getMessage() public view returns (string memory) {
        return message;
    }
}

To facilitate cross-chain messaging, it's essential to identify the destination Rayls Privacy Node Ledger's chain ID and the address of an equivalent contract deployed there. This setup underscores the protocol's use of Arbitrary Message Communication, wherein interactions are formatted as encoded method calls.

Consequently, any data transferred between Rayls Privacy Node Ledgers is managed via function calls on the destination Rayls Privacy Node Ledger, ensuring a standardized approach to cross-chain communication.

⚠️

Access control on receive functions

In this tutorial example, receiveGreetings is left open (public) for simplicity. In production contracts, any function that should only be called by the Rayls relayer must be marked with the restricted modifier and registered under the MESSAGE_EXECUTOR role. Calling a restricted function from an unauthorized address reverts with RaylsAccessManaged__Unauthorized.

See the RaylsApp page for details on how access control works in Rayls handlers.