8 releases

0.2.0 Apr 10, 2024
0.1.6 Nov 28, 2023
0.1.5 Oct 22, 2023
0.1.4 Jul 26, 2023

#160 in Web programming

Download history 1784/week @ 2024-01-02 1736/week @ 2024-01-09 1613/week @ 2024-01-16 1970/week @ 2024-01-23 1914/week @ 2024-01-30 1834/week @ 2024-02-06 2031/week @ 2024-02-13 2397/week @ 2024-02-20 2585/week @ 2024-02-27 2258/week @ 2024-03-05 2651/week @ 2024-03-12 2409/week @ 2024-03-19 2087/week @ 2024-03-26 1924/week @ 2024-04-02 2333/week @ 2024-04-09 1942/week @ 2024-04-16

8,660 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

~11–25MB
~402K SLoC