7 releases

0.1.6 Nov 28, 2023
0.1.5 Oct 22, 2023
0.1.4 Jul 26, 2023

#587 in Magic Beans

Download history 1132/week @ 2023-10-30 1084/week @ 2023-11-06 1346/week @ 2023-11-13 1386/week @ 2023-11-20 2141/week @ 2023-11-27 1885/week @ 2023-12-04 1747/week @ 2023-12-11 1956/week @ 2023-12-18 1604/week @ 2023-12-25 1806/week @ 2024-01-01 1803/week @ 2024-01-08 1785/week @ 2024-01-15 2050/week @ 2024-01-22 1984/week @ 2024-01-29 1905/week @ 2024-02-05 1831/week @ 2024-02-12

7,912 downloads per month
Used in mev-share

MIT/Apache

38KB
747 lines

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:


lib.rs:

Rust client implementation for the MEV-share protocol

Dependencies

~19–37MB
~606K SLoC