|
| 1 | +//! A generic Flashbots bundle API wrapper. |
| 2 | +use crate::utils::signer::LocalOrAws; |
| 3 | +use alloy::{ |
| 4 | + primitives::{keccak256, BlockNumber}, |
| 5 | + rpc::{ |
| 6 | + json_rpc::{Id, Response, ResponsePayload, RpcRecv, RpcSend}, |
| 7 | + types::mev::{EthBundleHash, MevSendBundle, SimBundleResponse}, |
| 8 | + }, |
| 9 | + signers::Signer, |
| 10 | +}; |
| 11 | +use init4_from_env_derive::FromEnv; |
| 12 | +use reqwest::header::CONTENT_TYPE; |
| 13 | +use serde_json::json; |
| 14 | +use std::borrow::Cow; |
| 15 | + |
| 16 | +/// Configuration for the Flashbots provider. |
| 17 | +#[derive(Debug, Clone, FromEnv)] |
| 18 | +#[from_env(crate)] |
| 19 | +pub struct FlashbotsConfig { |
| 20 | + /// Flashbots endpoint for privately submitting rollup blocks. |
| 21 | + #[from_env( |
| 22 | + var = "FLASHBOTS_ENDPOINT", |
| 23 | + desc = "Flashbots endpoint for privately submitting rollup blocks", |
| 24 | + optional |
| 25 | + )] |
| 26 | + pub flashbots_endpoint: Option<url::Url>, |
| 27 | +} |
| 28 | + |
| 29 | +impl FlashbotsConfig { |
| 30 | + /// Make a [`Flashbots`] instance from this config, using the specified signer. |
| 31 | + pub fn build(&self, signer: LocalOrAws) -> Option<Flashbots> { |
| 32 | + self.flashbots_endpoint |
| 33 | + .as_ref() |
| 34 | + .map(|url| Flashbots::new(url.clone(), signer)) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// A basic provider for common Flashbots Relay endpoints. |
| 39 | +#[derive(Debug)] |
| 40 | +pub struct Flashbots { |
| 41 | + /// The base URL for the Flashbots API. |
| 42 | + pub relay_url: url::Url, |
| 43 | + |
| 44 | + /// Signer is loaded once at startup. |
| 45 | + signer: LocalOrAws, |
| 46 | + |
| 47 | + /// The reqwest client to use for requests. |
| 48 | + client: reqwest::Client, |
| 49 | +} |
| 50 | + |
| 51 | +impl Flashbots { |
| 52 | + /// Instantiate a new provider from the URL and signer. |
| 53 | + pub fn new(relay_url: url::Url, signer: LocalOrAws) -> Self { |
| 54 | + Self { |
| 55 | + relay_url, |
| 56 | + client: Default::default(), |
| 57 | + signer, |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Instantiate a new provider from the URL and signer, with a specific |
| 62 | + /// Reqwest client. |
| 63 | + pub const fn new_with_client( |
| 64 | + relay_url: url::Url, |
| 65 | + signer: LocalOrAws, |
| 66 | + client: reqwest::Client, |
| 67 | + ) -> Self { |
| 68 | + Self { |
| 69 | + relay_url, |
| 70 | + client, |
| 71 | + signer, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Sends a bundle via `mev_sendBundle`. |
| 76 | + pub async fn send_bundle(&self, bundle: &MevSendBundle) -> eyre::Result<EthBundleHash> { |
| 77 | + self.raw_call("mev_sendBundle", &[bundle]).await |
| 78 | + } |
| 79 | + |
| 80 | + /// Simulate a bundle via `mev_simBundle`. |
| 81 | + pub async fn simulate_bundle(&self, bundle: &MevSendBundle) -> eyre::Result<()> { |
| 82 | + let resp: SimBundleResponse = self.raw_call("mev_simBundle", &[bundle]).await?; |
| 83 | + dbg!("successfully simulated bundle", &resp); |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + |
| 87 | + /// Fetch the bundle status by hash. |
| 88 | + pub async fn bundle_status( |
| 89 | + &self, |
| 90 | + hash: EthBundleHash, |
| 91 | + block_number: BlockNumber, |
| 92 | + ) -> eyre::Result<()> { |
| 93 | + let params = json!({ "bundleHash": hash, "blockNumber": block_number }); |
| 94 | + let _resp: serde_json::Value = self |
| 95 | + .raw_call("flashbots_getBundleStatsV2", &[params]) |
| 96 | + .await?; |
| 97 | + |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | + |
| 101 | + /// Make a raw JSON-RPC call with the Flashbots signature header to the |
| 102 | + /// method with the given params. |
| 103 | + async fn raw_call<Params: RpcSend, Payload: RpcRecv>( |
| 104 | + &self, |
| 105 | + method: &str, |
| 106 | + params: &Params, |
| 107 | + ) -> eyre::Result<Payload> { |
| 108 | + let req = alloy::rpc::json_rpc::Request::new( |
| 109 | + Cow::Owned(method.to_string()), |
| 110 | + Id::Number(1), |
| 111 | + params, |
| 112 | + ); |
| 113 | + let body_bz = serde_json::to_vec(&req)?; |
| 114 | + drop(req); |
| 115 | + |
| 116 | + let value = self.compute_signature(&body_bz).await?; |
| 117 | + |
| 118 | + let resp = self |
| 119 | + .client |
| 120 | + .post(self.relay_url.as_str()) |
| 121 | + .header(CONTENT_TYPE, "application/json") |
| 122 | + .header("X-Flashbots-Signature", value) |
| 123 | + .body(body_bz) |
| 124 | + .send() |
| 125 | + .await?; |
| 126 | + |
| 127 | + let resp: Response<Payload> = resp.json().await?; |
| 128 | + |
| 129 | + match resp.payload { |
| 130 | + ResponsePayload::Success(payload) => Ok(payload), |
| 131 | + ResponsePayload::Failure(err) => { |
| 132 | + eyre::bail!("flashbots error: {err}"); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /// Builds an EIP-191 signature for the given body bytes. This signature is |
| 138 | + /// used to authenticate to the relay API via a header |
| 139 | + async fn compute_signature(&self, body_bz: &[u8]) -> Result<String, eyre::Error> { |
| 140 | + let payload = keccak256(body_bz).to_string(); |
| 141 | + let signature = self.signer.sign_message(payload.as_ref()).await?; |
| 142 | + dbg!(signature.to_string()); |
| 143 | + let address = self.signer.address(); |
| 144 | + let value = format!("{address}:{signature}"); |
| 145 | + Ok(value) |
| 146 | + } |
| 147 | +} |
0 commit comments