MEV Protection
MEV (Maximal Extractable Value) refers to the profit that can be extracted from blockchain users by reordering, inserting, or censoring transactions.
Types of MEV Attacks
Frontrunning
An attacker sees your pending transaction and submits a similar transaction with higher gas to execute first.
Example: You submit a large swap on Uniswap. A bot sees this, buys the token before you (raising the price), then sells after your trade.
Sandwich Attack
Your transaction gets "sandwiched" between two attacker transactions.
1. Attacker buys token (price goes up)
2. Your swap executes at worse price
3. Attacker sells token (profits from price difference)
Backrunning
Attacker executes immediately after your transaction to capitalize on price changes.
Protection Strategies
1. Private Mempools
Send transactions directly to block builders, bypassing the public mempool.
import { sendPrivateTransaction } from '@universal-crypto-mcp/security';
const result = await sendPrivateTransaction({
chainId: 1,
signedTransaction: signedTx,
provider: 'flashbots', // or 'mevblocker'
});
console.log(`Included in block: ${result.blockNumber}`);
2. Flashbots Protect RPC
Use Flashbots Protect as your RPC endpoint:
Or configure in code:
import { createWalletClient, http } from 'viem';
import { mainnet } from 'viem/chains';
const client = createWalletClient({
chain: mainnet,
transport: http('https://rpc.flashbots.net'),
});
3. Slippage Protection
Set appropriate slippage limits:
// Calculate minimum output
const minOutput = expectedOutput * (1 - slippage / 100);
const swap = await uniswap.swap({
tokenIn: 'ETH',
tokenOut: 'USDC',
amount: '10.0',
slippage: 0.5, // 0.5% max slippage
minOutput, // Explicitly set minimum
});
4. Deadlines
Set transaction deadlines to prevent stale transactions:
const deadline = Math.floor(Date.now() / 1000) + 300; // 5 minutes
const swap = await uniswap.swap({
// ... other params
deadline,
});
5. Commit-Reveal Schemes
For high-value trades, use commit-reveal:
// Step 1: Commit (hash of your intent)
const commitment = keccak256(encodePacked(
['address', 'uint256', 'bytes32'],
[recipient, amount, secret]
));
await contract.commit(commitment);
// Step 2: Wait for commitment to be included
await waitForBlocks(1);
// Step 3: Reveal and execute
await contract.reveal(recipient, amount, secret);
MEV on Different Chains
| Chain | MEV Risk | Protection |
|---|---|---|
| Ethereum | 🔴 High | Flashbots, MEV Blocker |
| Arbitrum | 🟡 Medium | Sequencer ordering |
| Base | 🟡 Medium | Sequencer ordering |
| Optimism | 🟡 Medium | Sequencer ordering |
| Polygon | 🟡 Medium | FastLane |
| Solana | 🟢 Low | Jito bundles |
Detecting MEV Attacks
Analyze Past Transactions
import { analyzeMEV } from '@universal-crypto-mcp/security';
const analysis = await analyzeMEV({
txHash: '0x...',
chainId: 1,
});
if (analysis.wasAttacked) {
console.log(`Attack type: ${analysis.attackType}`);
console.log(`Loss: $${analysis.estimatedLoss}`);
console.log(`Attacker: ${analysis.attackerAddress}`);
}
Monitor for Suspicious Activity
import { monitorMEV } from '@universal-crypto-mcp/security';
const monitor = await monitorMEV({
address: '0xYourAddress',
onAttack: (attack) => {
console.log(`MEV attack detected: ${attack.type}`);
// Send alert
},
});
Best Practices
- Use private transaction submission for large trades
- Set tight slippage limits (0.5% or less for stablecoins)
- Use L2s for smaller trades (sequencer ordering reduces MEV)
- Split large orders into smaller trades
- Use limit orders instead of market orders when possible
- Time transactions during low-activity periods
Tools & Resources
- Flashbots Protect
- MEV Blocker
- EigenPhi - MEV analysis
- Flashbots Dashboard