Hi everyone!
I just published the state-machine module I developed for several game-related Soroban smart contracts to crates.io and GitHub to make it available to the wider community (open source MIT License). The module itself is a generic state machine implementation, not specific to games, and can be used for modeling concurrent state executions and complex runtime interactions for a wide range of use cases (see below).
FEATURES
- Easy setup via procedural attribute macros and traits (declarative macro available too).
- Flexible state concurrency via regions.
- Runtime behavior modeling via extended state variables.
- Transition control with guards and effects.
- State persistence with Soroban storage.
SIMPLE USAGE
Using the #[state_machine] attribute macro you can quickly configure a function for state transition within your finite state machine.
#[state_machine( state = "State:Ready:app", region = "Region:Specific:address", transition = true, storage = "instance" )] fn app_login(&self, env: &Env, address: &Address, app: &App) { }
TRANSITIONS
The attribute macro forces the function to panics if the state is invalid preventing any code execution inside the function body.
You can implement the TransitionHandler trait to apply guards and effects to control state transition. For example, this allows you to implement a time based / ledger based state transition just before state validation occurs.
EXAMPLE USE CASES
The state machine allows you to model complex state machine behaviors for concurrent users and also define extended states at runtime. Check out the Coffee Machine and Game Lobby examples in the repo.
Here are some more use case examples:
- Independent NFT minting windows for users based on variable attributes.
- Markets with concurrent auctions and bidders.
- Multiplayer game lobby with teams and individual access.
- Online app with concurrent users, variable access rights...
LINKS
soroban-kit: https://crates.io/crates/soroban-kit
code repo: https://github.com/FredericRezeau/soroban-kit
Note that all modules in crates are feature flagged (with feature forwarding), so you can compile just what you need and nothing more!
e.g., To configure the crate with state-machine module only:
[dependencies.soroban-macros] version = "0.1.2" default-features = false features = ["state-machine"]
Hope you find it useful.