Skip to main content
The nexus-contract-template repository is the fastest way to start writing a NEXUS smart contract. It includes a production-ready Cargo workspace, the nexus-sdk vendored locally, and a full test harness — no network access required to build or test.

Setup

Clone the template
Install the WASM target
Install WABT tools (required for WASM memory patching in tests)

Project Structure


Cargo.toml

The template Cargo.toml is pre-configured for contract builds:
crate-type = ["cdylib"] is mandatory. Without it, Cargo will not produce a .wasm file.

Writing Your First Contract

Edit src/lib.rs:
Key rules:
  • #![no_std] — contracts run in a bare WASM environment, no std library
  • extern crate alloc — gives you String, Vec, etc. via the alloc crate
  • nexus_fn! — the macro that exposes your function as a contract ABI entry point
  • ret::* — how you return values from a contract function
  • emit() — fire an event that indexers can listen to

Building

Output: target/wasm32-unknown-unknown/release/my_nexus_contract.wasm

Testing

Write tests in contract-tests/src/lib.rs:
Run tests:
TestEnv::deploy() automatically patches the WASM memory export — the same transformation used in production builds — so your tests run against a valid artifact.

Deploying to NEXUS

Once your contract is built and tested, deploy it using the CLI:
Or via the TypeScript SDK:

Available SDK Primitives


rust-toolchain.toml

The template pins a specific Rust nightly version to ensure reproducible builds:
Do not change the toolchain version without testing. NEXUS contracts use #![no_std] and some nightly features — a different toolchain version may cause compiler intrinsic errors.