Skip to content

Architecture ​

Every money movement is a double-entry journal transaction in Postgres; balances are a cache of those entries, written in the same transaction. One request = one Postgres transaction.

flowchart LR
  C([Client]) -->|POST + Idempotency-Key| I["IdempotencyInterceptor<br/>opens the transaction"]
  C -->|GET| CT
  I --> V["ValidationPipe<br/>DTOs"]
  V --> CT["Controllers<br/>wallets · deposits<br/>holds · transfers"]
  CT --> S["Services<br/>state rules"]
  S --> L["LedgerService<br/>postTransaction"]
  S -.->|this.tx.db| TH[("TransactionHost<br/>AsyncLocalStorage")]
  L -.->|this.tx.db| TH
  I -.-> TH
  TH --> PG[("Postgres<br/>one transaction")]
  F[DomainExceptionFilter] -.->|"error JSON"| C

Solid: a call. Dotted: everything shares the request's single Postgres client.

  • POST: the interceptor opens the transaction; controllers and services run inside it.
  • GET: handlers open their own with tx.run().
  • Services never open connections: they read the current client from TransactionHost.

Invariants and where they are enforced ​

#InvariantWhere
1Amounts are integer cents, never floatsbigint columns; pg parser rejects unsafe integers; DTOs with @IsInt
2Every transaction has ≥ 2 lines and sums to 0assertBalanced in src/ledger/ledger.service.ts
3The ledger is append-onlyforbid_mutation trigger in migrations/001_init.sql
4Balances are written with their entriessame postTransaction, same transaction
5No user account goes negative, even under concurrencyFOR UPDATE in account_id order + allow_negative check
6Exactly once per idempotency keykey, response and entries in one transaction
7A pending ACH is never spendabledeposits credit user:pending; spending only debits user:available

Reading guide ​

OverviewDeep dive
Accounts and entries: chart of accounts, sign conventionReal world: ACH, card holds, system accounts
Deposit and hold states: transitions and their entriesInside the Nest app: modules, lifecycle, TransactionHost
Idempotency: retries move no moneypostTransaction: the write path, traced
Concurrency: locks in a fixed orderErrors, reconciliation, pagination
Data model: tables, constraints, triggersTesting · API · Deployment

Why it's built this way: decision records.

Money is an integer number of cents. Every movement sums to zero.