detect-sandwich-risk-before-swapping
Use when a swap might be sandwiched, to estimate the extractable value, and to choose mitigations. Checks trade size versus pool depth, slippage tolerance, and transaction visibility.
- License 许可
- MIT
- 最近更新
- 2026-11-11
- Stars
- ★ 80
- Forks
- 17
01 前言 Front Matter
- name
- detect-sandwich-risk-before-swapping
- description
- Use when a swap might be sandwiched, to estimate the extractable value, and to choose mitigations. Checks trade size versus pool depth, slippage tolerance, and transaction visibility.
02 资源文件 3
- 许可证LICENSE1.0 KB
- 参考文档SOURCE.md362 B
- 参考文档TRUST.auto.yaml2.1 KB
03 SKILL.md 正文
Detect sandwich risk before swapping
A sandwich is a front-run that pushes price against your swap plus a back-run that unwinds the attacker into the move your fill created; the attacker's ceiling is exactly the slippage you allowed.
Procedure
-
Compute your price impact (see the slippage skill). The attacker's max profit ≈
impact% * notional - fees - gas. A $50,000 swap at 1% impact is worth up to $500 minus costs. -
Risk score: sandwichable if
notional / pool_depth > 0.001ANDslippage_tolerance > 0.005. Below either threshold, extraction is usually unprofitable after gas (~$5–50 on mainnet, ~$0.01 on L2). -
Check visibility. A transaction broadcast to a public mempool is observable immediately:
cast tx $TXHASH --rpc-url $RPC | grep '"to"'
If it went to a public RPC, assume a searcher scanned it.
-
Detect a sandwich after the fact by pulling the block and finding same-pair swaps adjacent to yours:
cast logs --from-block $B --to-block $B --address $POOL "Swap(address,uint256,uint256,uint256,uint256,address)" --rpc-url $RPC
Three consecutive swaps of the same pair, the middle one yours with the outer two from the same address, is a sandwich.
-
Mitigate in order of strength: (a) submit via a private/MEV-protected RPC; (b) set tolerance <= 0.5%; (c) split into pieces under the impact threshold; (d) use a batch auction or commit-reveal DEX.
-
Weigh the mitigation cost: near zero on L2 and free via Flashbots Protect. Leaving
amountOutMinloose to "avoid a revert" trades a guaranteed loss for a rare retry.
Pitfalls
- Tight slippage without a protected RPC just makes you revert — reverts still burn gas and are a loss.
- Assuming low impact alone is safe: repeated small swaps in one block are sandwichable if they net to a large move.
- Assuming L2s are sandwich-proof: sequencers can reorder, and priority-order-flow auctions still exist.
Verification
cast logs --from-block $B --to-block $B --address $POOL "Swap(address,uint256,uint256,uint256,uint256,address)" --rpc-url $RPC
Count swaps before and after yours in the same block; same-funder outer swaps confirm the sandwich.
Report impact, tolerance, the RPC submitted to, and whether adjacent swaps match your pool.
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.