fix(pancakeswap): fix add/remove-liquidity failures and sandwich vulnerability#1
fix(pancakeswap): fix add/remove-liquidity failures and sandwich vulnerability#1
Conversation
…erability Three bugs fixed: 1. add-liquidity: pre-flight balance check before approve/mint Wallet balance is now verified against amount0_desired/amount1_desired before submitting any transaction. Fails early with a clear message instead of wasting gas on a doomed approve + reverted mint. 2. add-liquidity: auto-compute tick range when omitted --tick-lower and --tick-upper are now optional. When both are omitted, the plugin fetches the pool's current tick via slot0() and builds a ±10% price range (≈±1000 ticks), aligned to the fee tier's tickSpacing. Callers can still pass explicit ticks for full control. 3. remove-liquidity: fix slippage protection (sandwich vulnerability) Previously amount0Min/amount1Min were computed from tokens_owed (accrued fees), which is unrelated to the amounts returned by decreaseLiquidity. For any position with tokens_owed=0 this silently set both minimums to 0, giving zero slippage protection and exposing the tx to sandwich attacks. Fix: fetch the pool's current sqrtPriceX96 + tick via slot0(), then apply standard Uniswap V3 liquidity math (amounts_from_liquidity in rpc.rs) to compute the actual expected token amounts. Slippage minimums are now derived from those real amounts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Phase 4: Summary + Pre-flight for
|
🔨 Phase 2: Build Verification — ✅ PASSED
Build succeeded. Compiled artifact uploaded as workflow artifact. Source integrity: commit SHA `` is the content fingerprint. |
📋 Phase 3: AI Code Review Report — Score: N/A/100
❌ AI review FAILED (HTTP 401): x-api-key header is required. Request size: 849428 bytes, plugin content: 161632 bytes. Generated by Claude AI via Anthropic API — review the full report before approving. |
Summary
Bug 1 — Balance check before approve/mint:
add-liquiditynow callsrpc::get_balance()for both tokens before submitting any transaction. Previously the command would approve tokens the wallet didn't have, burning gas before the mint reverted. Validated by the test note: "没有先查钱包余额,导致失败".Bug 2 — Auto tick range:
--tick-lowerand--tick-upperare now optional. When omitted, the plugin fetches the pool's current tick fromslot0()and computes a ±10% price range (~±1000 ticks) aligned to the fee tier'stickSpacing. Explicit ticks still work as before.Bug 3 — Sandwich vulnerability in remove-liquidity:
amount0Min/amount1MinfordecreaseLiquiditywere previously computed fromtokens_owed(already-accrued uncollected fees), which is completely unrelated to whatdecreaseLiquidityreturns. For any position withtokens_owed = 0both minimums silently became 0 — zero slippage protection, open to sandwich attacks. Fix: addedamounts_from_liquidity()torpc.rsimplementing standard Uniswap V3 liquidity math (using the pool's currentsqrtPriceX96fromslot0()), and slippage minimums are now derived from the actual expected output.Files changed
src/commands/add_liquidity.rssrc/commands/remove_liquidity.rssrc/rpc.rsamounts_from_liquidity()+tick_to_sqrt_price()src/main.rstick_lower/tick_upperchanged toOption<i32>Test plan
add-liquiditywith insufficient balance → clear error before any txadd-liquiditywithout--tick-lower/--tick-upper→ auto range printed, mint succeedsadd-liquiditywith explicit ticks → behaviour unchangedremove-liquidity --dry-run→ prints non-zeroamount0Min/amount1Minfor in-range positioncargo buildpasses with no new warnings🤖 Generated with Claude Code