P-LensOpen the app

P-Lens Engineering Notes

Law shapes the ledger: modeling Japanese pachinko

Based on the Japanese article “特殊景品に交換したところで帳簿を終わらせる理由”, restructured for readers outside Japan.

When Law Shapes the Ledger: Domain Modeling in Japanese Pachinko

In Japan, you cannot cash out at the pachinko parlor. A pachinko parlor (in the trade in Japan, a hall, ホール, hoh-ru) rents balls for cash and, at the end of play, exchanges the balls for prizes. It does not pay out cash, and it does not buy back the prizes it gave. Some of those prizes are bought for cash, but by a separate business, the prize exchange shop, outside the parlor. The arrangement as a whole is widely known as the three-shop system (三店方式, sahn-ten hoh-shi-ki).

P-Lens is an open-source bookkeeping app for a player’s own pachinko and pachislot results, with an hledger journal as its book. Following the domain model faithfully led to a model in which, inside a parlor, balls never turn into cash: there is no account and no kind of entry for it. The model does not route around the rule. It records the transactions the rule allows, where they happen, and stops where the parlor’s part ends.

This article explains the three-shop system from the start, then walks through the P-Lens domain model with the real code and journal, and ends with what the case says about domain modeling in general.

All code is quoted from p-lens at v0.10.117 (GPL-3.0-or-later). The journal examples come from its test fixture, apps/web/fixtures/basic-play; its parlor names, マレハン東京皇国第8支店 (read Ma-re-han Toh-kyoh Koh-ko-ku Dai-ha-chi Shi-ten, “Marehan, Tokyo Empire, Branch No. 8”) and 723豚の帽子亭店 (read Shi-chi-nissan Buta-no-boh-shi-tei, “723, the Pig’s Hat Tavern”), are invented.

The three-shop system, from zero

Pachinko and pachislot are machine games played in pachinko parlors across Japan. A pachinko machine is played with small steel balls, a pachislot machine with tokens (medals); for brevity this article says balls for both.

A visit has two steps at the parlor and one step outside it.

  1. Rental. The player hands the parlor cash and rents balls. The rental price is capped by regulation at 4 yen a ball and 20 yen a medal, plus consumption tax; many parlors also run cheaper corners, such as 1 yen a ball.
  2. Exchange at the counter. Whatever balls are left at the end of play go to the parlor’s counter, where they are counted and exchanged for prizes. Some prizes are general prizes (一般景品, ip-pan kay-hin): snacks, drinks, everyday goods. Others are special prizes (特殊景品, to-ku-shu kay-hin), the ones the next step is about.
  3. Outside the parlor. The player can take special prizes to a prize exchange shop (景品交換所, kay-hin koh-kahn-jo), a separate business usually close by, which buys them for cash.

The loop is closed by a fourth party. The exchange shop sells the special prizes it bought to a wholesaler (景品問屋, kay-hin don-ya), and the wholesaler sells them to the parlor, which hands them out at its counter again.

Diagram of the three-shop system. The player gives the parlor cash and rents balls; at the end of play the player gives the leftover balls to the parlor’s counter and receives prizes. Inside the parlor no step turns balls into cash. The player sells special prizes for cash at a prize exchange shop, a separate business nearby. The exchange shop sells the special prizes to a wholesaler, and the wholesaler sells them back to the parlor.

Inside the parlor, cash moves one way: in, for balls. Balls go out only as prizes. The one place where something the parlor handed out is exchanged for cash is the exchange shop, and what is exchanged there is a special prize, not a ball.

There is also a step a player can take instead of exchanging: leave the balls on deposit with the parlor (貯玉, cho-dah-ma, savings) and play them on another day. A deposit is a balance at that one parlor; balls saved at マレハン東京皇国第8支店 cannot be played at 723豚の帽子亭店.

For readers who know casinos, the contrast is the useful part. At a casino, the venue that sold the chips also cashes them out, at its own cage. At a pachinko parlor, the venue that rented the balls does not cash them out at all. That is the one sentence worth remembering from this section: in Japan, you cannot cash out at the pachinko parlor.

The thesis: record what changes hands, where

A ledger app for this domain can be modeled in two ways.

Two models of the same visit, compared. Top, labelled as a simplification for comparison and not what happens: cash becomes balls and balls become cash, as if the parlor paid out. Bottom, the transactions as law and practice draw them: inside the parlor, cash for balls at rental and balls for prizes at full value at the counter, which P-Lens records; outside it, special prizes for cash at a separate prize exchange shop, which pays less than the counter valued them, outside the book.

