Skip to content

Example: simple-arbitrage Zig port (Flashbots showcase) #37

@koko1123

Description

@koko1123

Motivation

flashbots/simple-arbitrage (2,100+ stars) is the canonical reference every new MEV developer reads first. It is written in TypeScript. A Zig port using eth.zig would:

  1. Give eth.zig a direct presence in the highest-traffic Flashbots repository
  2. Demonstrate eth.zig's comptime ABI selectors and fast decode in a real-world searcher context
  3. Show Flashbots bundle submission end-to-end with a working example
  4. Provide a contribution opportunity to the Flashbots org (PR to add Zig reference, or a linked simple-arbitrage-zig repo)

What the bot does

  1. Subscribe to new blocks via WebSocket
  2. On each block: fetch Uniswap V2 pair reserves (batch eth_call) across a set of WETH/token pairs
  3. Find crossed markets (pairs where arbitrage is profitable)
  4. Construct a bundle: uniswapWeth(amount, targets, payloads) call to a deployed BundleExecutor contract
  5. Simulate the bundle (eth_callBundle) to verify profit
  6. Submit the bundle to Flashbots relay (eth_sendBundle) targeting current + next block

Implementation outline

examples/08_simple_arbitrage.zig

// Key eth.zig features showcased:

// 1. Comptime selectors
const GET_RESERVES = comptime eth.keccak.selector("getReserves()");
const UNISWAP_WETH = comptime eth.keccak.selector("uniswapWeth(uint256,uint256,address[],bytes[])");

// 2. Batch eth_call for reserve fetching (issue #11)
const reserves = try provider.batchCall(allocator, reserve_calls);

// 3. UniswapV2 quote math (issue #16)
const profit = eth.uniswap.getAmountOut(amount_in, reserve_in, reserve_out);

// 4. Bundle construction + submission
var bundle = flashbots.Bundle.init(allocator);
try bundle.addTransaction(signed_arb_tx);
const result = try fb_client.sendBundle(allocator, .{
    .transactions = bundle.transactions(),
    .block_number = block_number + 1,
});

Prerequisites

Deliverable

A self-contained examples/08_simple_arbitrage.zig with:

  • A PAIRS list configurable via environment/comptime
  • BundleExecutor contract ABI inlined
  • Working against Anvil fork (integration test)
  • README section documenting the example and linking to the Flashbots original

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