Rust client library for Flashbots MEV-share
Find a file
Derek Cofausper c0f6fd94a2
chore: remove redundant package homepages (#55)
Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
History 2026-10-17 09:11:29 +02:00
.github/workflows feat: simulation types + cleanup (#13) 2023-07-11 14:06:55 -07:00
crates chore: remove redundant package homepages (#55) 2026-10-17 09:11:29 +02:00
examples feat: migrate mev-share-sse to alloy (#46) 2024-04-10 13:31:12 +02:00
.gitignore gitignore 2023-07-01 14:34:31 +02:00
Cargo.lock chore: Release 2025-05-17 16:55:18 +02:00
Cargo.toml chore: remove redundant package homepages (#55) 2026-10-17 09:11:29 +02:00
LICENSE-APACHE initial commit 2023-04-23 17:31:34 +02:00
LICENSE-MIT initial commit 2023-04-23 17:31:34 +02:00
README.md feat: simulation types + cleanup (#13) 2023-07-11 14:06:55 -07:00
rustfmt.toml chore: add rustfmt 2023-04-23 19:03:42 +02:00

mev-share-rs

Rust utils for MEV-share.

Server-Sent-Events

Subscribe to MEV-Share event stream as follows:

use futures_util::StreamExt;
use mev_share_sse::EventClient;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

#[tokio::main]
async fn main() {
    tracing_subscriber::registry().with(fmt::layer()).with(EnvFilter::from_default_env()).init();

    let mainnet_sse = "https://mev-share.flashbots.net";
    let client = EventClient::default();
    let mut stream = client.events(mainnet_sse).await.unwrap();
    println!("Subscribed to {}", stream.endpoint());

    while let Some(event) = stream.next().await {
        dbg!(&event);
    }
}

Sending bundles

Send MEV-Share bundles as follows:

//! Basic RPC api example

use jsonrpsee::http_client::{transport::Error as HttpError, HttpClientBuilder};
use mev_share_rpc_api::{BundleItem, FlashbotsSignerLayer, MevApiClient, SendBundleRequest};
use tower::ServiceBuilder;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

use ethers_core::{
    rand::thread_rng,
    types::{TransactionRequest, H256},
};
use ethers_signers::{LocalWallet, Signer};

#[tokio::main]
async fn main() {
    tracing_subscriber::registry().with(fmt::layer()).with(EnvFilter::from_default_env()).init();

    // The signer used to authenticate bundles
    let fb_signer = LocalWallet::new(&mut thread_rng());

    // The signer used to sign our transactions
    let tx_signer = LocalWallet::new(&mut thread_rng());

    // Set up flashbots-style auth middleware
    let signing_middleware = FlashbotsSignerLayer::new(fb_signer);
    let service_builder = ServiceBuilder::new()
        // map signer errors to http errors
        .map_err(|e| HttpError::Http(e))
        .layer(signing_middleware);

    // Set up the rpc client
    let url = "https://relay.flashbots.net:443";
    let client = HttpClientBuilder::default()
        .set_middleware(service_builder)
        .build(url)
        .expect("Failed to create http client");

    // Hash of the transaction we are trying to backrun
    let tx_hash = H256::random();

    // Our own tx that we want to include in the bundle
    let tx = TransactionRequest::pay("vitalik.eth", 100);
    let signature = tx_signer.sign_transaction(&tx.clone().into()).await.unwrap();
    let bytes = tx.rlp_signed(&signature);

    // Build bundle
    let mut bundle_body = Vec::new();
    bundle_body.push(BundleItem::Hash { hash: tx_hash });
    bundle_body.push(BundleItem::Tx { tx: bytes, can_revert: false });

    let bundle = SendBundleRequest { bundle_body, ..Default::default() };

    // Send bundle
    let resp = client.send_bundle(bundle.clone()).await;
    println!("Got a bundle response: {:?}", resp);

    // Simulate bundle 
    let sim_res = client.sim_bundle(bundle, Default::default()).await;
    println!("Got a simulation response: {:?}", sim_res);
}

License

Licensed under either of these: