LimitOrderHook: guard _fillOrder against filling non-converted positions (#132)#133
Open
impetus82 wants to merge 1 commit into
Open
LimitOrderHook: guard _fillOrder against filling non-converted positions (#132)#133impetus82 wants to merge 1 commit into
impetus82 wants to merge 1 commit into
Conversation
Addresses OpenZeppelin#132. A stale `_tickLowerLasts` -- left whenever a subclass performs an internal swap, which Uniswap V4 excludes from the `afterSwap` callback via self-call protection -- makes `_getCrossedTicks` propose a fill window whose order positions the live price has not converted. `_fillOrder` then mints/credits the deposited currency instead of the target, marks the order filled and resets it: an irrecoverable wrong-token fill. Add a defense-in-depth guard in `_fillOrder` that skips the fill unless live slot0 confirms full conversion (zeroForOne: sqrtPrice >= sqrtPriceAtTick(tickUpper); oneForZero: sqrtPrice <= sqrtPriceAtTick(tickLower)); on a skip the order stays active and cancellable. Also add an internal `_setTickLowerLast` so subclasses doing internal swaps can keep `_tickLowerLasts` fresh without storage-layout assembly. Includes a reproducing test; full suite green (305/305). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
✅ Deploy Preview for uniswap-hooks ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for uniswap-hooks-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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
Addresses #132 (irrecoverable wrong-token fill in
LimitOrderHook) by adding a defense-in-depth guard to_fillOrder, plus aninternal _setTickLowerLastso subclasses that perform internal swaps can keep_tickLowerLastsfresh.Root cause
_afterSwapmaintains_tickLowerLasts, and_getCrossedTicksderives the fill window from it. Uniswap V4's self-call protection inHooks.solskipsafterSwapwhenevermsg.sender == address(hook), so any subclass that callspoolManager.swap()from inside its ownunlockCallback(e.g. aswapAndPlaceOrderextension) never runs_afterSwapand leaves_tickLowerLastsstale. A stale_tickLowerLastslets_getCrossedTicksreturn a window that includes an order tick whose position the live price has not converted._fillOrderthen removes that position, mints the deposited currency (not the target), credits it, setsfilled = trueand resets the order id — permanently consuming the order and returning the wrong asset.The underlying gap:
_fillOrdertrusts the crossed-ticks window unconditionally.Fix
_fillOrder(correct regardless of caller / staleness): read liveslot0and skip the fill unless the position is fully converted —zeroForOne(sell currency0 → currency1): requiresqrtPriceX96 >= getSqrtPriceAtTick(tickLower + tickSpacing);oneForZero(sell currency1 → currency0): requiresqrtPriceX96 <= getSqrtPriceAtTick(tickLower).On a skip the order stays active and cancellable, so the worst case degrades from “wrong token, irrecoverable” to “fill deferred”.
internal _setTickLowerLast(PoolId, int24): lets subclasses performing internal swaps keep_tickLowerLastsconsistent with live price (addresses the root cause for that pattern) without the brittle storage-layoutsstoreworkaround.Test
test_fillOrder_skipsWhenPositionNotConverted_issue132sets a stale_tickLowerLasts(exactly the state a self-swap leaves) and drives_afterSwapto attempt a fill of a non-convertedoneForZeroorder. Without the guard the test fails (order filled,currency1credited to an order that expectedcurrency0); with the guard it passes (fill skipped, no miscredit, order cancellable). Full suite stays green (305/305),forge fmtclean.Notes
LimitOrderHook.sol; happy to rebase onto it._setTickLowerLastlets subclasses prevent the staleness itself.🤖 Generated with Claude Code