Hi everyone,
We’d like to introduce CKB Cell Framework, a Rust framework for building CKB contracts, developed by FoundationLab.
Our goal is to make CKB contracts easier to write, understand, and test. The framework brings together typed Cell loading, declarative constraints, automatic serialization, transaction construction, and local VM testing.
The main building blocks are:
CellCodecgenerates encoding, strict decoding, and wire-format schemas for application data.Cellsdeclares the inputs, outputs, and dependencies an operation needs, with constraints for scripts, capacity, positions, and data.Context<C>gives handlers access to the loaded Cell collection and executing Script.StateTransitiondefines pure transition logic that contracts, clients, and tests can share.
For example, a Counter update declares the relationship between its input and output Cells:
#[derive(Cells)]
#[cells(args = CounterArgs)]
pub struct Update {
#[cell(
source = Source::Input,
type_script = Some(&args.script),
capacity = STATE_CAPACITY,
constraint =
script::script_hash(before.lock()) == before.data().owner
)]
pub before: Cell<State>,
#[cell(
source = Source::Output,
type_script = Some(&args.script),
lock = before.lock(),
capacity = STATE_CAPACITY
)]
pub after: Cell<State>,
}
The handler receives Context<Update> and explicitly validates the expected state transition and actor authorization. Asset contracts additionally verify reserve provenance, supply changes, and actual net payments.
This separation keeps field loading concise while making the remaining protocol checks visible in ordinary Rust functions. Clients construct the proposed transaction; contracts validate its inputs, outputs, and witnesses.
The framework includes unsigned transaction builders for single and multiple reserves, helpers that bind Action witnesses to the actual State input, CKB-VM testing utilities, and schema/IDL export facilities.
Six examples demonstrate the current APIs:
- Counter: creation, updates, and owner authorization.
- Vault: deposits, withdrawals, and capacity refunds.
- Escrow: two-party payment release and refunds.
- Vesting: scheduled claims bound to the State input’s
since. - Limit Order: partial fills, exact payments, and integer rounding.
- AMM: two reserves, liquidity operations, constant-product swaps, and official xUDT LP tokens.
Workspace commands cover project generation, builds, tests, and registered interface exports:
cargo cell new my-contract
cargo check -p my-contract-script
cargo cell build my-contract-script
cargo cell test
We’d welcome feedback from the community, especially on API ergonomics, validation clarity, and patterns needed for real applications.
Repository and documentation: github.com/ckbswap/ckb-cell-framework