# Pending states The ledger has one state: committed. Anything that looks pending, reserved, authorised or unsettled is value parked in an account you control, and its status is the balance of that account. To add a workflow state, add an account, not a flag. ## Why there is nothing else to use Postings are immutable and a balance is a projection over them, so no row exists that a status could be written on. That is the whole of the constraint, and it is also the mechanism: because position is the only thing that can carry a state, every state change is a transfer, and every transfer is already conserved, content-addressed and in the log. A status modelled this way cannot drift from the money, because it *is* the money. A status column can. This is why there is no `Transfer::status` and no pending flag on an account. Asking for one is usually asking for an account. ## Escrow, end to end `crates/kuatia/examples/escrow.rs` is this page in runnable form. Run it with `cargo run -p kuatia --example escrow`. An agreement's money sits in its own escrow account, a subaccount of one top-level escrow account, so one agreement's funds can never be spent by another and each closes independently. ```rust const ESCROW: i64 = 50; fn escrow_for(agreement: i64) -> AccountId { AccountId::with_sub(ESCROW, agreement) } ``` The account forbids overdraft, which is what makes releasing more than was deposited impossible rather than merely incorrect: ```rust ledger .create_account(Account::debit_must_not_exceed_credit(escrow_for(agreement))) .await?; ``` Entering escrow is an ordinary payment, final the moment it commits: ```rust ledger.commit( TransferBuilder::new() .pay(buyer, escrow_for(agreement), ("120.00", &usd)) .reference("escrow-in-7001") .build()?, ).await?; ``` Leaving it is the same transfer with a different destination. Release pays the seller, refund pays the buyer, and nothing distinguishes them to the ledger: ```rust // The balance read hands back an amount, so it pays straight out again. let held = ledger.balance(&escrow_for(agreement), &usd).await?; ledger.commit( TransferBuilder::new() .pay(escrow_for(agreement), seller, held) // or back to buyer .reference("escrow-release-7001") .build()?, ).await?; ``` ```mermaid stateDiagram-v2 [*] --> InEscrow: pay buyer to escrow(7001) InEscrow --> Released: pay escrow(7001) to seller InEscrow --> Refunded: pay escrow(7001) to buyer Released --> [*]: close the drained account Refunded --> [*]: close the drained account ``` Three properties come free, and are the reason to model it this way rather than with a status field: - **The amount and the state are one read.** `balance(escrow_for(agreement), usd)` answers both "is this pending" and "how much", and cannot disagree with itself. - **Partial settlement needs no new concept.** Release part of the balance and the rest stays in escrow, still readable the same way. - **Finished is provable.** A drained escrow account holds no live postings, so `close` succeeds, and a closed account rejects further transfers. The agreement is finished rather than marked finished. ## The same shape, elsewhere | Business state | The account that holds it | What ends it | |---|---|---| | Card authorisation | Per-destination hold (implemented, see [inflight.md](inflight.md)) | Capture, or void on expiry | | Escrow | Escrow account per agreement | Release to seller, or refund to buyer | | Unsettled card batch | Clearing account per settlement cycle | Settlement file, netted to the bank account | | Chargeback reserve | Reserve account per merchant | Dispute resolved either way | | Unapplied receipt | Suspense account | Matched to an invoice | | Accrued but unbilled revenue | Accrual account per period | Invoice issued | Classical accounting calls the general shape a control account with a subsidiary ledger; payments systems call it a sub-ledger or virtual accounts under a master account. Subaccounts exist to make it cheap: an account is a base id plus a subaccount, so a per-agreement or per-cycle account is addressable without minting unrelated top-level accounts (ADR-0012). ## Inflight holds are the implemented instance [Inflight holds](inflight.md) apply this to authorisation and capture, with the per-destination holding account created for you. `authorize` rewrites every destination of a trade to a holding subaccount, `confirm` sweeps a hold to its destination, and `void` returns it to the funders recorded in the authorize transfer's leg table. All three are ordinary commits. Reach for them when the shape is authorise then capture, and build your own account when it is not. ## When the state holds no value Not every workflow state is money sitting somewhere. An authorisation you have accepted but not funded, a payment awaiting compliance review before any value moves, a quote: none of these park value, so none of them is an account. Those live outside the ledger, or as metadata on the transfer that eventually commits. The test is whether value has moved. If it has, the state is an account. If it has not, the ledger has nothing to record yet, and recording it anyway means inventing the status field this model does without.