The top half is how a casual observer might simplify a visit: yen went in, yen came out, the balls were in between. It is a fair summary of what a player’s wallet did over a day. It is not a description of any transaction that took place, because no party in it exchanged balls for cash.

The bottom half is the transactions as the institutions draw them. At the parlor, cash is exchanged for balls and balls for prizes. At the exchange shop, special prizes are exchanged for cash. Each exchange has a place and two parties, and the parlor’s box has no arrow from balls to cash.

P-Lens models the bottom half and keeps to the parlor’s box. Its book is about play: what the player rented, what was left, what is on deposit where, what was exchanged at the counter. The book ends at the counter. The model does not route around the rule; it records the transactions the rule allows, where they happen.

The domain model in P-Lens

Balls are a commodity, per parlor and per rate

In hledger, an amount is a quantity of a commodity, and a commodity can be any symbol. P-Lens makes each parlor’s balls, at each rental rate, a commodity of its own. From core/commodity/commodity.ts:

/**
 * The commodity a parlor's tokens are at one rental rate, e.g.
 * `玉(4円・マレハン東京皇国第8支店)`.
 *
 * One commodity per parlor, kind and rental rate, on purpose. A ball at one
 * parlor cannot be exchanged at another, nor a 1-yen ball as a 4-yen one, so
 * economically they are different things, and hledger keeps them apart only if
 * their symbols differ. The parlor and rate are in the symbol so the journal
 * stays readable without any lookup table.
 */
export const tokenCommodity = (kind: TokenKind, parlorName: string, tier: string): string => `${TOKEN_NAMES[kind]}(${tier}・${parlorName})`

This is the first place the law shows up in the model. Balls are not yen, because the parlor does not exchange them for yen. They are a unit that exists inside one parlor, and only there.

What a parlor’s balls are worth in yen is a price, the parlor’s exchange rate: what the exchange shop pays, in the end, for the special prizes a ball gets at the counter. It is an hledger P directive, and a change of rate is a new line on a later day. The fixture’s prices.journal:

P 2026-09-01 "玉(4円・マレハン東京皇国第8支店)" 3.57 JPY
P 2026-09-01 "枚(20円・マレハン東京皇国第8支店)" 17.86 JPY
P 2026-09-01 "玉(4円・723豚の帽子亭店)" 3.33 JPY
P 2026-09-01 "枚(20円・723豚の帽子亭店)" 18.20 JPY

P 2026-09-25 "玉(4円・マレハン東京皇国第8支店)" 3.50 JPY

The fixture has the two-place rates a person would type; a rate the app writes itself has six places, 3.571429 for 28 balls to 100 yen (valuation.ts).

A ball rented at 4 yen goes for 4 yen of prizes at the counter; nothing is lost there. The discount is at the exchange shop, which pays 3.57 yen a ball here (28 balls to 100 yen). Rent 500 balls for 2,000 yen and take them straight to the counter: three special prizes, five small bottles of Yakult, a candy and two cotton swabs, 2,000 yen’s worth at the counter’s prices. The exchange shop pays 1,500 yen for the three special prizes. So in cash, a rented ball is worth what the shop will pay for it, and the gap between the two prices is lost the moment the balls are rented. The book records it then, as a cost of play.

Renting balls is spending yen

One play is one entry. This is the first play in the fixture’s transactions/2026.journal:

2026-09-05 マレハン東京皇国第8支店 | 流星 123番台  ; play-id: made-up-0905, parlor-id: malehan-tokyokoukoku, game-kind: pachinko, machine: 流星, cabinet-number: 123
    資産:現金                        -12000 JPY
    費用:貸玉差額                    1290 JPY
    費用:遊技                        3000 "玉(4円・マレハン東京皇国第8支店)" @ 3.57 JPY
    資産:貯玉:マレハン東京皇国第8支店:4円         4800 "玉(4円・マレハン東京皇国第8支店)"
    収益:遊技                        -4800 "玉(4円・マレハン東京皇国第8支店)"
    (統計:期待値)                    5500 JPY
    (統計:時間)                      10800 秒

The account names are Japanese; the translations below are the ones the README uses. Read it as three parts.

The code that writes the rental part, from core/tally/compose.ts:

/**
 * Renting tokens and shooting them: cash spent on the play, split at the
 * moment of renting into what the tokens are worth at that day's exchange
 * rate and the spread lost on top of it. Rented tokens are never savings —
 * they go from the counter into the cabinet — so no savings account is
 * touched.
 *
 * ¥12,000 for 3,000 balls exchanged at ¥3.57 is ¥10,710 of balls shot and a
 * ¥1,290 loss, settled that day. A later change of rate moves what balls
 * are worth and never this loss.
 */
