Skip to content

Enforce ErrInsufficientBalance for Asset and Expense accounts - #2

Merged
raphi011 merged 2 commits into
claude/banking-system-go-1v6MGfrom
claude/account-features-cleanup-oSTHT
Feb 28, 2026
Merged

raphi011 merged 2 commits into
claude/banking-system-go-1v6MGfrom
claude/account-features-cleanup-oSTHT

Conversation

@raphi011

Copy link
Copy Markdown
Owner

No description provided.

Add defined types (LedgerID, SubledgerID, AccountID, TransactionID,
EntryID, HoldID) so map keys and method parameters clearly communicate
what kind of identifier they expect. These are defined types (not
aliases), giving compile-time safety against mixing e.g. a HoldID
where an AccountID is expected.

Change all public Service methods to return value types instead of
pointers to internal state. This prevents callers from accidentally
mutating the service's data. Transaction returns use a deep copy
helper (copyTransaction) that copies the Entries slice and Metadata
map. GetSnapshot now returns ErrSnapshotNotFound instead of nil when
no snapshot exists for the given parameters.

https://claude.ai/code/session_01947Cs6jqVPDDzmAqQMoB35
PostTransaction and CreateHold now check that the available balance
(book minus active holds) would not go negative for Asset and Expense
accounts. Liability, Equity, and Revenue accounts are not checked.

https://claude.ai/code/session_01947Cs6jqVPDDzmAqQMoB35
@raphi011
raphi011 merged commit a7da9da into claude/banking-system-go-1v6MG Feb 28, 2026
raphi011 added a commit that referenced this pull request Aug 6, 2026
Both customers bank at the same institution, so the money moves between
two of that bank's own deposit accounts. No interbank obligation comes
into existence: nothing to net, no reserves to move, and no camt.053 that
could tell a bank about a book it already holds. A real bank recognises
the beneficiary as its own and books it internally; it never reaches a
scheme.

Submitted to clearing anyway it produced three wrong answers, one per
institution. SettleReturnTx emitted two statements for the SAME book,
account and Reference differing only in sign, and PostSettlementAdviceTx
dedupes on (Book, Reference, Asset) — so the second was swallowed:

    camt.053 #1 to AURODEFFXXX: account=200.100.001 reference="pay_13" movement=-250000
    camt.053 #2 to AURODEFFXXX: account=200.100.001 reference="pay_13" movement=250000
    central bank's record of this bank's reserve: 250000 -> 250000
    the bank's OWN reserve mirror:                250000 -> 0
    the bank's clearing suspense after the return:        -250000

The nostro/vostro pair the README makes load-bearing off by the full
amount, and a permanently negative suspense. Second, the returning bank
was the returner on BOTH legs, so it refused its own customer's
unconditional eight-week refund. Third — pre-existing, outside this
branch's diff — a cycle holding nothing but an on-us payment nets to zero
and strands at Cleared for ever.

Each is a symptom of an instruction that should never have reached a
clearing house, so it is refused at the one door every submission comes
through, before the submitting bank's half runs. Patching SettleReturnTx
would have treated a symptom and left the Cleared strand standing.

Separately and regardless, PostReturnLegTx's mayRefuse is now a property
of the LEG: the clawback is refusable when the scheme is a push AND this
bank is the returner, and the refund never is. Asking only "is this bank
the returner" was true of both legs when one bank is both parties, which
is the inversion above. Stated correctly it no longer depends on the
boundary check holding — the defence in depth ReadReturn's id guard and
SettleReturnTx's own copy of it already use.

Measured before the fix: both a credit transfer and a collection between
two customers of one bank were accepted (Submit = <nil>), and the on-us
pull return came back "AURODEFFXXX cannot fund its own leg of the return:
insufficient available balance".

A book transfer between two customers of one bank is a real product and
belongs in its own task; the refusal says so rather than implying the
payment is illegitimate.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants