Double-entry ledger: the money left, but it hasn't arrived. Where is it? | jonathanjuliani.dev
Technical·Nov 6, 2026 · 7 min
Double-entry ledger: the money left, but it hasn't arrived. Where is it?
A balance lookup can't say where the money is mid-transfer. In a double-entry ledger, two rows add up to zero, and that answers where it is.
Jonathan Juliani
The payment left the sender's account, hasn't reached the recipient, and support asks: "where's the money?" If the answer means opening three dashboards, or digging through the logs, it starts to look like your system doesn't know. A ledger answers it with one lookup, and the next section says what a ledger is here.
Here's how it works in a project on GitHub, a code sample with a cross-border payout app. We'll simulate a transfer that sends US dollars and delivers Mexican pesos. To keep that simpler than converting USD straight into MXN, it converts to a stablecoin first, a digital coin that stays worth one dollar. Three currencies, three providers (the company holding the money at that step: the bank or the ramp), and the money can be sitting with any of them at any moment.
What we'll get straight:
What double entry is, and why the rule that has to stay true is "the sum has to come out to zero"
How to build the list of places money can sit (the chart of accounts, a way to track the money), with a middle currency (the stablecoin) visible
Why a balance is a SUM, never a column
The tradeoffs against a balance column and against a plain record
Prerequisites: basic SQL (SUM, GROUP BY) and the idea of storing money as integer cents (the smallest unit of the currency, a dollar cent, a peso cent, and so on, which is amount_minor). If that still isn't clear, the server-side pricing post covers it, and it's a short one.
Double entry in a few words: money doesn't appear, and it doesn't disappear
Every movement of money leaves one account and enters another. It should, anyway. That is the ledger: a book that doesn't only record that 10 dollars left your account. It records that 10 left yours to be added to someone else's, both facts together, the leaving and the arriving.
Subscribe to the newsletter
Get new posts and videos by email as soon as they are out.
In this example, when the dollars leave the sender's bank (collect in the code), the ledger does not write "collected $2,493.63". It writes where the money left and where it went: sender_bank -2,493.63 and usd_clearing +2,493.63. Two rows, a minus and a plus, and they cancel (they add up to zero). That is double entry. Money didn't appear from nowhere and didn't vanish. It moved. You can call the two sides a debit and a credit. Every row written for that event is a journal, a diary of that event, and they share an id (journal_id) and add up to zero per currency.
1sender_bank writes −2,493.63. The dollars left.
2usd_clearing writes +2,493.63. The two lines cancel.
In the project, the table is only this:
code
-- apps/api/src/db/schema.sqlCREATE TABLE IF NOT EXISTS ledger_entries ( id TEXT PRIMARY KEY, payout_id TEXT NOT NULL REFERENCES payouts(id), journal_id TEXT NOT NULL, account TEXT NOT NULL, currency TEXT NOT NULL CHECK (currency IN ('USD', 'DUSD', 'MXN')), amount_minor INTEGER NOT NULL, memo TEXT NOT NULL, created_at TEXT NOT NULL);
Notice what is not there: a balance column. There's even a test that reads schema.sql and fails if the word balance shows up (T6: there is no balance column). The reason is that the balance is not stored. It is a sum, so a balance column doesn't belong.
Three different paths, and what can break in each
Before getting to double entry, it's worth being fair to the alternatives, because they're what almost everyone builds first (me included).
Path
What it is
Where it breaks
A. Balance column
UPDATE accounts SET balance = balance - 2493
You lose the history. Two concurrent UPDATEs and the balance drifts with nothing left behind. You can't prove it's right
B. Plain record (single-entry)
One line per event: collect 2493.63 USD
You have history, but nothing checks that money which left also arrived. "Where is the money?" becomes logic scattered through the code
C. Double entry
Two rows per movement, and the balance sum comes out to zero per currency
More rows, more concepts, and a balance becomes a SUM (at scale, a saved photo of the total)
In this example we take C, because that's the one this post is here to explain. Read the rest as: the team picked C.
The cost is real: a payout (the payment that goes out) that reaches its destination writes 16 ledger rows. But each of those 16 is there to answer a question A and B can't answer in this case.
Implementation: a postJournal that refuses before it writes
Whoever writes to the ledger passes the movements from → to, and postJournal turns each of those into two rows:
code
// apps/api/src/ledger/post.ts (excerpt)export function postJournal(db, clock, log, payoutId, memo, entries: JournalLeg[]): string { const rows: SignedRow[] = []; for (const entry of entries) { if (!Number.isInteger(entry.amountMinor) || entry.amountMinor <= 0) { throw new LedgerError(`amount must be a positive integer, got ${entry.amountMinor}`); } rows.push({ currency: entry.currency, account: entry.from, amountMinor: -entry.amountMinor }); rows.push({ currency: entry.currency, account: entry.to, amountMinor: entry.amountMinor }); } return postSignedJournal(db, clock, log, payoutId, memo, rows);}
usd_clearing is the tray where the dollars wait, between leaving the sender and the conversion (a clearing account).
Look at onramp: think of it as the exchange counter, where dollars become the digital coin. It takes USD in and pays DUSD out.
offramp, further along the payout, is where the digital coin becomes pesos.
onramp is a conversion account: it exists in both currencies and closes at zero when the exchange is done.
Each currency still adds up to zero on its own, and the conversion is recorded as a fact, not as magic. And these are the places the money can be.
The stablecoin as its own currency: money in the middle of the trip
This is the decision the post is named after: the stablecoin stays visible as money in transit. In the example, the project's stablecoin (DUSD) is worth one dollar.
The decisions for this project are in DECISIONS.md. A real example from the project:
the recipient is getting 42,800.00 MXN at 17.25.
The sender pays $2,493.63 ($12.47 fee, $2,481.16 converted).
The charge succeeded, the dollars left the sender, and the blockchain transfer hasn't confirmed yet. So where do we look up the balance? And how do we answer "where is the money right now?"
We look it up in the ledger, summing and grouping by currency:
code
SELECT account, currency, SUM(amount_minor) AS totalFROM ledger_entriesWHERE payout_id = ?GROUP BY account, currency;
Account
Currency
Balance
sender_bank
USD
-2,493.63
fee_revenue
USD
+12.47
onramp
USD
+2,481.16
onramp
DUSD
-2,481.16
treasury_wallet
DUSD
+2,481.16
12,493.63 USD left the sender. 12.47 of it is already the fee.
22,481.16 USD sits on the onramp, still in dollars.
3The same onramp owes 2,481.16 DUSD. Dollars and the coin do not blend.
4The coin is in the wallet, waiting on the blockchain.
USD adds up to zero, DUSD adds up to zero, and the answer for support is the last row: the money is in the platform's wallet, as stablecoin, waiting on the blockchain. If DUSD were USD, that last row would blend into the dollars from the bank, and you couldn't tell whether the money had left the US side yet.
But Jon, isn't that just knowing which step the payout is on? The status table already says so.
Almost. Status says what the system thinks happened. The ledger says where the money actually is. If you have this system and the status disagrees with the ledger, you probably have a bug, and there is a reconciliation to catch exactly those cases: reconciliation compares the ledger with each provider's record and lists the differences.
What the docs don't tell you: a rate that moved, a negative balance, and the rest
When the rate gets worse after the quote, someone pays the difference. The idea of the product is: the recipient gets exactly the amount on screen. So the platform absorbs it (D8). In the ledger that's one more line in the journal entry that delivers the pesos: fx_variance → offramp, in MXN. That difference is the FX loss. In the simulator there's an example where the rate gets 2% worse and fx_variance ends at -856.00 MXN. The recipient gets the full 42,800.00, and the cost has a name and an amount.
A platform account can go negative, and that's fine. A negative fee_revenue, fx_variance, or network_fee_expense is the platform's cost, and that's the number reconciliation finds and reports. External accounts are different: a refunded payout has to leave sender_bank at zero and no leftover on any DUSD or MXN account. There's a test for that.
Wait, if postJournal always writes a from/to pair, the sum is zero by construction. Isn't the check pointless?
In our case, yes, for whoever uses postJournal the check is pointless, and that's the point: a good API makes a movement that doesn't balance impossible to write. The sum check protects anyone who calls postSignedJournal directly (a manual adjustment, a migration). And the accounts-per-currency list catches the mistake the pair can't: posting MXN to sender_bank.
Append-only here is a convention. Think of a notebook with no eraser: a mistake is undone by a new line, a reversal, never by erasing the old one. No code runs UPDATE or DELETE on ledger_entries, and the refund is that reversal, never an edit. But nothing in SQLite stops it. A trigger here is only a lock inside SQLite. In production we have to decide whether to reject UPDATE/DELETE, or give the app's database user INSERT only.
Which one to pick for which case
Case
Path
Points, internal credits, nothing anyone audits
A balance column is enough
History for a statement, one currency, one provider
A plain record holds up
Someone else's money, more than one provider or more than one currency
Double entry
High volume, the balance read all the time
Double entry plus a saved photo of the balance every so often (this project doesn't need one: the balance is per payout, and a snapshot is a later system-design step)
If your system has to answer "where is the money?" for someone outside it (support, an audit, the customer), double entry pays for itself the first time a provider gets stuck.
Quick reference
Concept
What to remember
Movement / journal entry
One thing that happened = a group of lines that add up to zero per currency. In the code, journal
Balance
SUM(amount_minor), never a column
Middle currency
A stablecoin is its own currency. That's how money in transit shows up
Conversion account
onramp is where dollars become the digital coin, offramp is where it becomes pesos. The account exists in both currencies
FX loss
The platform absorbs it in fx_variance. The recipient gets the quoted amount
Validation
Everything before the first INSERT. If it fails, nothing is written
Correction
A reversal: a new journal entry that undoes the previous one. Never UPDATE
Next steps
The quoted amount only closes because it's integer cents from start to finish. The reason is in the server-side pricing post.
A money rule spread across several services is the "God Service" from the wrong Node architectures post. Here it lives in one file, one function per event.
To see it running: docker compose up, create a payout at localhost:8801, and call GET /api/payouts/<id>/ledger. It returns every ledger line and the sum per currency (which has to come out to zero).
Does your system store the balance or always compute it? If a provider got stuck right now, could you say where the money is without a dashboard or a ticket? Tell me in the comments how your team handled it (or didn't). And if I got something wrong, or you see a better way, say so and I'll update the post.