Skip to main content

Building the Rust example program

Rust integration is simpler than C integration, since the Rust ecosystem has a standard package manager (cargo). The execution event SDK is made up of two packages, called monad-event-ring and monad-exec-events. These are described in more detail in the Rust API guide, but for now it's enough to just know their names.

In the future, Category Labs may publish these to crates.io, but that is not how the SDK is currently distributed.

Instead, the user's Cargo.toml file declares the upstream source of these dependencies to be a particular release branch of the git repository where the SDK source code is located. Dependencies which are sourced from git rather than crates.io are explained in this section of the Cargo Book.

Where is the Rust SDK source code?

The execution event Rust SDK lives in the monad-bft git repository, the same repository as the consensus daemon and the JSON-RPC server. Despite being the "execution events SDK," it does not live in execution repository alongside the C SDK, for several reasons:

  • All the code in the execution repository is written in C and C++, whereas everything in monad-bft is written in Rust

  • The execution (C++) repository is a dependency of the BFT (Rust) one. The Rust project uses some C and C++ functionality (via extern "C" FFI APIs) but the reverse is not true: C never calls Rust functions with C linkage. For this reason, all the cross-language interop machinery is in the Rust repository, and defining the Rust SDK in this repository keeps it that way

However, the C ecosystem still affects Rust: some of the functions in the execution events C SDK are reused by Rust, via an FFI interface. The main way this affects the Rust build is that you must ensure that a recent enough C compiler is selected when CMake is run from Cargo.

The C SDK uses some recent C23 language features, and requires either gcc-13 or clang-19. If you run cc -v and it reports an older compiler, you will need to set the CC environment variable to tell CMake to select a newer compiler.

Building the example program

Step 1: create a new package and copy the example code into it

First, create the new package:

$ cargo new --bin event-sdk-example-rust
$ cd event-sdk-example-rust

Next, we'll overwrite the default "Hello world" main.rs source file with the example program code, downloaded from github:

$ curl https://raw.githubusercontent.com/category-labs/monad-bft/refs/heads/release/exec-events-sdk-v1.x/monad-exec-events/examples/eventwatch.rs > src/main.rs

Step 2: integrating with the SDK packages

Create the following Cargo.toml file:

[package]
name = "event-sdk-example-rust"
version = "0.1.0"
edition = "2021"
[dependencies]
chrono = "0.4.34"
clap = { version = "4.2", features = ["derive"] }
lazy_static = "1.5.0"
[dependencies.monad-exec-events]
git = "https://github.com/category-labs/monad-bft"
branch = "release/exec-events-sdk-v1.x"
[dependencies.monad-event-ring]
git = "https://github.com/category-labs/monad-bft"
branch = "release/exec-events-sdk-v1.x"

Step 3: build the project

cargo build

If you need to pass a more recent compiler to CMake, you can do so using bash's terse syntax for setting environment variables in the scope of command to be run, for example:

CC=gcc-15 cargo build

The compilation should produce an executable file. Try running it with the -h flag to print the help:

cargo run -- -h

If all was successful, continue on to the last step in the guide.