Skip to content

MEV-Share client module (mev_share.zig) #34

@koko1123

Description

@koko1123

Motivation

MEV-Share is Flashbots' orderflow auction protocol — the primary mechanism for searchers to backrun private transactions on Ethereum today. The TypeScript reference client (mev-share-client-ts) is the only official SDK. A Zig client would be the fastest implementation and give eth.zig a concrete position in the active MEV ecosystem.

What MEV-Share enables

  • Private transaction backrunning: Subscribe to a stream of pending tx hints, backrun profitable ones
  • Bundle composition: Compose backrun bundles targeting specific pending txs
  • MEV profit sharing: A portion of searcher profit flows back to the user who originated the tx

API surface (mirrors mev-share-client-ts)

New file: src/mev_share.zig

pub const MevShareClient = struct {
    /// Subscribe to the MEV-Share event stream (SSE).
    /// Calls callback for each pending transaction or bundle event.
    pub fn on(
        self: *MevShareClient,
        allocator: std.mem.Allocator,
        callback: *const fn (event: PendingEvent) void,
    ) !void

    /// Submit a private transaction with MEV hints.
    /// Maps to eth_sendPrivateTransaction.
    pub fn sendTransaction(
        self: *MevShareClient,
        allocator: std.mem.Allocator,
        signed_tx: []const u8,
        opts: SendTransactionOpts,
    ) ![32]u8  // tx hash

    /// Simulate a MEV-Share bundle (mev_simBundle).
    pub fn simulateBundle(
        self: *MevShareClient,
        allocator: std.mem.Allocator,
        opts: flashbots.MevSendBundleOpts,
        sim_opts: SimBundleOpts,
    ) !SimBundleResult

    /// Fetch historical event stream data.
    pub fn getEventHistory(
        self: *MevShareClient,
        allocator: std.mem.Allocator,
        params: EventHistoryParams,
    ) ![]EventHistoryEntry
};

Key types needed

/// A pending transaction or bundle from the event stream.
pub const PendingEvent = union(enum) {
    transaction: PendingTransaction,
    bundle: PendingBundle,
};

pub const PendingTransaction = struct {
    hash: [32]u8,
    logs: ?[]Log,
    calldata: ?[]u8,
    function_selector: ?[4]u8,
    contract_address: ?[20]u8,
    to: ?[20]u8,
    value: ?u256,
};

pub const PendingBundle = struct {
    hash: [32]u8,
    txs: []PendingTransaction,
};

Implementation notes

  • Transport: Depends on sse_transport.zig (see SSE transport for MEV-Share event streaming #33) for event streaming; uses existing http_transport.zig for JSON-RPC calls
  • Auth: Reuse flashbots.zig auth header signing (X-Flashbots-Signature)
  • Networks: Mainnet endpoint https://mev-share.flashbots.net; Sepolia https://mev-share-goerli.flashbots.net
  • mev_sendBundle is already implemented in flashbots.zig — no duplication needed

Relationship to examples

Once this exists, an example backrunner bot (examples/08_mev_share_backrunner.zig) becomes straightforward.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions