mev-analysis

MEV exposure assessment, sandwich attack detection, and protection strategies for Solana DEX trading

By agiprolabs · 366 installs

npx skills add agiprolabs/claude-trading-skills --skill mev-analysis

Source repository · Upstream listing

MEV Analysis for Solana DEX Trading 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. What Is MEV on Solana? MEV occurs when someone with transaction ordering power profits at other traders' expense. On Solana, the MEV supply chain works as follows: 1. You submit a swap through an RPC endpoint 2. Searchers observe your transaction (via RPC forwarding, block engine access, or leader TPU sniffing) 3. Searcher constructs a profitable bundle (e.g., sandwich your swap) 4. Bundle submitted to Jito block engine with a tip to the validator 5. Validator includes the bundle in the block, earning the tip 6. You receive worse execution ; the searcher profits the difference How Solana MEV Differs from Ethereum Aspect Ethereum Solana Block time 12 seconds ~400ms slots Mempool Public mempool No mempool (but tx visible in transit) Ordering Proposer builder separation (PBS) Jito block engine (~85%+ validators) Bundle system Flashbots bundles Jito bundles with tips MEV cost Gas priority fees Jito tips (SOL) Latency pressure Moderate Extreme (sub 100ms decisions) Key Solana specific factors: No public mempool : Transactions flow RPC → TPU → Leader, but searchers tap into this flow via Jito's block engine and modified validators Known leader schedule : The leader (block producer) schedule is known ~2 epochs ahead, letting searchers target specific leaders Jito dominance : ~85%+ of validators run the Jito modified client, making Jito bundles the primary MEV vector Speed : 400ms slots mean MEV bots must operate in microseconds, favoring co located infrastructure MEV Types on Solana 1. Sandwich Attacks The most common MEV attack against retail traders. Mechanics: Your loss = price impact from front run + attacker's profit margin Attacker profit = your loss jito tip transaction fees Risk factors: Trade size: Larger trades = more profitable to sandwich Token liquidity: Illiquid tokens = easier price manipulation Slippage setting: Wide slippage = more room for the attacker Pool type: CPMM pools more vulnerable than CLMM pools at concentrated ranges 2. Arbitrage (Cross DEX) Searchers capture price discrepancies between DEXes. This is generally beneficial to the market — it equalizes prices across venues. However, your trade may trigger the arbitrage opportunity that the searcher captures. 3. Liquidation Extraction When DeFi positions (Solend, Marginfi, Kamino) become undercollateralized, searchers race to liquidate them and claim the liquidation bonus (typically 5 10%). 4. JIT (Just In Time) Liquidity 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. 5. Back Running 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. Estimating MEV Exposure Estimate your MEV risk before executing a trade: MEV Protection Strategies Strategy 1: Tight Slippage Settings Set slippageBps as low as feasible. Sandwich profit is bounded by your slippage tolerance. Token Liquidity Recommended Slippage $5M pool 50 bps (0.5%) $1M $5M pool 100 bps (1%) $100K $1M pool 150 200 bps < $100K pool 200 500 bps (high risk) Trade off: Too tight slippage causes failed transactions, costing you fees with no execution. Strategy 2: Jito Bundles Submit your swap as a Jito bundle with a priority tip: Tip guidelines: Normal priority: 0.0001 0.001 SOL High priority: 0.001 0.01 SOL Urgent (volatile market): 0.01 0.05 SOL Strategy 3: Private/Protected RPCs Send transactions through endpoints that do not expose them to searchers: Jito bundles (described above) Helius priority fee API with staked connections QuickNode private transaction submission Direct TPU forwarding (requires infrastructure) Strategy 4: Trade Splitting For large trades ( 1% of pool liquidity), split execution: Strategy 5: Jupiter MEV Protection Jupiter v6 includes built in MEV protection features: Dynamic slippage : Automatically adjusts slippage to minimize sandwich window Priority fee estimation : Sets appropriate compute unit price Transaction landing optimization : Retry logic with increasing priority Enable via Jupiter API: Detecting Sandwich Attacks After a trade, check whether you were sandwiched: 1. Fetch your transaction and identify the slot 2. Fetch all transactions in that slot involving the same token 3. Look for the pattern : Transaction A: Buy TOKEN X (before your tx in slot ordering) Your transaction: Buy TOKEN X (worse price than expected) Transaction B: Sell TOKEN X (after your tx, same signer as A) 4. Verify : Signer of A and B is the same wallet (the attacker) 5. Estimate cost : Difference between your expected and actual execution price See scripts/sandwich detector.py for a working implementation. Known MEV indicators: Transaction signer has thousands of transactions per day Same slot buy then sell of the same token around your swap Signer interacts with Jito tip program frequently Wallet has no token holdings (just in and out) Integration with Other Skills slippage modeling : Use slippage estimates to set protective limits liquidity analysis : Pool liquidity determines MEV vulnerability jupiter api : Jupiter's MEV protection features and swap execution solana onchain : On chain transaction analysis for sandwich detection helius api : Transaction parsing and historical analysis Files References references/solana mev mechanics.md — Solana block production, Jito block engine, MEV supply chain, and transaction flow paths references/protection strategies.md — Detailed protection strategies with implementation guidance, cost benefit analysis, and decision matrix Scripts scripts/sandwich detector.py — Detects sandwich attacks around a given transaction signature using on chain data scripts/mev risk estimator.py — Estimates MEV exposure for a planned trade based on token liquidity, trade size, and slippage settings