Skip to main content

Overview

NEXUS supports WebAssembly (WASM) smart contracts written in Rust.

Contract Lifecycle

Write (Rust) ──► Compile (WASM) ──► Deploy ──► Execute

Writing Contracts

NEXUS contracts use the nexus_fn! macro for ABI-compatible function exports:
use nexus_sdk::{
    solidity::{*, SafeMath},
    contract_api::ez::ret,
    require,
};
use nexus_sdk::solidity::uint256 as U256;

// State variables using Solidity-compatible Mappings
static VALUE: Mapping<&[u8], U256> = Mapping::new(b"value");
static OWNER: Mapping<&[u8], Address> = Mapping::new(b"owner");

nexus_fn! {
    fn init() {
        let sender = Blockchain::msg.sender();
        OWNER.set(&b"val".as_slice(), sender);
        VALUE.set(&b"val".as_slice(), U256::from(0));
        ret::u32(1)
    }
}

nexus_fn! {
    fn my_function() {
        let current = VALUE.get(&b"val".as_slice());
        let mut out = [0u8; 32];
        current.to_little_endian(&mut out);
        ret::u256(&out)
    }
}

Available Features

Storage

Persistent key-value storage for contract state.
storage::set(b"key", b"value");
let value = storage::get(b"key");

Events

Emit events for off-chain indexing.
events::emit("Transfer", &data);

Cross-Contract Calls

Call other contracts.
let result = xcc::call(&contract_id, "function", &args);

Bitcoin Integration

Access Bitcoin L1 data.
let height = bitcoin::height();
let hash = bitcoin::hash_at(100);

Gas Metering

All operations consume gas:
OperationGas Cost
Storage read200
Storage write5000
Hash (SHA256)60
Cross-contract call2500 + subcall

Compilation

cargo build --target wasm32-unknown-unknown --release