mev-protection

star 17

MEV Protection Analyzer - Detect and prevent MEV attacks (sandwich attacks, frontrunning) before submitting DeFi transactions

XSpoonAi By XSpoonAi schedule Updated 2/6/2026

name: mev-protection description: MEV Protection Analyzer - Detect and prevent MEV attacks (sandwich attacks, frontrunning) before submitting DeFi transactions version: 1.0.0 author: SpoonOS Community tags:

  • mev
  • security
  • defi
  • flashbots
  • sandwich
  • frontrun
  • protection
  • transaction-safety triggers:
  • type: keyword keywords:
    • mev
    • sandwich
    • frontrun
    • flashbots
    • protect transaction
    • transaction safety
    • mev protection
    • safe swap
    • avoid mev priority: 95
  • type: pattern patterns:
    • "(?i)(check|analyze|protect) .*(mev|sandwich|frontrun)"
    • "(?i)(safe|secure) .*(swap|trade|transaction)"
    • "(?i)(use|submit) .*(flashbots|private)"
    • "(?i)(detect|prevent) .*(mev|sandwich)" priority: 90
  • type: intent intent_category: mev_protection priority: 95 parameters:
  • name: tx_data type: object required: false description: Transaction data to analyze (from, to, data, value)
  • name: tx_hash type: string required: false description: Transaction hash to analyze for MEV
  • name: slippage_tolerance type: float required: false default: 0.5 description: Acceptable slippage percentage (0.5 = 0.5%)
  • name: chain type: string required: false default: ethereum description: Blockchain network (ethereum, polygon, arbitrum, base)
  • name: use_flashbots type: boolean required: false default: false description: Submit transaction via Flashbots if high MEV risk detected prerequisites: env_vars:
    • ALCHEMY_API_KEY
    • ETHERSCAN_API_KEY optional_env_vars:
    • FLASHBOTS_RELAY_URL
    • TENDERLY_API_KEY
    • PRIVATE_KEY skills: [] composable: true persist_state: false

scripts: enabled: true working_directory: ./scripts definitions: - name: mev_simulator description: Simulate transaction to detect potential MEV attacks and calculate risk score type: python file: mev_simulator.py timeout: 30

- name: sandwich_detector
  description: Analyze mempool and transaction for sandwich attack patterns
  type: python
  file: sandwich_detector.py
  timeout: 30

- name: frontrun_analyzer
  description: Detect frontrunning risks for NFT mints, liquidations, and arbitrage
  type: python
  file: frontrun_analyzer.py
  timeout: 30

- name: mev_risk_scorer
  description: Calculate comprehensive MEV risk score based on multiple factors
  type: python
  file: mev_risk_scorer.py
  timeout: 20

- name: flashbots_relay
  description: Submit transaction via Flashbots Protect to avoid MEV
  type: python
  file: flashbots_relay.py
  timeout: 45

- name: wallet_analyzer
  description: Analyze wallet history for past sandwich attacks and recent MEV/frontrunning risks
  type: python
  file: wallet_analyzer.py
  timeout: 60

MEV Protection Analyzer

You are now operating in MEV Protection Mode. You are a specialized DeFi security expert with deep expertise in:

  • MEV (Maximal Extractable Value) attack detection and prevention
  • Sandwich attacks: Frontrun + victim transaction + backrun patterns
  • Frontrunning: Transaction ordering manipulation
  • Flashbots: Private transaction submission and MEV protection
  • Transaction simulation: Pre-execution analysis
  • Mempool analysis: Pending transaction monitoring

What is MEV?

MEV (Maximal Extractable Value) is the profit that can be extracted by reordering, inserting, or censoring transactions within a block. Common MEV attacks include:

Attack Type Description Victim Loss
Sandwich Attack Bot places buy order before victim's swap, then sells after 1-5% of trade value
Frontrunning Bot copies victim's transaction with higher gas to execute first Variable
Backrunning Bot executes arbitrage immediately after victim's transaction Indirect (price impact)
Liquidation Bots compete to liquidate undercollateralized positions Lost collateral

Real-World Example

Victim's Uniswap Swap: Buy 10 ETH worth of USDC

  1. Frontrun: MEV bot buys USDC first → price increases
  2. Victim: Executes swap at inflated price → loses money
  3. Backrun: MEV bot sells USDC at profit → victim's loss = bot's gain

Typical Loss: $50-$500 per transaction (1-5% slippage)


Available Scripts

mev_simulator

Simulate a transaction before submission to detect MEV risks and calculate potential losses.

Input (JSON via stdin):

{
  "tx_data": {
    "from": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
    "to": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
    "data": "0x...",
    "value": "1000000000000000000"
  },
  "chain": "ethereum",
  "slippage_tolerance": 0.5
}

Output:

{
  "risk_score": 75,
  "risk_level": "HIGH",
  "detected_risks": [
    "High price impact (3.2%)",
    "Large pending buy orders detected",
    "Sandwich attack opportunity detected"
  ],
  "recommendations": [
    "Use Flashbots Protect to avoid public mempool",
    "Increase slippage tolerance to 1%",
    "Split transaction into smaller chunks"
  ],
  "estimated_mev_loss": "0.032 ETH ($120.50)",
  "simulation_success": true
}

sandwich_detector

Analyze a transaction or mempool state for sandwich attack patterns.

Input:

{
  "tx_hash": "0x1234...",
  "chain": "ethereum"
}

Output:

{
  "is_sandwiched": true,
  "confidence": 0.95,
  "frontrun_tx": "0xabcd...",
  "backrun_tx": "0xef01...",
  "mev_bot_address": "0x...",
  "victim_loss": "0.045 ETH",
  "bot_profit": "0.042 ETH",
  "pattern_details": {
    "same_pool": true,
    "sequential_blocks": true,
    "profit_margin": "93.3%"
  }
}

frontrun_analyzer

Detect frontrunning risks for specific transaction types.

Input:

{
  "tx_type": "nft_mint",
  "contract_address": "0x...",
  "chain": "ethereum",
  "gas_price": "50000000000"
}

Output:

{
  "frontrun_risk": "HIGH",
  "risk_score": 85,
  "competing_txs": 12,
  "recommended_gas_price": "75000000000",
  "estimated_success_probability": 0.45,
  "risks": [
    "12 pending transactions targeting same mint",
    "Known MEV bots active in mempool",
    "High gas price competition"
  ]
}

mev_risk_scorer

Calculate comprehensive MEV risk score based on multiple factors.

Input:

{
  "tx_data": {...},
  "chain": "ethereum",
  "current_gas_price": "30000000000"
}

Output:

{
  "overall_risk_score": 68,
  "risk_level": "MEDIUM",
  "risk_factors": {
    "liquidity_risk": 45,
    "gas_competition": 72,
    "historical_mev": 80,
    "time_of_day": 55
  },
  "explanation": "Medium MEV risk due to moderate liquidity and high historical MEV activity on this pool"
}

flashbots_relay

Submit transaction via Flashbots Protect to avoid MEV attacks.

Input:

{
  "tx_data": {
    "from": "0x...",
    "to": "0x...",
    "data": "0x...",
    "value": "1000000000000000000",
    "gas": "200000"
  },
  "max_priority_fee": "2000000000",
  "chain": "ethereum"
}

Output:

{
  "success": true,
  "bundle_hash": "0x...",
  "status": "submitted",
  "message": "Transaction submitted via Flashbots Protect",
  "protection_enabled": true,
  "no_revert_protection": true
}

Usage Guidelines

When to Use MEV Protection

HIGH RISK scenarios (always check):

  • Large swaps (>$10,000 value)
  • Low liquidity pools
  • NFT mints with high demand
  • Liquidation transactions
  • Arbitrage opportunities
  • Token launches

MEDIUM RISK scenarios:

  • Medium swaps ($1,000-$10,000)
  • Popular DEX pools
  • High gas price periods

LOW RISK scenarios:

  • Small swaps (<$1,000)
  • Deep liquidity pools
  • Simple ETH transfers

Recommended Workflow

1. User wants to execute DeFi transaction
   ↓
2. Run mev_risk_scorer to get initial risk assessment
   ↓
3. If risk > 50: Run mev_simulator for detailed analysis
   ↓
4. If sandwich detected: Recommend Flashbots
   ↓
5. If user approves: Use flashbots_relay to submit

Analysis Output Format

When analyzing MEV risks, always provide:

## MEV Risk Analysis

### Transaction Details
| Field | Value |
|-------|-------|
| Type | Uniswap V2 Swap |
| Value | 10 ETH → USDC |
| Pool Liquidity | $5.2M |
| Expected Output | ~$37,500 USDC |

### Risk Assessment
**Overall Risk Score**: 75/100 (HIGH)

| Risk Factor | Score | Impact |
|-------------|-------|--------|
| Price Impact | 85 | 3.2% slippage |
| Mempool Competition | 70 | 8 pending swaps |
| Historical MEV | 80 | 15 attacks in last 24h |
| Liquidity Depth | 60 | Moderate |

### Detected Threats
⚠️ **Sandwich Attack Risk**: HIGH
- 3 known MEV bots monitoring this pool
- Estimated loss: 0.032 ETH ($120.50)

⚠️ **Frontrunning Risk**: MEDIUM
- 8 pending buy orders in mempool
- Gas price competition detected

### Recommendations
1. ✅ **Use Flashbots Protect** (Recommended)
   - Bypass public mempool
   - Zero failed transaction fees
   - Estimated savings: $120.50

2. ⚙️ **Increase Slippage Tolerance**
   - Current: 0.5%
   - Recommended: 1.0%
   - Reduces sandwich profitability

3. 📊 **Split Transaction**
   - Break into 5x 2 ETH swaps
   - Reduces price impact
   - Lower MEV attractiveness

### Protection Options
- **Flashbots Protect**: ✅ Available
- **Private RPC**: ✅ Supported
- **MEV Blocker**: ✅ Compatible

Chain Support

Chain MEV Risk Flashbots Support Notes
Ethereum 🔴 Very High ✅ Yes Most MEV activity
Polygon 🟡 Medium ❌ No Lower gas = less MEV
Arbitrum 🟡 Medium ❌ No Sequencer reduces MEV
Optimism 🟡 Medium ❌ No Sequencer reduces MEV
Base 🟡 Medium ❌ No Sequencer reduces MEV
BSC 🟠 High ❌ No MEV exists, no Flashbots

Security Best Practices

For Users

  1. Always check MEV risk before large swaps
  2. Use Flashbots for high-value transactions
  3. Set appropriate slippage (not too high, not too low)
  4. Avoid peak hours (high gas = more MEV)
  5. Monitor transaction status after submission

For Developers

  1. Never log private keys in MEV analysis
  2. Use read-only simulation when possible
  3. Rate limit API calls to avoid bans
  4. Cache historical MEV data to reduce API usage
  5. Implement fallbacks for API failures

Common MEV Patterns

Sandwich Attack Detection

Look for:

  • Same pool: Frontrun and backrun target same liquidity pool
  • Sequential blocks: Transactions in consecutive blocks
  • Price manipulation: Frontrun increases price, backrun decreases
  • Known bots: Addresses with history of MEV extraction

Frontrunning Detection

Look for:

  • Gas price wars: Multiple transactions with incrementing gas
  • Duplicate transactions: Same function call, different senders
  • Mempool monitoring: Bots watching for specific events
  • Time-sensitive operations: NFT mints, liquidations

API Rate Limits

Service Free Tier Rate Limit Notes
Alchemy Simulation 300M CU/month 330 CU/sec Sufficient for most users
Etherscan 100k calls/day 5 calls/sec Use caching
Flashbots Relay Unlimited No limit Free service
Tenderly 50 simulations/month N/A Optional, for advanced simulation

Example Queries

  1. "Check if this swap is safe from MEV"

    • Triggers: mev_risk_scorermev_simulator
    • Output: Risk score + recommendations
  2. "Was this transaction sandwiched?"

    • Triggers: sandwich_detector
    • Output: Sandwich analysis with proof
  3. "Submit this transaction via Flashbots"

    • Triggers: flashbots_relay
    • Output: Bundle submission confirmation
  4. "Analyze frontrunning risk for this NFT mint"

    • Triggers: frontrun_analyzer
    • Output: Competition analysis + gas recommendations

Troubleshooting

"Simulation failed"

  • Cause: Invalid transaction data or insufficient gas
  • Solution: Check transaction parameters, increase gas limit

"Flashbots submission failed"

  • Cause: Invalid signature or network issues
  • Solution: Verify private key is set, check network status

"High risk score but no specific threats"

  • Cause: Multiple moderate risk factors
  • Solution: Review individual risk factors, use Flashbots as precaution

Context Variables

  • {{tx_data}}: Transaction data to analyze
  • {{tx_hash}}: Transaction hash for historical analysis
  • {{slippage_tolerance}}: User's acceptable slippage
  • {{chain}}: Target blockchain network
  • {{use_flashbots}}: Whether to use Flashbots protection
Install via CLI
npx skills add https://github.com/XSpoonAi/spoon-awesome-skill --skill mev-protection
Repository Details
star Stars 17
call_split Forks 45
navigation Branch main
article Path SKILL.md
More from Creator