test(layer2): prove cross-instance NBBO + SOR split over real venues#17
Merged
Conversation
The "one market" thesis — independent venues merge into one NBBO ladder and a single order splits across them — was only ever exercised at N=1, and the SOR math (router.check.ts) only over hand-built fixtures. This proves the real aggregation composes end-to-end over real venue processes. - scripts/nbbo-sor-e2e.sh + app/src/lib/nbbo-sor.check.ts: boots TWO independent operator-lite venues (separate operators, no shared CLOB = two instances), rests a cheaper ask at B and a dearer one at A, then drives the SHIPPED fetchAggBook + planRoute over their live /book. Asserts the merged NBBO touch, per-operator attribution, the price-sorted cross-venue ladder, an 8000-token buy splitting 5000@B + 3000@A (blended avg strictly between the two), and the limit guard leaving dearer venue-A depth untouched. 13/13. - app/src/lib/nbbo-live.check.ts: read-only demo against the production fleet — the same code consolidates op 0x483f (4 levels) + op 0x2420 (3 levels) into one sorted 7-level ladder; an 18.5M buy splits across both operators (5 legs). - app/src/lib/api.ts: `import.meta.env?.` so the lib modules load under plain node/tsx (these checks run outside Vite); the Vite build is unchanged. Verified: e2e 13/13, live demo splits across 2 fleet operators, app tsc clean, router.check.ts still 15/15.
nbbo-sor.check.ts proved the SOR splits across venues; it did not prove the split clears. This does: two operators rest signed SELLs at different prices (/order-signed, so the same orders the SOR routes over are settleable), the shipped fetchAggBook + planRoute decide the split, and the buyer crosses each leg's venue and flushes — two settleFills on the one global contract. Asserts the route AND the on-chain truth: an 8000 buy splits 5000@14.0 (venue B) + 3000@15.0 (venue A), both settle, and the buyer's settlement balance is debited by exactly the blended 115000 micro across the two legs. 5/5. This makes the two-layer thesis end-to-end: one market view → one order → SOR split across independent venues → N independent on-chain settlements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The smallest real test of the architecture's central claim — "one market" assembled from independent books — which until now ran only at N=1, with the SOR math fixture-checked but never composed over real venue output.
What it proves
Two independent
operator-litevenues (separate operators, no shared CLOB = two instances) rest asks at different prices for the same instrument. The shippedfetchAggBookmerges their/bookendpoints into one NBBO ladder and the shippedplanRoutesplits one order across both — no reimplementation of either.Hermetic e2e (
scripts/nbbo-sor-e2e.sh+app/src/lib/nbbo-sor.check.ts) — 13/13:Asserts: NBBO touch = cheaper venue, both operators attributed, price-sorted cross-venue ladder, the split, blended avg strictly between the two venues, and the limit guard leaving dearer depth untouched.
Live read-only demo (
app/src/lib/nbbo-live.check.ts) — same code against the production fleet:op5 (attester-only, dead sidecar) correctly contributes an empty book.
Also
app/src/lib/api.tsnow readsimport.meta.env?.so the lib modules load under plain node/tsx (the checks run outside Vite) — the Vite build resolves identically. Apptscclean;router.check.tsstill 15/15.What this does not prove
Per-leg on-chain settlement (each leg is a portable signed order clearing via the already-proven single-venue path; the split = N such settlements). And it's still N=2 — the settlement-throughput and liquidity-fragmentation ceilings from the design critique are unchanged.
🤖 Generated with Claude Code