|
|
il y a 3 jours | |
|---|---|---|
| .. | ||
| examples | il y a 3 jours | |
| src | il y a 3 jours | |
| tests | il y a 3 jours | |
| Cargo.toml | il y a 3 jours | |
| README.md | il y a 3 jours | |
Async ledger resource — the main entry point for callers.
Composes kuatia-core (validation) and kuatia-storage (persistence) into an
atomic commit engine: resolve, validate, then apply the whole transfer in one
store transaction.
This is the only crate an application depends on. The whole chain is re-exported
here, the SQL backend as kuatia::sql behind the sqlite or postgres
feature, and kuatia::prelude::* brings the domain types, the builders and the
stores into scope. The other crates stay published for custom development, such
as a hand-written Store.
Build transfers with TransferBuilder, then commit them:
let usd = ledger.asset("USD", "$", 2).await?;
let transfer = TransferBuilder::new()
.deposit(alice, ("100.00", &usd), bank)
.build()?;
let receipt = ledger.commit(transfer).await?;
Every verb takes one amount, which carries the asset it is denominated in, so
build() is where a bad amount surfaces.
| Builder method | Description |
|---|---|
.pay(from, to, amount) |
Transfer with automatic posting selection and change |
.deposit(to, amount, external) |
Fund an account from an external source |
.withdraw(from, amount, external) |
Send value to an external destination |
.movement(from, to, amount) |
Raw movement for custom operations |
Every commit is atomic — one store transaction:
commit(transfer) — resolves the intent into a concrete envelope (read-only),
then runs commit_envelope.commit_envelope(envelope) — the one commit path. Validates against loaded
state in the pure core, then calls store.commit_envelope(..), which in one
transaction spends the consumed postings, inserts the created ones, stores the
transfer, and appends the committed event — re-checking double-spend,
freeze/close, and the overdraft floor inside it.reverse(id) — builds a reversal envelope and runs the same path.A domain refusal (double-spend, frozen, closed, overdraft) comes back as a typed
LedgerError, and nothing is applied. Because the write is all-or-nothing, a
crash leaves no half-applied state — there is no write-ahead log and no recovery
step.
| Method | Description |
|---|---|
create_account(account) |
Create account and emit AccountCreated event |
freeze(id) |
Set FROZEN flag |
unfreeze(id) |
Clear FROZEN flag |
close(id) |
Set CLOSED flag (requires zero active postings) |
| Method | Description |
|---|---|
balance(account, asset) |
Current balance (sum of the unspent postings) |
query_transfers(query) |
Paginated, filtered transfer history |
history(account) |
All transfers for an account |
postings(account) |
All postings (live and spent) |
get_events_since(seq, limit) |
Query ledger event log |
Combine transfers into an all-or-nothing workflow with run_transfers, which
commits them in order and reverses the committed ones LIFO on failure:
use kuatia::workflow::run_transfers;
let receipts = run_transfers(&ledger, &[
TransferBuilder::new().deposit(alice, ("100.00", &usd), bank).build()?,
TransferBuilder::new().pay(alice, bob, ("40.00", &usd)).build()?,
])
.await?;
quickstart runs on the in-memory store, so it needs no feature and no
database:
cargo run -p kuatia --example quickstart
The rest in examples/ open a real SQLite-backed ledger with
Ledger::connect("sqlite::memory:"), so they need the backend feature:
cargo run -p kuatia --features sqlite --example create_accounts # user/system/external accounts
cargo run -p kuatia --features sqlite --example fund_and_trade # fund two assets, then swap
cargo run -p kuatia --features sqlite --example withdraw # withdraw out of the ledger
cargo run -p kuatia --features sqlite --example overdraft # the shortfall posting
cargo run -p kuatia --features sqlite --example inflight_hold # authorize, confirm, void
cargo run -p kuatia --features sqlite --example escrow # per-agreement subaccounts
Point the connection string at a file (sqlite://ledger.db) or a Postgres URL
for a persistent ledger.