| 
 | 1 | +//! A generic Flashbots bundle API wrapper.  | 
 | 2 | +use alloy::network::{Ethereum, Network};  | 
 | 3 | +use alloy::primitives::Address;  | 
 | 4 | +use alloy::providers::{  | 
 | 5 | +    Provider, SendableTx,  | 
 | 6 | +    fillers::{FillerControlFlow, TxFiller},  | 
 | 7 | +};  | 
 | 8 | +use alloy::rpc::types::eth::TransactionRequest;  | 
 | 9 | +use alloy::rpc::types::mev::{EthBundleHash, MevSendBundle, ProtocolVersion};  | 
 | 10 | +use alloy_transport::TransportResult;  | 
 | 11 | +use eyre::Context as _;  | 
 | 12 | +use std::ops::Deref;  | 
 | 13 | + | 
 | 14 | +use crate::tasks::block::sim::SimResult;  | 
 | 15 | +use signet_types::SignedFill;  | 
 | 16 | + | 
 | 17 | +/// A wrapper over a `Provider` that adds Flashbots MEV bundle helpers.  | 
 | 18 | +#[derive(Debug, Clone)]  | 
 | 19 | +pub struct FlashbotsProvider<P> {  | 
 | 20 | +    inner: P,  | 
 | 21 | +    /// The base URL for the Flashbots API.  | 
 | 22 | +    pub relay_url: url::Url,  | 
 | 23 | +}  | 
 | 24 | + | 
 | 25 | +impl<P: Provider<Ethereum>> FlashbotsProvider<P> {  | 
 | 26 | +    /// Wraps a provider with the URL and returns a new `FlashbotsProvider`.  | 
 | 27 | +    pub fn new(inner: P, relay_url: url::Url) -> Self {  | 
 | 28 | +        Self { inner, relay_url }  | 
 | 29 | +    }  | 
 | 30 | + | 
 | 31 | +    /// Consume self and return the inner provider.  | 
 | 32 | +    pub fn into_inner(self) -> P {  | 
 | 33 | +        self.inner  | 
 | 34 | +    }  | 
 | 35 | + | 
 | 36 | +    /// Borrow the inner provider.  | 
 | 37 | +    pub const fn inner(&self) -> &P {  | 
 | 38 | +        &self.inner  | 
 | 39 | +    }  | 
 | 40 | +}  | 
 | 41 | + | 
 | 42 | +impl<P> Deref for FlashbotsProvider<P> {  | 
 | 43 | +    type Target = P;  | 
 | 44 | +    fn deref(&self) -> &Self::Target {  | 
 | 45 | +        &self.inner  | 
 | 46 | +    }  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +impl<P> FlashbotsProvider<P>  | 
 | 50 | +where  | 
 | 51 | +    P: Provider<Ethereum> + Clone + Send + Sync + 'static,  | 
 | 52 | +{  | 
 | 53 | +    /// Convert a SignedFill to a TransactionRequest calling the Orders contract.  | 
 | 54 | +    ///  | 
 | 55 | +    /// This prepares the calldata for RollupOrders::fillPermit2(outputs, permit2) and sets  | 
 | 56 | +    /// `to` to the given Orders contract address. The returned request is unsigned.  | 
 | 57 | +    pub fn fill_to_tx_request(fill: &SignedFill, orders_contract: Address) -> TransactionRequest {  | 
 | 58 | +        fill.to_fill_tx(orders_contract)  | 
 | 59 | +    }  | 
 | 60 | + | 
 | 61 | +    /// Construct a new empty bundle template for the given block number.  | 
 | 62 | +    pub fn empty_bundle(&self, target_block: u64) -> MevSendBundle {  | 
 | 63 | +        MevSendBundle::new(target_block, Some(target_block), ProtocolVersion::V0_1, vec![])  | 
 | 64 | +    }  | 
 | 65 | + | 
 | 66 | +    /// Prepares a bundle transaction from the simulation result.  | 
 | 67 | +    pub fn prepare_bundle(&self, sim_result: &SimResult, target_block: u64) -> MevSendBundle {  | 
 | 68 | +        let bundle_body = Vec::new();  | 
 | 69 | + | 
 | 70 | +        // Populate the bundle body with the simulation result.  | 
 | 71 | + | 
 | 72 | +        // TODO: Push host fills into the Flashbots bundle body.  | 
 | 73 | +        let _host_fills = sim_result.block.host_fills();  | 
 | 74 | +        // _host_fills.iter().map(|f| f.to_fill_tx(todo!()));  | 
 | 75 | + | 
 | 76 | +        // TODO: Add the rollup block blob transaction to the Flashbots bundle body.  | 
 | 77 | +        // let blob_tx = ...;  | 
 | 78 | +        let _ = &sim_result; // keep param used until wired  | 
 | 79 | + | 
 | 80 | +        // Create the bundle from the target block and bundle body  | 
 | 81 | +        MevSendBundle::new(target_block, Some(target_block), ProtocolVersion::V0_1, bundle_body)  | 
 | 82 | +    }  | 
 | 83 | + | 
 | 84 | +    /// Submit the prepared Flashbots bundle to the relay via `mev_sendBundle`.  | 
 | 85 | +    pub async fn send_bundle(&self, bundle: MevSendBundle) -> eyre::Result<EthBundleHash> {  | 
 | 86 | +        // NOTE: The Flashbots relay expects a single parameter which is the bundle object.  | 
 | 87 | +        // Alloy's `raw_request` accepts any serializable params; wrapping in a 1-tuple is fine.  | 
 | 88 | +        let hash: EthBundleHash = self  | 
 | 89 | +            .inner  | 
 | 90 | +            .raw_request("mev_sendBundle".into(), (bundle,))  | 
 | 91 | +            .await  | 
 | 92 | +            .wrap_err("flashbots mev_sendBundle RPC failed")?;  | 
 | 93 | +        Ok(hash)  | 
 | 94 | +    }  | 
 | 95 | + | 
 | 96 | +    /// Simulate a bundle via `mev_simBundle`.  | 
 | 97 | +    pub async fn simulate_bundle(&self, bundle: &MevSendBundle) -> eyre::Result<()> {  | 
 | 98 | +        // We ignore the response (likely a JSON object with sim traces) for now and just ensure success.  | 
 | 99 | +        let _resp: serde_json::Value = self  | 
 | 100 | +            .inner  | 
 | 101 | +            .raw_request("mev_simBundle".into(), (bundle.clone(),))  | 
 | 102 | +            .await  | 
 | 103 | +            .wrap_err("flashbots mev_simBundle RPC failed")?;  | 
 | 104 | +        Ok(())  | 
 | 105 | +    }  | 
 | 106 | + | 
 | 107 | +    /// Check the status of a previously submitted bundle.  | 
 | 108 | +    pub async fn bundle_status(&self, _hash: EthBundleHash) -> eyre::Result<()> {  | 
 | 109 | +        eyre::bail!("FlashbotsProvider::bundle_status unimplemented")  | 
 | 110 | +    }  | 
 | 111 | +}  | 
 | 112 | + | 
 | 113 | +impl<N, P> TxFiller<N> for FlashbotsProvider<P>  | 
 | 114 | +where  | 
 | 115 | +    N: Network,  | 
 | 116 | +    P: TxFiller<N> + Provider<N> + Clone + Send + Sync + core::fmt::Debug + 'static,  | 
 | 117 | +{  | 
 | 118 | +    type Fillable = <P as TxFiller<N>>::Fillable;  | 
 | 119 | + | 
 | 120 | +    fn status(&self, tx: &N::TransactionRequest) -> FillerControlFlow {  | 
 | 121 | +        TxFiller::<N>::status(&self.inner, tx)  | 
 | 122 | +    }  | 
 | 123 | + | 
 | 124 | +    fn fill_sync(&self, tx: &mut SendableTx<N>) {  | 
 | 125 | +        TxFiller::<N>::fill_sync(&self.inner, tx)  | 
 | 126 | +    }  | 
 | 127 | + | 
 | 128 | +    fn prepare<Prov: Provider<N>>(  | 
 | 129 | +        &self,  | 
 | 130 | +        provider: &Prov,  | 
 | 131 | +        tx: &N::TransactionRequest,  | 
 | 132 | +    ) -> impl core::future::Future<Output = TransportResult<Self::Fillable>> + Send {  | 
 | 133 | +        TxFiller::<N>::prepare(&self.inner, provider, tx)  | 
 | 134 | +    }  | 
 | 135 | + | 
 | 136 | +    fn fill(  | 
 | 137 | +        &self,  | 
 | 138 | +        fillable: Self::Fillable,  | 
 | 139 | +        tx: SendableTx<N>,  | 
 | 140 | +    ) -> impl core::future::Future<Output = TransportResult<SendableTx<N>>> + Send {  | 
 | 141 | +        TxFiller::<N>::fill(&self.inner, fillable, tx)  | 
 | 142 | +    }  | 
 | 143 | +}  | 
0 commit comments