jonnify·codemojex /codemojex/the-economy

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.

The chapter, end to end — select a stage
C5.1 · three currencies keys | clips the spend diamonds the prize C5.2 · the wallet lock · check FOR UPDATE write + TXN all or nothing C5.3 · the pool prize_pool on the game seed | buy-ins rake forward
C5.1 → three currencies, each in its own lane. Keys pay in paid rooms and are bought through the KeyShop; clips pay 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.
§1

Correctness outranks speed

the wallet discipline

The 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.

§2

The three dives

hub + 3 dives

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.

§3

References

sources · related

Sources

Related