Skip to main content
Full working code: The complete source code for this example is shown on this page. More complete example contracts are available in test-contracts/
This example walks you through creating your first Sigil contract—a simple “Hello World” that demonstrates the fundamental structure and workflow.

Project Structure

Step 1: Workspace Configuration

Root Cargo.toml:
The paths shown assume the Documentation and Kontor repos are siblings. If you cloned them to different locations, adjust these paths to point to your Kontor installation. See Getting Started for setup details.

Step 2: Contract Configuration

contract/Cargo.toml:
Key points:
  • crate-type = ["cdylib"] - Builds a dynamic library for WASM
  • stdlib provides the contract! macro and runtime primitives

Step 3: Define the Contract Interface (WIT)

Create contract/wit/contract.wit:
What this defines:
  • init - Called once when contract is deployed
  • hello-world - Returns a greeting string (read-only)
The kontor:built-in types are available via a symlink at wit/deps/built-in.wit.

Step 4: Implement the Contract

Create contract/src/lib.rs:
contract/src/lib.rs
How it works:
  • contract!(name = "hello-world") - Generates the Guest trait and WASM bindings
  • impl Guest for HelloWorld - The contract name is converted to PascalCase
  • init() - Empty because we don’t need storage
  • hello_world() - Returns a static string

Step 5: Build and Test

The contract is automatically built when you run tests. The test directory’s build.rs handles compilation, optimization, and compression:
The build.rs automatically:
  1. Compiles the contract to WASM
  2. Optimizes with wasm-opt -Oz --enable-bulk-memory --enable-sign-ext
  3. Compresses with brotli to create .wasm.br
To build manually without running tests:

Step 6: Write Tests

Create test/src/lib.rs:
test/src/lib.rs
What this does:
  • interface! - Generates type-safe bindings from the WIT file
  • #[testlib::test] - Auto-injects the runtime variable
  • runtime.identity() - Creates a test user with gas funding
  • runtime.publish() - Deploys the contract and returns its address
  • hello_world::hello_world() - Calls the contract function
Run tests:

Common First-Time Issues

contract! macro not found
Solution: Add use stdlib::*; at the top of your file Missing Guest trait implementation
Solution: Ensure you’ve implemented all functions listed in your WIT file. Function names with hyphens in WIT become underscores in Rust. WIT file parse error
Solution: Ensure you have include kontor:built-in/built-in; in your WIT world Build target not installed
Solution: Install the WASM target: rustup target add wasm32-unknown-unknown