Solidity

💡

Layer N is currently in closed early access and only available to select developers. These docs are meant to provide insight into what developing on Layer N feels like. If you are interested in developing an app on Layer N, sign up to the Early Access Program ↗ (opens in a new tab).

Layer N provides a Solidity interface for interacting with the Layer N precompile for sending and receiving messages to and from the rest of the network. This is used to asynchronously compose with other programs. we offer a normal Ethereum RPC interface.

interface IMessageSender {
    function send(bytes memory message, address dest) external;
}
 
interface IMessageReceiver {
    function onRecv(bytes memory message, address sender) external;
}
 
contract MyProgram is IMessageReceiver {
    // Set this to the address of the IMessage contract precompile.
    address public constant MESSAGE_ADDRESS = address(0);
 
    function sendExample() external {
        IMessageSender(MESSAGE_ADDRESS).send("hi!", address(1));
    }
 
    function onRecv(bytes memory message, address sender) external override {
        // Do something with the message.
    }
}

For example, our Nord orderbook provides a Solidity interface for encoding and decoding messages, which can be used to implement applications such as trading bots.

import { IMessageReceiver, IMessageSender } from "./IMessageReceiver.sol";
import { INord } from "./INord.sol";
 
contract MyBot is IMessageReceiver {
    // Set this to the address of the Nord contract.
    address public constant NORD = 0x000000000000000000000000000000000000dead;
 
    function execute(bytes memory action) external override {
        IMessageSender(NORD).send(INord.encodePlaceOrder(0, 100, 100), address(this));
    }
 
    function onRecv(bytes memory message, address sender) external override {
        if (sender == NORD) {
            INord.Receipt memory receipt = INord.decodeReceipt(message);
 
            if (receipt.kind == INord.ReceiptKind.OrderPlaced) {
                // Do something with the receipt.
            }
        }
    }
}