Rust

💡

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).

We provide a wasm (i.e. WebAssembly) execution environment for developers to deploy their Rust applications to. We use standard wasm tooling for Rust and provide an interface for sending and receving messages to and from the rest of the network. This is used to asynchronously compose with other programs. To get started, first go familiarize yourself with the Rust and WebAssembly book (opens in a new tab) before starting. We provide a thin layern crate that you can use to send and receive messages. Below is a minimal example of a Layer N program written in Rust showing the most important functionality provided by the layern crate.

use wasm_bindgen::prelude::*;
use layern::{Result, send, U256};
 
#[no_mangle]
#[wasm_bindgen]
pub fn execute(action: &[u8]) -> Result<()> {
    // Your entrypoint code here. You define the serialization format
    // for your actions and return the result.
 
    // For example, you could choose to use JSON to encode
    // the actions that your program supports.
    #[derive(serde::Serialize, serde::Deserialize)]
    enum MyAction {
        Increment,
        Decrement,
    }
 
    // You can deserialize the action like this:
    let action: MyAction = serde_json::from_slice(action)?;
 
    // And then you can do something with it:
    match action {
        MyAction::Increment => todo!(),
        MyAction::Decrement => todo!(),
    }
 
    // For sending a message, you can use the `send` function:
    let payload = b"Hello, world!";
    let dest = U256::from(0x01);
    send(&payload, dest)?;
 
    todo!()
}
 
#[no_mangle]
#[wasm_bindgen]
pub fn on_recv(payload: &[u8], sender: U256) -> Result<()> {
    // This function is called when a message is received.
    // The semantics of the payload are up to you.
    todo!()
}

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

use wasm_bindgen::prelude::*;
use layern::{Result, send, U256};
 
let NORD = U256::from(/* nord's address */);
 
#[no_mangle]
#[wasm_bindgen]
pub fn execute(_action: &[u8]) -> Result<()> {
    send(
        nord::actions::PlaceOrder {
            market_id: 0,
            price: 100,
            size: 100,
            ..Default::default()
        }.encode(),
        NORD,
    )?;
    Ok(())
}
 
#[no_mangle]
#[wasm_bindgen]
pub fn on_recv(payload: &[u8], sender: U256) -> Result<()> {
    if sender == NORD {
        let receipt = nord::receipt::Receipt::decode(payload)?;
 
        match receipt {
            nord::receipt::Receipt::OrderPlaced { .. } => {
                // Do something with the receipt.
                todo!()
            }
            _ => todo!()
        }
    }
    Ok(())
}