driftbrake
A chain-agnostic Rust crate that pre-flight simulates a transaction through REVM, then halts your strategy the moment realized profit drifts from the simulated prediction — before drift compounds into real capital loss.
Any bot's internal model of the world — its REVM fork, its price feed, its assumption about which venue fills first — can go stale relative to actual chain state. driftbrake packages three tested fixes for the failure modes that causes (ratio-direction inversion, blocking-thread starvation, unbounded RPC saturation) behind a small set of chain-agnostic traits, plus a dual-guard halt policy that catches both a sudden break and a slow bleed. For the full mechanism specification and correctness argument, see the whitepaper — or read it rendered at xtley001.github.io/driftbrake.
Documentation
- xtley001.github.io/driftbrake — the design docs (architecture, whitepaper, API reference, benchmark methodology) as a browsable site, built from
docs/via mdBook. This is the "why." - docs.rs/driftbrake — generated API reference (every public trait/type, from doc comments). This is the "what."
- This README is the "how" — the Quickstart below.
Installation
# Add the REVM simulation backend too, unless you're bringing your own SimEngine:
Quickstart
use ;
// The REVM backend is a separate, opt-in crate — `cargo add driftbrake-revm-backend`.
// (Bringing your own simulation stack instead? Implement `SimEngine` against it and
// skip this import entirely — see docs/ARCHITECTURE.md's "swappable simulation backend".)
use ;
// 1. `SimEngine` needs a `DbFactory` — something that forks chain state.
// Bring your own (a live RPC-backed one, or a test fixture).
;
// 2. Implement `ProfitDecoder` / `RealizedProfitDecoder` for your executor
// contract's specific ABI shape — see docs/API.md.
;
;
async
This example is kept honest against the real API: it's a doctest in the driftbrake crate (cargo test --doc -p driftbrake), so it can't silently drift from what actually compiles the way a hand-maintained snippet can.
Architecture
driftbrake/
├── driftbrake/ # facade crate: re-exports core + reconcile + receipt-poller
│ ├── core/ # chain-agnostic traits, zero REVM/alloy dependency
│ │ ├── ProfitDecoder
│ │ ├── RealizedProfitDecoder
│ │ └── HaltPolicy
│ ├── reconcile/ # the halt-guard logic (phantom-guard) — see docs/ARCHITECTURE.md
│ └── receipt-poller/ # confirms receipts, decodes realized profit
├── revm-backend/ # separate, opt-in: concrete SimEngine (fork-and-simulate,
│ # spawn_blocking, bounded concurrency) — not bundled into
│ # `driftbrake` itself, see docs/ARCHITECTURE.md
├── telemetry/ # planned, not yet built — structured events for the benchmark harness
├── benchmark/ # docs/BENCHMARK.md's methodology as a runnable tool (cargo run -p driftbrake-benchmark)
└── examples/
└── toy-arbitrage/ # minimal 2-pool arb wired end-to-end against a public testnet fork
For the full module breakdown, trait contracts, and why the two halt guards are independently necessary, see docs/ARCHITECTURE.md.
Benchmark
The fast/slow guard thresholds (0.50, 0.70) and window sizes (3, 20) are not asserted defaults — they're derived from a synthetic (sim, realized) sweep plotting false-halt rate against missed-catch rate. See docs/BENCHMARK.md to reproduce the sweep or re-tune it against your own historical data.
Testing
The reconcile module's ratio-direction regression test is the single most important test in the suite — see docs/whitepaper.md for why.
Security
driftbrake gates a live trading loop, so a bug in reconcile or revm-backend has direct capital consequences. Report vulnerabilities per our security policy. This code has not been independently audited — review the reconcile and HaltPolicy logic yourself before running it against real capital.
Contributing
See CONTRIBUTING.md for dev environment setup, module boundaries, and PR guidelines.
License
Released under the MIT License.