Codemojex · chapter C5
The economy and the bank.
Money is real, so it lives on the floor: three currencies each in its own lane, every balance change a database transaction, and a settlement that pays the same on a re-run. Scoring is pure math; the wallet is the one system where correctness outranks speed. The candid economy — fees stated, the pool stated up front, the conversion rate fixed and public.
Grounding — echo/apps/codemojex/lib/codemojex: wallet.ex · economy.ex · ledger.ex · schemas/{player,transaction}.ex · design §Core flows / §Privacy and fairness · economy/economy.md §8.
Correctness outranks speed
the wallet disciplineThe pure half. Codemojex.Economy is arithmetic — the conversion rate, the winner-take-all split, the sealed top-K split (met in C4). Pure functions have no balances to protect, so they carry no locks.
The careful half. Codemojex.Wallet holds the balances, and it is the one system where correctness outranks speed. Every mutation is one Repo.transaction that locks the player row (SELECT … FOR UPDATE), checks the non-negative invariant, writes the balance, and inserts the paired TXN ledger row — all or nothing. The players_non_negative CHECK is the database backstop if application logic ever slips.
No funnel. The row lock serializes only same-player mutations, so the whole field is never funnelled through one writer process. The database does the work a single-writer GenServer used to, and scales with it — the ledger gives correctness, the row lock gives concurrency.
The candid economy is a design stance, not a slogan: every guess pays a stated fee, the prize pool is stated up front, and the conversion rate is fixed and public — rather than hidden house entries playing against the field.
The three dives
hub + 3 divesThree currencies
Keys pay in paid rooms, clips in free rooms and carry no value, diamonds are prizes convertible to keys at a fixed 10:1 — the paid and free paths never cross.
C5.2The transactional wallet
Lock the row, check non-negative, write, insert the paired TXN — all or nothing; buy_in is the two-sided, exactly-once form behind a partial unique index.
The bank, the pool, and the rake
The pool on the game’s prize_pool; the fee waterfall; break-even by design; the BNK escrow and the published rake are forward.
Grounding — read on the running code: Codemojex.Wallet (wallet.ex), Codemojex.Economy (economy.ex), Codemojex.Ledger (ledger.ex), the Player/Transaction schemas, economy/economy.md §8, and the wallet · economy · golden-economy · golden-tournament stories.
References
sources · relatedSources
- All-pay auctionevery guess pays a fee whether or not it wins
- Repo.transactionthe all-or-nothing balance-and-ledger transaction
Related
- BCS — the architecture lawone writer per book — the ledger discipline
- C4 — Scoring and Settlementwhere the payouts are computed
- C6 — The Revenue Ledger and the KeyShopthe house’s own signed books, next