Repository navigation
feat(pricing): liquidity-depth & slippage estimation endpoint (#84) - #531
Conversation
`getDepth` carried a `NOTE: getDepth reads order-book data with no network
column yet — see L048`, and that note was the bug: `pool_snapshots` and
`price_points` both hold rows for every network the process indexes, so an
unfiltered read could answer a mainnet depth query with testnet reserves. Both
queries are now scoped, and `network` threads through from the route.
Also adds `calculateSlippage`, guards `calculateAMMPrice` against a
non-positive amount, and documents each formula.
Worth noting separately: it deletes a block of thinking-out-loud comments in
`generateAMMDepthLevels` ("Wait no: actually for pair A/B...") that talked
itself into and out of inverting the bid price. The code was already right —
`price = amountB / amountA` is unchanged — and the comment was the only thing
suggesting otherwise.
One change while merging: the new `getAMMDepth` branched on `network` and kept
an unscoped copy of the query for the falsy case. That branch is unreachable,
because `network` defaults to `activeNetwork` and passing `undefined` takes
the default — so it was a second copy of the SQL that could only drift from
the first. Collapsed to the single scoped query, with a comment saying why
there is deliberately no unscoped path.
Verified on the merge result: `tsc --noEmit` silent, 885 tests pass across 82
suites, nothing red.
Closes #84
Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB
|
Merged. #84 is unassigned so this is yours, and the core of it is a real correctness fix rather than a feature. That The thing I liked most is one you might not have thought of as a contribution: you deleted the block of thinking-out-loud comments in
The code was already correct ( Also good: guarding One change while mergingThe new const query = network ? `...WHERE ps.network = $2...` : `...` // no network filter
const params = network ? [pairKey, network] : [pairKey]That branch can't be reached — Worth keeping in mind generally: a defensive fallback for a case the type system already rules out tends to become the stale copy. Verified on the merge result rather than your branch: Closes #84. |
Closes #84
Summary of Changes
src/pricing/depth.tsbacked by constant-product AMM curves and order-book snapshots.calculateSlippage(spotPrice, executionPrice)with mathematical verification against known constant-product formulas:getDepthandgetAMMDepthto accept network parameter and filter pool snapshots per network./price/:assetA/:assetB/depthroute insrc/api/rest.ts.src/__tests__/depth.test.tsverifying monotonic slippage increase, zero/negative size handling, and database mocking.