const rentalPostings = (rental: Rental, rate: Decimal, holding: Holding): readonly Posting[] => {
  const spread = subtract(rental.outlay, multiply(rental.tokens, rate))
  return unlessZero(rental.tokens, [
    real(ACCOUNTS.cash, negate(rental.outlay), YEN),
    ...unlessZero(spread, [real(rentalSpreadAccount(holding.kind), spread, YEN)]),
    { ...real(ACCOUNTS.playExpense, rental.tokens, holding.commodity), cost: { rate, commodity: YEN } },
  ])
}

Cash appears once, with a minus sign. That is the only direction it moves at the parlor.

Balls on deposit are an asset, in balls

The deposit account holds balls, not yen: 4800 "玉(4円・マレハン東京皇国第8支店)". The account name carries the parlor and the rate, and the commodity carries them again. A balance report shows the deposit in balls, and in yen only when asked to value it at the parlor’s rate (--value), which hledger does from the P lines.

The README sums up this part of the model in one line: “Balls and medals are a parlor’s own currency.” (README.md)

The result of the play, what the README calls 収支, is the income statement: revenue less expense, with the balls valued at the day’s rate. It is settled on the day of play, not on the day the balls are exchanged. This matters for the next step.

Exchanging is balls for prizes

When the player takes deposited balls to the counter, the book records an exchange of balls for prizes. The code, from core/savings/prize-exchange.ts, opens with what the exchange is:

/**
 * Savings exchanged for prizes at a parlor's counter: so many tokens out of
 * one pool on a day, and what the special prizes (特殊景品) handed over for
 * them are worth in yen. A parlor gives prizes, never money, and the book
 * says what the parlor did: what becomes of a prize afterwards is outside
 * it. It is no tally and no tally's result — the tokens were won, or lost,
 * when they were played for.
 */

and the entry it writes (L135-L166):

/**
 * A prize exchange as one journal entry. The tokens leave savings at the
 * book's exchange rate for the day — what they stood at in every figure
 * until then — and leave the book as prizes: the special prizes at what
 * they are worth, and the odd tokens beside them as odd prizes (端玉景品等),
 * the sweets tokens short of a special prize are given as, at the rental
 * price. What the tokens were worth at the rate and what the prizes are
 * worth do not meet, so the difference is the translation adjustment
 * (換算調整勘定), as the difference left by restating one unit in another
 * is kept — written with no amount, for hledger to balance. All three are equity, as an opening balance is: taking prizes
 * home is no tally's result, so no income statement counts it, and the book
 * never says money came of it.
 */
export const prizeExchangeEntry = (exchange: PrizeExchange, prices: readonly PricePoint[]): Result<Entry, PrizeExchangeProblem> => {
  const valid = checked(exchange)
  if (!valid.ok) return valid
  const commodity = tokenCommodity(exchange.pool.kind, exchange.parlor.name, exchange.pool.tier)
  const point = rateOn(prices, commodity, exchange.date)
  if (point === undefined) return Err({ type: "no-exchange-rate", commodity, date: exchange.date })
  const odd = exchangedOddPrizes(exchange.tokens, point, exchange.specialPrizes, rentalPriceOfTier(exchange.pool.tier))
  return Ok({
    date: exchange.date,
    description: describedBy(exchange.parlor.name, "景品交換"),
    tags: [...identityTags(exchange.parlor.identity), [SAVINGS_TAG, "prize-exchange"]],
    postings: [
      { ...real(savingsAccount(exchange.pool.kind, exchange.parlor.name, exchange.pool.tier), negate(exchange.tokens), commodity), cost: { rate: point.rate, commodity: YEN } },
      ...unlessZero(exchange.specialPrizes, real(ACCOUNTS.specialPrizes, exchange.specialPrizes, YEN)),
      ...unlessZero(odd, real(ACCOUNTS.oddPrizes, odd, YEN)),
      { account: ACCOUNTS.translationAdjustment, kind: "balancing" },
    ],
  })
}

The fixture holds no exchange, but the app’s tests record one on top of it (tests/hledger/savings.test.ts): 4,000 of the 4,800 deposited balls exchanged at マレハン東京皇国第8支店 on October 4 for special prizes the exchange shop pays 13,800 yen for. Run through prizeExchangeEntry with the fixture’s prices, it is this entry:

2026-10-04 マレハン東京皇国第8支店 | 景品交換  ; parlor-id: malehan-tokyokoukoku, savings: prize-exchange
    資産:貯玉:マレハン東京皇国第8支店:4円  -4000 "玉(4円・マレハン東京皇国第8支店)" @ 3.5 JPY
    純資産:特殊景品                     13800 JPY
    純資産:端玉景品等                   228 JPY
    純資産:換算調整勘定

