Commit 6abe2ca
fix: bigint micro-credit math in order-selector (audit C1)
Closes part of audit C1-HIGH (the order-selector hot path; pool.ts
and evm-wallet.ts remain for follow-up PRs). Previously the greedy
fill loop used parseFloat() and JS numbers throughout:
src/services/order-selector.ts:124 parseFloat(order.quantity)
src/services/order-selector.ts:127 Math.min(remaining, available)
src/services/order-selector.ts:131 BigInt(Math.ceil(take * 1_000_000))
The audit flagged three concrete failure modes:
- sell-order quantity strings with >15 significant digits lose precision
when parsed through `parseFloat`
- float subtraction in `remaining -= take` accumulates rounding error
across iterations of the greedy fill
- `take * 1_000_000` produces results like 100000.00000000001, then
Math.ceil rounds the spurious digit UP and over-charges by 1 micro
This rewrites the loop to use bigint micro-credits end-to-end. Adds
three small pure helpers:
- parseQuantityToMicro(decimal: string): bigint
- formatMicroToQuantity(micro: bigint): string
- numberToMicro(quantity: number): bigint
The single float touch is `numberToMicro` at the entry, where a JS
`number` from the caller is rounded to the nearest micro-credit.
That's the unavoidable cost of accepting `quantity: number` at the
public API; everywhere else the math is exact.
Cost rounding now uses ceiling division so any sub-micro fraction
is charged to the buyer rather than absorbed by the seller (matches
how on-chain MsgBuyDirect would settle).
Tests: 14 new vitest cases. Coverage:
- parseQuantityToMicro: integer / fractional / sub-micro truncation
/ malformed input rejection / 0.1 + 0.2 = 0.3 exactness
- formatMicroToQuantity round-trip
- numberToMicro half-rounding behavior
- selectBestOrders: exact fill, multi-order cheapest-first walk,
insufficient supply, malformed-quantity skip-don't-fail
- REGRESSION: 10 × 0.1 sums to exactly 1.0 (no float drift)
- exact cost for fractional take with high-precision price
- cost rounds UP not down
Full suite: 63/63 passing (49 prior + 14 new).
The internal helpers are exported under __orderSelectorInternals
for direct unit testing — they're pure, simple, and worth covering.
Refs: AUDIT.md C1-HIGH (order-selector portion)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 66e0677 commit 6abe2ca
2 files changed
Lines changed: 249 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
6 | 12 | | |
7 | 13 | | |
8 | 14 | | |
9 | 15 | | |
10 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
11 | 71 | | |
12 | 72 | | |
13 | 73 | | |
| |||
112 | 172 | | |
113 | 173 | | |
114 | 174 | | |
115 | | - | |
116 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
117 | 182 | | |
118 | 183 | | |
119 | 184 | | |
120 | 185 | | |
121 | 186 | | |
122 | | - | |
| 187 | + | |
123 | 188 | | |
124 | | - | |
125 | | - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
126 | 196 | | |
127 | | - | |
| 197 | + | |
128 | 198 | | |
129 | | - | |
130 | | - | |
131 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
132 | 203 | | |
133 | 204 | | |
134 | 205 | | |
135 | 206 | | |
136 | | - | |
| 207 | + | |
137 | 208 | | |
138 | 209 | | |
139 | 210 | | |
140 | 211 | | |
141 | 212 | | |
142 | 213 | | |
143 | 214 | | |
144 | | - | |
| 215 | + | |
145 | 216 | | |
146 | 217 | | |
147 | | - | |
| 218 | + | |
| 219 | + | |
148 | 220 | | |
149 | 221 | | |
150 | 222 | | |
151 | | - | |
| 223 | + | |
152 | 224 | | |
153 | 225 | | |
154 | 226 | | |
155 | | - | |
| 227 | + | |
156 | 228 | | |
157 | 229 | | |
158 | 230 | | |
| |||
0 commit comments