contracts-mev-sandwich
它能做什么
这项技能说明三明治攻击:有人在受害者的兑换交易前后下单,从价格变动中牟利。适合希望保护兑换交易的 DeFi 开发者和交易者。
适用场景
- ✓当你在开发交易所前端,想降低用户的滑点风险时。
- ✓当交易者想了解为什么某次兑换拿到的价格更差时。
- ✓当协议团队审查交易路由和私密提交选项时。
技能链接
https://github.com/BitterSecurity/Decepticon/blob/main/packages/decepticon/decepticon/skills/standard/contracts/mev-sandwich/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“为什么我在交易所的兑换价格比预期差?以后我该如何保护兑换交易?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体用通俗的语言解释三明治攻击如何运作,检查滑点设置,并建议能降低风险的设置和路由选择。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
MEV Sandwich Attack
You observe a victim's DEX swap in the mempool. Sandwich it: buy the same token before the victim (pushing price up), let the victim swap at a worse price, then sell after them.
Detect a sandwichable swap
# Stream pending transactions
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://eth.merkle.io"))
# Or: subscribe to alchemy/blocknative streams
# Filter for Uniswap V2 swapExactTokensForTokens or V3 exactInputSingle
# Look at the swap amount + slippage tolerance (deadline + minAmountOut)
# Sandwichable when: swap_amount * slippage > sandwich_gas_cost * 2
Math: profitable when victim's slippage tolerance exceeds your gas cost. Typical threshold for ETH/USDC swaps: victim swap >$10k with >1% slippage.
Construct the sandwich bundle
// Bundle: 3 tx in same block, in order
// 1. attacker_tx_1: swap baseToken -> token (push price UP)
// 2. victim_tx: swap baseToken -> token (worse price now)
// 3. attacker_tx_2: swap token -> baseToken (sell back at higher price)
// Flashbots bundle (Ethereum mainnet — others have similar private RPCs)
const ethers = require("ethers");
const { FlashbotsBundleProvider } = require("@flashbots/ethers-provider-bundle");
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC);
const wallet = new ethers.Wallet(process.env.PK, provider);
const fb = await FlashbotsBundleProvider.create(provider, wallet, "https://relay.flashbots.net");
const blockNumber = await provider.getBlockNumber();
const bundle = [
{ signer: wallet, transaction: attackerTx1 }, // front-run
{ signedTransaction: rawVictimTx }, // victim from mempool
{ signer: wallet, transaction: attackerTx2 }, // back-run
];
const signedBundle = await fb.signBundle(bundle);
await fb.sendRawBundle(signedBundle, blockNumber + 1);
JIT (Just-In-Time) liquidity sandwich
Uniswap V3 concentrated liquidity lets you provide liquidity in a tight range for one block:
// 1. Just before victim's swap: mint a position concentrated at victim's price
// 2. Victim swaps — your liquidity earns 100% of the fee
// 3. Just after: burn the position
// No price-impact risk; pure fee capture.
Works when victim's swap is large enough that the fee exceeds gas + capital cost.
Multi-block MEV
Cross-block sandwiches when validator is also the searcher (or in collusion):
Block N: attacker front-run TX
Block N+1: victim's TX (separate user, expects same price)
Block N+2: attacker back-run TX
Requires builder/searcher cooperation. PBS (proposer-builder separation) on Ethereum makes this harder; still works on chains with single-block validators.
Defenses that break sandwiches (what to look for as "harder targets")
- Victim uses private mempool (Flashbots Protect, MEV-Blocker, bloXroute Private Tx)
- Victim contract has tight
minAmountOut(low slippage like 0.1%) - DEX uses commit-reveal (rare)
- Threshold encryption mempools (Shutter, etc.)
Common pitfalls (sandwich-of-sandwich = your sandwich gets sandwiched)
- Don't broadcast your sandwich publicly — always use Flashbots / private relay
- Other searchers also see the victim — outbid them via gas price + priority fee
- Don't sandwich your own user's swap from same Address — easy attribution
Tooling
mev-inspect-py(Flashbots) — historical sandwich detection (defender + reverse research)mev-share— partial-order-flow ecosystemsearcher-sage— open-source sandwich bot patternsEigenphi— MEV analytics dashboard (for reconning known searcher patterns)
OPSEC / ethics / legal
- Sandwich attacks are public mempool predation, generally legal but ethically grey. In bounty context: only ever test on test networks or on a target you own.
- For research / detection: use mev-inspect-py against historical blocks; no harm.
- Some chains (e.g., Solana / Jito) have different MEV architectures — adapt accordingly.
References
- "Flash Boys 2.0" (Daian et al., 2019) — original MEV academic paper
- Flashbots docs — docs.flashbots.net
- "MEV Wiki" — github.com/flashbots/mev-research
- EthCC / Devcon talks: search "MEV" on YouTube
常见问题
三明治攻击伤害了谁?
受害的交易者会因为其交易前后被插入的额外交易而拿到更差的价格。
哪项设置有帮助?
较低的滑点容忍度可以限制价格变动的幅度,超出后兑换会失败。
什么是私密交易路由?
它在不暴露于公开待处理池的情况下发送交易,该技能将其列为一种可选方案。
详情
作者的原始描述
MEV sandwich attacks — front-run + back-run a victim swap on Uniswap V2/V3, Curve, Balancer; mempool monitoring via Flashbots / private RPC, slippage tolerance exploitation, JIT liquidity sandwich, multi-block MEV.
热度
- GitHub 星标
- 6.5k
- 排名
- #14009
- 许可证
- Apache-2.0
- 更新时间
- 2026/11/1
评论
- …