Yen appears in this entry, and it is worth being exact about what it records: the value of the prizes the parlor handed over, as equity. All three accounts are under 純資産 (equity), not assets. The README calls it “the terminal account”:

Equity is the terminal account. The booking ends with the prize exchange: the prizes received are entered not as assets but in 純資産:特殊景品 (special prizes) and 純資産:端玉景品等 (odd prizes), and the part of a yen left when balls or medals are restated in yen in 純資産:換算調整勘定 (the translation adjustment). The keeping, consumption, transfer or any other transaction of a prize after it is received is not booked.

(README.md) A prize is not booked as an asset because an asset would need a later entry for what became of it, and the next thing that becomes of a special prize happens at the exchange shop. Equity has no later entry.

The exchange is also not a result. The play’s result was settled on the day of play, and the exchange appears in no income statement. The same test file checks that the totals over all days are the same before and after the exchange (L145-L151).

Where the book ends

What P-Lens records, step by step. Card one, renting balls at the parlor: cash minus 12,000 yen, a rental spread of 1,290 yen, and a play expense of 3,000 balls at 3.57 yen. Card two, balls left at the end: 4,800 balls on deposit, an asset in balls, against play revenue. Card three, exchange at the counter: minus 4,000 balls at 3.5 yen, special prizes 13,800 yen, odd prizes 228 yen, and a translation adjustment of minus 28 yen balanced by hledger, all equity. Then a line marked as where the book ends. Below it, special prizes for cash at the exchange shop: no entry and no account.

The book ends at the counter. Past it, the model has nothing to write with:

The terms of use say the same in Japanese (terms.md): what the app records is the record of play, from renting balls or medals with cash to exchanging them for prizes, limited to what happens inside the parlor; what happens after the prizes are received is not recorded.

One more path is worth checking, because it looks as if it could reintroduce cash: a play kept in yen alone, where the player types what went in (投資額) and what came back (回収額). A gain there is still not cash. From core/tally/sides.ts:

/**
 * What lies between a tally's two sides in yen. A loss is cash spent. A gain
 * is never cash received — cash in a book only ever falls: it is prizes
 * taken home, the special prizes in whole yen and the part of a yen left
 * over the translation adjustment for hledger to balance, as a prize
 * exchange writes them.
 */
const betweenPostings = (between: Decimal): readonly Posting[] =>
  isNegative(between) ? real(ACCOUNTS.cash, between) : isZero(between) ? [] : [...real(ACCOUNTS.specialPrizes, rounded(between, 0)), TRANSLATION_ADJUSTMENT]

The field is labelled in yen, and the book records what the parlor could have handed over: prizes.

What this says about domain modeling

The usual advice is to model the domain, not the database. This case adds: find out where the domain’s boundaries are, and who drew them. Here they were drawn by law and by long practice, and they are sharp.

The boundary is a bounded context, and it was given. The parlor is one context: rental, play, deposit, counter. The exchange shop is another business, with its own counterparties and its own rules. In DDD terms the book models one bounded context and does not model the other. Nobody had to decide where to cut; the law and the way the trade is organised had already cut it. The model’s job was to notice.

Units follow the counterparty. A ball is worth something only at the parlor that rented it, so the unit is per parlor and per rate. A model that converted to yen on entry would be simpler to sum and wrong about what the player holds. The same pattern applies to anything one issuer controls: store credit, loyalty points, airline miles, a game’s currency, gift cards. Each is a commodity of its issuer, with a price, not a currency.

Record the exchanges that happen, at the place they happen. The simplified view (cash in, cash out) is a reasonable summary and a poor model, because the transaction it implies does not exist. Every play and every prize exchange in P-Lens is written as handovers that took place at the parlor: cash for balls, balls for prizes. An entry that opens, corrects or writes off a deposit is none, and has an account of its own that says so. A ledger whose entries are real exchanges stays explainable; one with a summary entry has to explain who the other party was.

Make the end of the model a structure, not a policy. “We do not record what happens after the counter” could have been a line in a document. In P-Lens it is the shape of the account tree (prizes are equity, so there is nothing to continue from), a test over every entry the app writes, and a list of the files allowed to touch cash. A boundary that is a structure survives refactoring better than a boundary that is a convention.

Let the language carry the domain’s words. The account names are the trade’s own words: 貯玉, 特殊景品, 端玉景品等. The code comments use them too. When a parlor, a player and the code all say 特殊景品, there is one meaning to keep straight instead of three.

Summary

All articles