Skip to content

Commit 3300fef

Browse files
author
Edward (Mike's bot)
committed
docs(order-book): match README to widened u128 money math and CEI order in settle_funds
Two README updates to keep prose in sync with the code: 1. Section on integer math: The old line claimed only the fee division uses u128. After the widening commit, *every* product of two u64 money values is computed in u128 and narrowed back via try_into. Also mention the per-fill fee_quote <= gross_quote invariant. 2. Section 3.5 (settle_funds): Document that unsettled_* are zeroed before the transfer CPIs, not after, and explain why (checks-effects-interactions defence). This is the README counterpart to the settle_funds CEI commit.
1 parent a067c24 commit 3300fef

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

defi/order-book/anchor/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,10 @@ resting order.
476476
`OrderBookFull`
477477
- Integer math throughout: every multiplication uses
478478
`checked_mul`; every addition on balances uses `checked_add`;
479-
fee division uses `u128` to avoid intermediate overflow
480-
`NumericalOverflow`
479+
every product of two `u64` money values is computed in `u128`
480+
to avoid intermediate overflow and then narrowed back to `u64`
481+
with `try_into` → `NumericalOverflow`. After each per-fill fee
482+
calculation an invariant check enforces `fee_quote <= gross_quote`.
481483

482484
**Token movements (up front):**
483485

@@ -622,6 +624,13 @@ mint checks on token accounts, PDA seeds).
622624
Both transfers are CPIs to the Token program, signed by the
623625
`Market` PDA using seeds `["market", base_mint, quote_mint, bump]`.
624626
627+
Order of operations is checks-effects-interactions: the
628+
`unsettled_*` counters are zeroed *before* the transfer CPIs, then
629+
the transfers run. Solana CPIs aren't reentrant in the EVM sense,
630+
but zeroing state first means no future token-program extension or
631+
transfer hook can observe stale unsettled balances mid-CPI and
632+
double-withdraw.
633+
625634
**State changes:**
626635
627636
- `market_user.unsettled_base = 0`

0 commit comments

Comments
 (0)