pending-states.md 5.3 KB

Pending states

The ledger has one state: committed. Anything that looks pending, reserved, authorised or unsettled is value parked in an account that you control. Its status is the balance of that account. To add a workflow state, add an account and not a flag.

Why there is nothing else to use

A posting is immutable and a balance is a projection over the postings, so no row exists that can hold a status. That is the whole of the constraint, and it is also the mechanism. Position is the only thing that can carry a state, so every state change is a transfer. Every transfer is already conserved, content-addressed and in the log.

A status in this model cannot drift from the money, because it is the money. A status column can drift.

This is the reason there is no Transfer::status and no pending flag on an account. A request for one is usually a request 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.

The money of an agreement sits in its own escrow account, which is a subaccount of one top-level escrow account. So no agreement can spend the funds of another, and each one closes on its own.

const ESCROW: i64 = 50;

fn escrow_for(agreement: i64) -> AccountId {
    AccountId::with_sub(ESCROW, agreement)
}

The account forbids overdraft. A release of more than the deposit is then impossible, and not merely incorrect:

ledger
    .create_account(Account::debit_must_not_exceed_credit(escrow_for(agreement)))
    .await?;

Entry into escrow is an ordinary payment. It is final the moment it commits:

ledger.commit(
    TransferBuilder::new()
        .pay(buyer, escrow_for(agreement), ("120.00", &usd))
        .reference("escrow-in-7001")
        .build()?,
).await?;

Exit from escrow is the same transfer with a different destination. A release pays the seller and a refund pays the buyer, and the ledger cannot tell the two apart:

// 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?;
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. They are the reason to model escrow 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 it cannot disagree with itself.
  • Partial settlement needs no new concept. Release part of the balance, and the rest stays in escrow. The same read still reports it.
  • A reader can prove that the work is finished. A drained escrow account holds no live posting, so close succeeds. A closed account then rejects every later transfer. The agreement is finished rather than marked as finished.

The same shape, elsewhere

Business state The account that holds it What ends it
Card authorisation Per-destination hold (implemented, see 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. A payments system calls it a sub-ledger, or virtual accounts under a master account. Subaccounts exist to make the shape cheap. An account is a base id plus a subaccount. So a caller can address a per-agreement or per-cycle account, and needs no unrelated top-level account (ADR-0012).

Inflight holds are the implemented instance

Inflight holds apply this to authorisation and capture. The ledger creates the per-destination holding account for you. authorize rewrites every destination of a trade to a holding subaccount. confirm sweeps a hold to its destination. void returns the hold to the funders that the leg table of the authorize transfer records. All three are ordinary commits.

Reach for them when the shape is authorise and then capture. Build your own account when the shape is something else.

When the state holds no value

Not every workflow state is money that sits somewhere. Take an authorisation that you accepted but did not fund, a payment that waits for a compliance review before any value moves, or a quote. None of these park value, so none of them is an account. They live outside the ledger, or as metadata on the transfer that commits in the end.

The test is whether value moved. If it moved, the state is an account. If it did not move, the ledger has nothing to record yet. A record anyway means the invention of the status field that this model does without.