Skip to content

test(layer2): prove cross-instance NBBO + SOR split over real venues#17

Merged
drewstone merged 2 commits into
mainfrom
feat/layer2-nbbo-proof
Jun 13, 2026
Merged

test(layer2): prove cross-instance NBBO + SOR split over real venues#17
drewstone merged 2 commits into
mainfrom
feat/layer2-nbbo-proof

Conversation

@drewstone

Copy link
Copy Markdown
Contributor

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-lite venues (separate operators, no shared CLOB = two instances) rest asks at different prices for the same instrument. The shipped fetchAggBook merges their /book endpoints into one NBBO ladder and the shipped planRoute splits one order across both — no reimplementation of either.

Hermetic e2e (scripts/nbbo-sor-e2e.sh + app/src/lib/nbbo-sor.check.ts) — 13/13:

merged NBBO ladder (asks, best-first):
    14000000  x5000    ⟵ venue B (cheaper)
    15000000  x10000   ⟵ venue A
SOR plan for an 8000-token buy:
  lift 5000 @ 14000000  from B
  lift 3000 @ 15000000  from A
  filled 8000/8000  avg 14375000  partial=false

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:

LIVE consolidated NBBO (3 venues responded):
  13.246–13.366  ⟵ op 0x483f  (4 levels)
  13.696–13.778  ⟵ op 0x2420  (3 levels)
an 18.5M-token buy → 5 legs across BOTH operators, avg 13.353837

op5 (attester-only, dead sidecar) correctly contributes an empty book.

Also

app/src/lib/api.ts now reads import.meta.env?. so the lib modules load under plain node/tsx (the checks run outside Vite) — the Vite build resolves identically. App tsc clean; router.check.ts still 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

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.
@drewstone drewstone merged commit 0bfd29f into main Jun 13, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant