Xspoonai official mev protection

From jiayaoqijia/cryptoskill · View interactive version

Xspoonai official mev protection is an AI agent skill from the GitHub repository jiayaoqijia/cryptoskill. MEV Protection Analyzer - Detect and prevent MEV attacks (sandwich attacks, frontrunning) before submitting DeFi transactions

How to use this skill

Skill file

MEV Protection Analyzer

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

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):

MEDIUM RISK scenarios:

LOW RISK scenarios:

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:

Frontrunning Detection

Look for:


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"
  1. "Was this transaction sandwiched?"
  1. "Submit this transaction via Flashbots"
  1. "Analyze frontrunning risk for this NFT mint"

Troubleshooting

"Simulation failed"

"Flashbots submission failed"

"High risk score but no specific threats"


Context Variables

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.