From everything-claude-trading
- Analyzing Maximal Extractable Value (MEV) opportunities or risks in DeFi transactions
npx claudepluginhub brainbytes-dev/everything-claude-tradingThis skill uses the workspace's default tool permissions.
- Analyzing Maximal Extractable Value (MEV) opportunities or risks in DeFi transactions
Provides Ktor server patterns for routing DSL, plugins (auth, CORS, serialization), Koin DI, WebSockets, services, and testApplication testing.
Conducts multi-source web research with firecrawl and exa MCPs: searches, scrapes pages, synthesizes cited reports. For deep dives, competitive analysis, tech evaluations, or due diligence.
Provides demand forecasting, safety stock optimization, replenishment planning, and promotional lift estimation for multi-location retailers managing 300-800 SKUs.
Definition: MEV is the maximum value that can be extracted from block production beyond the standard block reward and gas fees, by including, excluding, or reordering transactions within a block.
Primary MEV Types:
DEX Arbitrage — exploiting price differences between DEXs or between DEX pools
Sandwich Attacks — frontrunning and backrunning a victim's swap
Liquidation MEV — racing to liquidate undercollateralized positions
NFT MEV — sniping underpriced listings, frontrunning mints
Long-tail MEV — protocol-specific extraction (governance, oracle manipulation)
Mempool state: Victim wants to swap 10 ETH -> USDC on Uniswap
Pool state: 1000 ETH / 2,000,000 USDC (price = 2000 USDC/ETH)
Victim slippage tolerance: 1%
Attacker frontrun:
- Buy with 50 ETH -> receives ~95,238 USDC
- New pool: 1050 ETH / 1,904,762 USDC (price = 1814 USDC/ETH)
Victim swap executes:
- 10 ETH -> receives ~17,878 USDC (vs ~19,802 without sandwich)
- Victim loses ~$1,924
Attacker backrun:
- Sell 95,238 USDC -> receives ~51.8 ETH
- Profit: ~1.8 ETH minus gas costs (~0.1 ETH)
- Net MEV extracted: ~1.7 ETH ($3,400)
Flashbots Ecosystem:
MEV-Boost Architecture:
Transaction flow:
User -> Mempool -> Searchers (find MEV) -> Builders (construct blocks)
-> Relays (validate blocks) -> Proposers (select highest-bid block)
Key actors:
- Searchers: find MEV opportunities, create bundles
- Builders: aggregate bundles into full blocks, bid for inclusion
- Relays: trusted intermediaries that validate builder blocks
- Proposers: validators who select the winning block
Proposer-Builder Separation (PBS):
For Users:
For Protocols:
Expected profit per opportunity:
E[profit] = P(win_auction) * (gross_MEV - gas - bid) - P(lose_auction) * wasted_gas
Where:
- P(win_auction) decreases as more searchers compete
- Bid converges toward gross_MEV in competitive markets
- Wasted gas = cost of failed/reverted transactions
- Mature MEV types (DEX arb) have thin margins; novel types have wide margins
1. Estimate trade size relative to pool liquidity
2. Calculate expected price impact without MEV
3. Add MEV extraction estimate (typically 20-80% of available sandwich profit)
4. Compare total execution cost vs. using private transaction channels
5. For large trades: evaluate TWAP, RFQ, or batch auction alternatives
Opportunity:
- Uniswap ETH/USDC pool: ETH price = $2,010
- SushiSwap ETH/USDC pool: ETH price = $2,000
- Price discrepancy: $10 per ETH
Execution:
1. Flash loan 100 ETH from Aave
2. Sell 100 ETH on Uniswap at $2,010 = $201,000
3. Buy ~100.5 ETH on SushiSwap for $201,000
4. Repay 100 ETH flash loan + 0.05 ETH fee
5. Profit: 0.45 ETH (~$900) minus gas and builder tip
Bundle submission:
- Submit via Flashbots to avoid mempool exposure
- Bid 60% of profit to builder ($540) to ensure inclusion
- Net profit: ~$360 per opportunity
Monitoring:
- Track 10,000+ Aave positions continuously
- Pre-compute liquidation prices for each position
- Subscribe to pending block headers for price updates
Execution when position becomes liquidatable:
- Health factor drops below 1.0
- Flash loan the debt asset
- Call liquidate() repaying debt, receiving collateral + bonus
- Swap collateral back to debt asset
- Repay flash loan
Economics:
- Average liquidation bonus: 5%
- Average liquidation size: $50,000
- Gross profit: $2,500
- Gas + builder tip: $500-1,000
- Net profit: $1,500-2,000 per liquidation
- Competition: high, latency-sensitive (sub-block timing)
Trade: Swap 50 ETH -> USDC
Route 1 — Public mempool (Uniswap directly):
- Expected output: 99,500 USDC
- Sandwich extraction: -$800 (estimated)
- Actual output: ~98,700 USDC
Route 2 — Flashbots Protect:
- Expected output: 99,500 USDC
- No sandwich possible (private submission)
- Actual output: ~99,400 USDC (normal slippage only)
Route 3 — CoW Swap (batch auction):
- Expected output: 99,600 USDC (surplus from batch matching)
- No MEV extraction + potential price improvement
- Actual output: ~99,550 USDC
Recommendation: For trades >$10K, always use MEV protection.
CoW Swap provides best execution for large stablecoin swaps.
Before engaging with MEV strategies or assessing MEV risk, verify: