Skip to content

[HARD][FINANCE] Implement an immutable double-entry ledger for all balance movements (#81) - #230

Merged
Obiajulu-gif merged 1 commit into
Chainmove:mainfrom
edehvictor:feature/immutable-ledger-81
Aug 30, 2026
Merged

[HARD][FINANCE] Implement an immutable double-entry ledger for all balance movements (#81)#230
Obiajulu-gif merged 1 commit into
Chainmove:mainfrom
edehvictor:feature/immutable-ledger-81

Conversation

@edehvictor

Copy link
Copy Markdown
Contributor

Overview

Introduces an immutable double-entry accounting ledger as the single source of truth for ChainMove wallet, investment, repayment, payout, fee, and adjustment balances.

Summary of Changes

  1. Ledger Models (models/LedgerAccount.ts, models/LedgerJournal.ts, models/LedgerEntry.ts):

    • Defined \LedgerAccount\ covering investor wallets, driver balances, pool escrow, platform clearing, revenue/fees, repayments receivable, payouts payable, and adjustment accounts.
    • Defined \LedgerJournal\ with unique
      eferenceKey\ for idempotency, event types, reversal tracking, and Mongoose hooks enforcing immutability.
    • Defined \LedgerEntry\ for explicit debit and credit entries with currency enforcement and immutability controls.
  2. Posting Service (lib/ledger/posting.service.ts):

    • Enforces system-wide balance validation (debits == credits per currency).
    • Domain helpers: \postWalletFunding, \postWalletDebit, \postPoolInvestment, \postDownPayment, \postRepayment, \postRefund, \postPayout, \postFee, and \postAdjustment.
    • Balanced reversal mechanism (
      everseJournal) preserving audit trails.
  3. Projection & Reconciliation (lib/ledger/projection.service.ts):

    • \computeAccountBalance: Calculates net balance according to standard accounting rules.

    • ebuildUserBalances: Syncs cached \User.availableBalance\ from ledger entries.

    • econcileLedger: Audits system-wide invariant (sum of debits == sum of credits per currency).
  4. Backfill Migration (scripts/migrations/backfill-ledger.ts):

    • Migration script supporting --dry-run\ and --resume\ flags for backfilling legacy transactions.
  5. Test Suite (tests/lib/ledger.test.ts):

    • Unit tests covering journal balancing, idempotency, reversals, projection rebuilds, and reconciliation.

Fixes #81

@Obiajulu-gif
Obiajulu-gif merged commit 67a3a71 into Chainmove:main Aug 30, 2026
1 of 2 checks passed
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.

[HARD][FINANCE] Implement an immutable double-entry ledger for all balance movements

2 participants