Giter Club home page Giter Club logo

mev-share-rs's Introduction

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:

mev-share-rs's People

Contributors

mattsse avatar frankieislost avatar zeroxbrock avatar grayleonard avatar y-pakorn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.