alloy_provider/ext/mev/
mod.rs

1mod with_auth;
2
3pub use self::with_auth::{sign_flashbots_payload, MevBuilder};
4use crate::Provider;
5use alloy_network::Network;
6use alloy_primitives::{hex, TxHash};
7use alloy_rpc_types_mev::{
8    EthBundleHash, EthCallBundle, EthCallBundleResponse, EthCancelBundle,
9    EthCancelPrivateTransaction, EthSendBlobs, EthSendBundle, EthSendEndOfBlockBundle,
10    EthSendPrivateTransaction, MevSendBundle, PrivateTransactionPreferences,
11};
12
13/// The HTTP header used for Flashbots signature authentication.
14pub const FLASHBOTS_SIGNATURE_HEADER: &str = "x-flashbots-signature";
15
16/// This module provides support for interacting with non-standard MEV-related RPC endpoints.
17#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
18#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
19pub trait MevApi<N>: Send + Sync {
20    /// Simulates a bundle of transactions using the `eth_callBundle` RPC method.
21    fn call_bundle(
22        &self,
23        bundle: EthCallBundle,
24    ) -> MevBuilder<(EthCallBundle,), Option<EthCallBundleResponse>>;
25
26    /// Sends a MEV bundle using the `eth_sendBundle` RPC method.
27    /// Returns the resulting bundle hash on success.
28    fn send_bundle(
29        &self,
30        bundle: EthSendBundle,
31    ) -> MevBuilder<(EthSendBundle,), Option<EthBundleHash>>;
32
33    /// Cancels a previously sent MEV bundle using the `eth_cancelBundle` RPC method.
34    fn cancel_bundle(&self, replacement_uuid: String) -> MevBuilder<(EthCancelBundle,), ()>;
35
36    /// Sends blob transaction permutations using the `eth_sendBlobs` RPC method.
37    fn send_blobs(&self, blobs: EthSendBlobs) -> MevBuilder<(EthSendBlobs,), ()>;
38
39    /// Sends a private transaction using the `eth_sendPrivateTransaction` RPC method.
40    fn send_private_transaction(
41        &self,
42        private_tx: EthSendPrivateTransaction,
43    ) -> MevBuilder<(EthSendPrivateTransaction,), Option<TxHash>>;
44
45    /// Sends a private transaction using the `eth_sendPrivateRawTransaction` RPC method.
46    fn send_private_raw_transaction(
47        &self,
48        encoded_tx: &[u8],
49        preferences: Option<PrivateTransactionPreferences>,
50    ) -> MevBuilder<(String, Option<PrivateTransactionPreferences>), Option<TxHash>>;
51
52    /// Cancels a previously sent private transaction using the `eth_cancelPrivateTransaction` RPC
53    /// method.
54    fn cancel_private_transaction(
55        &self,
56        tx_hash: TxHash,
57    ) -> MevBuilder<(EthCancelPrivateTransaction,), bool>;
58
59    /// Sends end-of-block bundle using the `eth_sendEndOfBlockBundle` RPC method.
60    /// Returns the resulting bundle hash on success.
61    fn send_end_of_block_bundle(
62        &self,
63        bundle: EthSendEndOfBlockBundle,
64    ) -> MevBuilder<(EthSendEndOfBlockBundle,), Option<EthBundleHash>>;
65
66    /// Sends a MEV bundle using the `mev_sendBundle` RPC method.
67    /// Returns the resulting bundle hash on success.
68    fn send_mev_bundle(
69        &self,
70        bundle: MevSendBundle,
71    ) -> MevBuilder<(MevSendBundle,), Option<EthBundleHash>>;
72}
73
74#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
75#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
76impl<N, P> MevApi<N> for P
77where
78    N: Network,
79    P: Provider<N>,
80{
81    fn call_bundle(
82        &self,
83        bundle: EthCallBundle,
84    ) -> MevBuilder<(EthCallBundle,), Option<EthCallBundleResponse>> {
85        MevBuilder::new_rpc(self.client().request("eth_callBundle", (bundle,)))
86    }
87
88    fn send_bundle(
89        &self,
90        bundle: EthSendBundle,
91    ) -> MevBuilder<(EthSendBundle,), Option<EthBundleHash>> {
92        MevBuilder::new_rpc(self.client().request("eth_sendBundle", (bundle,)))
93    }
94
95    fn cancel_bundle(&self, replacement_uuid: String) -> MevBuilder<(EthCancelBundle,), ()> {
96        MevBuilder::new_rpc(
97            self.client().request("eth_cancelBundle", (EthCancelBundle { replacement_uuid },)),
98        )
99    }
100
101    fn send_blobs(&self, blobs: EthSendBlobs) -> MevBuilder<(EthSendBlobs,), ()> {
102        MevBuilder::new_rpc(self.client().request("eth_sendBlobs", (blobs,)))
103    }
104
105    fn send_private_transaction(
106        &self,
107        private_tx: EthSendPrivateTransaction,
108    ) -> MevBuilder<(EthSendPrivateTransaction,), Option<TxHash>> {
109        MevBuilder::new_rpc(self.client().request("eth_sendPrivateTransaction", (private_tx,)))
110    }
111
112    fn send_private_raw_transaction(
113        &self,
114        encoded_tx: &[u8],
115        preferences: Option<PrivateTransactionPreferences>,
116    ) -> MevBuilder<(String, Option<PrivateTransactionPreferences>), Option<TxHash>> {
117        let rlp_hex = hex::encode_prefixed(encoded_tx);
118        MevBuilder::new_rpc(
119            self.client().request("eth_sendPrivateRawTransaction", (rlp_hex, preferences)),
120        )
121    }
122
123    fn cancel_private_transaction(
124        &self,
125        tx_hash: TxHash,
126    ) -> MevBuilder<(EthCancelPrivateTransaction,), bool> {
127        MevBuilder::new_rpc(
128            self.client().request(
129                "eth_cancelPrivateTransaction",
130                (EthCancelPrivateTransaction { tx_hash },),
131            ),
132        )
133    }
134
135    fn send_end_of_block_bundle(
136        &self,
137        bundle: EthSendEndOfBlockBundle,
138    ) -> MevBuilder<(EthSendEndOfBlockBundle,), Option<EthBundleHash>> {
139        MevBuilder::new_rpc(self.client().request("eth_sendEndOfBlockBundle", (bundle,)))
140    }
141
142    fn send_mev_bundle(
143        &self,
144        bundle: MevSendBundle,
145    ) -> MevBuilder<(MevSendBundle,), Option<EthBundleHash>> {
146        MevBuilder::new_rpc(self.client().request("mev_sendBundle", (bundle,)))
147    }
148}