feat(swap-vm): add computeSingleSidedRange to ConcentrateLiquidityCalculator#79
Draft
krboktv wants to merge 2 commits into
Draft
feat(swap-vm): add computeSingleSidedRange to ConcentrateLiquidityCalculator#79krboktv wants to merge 2 commits into
krboktv wants to merge 2 commits into
Conversation
…culator Build a single-sided range from a spot price, one price bound and a single reserve: the spot itself becomes the second bound (min for a token0 deposit, max for a token1 deposit) and the opposite reserve defaults to zero. Returns the resulting PriceAllocationRange together with the reserves, ready for the allocation methods and the SwapVM concentrate instructions. Also export ConcentrateLiquidityCalculator from the concentrate instructions index (previously only its types were exported, contrary to the docs). Co-authored-by: Kirill <krboktv@gmail.com>
…stead of accepting it Per review: the method now accepts only spotPrice, reserveForToken and reserve, and returns the price range. The spot sits on the min bound for a token0 deposit and on the max bound for a token1 deposit; the opposite bound is derived from the other token's maxAvailableLiquidity via the range-order relation sqrtPmin * sqrtPmax = amountGt / amountLt, i.e. the price at which the deposit fully converts into exactly that amount. Drop the now-unneeded SingleSidedRangeInfo type. Co-authored-by: Kirill <krboktv@gmail.com>
|
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.



Summary
Adds
ConcentrateLiquidityCalculator.computeSingleSidedRange(spotPrice, reserveForToken, reserve): builds a single-sided price range from the spot price and a single deposited reserve, and returns the resultingPriceAllocationRange.The spot price sits exactly on one bound, depending on the deposited token (in sqrt(token1/token0) terms):
The opposite bound is derived from the other token's
maxAvailableLiquidity: it is the price at which the deposited reserve fully converts into exactly that amount, via the range-order relationsqrtPmin * sqrtPmax = amountGt / amountLt(the geometric mean of the bounds is the average execution price across the range). The opposite token'smaxAvailableLiquiditymust therefore exceed the spot-equivalent value of the deposit, otherwise the method throws.Example (USDC 6dp / WETH 18dp): deposit 1,000,000 USDC at spot 2500 USDC/WETH with
maxAvailableLiquidityof 800 WETH → range [2500, 625] USDC per WETH; average execution sqrt(2500·625) = 1250, and the deposit converts into exactly 800 WETH at the far bound (verified bit-exact in tests).Changes
concentrate-liquidity-calculator.ts: newcomputeSingleSidedRangemethod with validation (positive reserve, pair-matching spot price, reserve token in pair, oppositemaxAvailableLiquiditylarge enough for a non-degenerate range).index.ts: export theConcentrateLiquidityCalculatorclass (previously only its types were exported, contrary to the module docs).CONCENTRATE-LIQUIDITY-CALCULATOR.md: documentation for the new method.concentrate-liquidity-calculator.test.tswith 11 tests: both deposit directions (spot placement, derived bound value, exact full conversion into the target at the far bound via the liquidity math, compatibility withcomputeFixedAllocation) and all error paths.Testing
pnpm testintypescript/swap-vm: 606 tests passed (45 files), including the 11 new ones.pnpm type-check,pnpm lint,pnpm format:check: all clean.