$npx -y skills add agiprolabs/claude-trading-skills --skill mev-analysisMEV exposure assessment, sandwich attack detection, and protection strategies for Solana DEX trading
| 1 | # MEV Analysis for Solana DEX Trading |
| 2 | |
| 3 | Maximal Extractable Value (MEV) is the profit that validators and searchers can extract by reordering, inserting, or censoring transactions within a block. On Solana DEXes, MEV primarily manifests as sandwich attacks against swaps, cross-DEX arbitrage, and liquidation extraction. This skill covers detection, estimation, and protection strategies. |
| 4 | |
| 5 | ## What Is MEV on Solana? |
| 6 | |
| 7 | MEV occurs when someone with transaction ordering power profits at other traders' expense. On Solana, the MEV supply chain works as follows: |
| 8 | |
| 9 | 1. **You submit a swap** through an RPC endpoint |
| 10 | 2. **Searchers observe** your transaction (via RPC forwarding, block engine access, or leader TPU sniffing) |
| 11 | 3. **Searcher constructs a profitable bundle** (e.g., sandwich your swap) |
| 12 | 4. **Bundle submitted to Jito block engine** with a tip to the validator |
| 13 | 5. **Validator includes the bundle** in the block, earning the tip |
| 14 | 6. **You receive worse execution**; the searcher profits the difference |
| 15 | |
| 16 | ### How Solana MEV Differs from Ethereum |
| 17 | |
| 18 | | Aspect | Ethereum | Solana | |
| 19 | |--------|----------|--------| |
| 20 | | Block time | 12 seconds | ~400ms slots | |
| 21 | | Mempool | Public mempool | No mempool (but tx visible in transit) | |
| 22 | | Ordering | Proposer-builder separation (PBS) | Jito block engine (~85%+ validators) | |
| 23 | | Bundle system | Flashbots bundles | Jito bundles with tips | |
| 24 | | MEV cost | Gas priority fees | Jito tips (SOL) | |
| 25 | | Latency pressure | Moderate | Extreme (sub-100ms decisions) | |
| 26 | |
| 27 | Key Solana-specific factors: |
| 28 | - **No public mempool**: Transactions flow RPC → TPU → Leader, but searchers tap into this flow via Jito's block engine and modified validators |
| 29 | - **Known leader schedule**: The leader (block producer) schedule is known ~2 epochs ahead, letting searchers target specific leaders |
| 30 | - **Jito dominance**: ~85%+ of validators run the Jito-modified client, making Jito bundles the primary MEV vector |
| 31 | - **Speed**: 400ms slots mean MEV bots must operate in microseconds, favoring co-located infrastructure |
| 32 | |
| 33 | ## MEV Types on Solana |
| 34 | |
| 35 | ### 1. Sandwich Attacks |
| 36 | |
| 37 | The most common MEV attack against retail traders. |
| 38 | |
| 39 | **Mechanics:** |
| 40 | ``` |
| 41 | 1. Attacker sees your pending swap: Buy 10 SOL worth of TOKEN_X |
| 42 | 2. Front-run: Attacker buys TOKEN_X first → price rises |
| 43 | 3. Your swap: You buy TOKEN_X at higher price → worse execution |
| 44 | 4. Back-run: Attacker sells TOKEN_X → profits the difference |
| 45 | ``` |
| 46 | |
| 47 | **Your loss** = price impact from front-run + attacker's profit margin |
| 48 | **Attacker profit** = your_loss - jito_tip - transaction_fees |
| 49 | |
| 50 | **Risk factors:** |
| 51 | - Trade size: Larger trades = more profitable to sandwich |
| 52 | - Token liquidity: Illiquid tokens = easier price manipulation |
| 53 | - Slippage setting: Wide slippage = more room for the attacker |
| 54 | - Pool type: CPMM pools more vulnerable than CLMM pools at concentrated ranges |
| 55 | |
| 56 | ### 2. Arbitrage (Cross-DEX) |
| 57 | |
| 58 | Searchers capture price discrepancies between DEXes. |
| 59 | |
| 60 | ``` |
| 61 | Pool A: TOKEN_X = 1.00 USDC |
| 62 | Pool B: TOKEN_X = 1.02 USDC |
| 63 | → Buy on A, sell on B, profit 0.02 USDC per token (minus fees) |
| 64 | ``` |
| 65 | |
| 66 | This is generally **beneficial** to the market — it equalizes prices across venues. However, your trade may trigger the arbitrage opportunity that the searcher captures. |
| 67 | |
| 68 | ### 3. Liquidation Extraction |
| 69 | |
| 70 | When DeFi positions (Solend, Marginfi, Kamino) become undercollateralized, searchers race to liquidate them and claim the liquidation bonus (typically 5-10%). |
| 71 | |
| 72 | ### 4. JIT (Just-In-Time) Liquidity |
| 73 | |
| 74 | Searchers add concentrated liquidity to a CLMM pool just before a large swap and remove it immediately after, earning swap fees without sustained impermanent loss exposure. This is a sophisticated MEV form that can actually **improve** execution for the swapper. |
| 75 | |
| 76 | ### 5. Back-Running |
| 77 | |
| 78 | Trading immediately after a large swap that moved the price, capturing the reversion. Less harmful than sandwiching because it does not worsen your execution — it profits from the market response to your trade. |
| 79 | |
| 80 | ## Estimating MEV Exposure |
| 81 | |
| 82 | Estimate your MEV risk before executing a trade: |
| 83 | |
| 84 | ```python |
| 85 | import httpx |
| 86 | |
| 87 | def estimate_mev_risk( |
| 88 | trade_size_sol: float, |
| 89 | pool_liquidity_usd: float, |
| 90 | slippage_bps: int, |
| 91 | token_daily_volume_usd: float, |
| 92 | ) -> dict: |
| 93 | """Estimate sandwich attack profitability for a given trade. |
| 94 | |
| 95 | Returns risk assessment with estimated cost and recommendations. |
| 96 | """ |
| 97 | # Trade as percentage of pool liquidity |
| 98 | sol_price = 150.0 # approximate; fetch live price in production |
| 99 | trade_usd = trade_size_sol * sol_price |
| 100 | trade_pct_of_pool = (trade_usd / pool_liquidity_usd) * 100 |
| 101 | |
| 102 | # Estimated price impact from constant-product AMM |
| 103 | # price_impact ≈ trade_size / pool_liquidity (simplified) |
| 104 | price_impact_bps = int(trade_pct_of_pool * 100) |
| 105 | |
| 106 | # Sandwich profitability: attacker captures portion of slippage headroom |
| 107 | # Rough model: sandwich_profit ≈ 0.5 * slippage_headroom * trade_size |