Skip to content

Example: MEV-Share backrunner bot #38

@koko1123

Description

@koko1123

Motivation

MEV-Share is the active Flashbots orderflow protocol. The canonical developer journey is: "read the docs → clone mev-share-client-ts → write a backrunner". A Zig backrunner example gives eth.zig a natural home in that journey for performance-conscious searchers.

What the bot does

  1. Connect to the MEV-Share event stream (SSE) and listen for pending transactions
  2. For each pending tx event: check if it involves a known token swap (by function selector hint)
  3. If profitable: construct a backrun bundle (user tx hash + our backrun tx)
  4. Submit via mev_sendBundle to the MEV-Share relay

Implementation outline

examples/09_mev_share_backrunner.zig

const eth = @import("eth");
const mev_share = eth.mev_share;

// Comptime: pre-hash the swap selectors we care about
const UNISWAP_V2_SWAP = comptime eth.keccak.selector("swapExactTokensForTokens(uint256,uint256,address[],address,uint256)");
const UNISWAP_V3_SWAP = comptime eth.keccak.selector("exactInputSingle((address,address,uint24,address,uint256,uint256,uint160))");

var client = try mev_share.MevShareClient.init(allocator, auth_signer, .mainnet);

try client.on(allocator, struct {
    fn handle(event: mev_share.PendingEvent) void {
        switch (event) {
            .transaction => |tx| {
                // Check if this tx is a swap we can backrun
                if (tx.function_selector) |sel| {
                    if (std.mem.eql(u8, &sel, &UNISWAP_V2_SWAP)) {
                        submitBackrun(tx.hash);
                    }
                }
            },
            else => {},
        }
    }
}.handle);

Key eth.zig features showcased

  • Comptime function selector matching (zero-cost dispatch on 4-byte selectors)
  • MEV-Share client (SSE streaming + mev_sendBundle)
  • EIP-1559 transaction construction and signing
  • Fast ABI encode for the backrun payload

Prerequisites

Deliverable

examples/09_mev_share_backrunner.zig with instructions for running against the Sepolia MEV-Share endpoint (no mainnet funds required to test the subscription flow).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestexampleExample code / showcase

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions