MEV防护 - 完整API参考
保护交易免受MEV(最大可提取价值)攻击,包括夹心攻击和抢先交易。
聊天命令
保护设置
/mev 显示当前保护
/mev status 保护状态
/mev enable 启用保护
/mev disable 禁用保护
配置保护
/mev level aggressive 最大保护
/mev level standard 平衡保护
/mev level minimal 基本保护
/mev provider flashbots 使用Flashbots
/mev provider mev-blocker 使用MEV Blocker
检查交易
/mev check <tx-hash> 检查交易是否被攻击
/mev simulate <order> 模拟MEV风险
TypeScript API参考
创建MEV保护
import { createMEVProtection } from 'clodds/mev';
const mev = createMEVProtection({
// 默认级别
level: 'standard',
// 提供者
providers: {
ethereum: 'flashbots', // 'flashbots' | 'mev-blocker'
solana: 'jito', // 'jito' | 'standard'
},
// 设置
maxPriorityFee: 5, // gwei
bundleTimeout: 60, // 秒
});
执行受保护的交易
// EVM交易保护
const result = await mev.executeProtected({
chain: 'ethereum',
type: 'swap',
tokenIn: 'USDC',
tokenOut: 'ETH',
amountIn: 10000,
minAmountOut: calculateMinOut(10000, 0.5), // 0.5%滑点
});
console.log(`Tx hash: ${result.txHash}`);
console.log(`Protected: ${result.protected}`);
console.log(`Bundle ID: ${result.bundleId}`);
console.log(`Savings: $${result.estimatedSavings}`);
Flashbots (Ethereum)
// 通过Flashbots Protect提交
const result = await mev.flashbots({
to: routerAddress,
data: swapCalldata,
value: 0,
maxFeePerGas: parseGwei('50'),
maxPriorityFeePerGas: parseGwei('2'),
});
console.log(`Submitted to Flashbots`);
console.log(`Bundle hash: ${result.bundleHash}`);
// 等待包含
const status = await mev.waitForInclusion(result.bundleHash);
console.log(`Included in block: ${status.blockNumber}`);
MEV Blocker (Ethereum)
// 使用CoW协议的MEV Blocker
const result = await mev.mevBlocker({
to: routerAddress,
data: swapCalldata,
value: 0,
});
// MEV Blocker自动:
// - 防止夹心攻击
// - 将有利可图的MEV回跑给你
// - 返回任何捕获的MEV
console.log(`MEV captured: $${result.mevCaptured}`);
Jito (Solana)
// 通过Jito bundles提交
const result = await mev.jito({
instructions: swapInstructions,
tip: 10000, // lamports tip to validators
});
console.log(`Bundle ID: ${result.bundleId}`);
console.log(`Status: ${result.status}`);
检查交易
// 检查过去的交易是否被攻击
const analysis = await mev.analyzeTransaction(txHash);
console.log(`Was attacked: ${analysis.wasAttacked}`);
if (analysis.wasAttacked) {
console.log(`Attack type: ${analysis.attackType}`);
console.log(`Attacker: ${analysis.attacker}`);
console.log(`Loss: $${analysis.estimatedLoss}`);
console.log(`Frontrun tx: ${analysis.frontrunTx}`);
console.log(`Backrun tx: ${analysis.backrunTx}`);
}
模拟MEV风险
// 在交易前,检查MEV风险
const risk = await mev.simulateRisk({
chain: 'ethereum',
type: 'swap',
tokenIn: 'USDC',
tokenOut: 'PEPE',
amountIn: 50000,
});
console.log(`MEV risk: ${risk.level}`); // 'low' | 'medium' | 'high'
console.log(`Estimated max loss: $${risk.maxLoss}`);
console.log(`Recommendation: ${risk.recommendation}`);
if (risk.level === 'high') {
console.log('⚠️ High MEV risk - use protection!');
}
保护级别
// 激进 - 最大保护,速度慢
mev.setLevel('aggressive', {
usePrivateMempool: true,
bundleOnly: true,
maxSlippage: 0.1,
waitForProtection: true,
});
// 标准 - 平衡保护
mev.setLevel('standard', {
usePrivateMempool: true,
bundleOnly: false,
maxSlippage: 0.5,
});
// 最小 - 基本保护
mev.setLevel('minimal', {
usePrivateMempool: false,
bundleOnly: false,
maxSlippage: 1.0,
});
MEV攻击类型
| 攻击 |
描述 |
保护 |
| 夹心 |
前后夹击你的交易 |
私有内存池 |
| 抢先交易 |
先复制你的交易 |
私有内存池 |
| 回跑交易 |
在你的交易后获利 |
Jito/Flashbots |
| JIT流动性 |
操纵池 |
滑点限制 |
保护提供者
| 链 |
提供者 |
方法 |
| 以太坊 |
Flashbots Protect |
私有中继 |
| 以太坊 |
MEV Blocker |
CoW协议 |
| 索拉纳 |
Jito |
捆绑提交 |
| L2s |
原生 |
序列器保护 |
何时使用保护
| 交易规模 |
代币 |
建议 |
| < $1,000 |
主要 |
最小 |
| $1,000 - $10,000 |
主要 |
标准 |
| > $10,000 |
主要 |
激进 |
| 任何大小 |
迷因/低流动性 |
激进 |
最佳实践
- 始终保护大额交易 — MEV机器人监视一切
- 使用紧密滑点 — 限制攻击盈利性
- 交易前检查 — 模拟MEV风险
- 审查交易 — 从过去的攻击中学习
- L2s更安全 — 序列器提供自然保护