Skip to content

Add MEV-Share backrunner example (closes #38)#78

Merged
koko1123 merged 1 commit into
mainfrom
feat/backrunner-example
Jun 10, 2026
Merged

Add MEV-Share backrunner example (closes #38)#78
koko1123 merged 1 commit into
mainfrom
feat/backrunner-example

Conversation

@koko1123

@koko1123 koko1123 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

What

examples/08_mev_share_backrunner.zig — a teaching MEV-Share backrunner that ties together the new MEV stack: comptime-built Uniswap V2/V3 swap-selector table, the mev_share SSE hint stream, EIP-1559 backrun construction/signing, and mev_sendBundle bundle composition. DRY_RUN=1 default (prints the bundle JSON instead of submitting); env-var config with Sepolia defaults.

It works against the live network

Run against the mainnet stream, it matched hash-only and bundle hints in real time and printed the bundle it would submit. Sample:

[hash-only]   0x1ad30a93...0241 (no selector hint, skipping)
[bundle]      2 txs, skipping

Bug fixes from dog-fooding (first consumer of mev_share)

MevShareClient.on was uncompilable for any consumer: sse_transport.SseParser.feedLine used std.mem.trimRight (renamed trimEnd in Zig 0.16) and built two anonymous structs with mismatched inferred types. Neither surfaced in CI because feedLine is only reached via the network path, not unit tests. Fixed both in src/sse_transport.zig; zig build test still passes. (The refAllDeclsRecursive test added in #63 covers mev_share itself but not sse_transport — I'll file a follow-up to extend that guard.)

Also

README + docs/content/docs/examples.mdx entries; CHANGELOG Added/Fixed bullets.

Verification

examples build, root zig build test, zig fmt --check, and pnpm -C docs build all pass on Zig 0.16.0.

Closes #38

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added MEV-Share backrunner example—a bot that monitors the MEV-Share event stream and constructs backrun bundles for Uniswap trades.
  • Bug Fixes

    • Fixed SSE stream parsing to correctly handle line-ending characters.
  • Documentation

    • Updated examples with new MEV-Share backrunner entry and usage instructions.

examples/08_mev_share_backrunner.zig: a heavily-commented teaching bot
that subscribes to the MEV-Share SSE hint stream, matches pending-tx
hints against a comptime-built table of Uniswap V2/V3 swap selectors,
and composes a backrun bundle (user tx hash + our signed EIP-1559 tx)
submitted via mev_sendBundle. DRY_RUN=1 default prints the bundle JSON
instead of submitting; all config via env vars with Sepolia defaults.

Showcases: comptime selector dispatch, the mev_share client + SSE
stream, EIP-1559 construction/signing/serialization, and bundle
composition.

Dog-fooding this first consumer of mev_share surfaced and fixed two
latent Zig 0.16 breakages in sse_transport.SseParser.feedLine
(std.mem.trimRight renamed to trimEnd; mismatched anonymous-struct
types) -- the on() stream path was uncompilable for any consumer and
is not exercised by unit tests. Verified by streaming live mainnet
MEV-Share events.
@vercel

vercel Bot commented Jun 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
eth-zig Ready Ready Preview, Comment Jun 10, 2026 3:55pm

Request Review

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7e88da1d-705d-47dc-9f31-170468ef3dd0

📥 Commits

Reviewing files that changed from the base of the PR and between 2d74584 and 28f1e79.

📒 Files selected for processing (6)
  • CHANGELOG.md
  • README.md
  • docs/content/docs/examples.mdx
  • examples/08_mev_share_backrunner.zig
  • examples/build.zig
  • src/sse_transport.zig

📝 Walkthrough

Walkthrough

This PR introduces a complete MEV-Share backrunner example that demonstrates subscribing to the Flashbots MEV-Share pending-transaction stream, matching hints against compile-time Uniswap swap selectors, and submitting backrun bundles. The change includes a critical fix to the SSE parser and comprehensive documentation.

Changes

MEV-Share Backrunner Bot and Tooling

Layer / File(s) Summary
SSE Transport Parser Fix
src/sse_transport.zig
Replaces std.mem.trimRight with std.mem.trimEnd for trailing carriage-return removal and refactors field/value parsing to use an explicit Parsed struct. Fixes a latent semantic break that affected MevShareClient.on consumption of SSE events.
Backrunner Bot Configuration and Utilities
examples/08_mev_share_backrunner.zig (lines 1–90)
Defines compile-time WatchedSwap metadata and a table of Uniswap function selectors via eth.keccak.selector, and provides matchSwap to identify incoming 4-byte selectors against the pre-hashed table.
Bot State and Event Subscription
examples/08_mev_share_backrunner.zig (lines 91–141)
Declares the Bot state struct (signer, MEV-Share client, optional RPC provider, chain ID, event counters) and implements the onEvent SSE callback wrapper that catches errors, flushes output, increments event count, and exits when MAX_EVENTS is reached.
Event Processing and Bundle Submission
examples/08_mev_share_backrunner.zig (lines 142–283)
Implements handleEvent to extract pending transactions from SSE hints, skip bundle-style events, match revealed selectors, and log outcomes; submitBackrun to construct EIP-1559 transactions, sign with the signer key, and submit bundles (or print dry-run JSON-RPC); and parseKey for hex private-key decoding.
Main Entry Point and Lifecycle
examples/08_mev_share_backrunner.zig (lines 285–390)
Wires up stdout buffering, reads environment configuration (auth/signer keys, URLs, dry-run mode), initializes the MEV-Share client and optional RPC provider, prints startup banner and watched selectors, installs SSE subscription with error handling, and logs stream termination statistics.
Build and Documentation
examples/build.zig, CHANGELOG.md, README.md, docs/content/docs/examples.mdx
Registers the new example executable in the build manifest, records the SSE parser fix and new backrunner example in the changelog, adds the example to the README examples table, and provides detailed documentation describing the compile-time selector computation, SSE subscription flow, bundle construction, and dry-run execution command targeting Sepolia.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

  • StrobeLabs/eth.zig#38: This PR implements the exact MEV-Share backrunner example requested and fixes the SSE parser issue that was blocking the feature.
  • StrobeLabs/eth.zig#34: The SSE transport parser fix directly addresses the underlying transport behavior that the MEV-Share client depends on.

Possibly related PRs

  • StrobeLabs/eth.zig#48: The SSE transport parser fix in this PR directly corrects behavior introduced in the SSE transport implementation.
  • StrobeLabs/eth.zig#63: This PR's src/sse_transport.zig fix addresses the latent semantic break in MevShareClient.on that was surfaced while building the backrunner example.
  • StrobeLabs/eth.zig#32: The backrunner example wires up MEV-Share bundle submission via flashbots.Relay and mev_sendBundle functionality introduced in that PR.

Poem

🐰 A backrunner hops through the SSE stream,
Matching swaps with selectors, a fast-paced dream,
Bundle-signing with Zig's compiled grace,
The parser trimmed right, now trimEnd takes its place! 🚀

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: adding a MEV-Share backrunner example. It is concise, descriptive, and directly reflects the primary addition in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/backrunner-example

Comment @coderabbitai help to get the list of available commands and usage tips.

@koko1123
koko1123 merged commit ecffc74 into main Jun 10, 2026
13 of 14 checks passed
@koko1123
koko1123 deleted the feat/backrunner-example branch June 10, 2026 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Example: MEV-Share backrunner bot

1 participant