2.02 Deposits, Withdrawals, Clearing and Suspense Accounts: Exit Evidence
Evidence · Step: 2.02 Deposits, Withdrawals, Clearing and Suspense Accounts
| Step | 2.02 Deposits, Withdrawals, Clearing and Suspense Accounts |
| Date | 2026-11-14 |
| Database | PostgreSQL 18 (postgres:18-alpine, compose.yaml) in Docker Desktop on Windows 11 |
| Result | Passed |
Deliverables
Section titled “Deliverables”| Deliverable | Where |
|---|---|
| Seven templates | crates/ops/src/deposits.rs: deposit_confirmed.v1, deposit_unmatched.v1, suspense_resolve.v1. crates/ops/src/withdrawals.rs: withdrawal_request.v1, withdrawal_sent.v1, withdrawal_confirmed.v1, withdrawal_failed.v1 |
| Template rules for items | Accounts in crates/ops/src/template.rs (require_whole_balance, require_new); errors 2014 and 2015 in crates/ops/src/error.rs |
| Ageing report | mezgeb_engine::ageing (crates/engine/src/ageing.rs), with the codes in mezgeb_ops::AGED_ACCOUNTS; the buckets in clearing and suspense |
| Chart | charts/wallet-provider.toml: new 9030 Clearing: withdrawals in transit; 9010 Suspense is now active, one sub-account per item |
| Accounting model | clearing-and-suspense.md (new); E01–E05 rewritten in deposits-and-withdrawals.md; E19–E20 in suspense.md (new); trial balance after E20; chart of accounts |
| Tests | crates/ops/tests/deposits_and_withdrawals.rs, crates/engine/tests/ageing.rs |
| Documentation | spec.md 4.6 and 4.7 |
Implementation tasks
Section titled “Implementation tasks”| Task | Done |
|---|---|
deposit_confirmed.v1 |
Yes: Dr 1010 / Cr wallet (E01; also E05, a returned payout) |
deposit_unmatched.v1 (to suspense) |
Yes: Dr 1010 / Cr a new 9010:sus-… item (E19) |
suspense_resolve.v1 |
Yes: the whole item to a customer (2010) or merchant (2030) (E20) |
withdrawal_request.v1 (hold) |
Yes: Dr wallet / Cr a new 2020:wd-… item (E02). Booked as an ordinary posting, not a ledger hold: see the decision |
withdrawal_sent.v1 (held → clearing) |
Yes: the whole pending item to a new 9030:wdt-… item (E03 (a)) |
withdrawal_confirmed.v1 (clearing → bank asset) |
Yes: the whole in-transit item to 1010 (E03 (b)) |
withdrawal_failed.v1 (clearing → wallet) |
Yes: the whole in-transit item back to the wallet (E04 (c)) |
| An ageing query for suspense and clearing balances | Yes: every open item under the given codes, oldest first, with its age at the caller’s now |
Template::book now receives the bound accounts, by role, so a template
can check its own rules against their balances. This is a change to the
Template trait from step 2.01, made before any external user exists.
Testing requirements
Section titled “Testing requirements”| Requirement | Test | Result |
|---|---|---|
| Full happy path and every failure path; balances checked at each step | the_happy_path_balances_at_every_step, a_failed_withdrawal_returns_exactly_the_original_amount, each_step_refuses_what_breaks_the_policy |
Pass. The policy test covers sending before the request, requesting into a used item (2015), requesting more than the wallet (1012), partial sends (2014), the wrong kind of account (2010), sending twice, reusing a transit item, and the suspense rules: new item (2015), whole balance (2014), owner a customer or merchant only (2010), resolved once |
| A failed withdrawal returns exactly the original amount | a_failed_withdrawal_returns_exactly_the_original_amount |
Pass: 1 santim more or less, or any other amount, is refused with 2014; the wallet ends exactly where it started |
| Confirm-after-fail and fail-after-confirm are both rejected | confirm_after_fail_and_fail_after_confirm_are_rejected |
Pass, for all four orders (also confirm twice and fail twice). The template refuses the second outcome (2014). A second outcome drafted before the first posted, as two racing bank notices would be, is refused by the posting rules (1012) |
| Clearing nets to zero after every completed lifecycle | All of the above; the 10k run checks every finished withdrawal | Pass: both sub-accounts of every finished withdrawal end at zero |
| Each template against its worked entry | each_template_books_its_worked_entry |
Pass: the whole story OB–E20 is replayed, with all ten tables of this step booked by their templates; each draft equals its table line for line, and the story ends at the trial balance |
| The ageing query | ageing_lists_open_items_oldest_first (PostgreSQL) |
Pass: open items only, oldest first, exact ages, only the codes asked for, a clock behind the ledger gives age 0, and a closed item drops out |
Acceptance criteria
Section titled “Acceptance criteria”After 10k random deposit/withdrawal lifecycles, clearing =
SUM(in-flight withdrawals).
clearing_equals_the_withdrawals_in_flight_after_10k_lifecycles runs
10,000 complete withdrawal lifecycles (about a quarter rejected), mixed
with matched and unmatched deposits and suspense resolutions, across 8
customers and one merchant. After every step:
- the sum of the open
9030items equals the sum of the withdrawals in flight, and the sum of the open2020items equals the withdrawals requested and not yet sent.
At the end, across every account:
- 9030 = withdrawals in flight, 2020 = withdrawals pending, 9010 = unresolved suspense;
- every finished withdrawal’s two sub-accounts are at zero;
- 1010 = deposits − confirmed payouts, and 1010 = 2010 + 2020 + 2030 + 9010
- 9030 exactly (no fees yet).
Along the way, more than 1,000 steps that break a lifecycle are tried: a second confirmation, a rejection after an outcome, a partial send, and a new request into a finished withdrawal’s sub-account. Every one is refused and posts nothing. The run takes about 4 seconds in a debug build.
Exit gate
Section titled “Exit gate”The suspense and clearing account policies are documented in the
accounting model (docs/accounting/). They are in
clearing-and-suspense.md: the
accounts, six rules, each lifecycle with its templates and worked entries,
and the ageing buckets. The worked entries use them:
- E01’s open questions about clearing and unmatched deposits, and E05’s about unmatched returns, are decided and linked to the policy.
- E03 is now two entries (sent, then confirmed) and E04 three (requested,
sent, rejected by the bank), through
9030:wdt-…. - E19 and E20 book an unidentified deposit and its resolution.
- The trial balance is recomputed after E20 (28 tables, 10,655.60 posted on
each side), and
worked_entries.rsand the engine’sposting.rsreplay the new story to it.
Why a withdrawal is not a ledger hold
Section titled “Why a withdrawal is not a ledger hold”The step lists withdrawal_request.v1 as a hold. Step 1.14’s holds were
considered and not used, because a hold expires by itself and returns
the money. Once a payout may already be with the bank, an automatic return
would pay the customer twice. Instead:
- the request is an ordinary posting into the withdrawal’s own
2020:wd-…sub-account, which stops the money being spent, exactly as a hold would (E02); - each later step moves the whole item, and none of these accounts may go below zero, so the ledger itself enforces one outcome per withdrawal;
- a withdrawal stuck too long appears in the ageing report, and a person decides.
What a template check does not guarantee
Section titled “What a template check does not guarantee”A template reads balances when the draft is made, outside the posting
transaction. Two consequences, both documented on Template::book:
- Whole-balance checks (2014) are backed by the posting rules. A second outcome drafted before the first posted passes 2014 but is refused by 1012 when it posts, because the item is already at zero. The test above shows this.
- The new-account check (2015) is not. Two different requests drafted at the same moment into the same new sub-account would both pass it, and both post. The client must give each withdrawal and suspense item its own sub-account ID (for example, the withdrawal’s own UUID), and the idempotency key stops a retried request posting twice. Reconciliation (step 2.15) would show such a double request.
Not done in this step
Section titled “Not done in this step”- Fees on deposits, withdrawals and returns (4020, 5020): step 2.03.
- Cancelling a withdrawal before it is sent, and returning unmatched money to its sender: documented as open questions, no template yet.
- 9020 Clearing: deposits in transit stays reserved: the bank notices booked today are already final.