Advanced

MEV Mitigation — Hands-on lab

Avoid getting exploited at the mempool layer.

Developer Advanced
6/8 — MEV Mitigation — Hands-on lab

ObserveSubmit the same swap via public RPC vs Flashbots Protect; compare slippage and inclusion latency.

InstrumentLog hashes, nonces, and minOut to see how often public mempool submissions get sandwiched.

import { ethers } from "ethers";

const provider = new ethers.JsonRpcProvider("https://rpc.flashbots.net");
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const tx = {
  to: ROUTER,
  data: swapCalldata,
  gasLimit: 300000,
  maxFeePerGas: ethers.parseUnits("40", "gwei"),
  maxPriorityFeePerGas: ethers.parseUnits("1", "gwei")
};
const signed = await wallet.signTransaction(tx);
const res = await provider.sendTransaction(signed);
console.log("Submitted privately:", res.hash);
Steps
  1. Send a small swap:Via public RPC; check mempool explorers for visibility.
  2. Repeat via Flashbots Protect/private:RPC; confirm it stays hidden until inclusion.
  3. Simulate a sandwich on:A forked network to see the impact of wide slippage.
  4. Tighten slippage and deadlines;:Measure failed vs successful executions.
  5. Log hashes and inclusion:Slots to spot reorg sensitivity.
← Previous section
Next section →