BBackend CourseLeroy Labs
0/0
Search the course

Double-entry ledgers for money you can trust

Debits and credits, journal entries and lines, the balance invariant, chart of accounts, account types, gap-free numbering, reversals not edits, open periods, and wallets built on a ledger.

● expertIR3D (Kontentt)SperoTek (in dev)

Why not just balance = balance - amount?

A mutable balance column can’t answer basic questions: Why is it 340? Where did the money go? Was anything lost? And a single bug corrupts it silently. Double-entry records every movement as at least two lines that sum to zero:

Journal Entry JE-000042 (2026-11-25) "Creator payout"
   Debit   Creator Wallet (liability)     150.00
   Credit  Paystack Clearing (asset)                150.00
                                         ------  ------
                                          150.00  150.00   <- MUST balance

Money is never created or destroyed, only moved between accounts. Balances are derived by summing lines.

Account types and normal balances

Type Increases with Examples
Asset Debit Cash, Bank, Receivables, Paystack clearing
Liability Credit Creator wallets (money you owe), Payables
Equity Credit Owner’s capital, Retained earnings
Revenue Credit Platform fees, Sales
Expense Debit Payment processing fees, Salaries

The accounting equation: Assets = Liabilities + Equity + (Revenue − Expenses).

Data model

accounts(id, company_id, code, name, type, parent_id, is_control)
journal_entries(id, company_id, number, entry_date, period_id, status, description,
                reverses, idempotency_key, version)
journal_lines(id, company_id, entry_id, line_number, account_id,
              debit NUMERIC(19,4), credit NUMERIC(19,4),
              CHECK ((debit > 0 AND credit = 0) OR (credit > 0 AND debit = 0)))

Rules a real ledger enforces

  1. Balance. Σ debits = Σ credits per entry.
  2. Immutability. Posted entries never change. Corrections are reversal entries (swap debits and credits) plus a new correct entry.
  3. Gap-free sequential numbering, for auditability.
  4. Open period. You can’t post into a closed month or year.
  5. Valid accounts. Active, postable (not a header), in the same company.
  6. Idempotent posting. The same request twice must not post twice.

In your projects

SperoTek (in dev)ledger/application/DefaultPostingEngine · JournalRules · FiscalCalendar · JdbcDocumentNumbers · 009-ledger.sql

All six rules:

  • JournalRules.check(...) validates balance and accounts.
  • There is a single path to POSTED (finish()).
  • FiscalCalendar.openPeriodOn(date), otherwise PERIOD_NOT_OPEN.
  • "JE-%06d".formatted(numbers.next(...)) under a per-company advisory lock.
  • uq_journal_idempotency (partial unique) plus findByIdempotencyKey.
  • DB triggers block changes to posted entries, and uq_journal_one_reversal means an entry can be reversed once, with a mandatory reason.
  • Base currency is locked after the first posting.
  • Composite FKs stop a line from referencing another company’s account.
  • ControlAccount (e.g. AR/AP control accounts), chart-of-accounts templates (AccountTemplate, /accounts/from-template), CSV import.
  • JournalPosted events feed reporting: trial balance, balance sheet, P&L and general ledger.
IR3D (Kontentt)finance/application/LedgerService · ledger_accounts/ledger_entries/ledger_transactions · LedgerReconciliationJob

A wallet ledger. Every pay-in, hold, release, payout, fee and refund is a LedgerService transaction built from Posting.debit/credit, and it “rejects anything where debits != credits”. Wallet rows are locked (findByUserIdForUpdate) during movements. A nightly reconciliation recomputes balances from the ledger and compares them with wallet balances and Paystack.

Reversal, not edit

JE-000042  Debit Expenses 100 / Credit Bank 100      (posted - wrong account!)
JE-000043  REVERSES JE-000042: Debit Bank 100 / Credit Expenses 100   reason: "wrong account"
JE-000044  Debit Rent 100 / Credit Bank 100          (correct entry)

The audit trail shows exactly what happened and why.

Alternatives

Option Notes
Build your own (yours) Full control; you must get every rule right
TigerBeetle A purpose-built, extremely fast financial transactions database
Formance, Modern Treasury, Fragment Ledger-as-a-service APIs
Plain balance columns ❌ Not auditable
  • Trial balance. Sum of all debits = sum of all credits across the ledger, which is the first sanity check.
  • Sub-ledgers and control accounts. Customer-level receivables roll up into one AR control account.
  • Accrual vs cash accounting, and period close (lock a period, post adjusting entries).
  • Multi-currency. Transaction currency, base currency, FX rate per line, and realised/unrealised gains.
  • Immutability ≈ event sourcing. A ledger is the original event store.

Check yourself

1. A posted entry used the wrong account. Correct fix?
2. Creator wallet balances on IR3D are which account type (from the platform’s view)?
3. What does ck_journal_line_one_sided enforce?