Spiral Stake — mcp read-data backbone (Step 1a)
The composition authority + read API for Spiral Stake strategy data. It ports the app's
client-side data composition server-side and serves it back as JSON, so three consumers run
off one core:
- The app — the complete read surface it currently fetches, for a like-for-like Step-2 swap.
- Agents — the frozen
/strategies contract.
- Partners (later) — a clean, versioned, self-documenting REST surface (OpenAPI).
Read-only. No contract writes, no tx building, no signing, no private keys. It composes
numbers and serves JSON. The MCP tool layer, execution/build_*, and partner auth are later
steps — the seams are pre-cut (core + http) but not built.
Architecture
Data flow: warmer fetches upstreams on the CONTRACT cadence → writes raw to the cache
(last-good on failure) → core composes strategies from warm raw only → http serves them.
A stale/failed upstream serves last-good with a visible stale age per field-group — never
dropped, never coerced to 0.
Run
/health (liveness + cache diagnostics) and /ready (503 until the warmer primes the required
data) are available immediately on boot.
Endpoints — app-read inventory → endpoint mapping
Every data read the app performs in v2-client/src/api-services/* maps to an endpoint here
(positions stay on the dashboard backend and are not reimplemented):
Cross-cutting: /v1 versioning, CORS limited to CORS_ORIGINS, a consistent error envelope
({ error: { code, message, correlationId } }), a correlation id per request (echoed as
x-correlation-id), structured JSON logs, and an OpenAPI 3.1 spec at /openapi.json (+ committed
openapi.json).
Parity gates
leverage.ts golden-vector — test/golden/leverage.golden.test.ts freezes fixed inputs →
exact outputs; leverage.parity.test.ts asserts the file is a byte-for-byte copy of the
app's leverage.ts. This locks every APY/LTV/ladder number to the app.
/strategies composition — test/parity/strategies.compose.test.ts seeds the raw cache
with a controlled fixture and asserts the composed contract field-by-field (sourcing, LTV math,
ladder via the verbatim leverage.ts, freshness cadences, null-vs-absent, spiralHints
isolation). Runs offline in CI.
- Live golden-set + app-surface parity —
scripts/capture-parity.ts primes the warmer
against real upstreams and writes the composed /strategies (+ apy/borrow histories) for a
fixed market set to test/fixtures/. The parity diff vs the app is produced by feeding the
same fixed inputs to the app's client-side composition and comparing (the app logs its composed
markets); the PR commits the fixtures and the empty diff. This is the gate that lets us offload
TVL safely.
Frozen decisions (parity-critical)
bignumber.js pinned to the app's 9.3.1; no global BigNumber.config change →
toFixed(2) / rounding (ROUND_HALF_UP) identical.
- On-chain reads via viem replace wagmi:
getCollateralValueInLoanToken (multicall3), stUSDS
str(), spUSDG vsr() (chain 4663). The app's (intentionally lossy) BigInt(liqLtv) from the
JSON number is matched exactly, not "fixed".
maxLeverage = calcLeverage(maxLtv); defaultLeverage = calcLeverage(safeLtv),
safeLtv = maxLtv% − 0.75 — exactly as the app derives them. Ladder = integer steps
1x … ⌊maxLeverage⌋ (LTV =(1 − 1/lev)·100) then the exact max.
- Exit slippage is baked into
collateralTokens.json by the app's weekly refresh script; its file
mtime is the exitLiquidity.asOf. The tier (the only verdict) lives in spiralHints with
its thresholds; raw slippagePct stays under exitLiquidity, null (no route) preserved.