Action Layer Driver
A Rust library for building Action Layer (CHIP-0050) singleton spends on the Chia blockchain.
Overview
The Action Layer is a pattern for Chia singletons that enables:
- State Management: Singletons that maintain and update state across spends
- Action Dispatch: Multiple actions (puzzles) that can be invoked via a merkle tree
- Child Spawning: Parent singletons that can create child singletons
- Composable Patterns: Building blocks for complex on-chain applications
This library provides a high-level Rust API for constructing Action Layer spends, handling proofs, and managing singleton lifecycle.
Features
SingletonDriver<S>- Generic driver for Action Layer singletonsPuzzleModule- Load and curry CLVM puzzles with typed argumentsActionLayerConfig- Configure action merkle trees- Automatic proof management (eve and lineage proofs)
- Child singleton spawning helpers
- Compatible with
chia-wallet-sdk0.30+
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use ;
use ;
use SpendContext;
use ;
use ToTreeHash;
// 1. Define your state type
// 2. Compute action puzzle hashes
let action_hashes = vec!;
// 3. Create driver with initial state
let initial_state = MyState ;
let mut driver = new;
// 4. Launch the singleton
let ctx = &mut new;
let result = driver.launch?;
println!;
// 5. Build action spends
driver.build_action_spend?;
// 6. Apply state after confirmation
driver.apply_spend;
API Reference
SingletonDriver<S>
The core driver for Action Layer singletons, generic over state type S.
// Create a new driver (not yet launched)
let driver = new;
// Create from existing on-chain singleton
let driver = from_coin;
// Launch the singleton
let result = driver.launch?;
// Build an action spend
driver.build_action_spend?;
// Update state after confirmation
driver.apply_spend;
// Mark as melted (destroyed)
driver.mark_melted;
// Accessors
driver.launcher_id // Option<Bytes32>
driver.current_coin // Option<&Coin>
driver.state // &S
driver.proof // Option<Proof>
driver.inner_puzzle_hash // TreeHash
driver.expected_new_coin // Option<Coin>
driver.expected_child_launcher_id // Option<Bytes32>
PuzzleModule
Load and curry CLVM puzzles with typed arguments.
// Load from hex string
let module = from_hex?;
// Get module hash
let mod_hash = module.mod_hash;
// Curry with typed arguments
let curried_ptr = module.curry_puzzle?;
let curried_hash = module.curry_tree_hash;
ActionLayerConfig
Configure action layer with merkle tree of action hashes.
let config = new;
// Get inner puzzle hash for a state
let inner_hash = config.inner_puzzle_hash;
// Build action spend (returns inner puzzle and solution)
let = config.build_action_spend?;
Helper Functions
// Proof creation
let eve_proof = create_eve_proof;
let lineage_proof = create_lineage_proof;
// Puzzle hash computation
let puzzle_hash = singleton_puzzle_hash;
let child_hash = child_singleton_puzzle_hash;
let child_id = expected_child_launcher_id;
// Child spawning
let result = spawn_child_singleton?;
Examples
singlelaunch
Demonstrates a two-action singleton that spawns child singletons:
# Build the example
# Show help
# Create a wallet
# Check balance
# Run the two-action test (requires funded wallet)
wallet
A wallet management library for Chia L2 applications:
Puzzles
The library works with Rue-compiled puzzles. Example puzzles are in puzzles/src/:
emit_child_action.rue- Action that spawns a child singletonchild_inner_puzzle.rue- Inner puzzle for child type 1child_inner_puzzle_2.rue- Inner puzzle for child type 2constants.rue- Shared constants
To compile puzzles with Rue:
Compiled outputs are in puzzles/output/.
State Type Requirements
Your state type S must implement:
S: Clone + + + ToTreeHash
The easiest way is to derive these traits:
Architecture
action-layer-driver/
├── src/
│ ├── lib.rs # Library entry point
│ ├── puzzle.rs # PuzzleModule for loading/currying puzzles
│ ├── action_layer.rs # ActionLayerConfig for merkle tree
│ ├── error.rs # Error types
│ └── singleton/
│ ├── mod.rs # Singleton module exports
│ ├── driver.rs # SingletonDriver<S>
│ ├── types.rs # Core types (LaunchResult, etc.)
│ ├── helpers.rs # Standalone helper functions
│ └── spend_options.rs # SpendOptions for broadcasts
├── examples/
│ ├── singlelaunch/ # Two-action singleton demo
│ └── wallet/ # Wallet utilities
├── puzzles/
│ ├── src/ # Rue puzzle sources
│ └── output/ # Compiled puzzle hex
└── README.md
Related
- CHIP-0050 - Action Layer specification
- chia-wallet-sdk - Chia wallet SDK
- Rue - Chia puzzle language
License
MIT