Press ESC to close

Code an Anti-MEV Stealth Swap Bot: Step-by-Step Node.js Guide

Over at the EXMON Academy blog, we’ve already dropped a post on how MEV bots straight-up rob regular users in the "dark forest" of the mempool. Today, we’re getting our hands dirty and building the foundation of our own tool. The goal is simple: learn how to route transactions directly to validators, completely bypassing the public mempool.

1. The Bundle Concept: Why is it Safer?

Normally, when you fire off a transaction, it lands in the mempool — basically a public waiting room. Everyone can see it there, from standard nodes to predatory MEV bots. These bots sniff out profitable moves (like a fat swap) and hit you with a "sandwich attack" before you even know what happened.

Flashbots offers a workaround: Bundles. These are packages of one or more transactions sent straight to validators through a private relay server.

The big wins with bundles:

  • Total Stealth: Your transaction never hits the public mempool. The world only finds out about it once it’s already tucked into a block. Bots don't have a window to react.
  • Atomicity: It’s all or nothing. Either every transaction in your bundle executes, or none of them do. This kills the risk of your trade going through partially or under crappy conditions.
  • No Pay for Fails: If your bundle doesn't make the cut (maybe the gas price was too low), you don't pay a dime in revert fees. No failed transaction costs.

 

2. Prepping the Infrastructure

To get this moving, you’ll need two things: access to an Ethereum node and two private keys.

Why two keys?

  • SENDER_PRIVATE_KEY: This is your main wallet. It holds your funds for the swap and the ETH for gas.
  • FLASHBOTS_AUTH_KEY: Think of this as your "ID card." It should be a fresh, totally empty wallet. Flashbots uses it to track your "reputation" as a searcher. If you spam garbage bundles, this key gets blacklisted, but your main stack stays safe.

Environment Setup

Pop a .env file into your project root:

ETH_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY
SENDER_PRIVATE_KEY=your_main_key_here
FLASHBOTS_AUTH_KEY=your_empty_reputation_key_here

 

3. Spinning up the Stealth Provider

We’re rocking TypeScript, Ethers.js v6, and the official Flashbots SDK.

Grab the dependencies: npm install ethers @flashbots/ethers-provider-bundle dotenv

Here’s the boilerplate to get our tool initialized:

import { ethers } from "ethers";
import { FlashbotsBundleProvider } from "@flashbots/ethers-provider-bundle";
import * as dotenv from "dotenv";
dotenv.config();

async function initStealthProvider() {
    // 1. Hook up to a standard node (Alchemy/Infura)
    const provider = new ethers.JsonRpcProvider(process.env.ETH_RPC_URL);

    // 2. Set up the wallets
    const wallet = new ethers.Wallet(process.env.SENDER_PRIVATE_KEY!, provider);
    const authSigner = new ethers.Wallet(process.env.FLASHBOTS_AUTH_KEY!, provider);

    // 3. Create the Flashbots provider
    // This is our private tunnel to the validators
    const flashbotsProvider = await FlashbotsBundleProvider.create(
        provider, 
        authSigner,
        "https://relay.flashbots.net", // Mainnet relay address
        "mainnet"
    );

    return { wallet, flashbotsProvider, provider };
}

async function main() {
    try {
        const { wallet, flashbotsProvider } = await initStealthProvider();
        console.log("--- Stealth Infrastructure Ready ---");
        console.log(`Main Wallet: ${wallet.address}`);
        console.log(`Relay: https://relay.flashbots.net`);
    } catch (error) {
        console.error("Initialization failed:", error);
    }
}

main();

 

What did we just build?

Right now, we’ve established a "secure line." We aren't just shouting into the blockchain; we're officially authenticated with the Flashbots network.

Pro tip: Flashbots is essentially an auction. For your bundle to get picked up by a validator, it has to be more profitable than the competition. In the next part, we’ll dive into calculating fees correctly and building an actual Uniswap V3 swap that no bot-scanner will ever see coming.

Coming up next: Building the Bundle, simulating the trade, and bypassing balance checks.

Sying Yu

I am a blockchain developer specializing in building secure, scalable, and innovative decentralized solutions. My expertise covers smart contracts, payment systems, and integrating crypto with fiat to optimize financial workflows. I thrive on creating modern, efficient tools for the evolving digital economy....

Leave a comment

Your email address will not be published. Required fields are marked *