1mod with_auth;
2
3pub use self::with_auth::{
4 sign_flashbots_payload, verify_flashbots_signature, FlashbotsSignatureError, MevBuilder,
5};
6use crate::Provider;
7use alloy_network::Network;
8use alloy_primitives::{hex, TxHash};
9use alloy_rpc_types_mev::{
10 EthBundleHash, EthCallBundle, EthCallBundleResponse, EthCancelBundle,
11 EthCancelPrivateTransaction, EthSendBlobs, EthSendBundle, EthSendEndOfBlockBundle,
12 EthSendPrivateTransaction, MevSendBundle, PrivateTransactionPreferences, SimBundleResponse,
13};
14
15pub const FLASHBOTS_SIGNATURE_HEADER: &str = "x-flashbots-signature";
17
18#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
34#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
35pub trait MevApi<N>: Send + Sync {
36 fn call_bundle(
38 &self,
39 bundle: EthCallBundle,
40 ) -> MevBuilder<(EthCallBundle,), Option<EthCallBundleResponse>>;
41
42 fn send_bundle(
50 &self,
51 bundle: EthSendBundle,
52 ) -> MevBuilder<(EthSendBundle,), Option<EthBundleHash>>;
53
54 fn cancel_bundle(&self, replacement_uuid: String) -> MevBuilder<(EthCancelBundle,), ()>;
56
57 fn send_blobs(&self, blobs: EthSendBlobs) -> MevBuilder<(EthSendBlobs,), ()>;
59
60 fn send_private_transaction(
62 &self,
63 private_tx: EthSendPrivateTransaction,
64 ) -> MevBuilder<(EthSendPrivateTransaction,), Option<TxHash>>;
65
66 fn send_private_raw_transaction(
68 &self,
69 encoded_tx: &[u8],
70 preferences: Option<PrivateTransactionPreferences>,
71 ) -> MevBuilder<(String, Option<PrivateTransactionPreferences>), Option<TxHash>>;
72
73 fn cancel_private_transaction(
76 &self,
77 tx_hash: TxHash,
78 ) -> MevBuilder<(EthCancelPrivateTransaction,), bool>;
79
80 fn send_end_of_block_bundle(
83 &self,
84 bundle: EthSendEndOfBlockBundle,
85 ) -> MevBuilder<(EthSendEndOfBlockBundle,), Option<EthBundleHash>>;
86
87 fn send_mev_bundle(
95 &self,
96 bundle: MevSendBundle,
97 ) -> MevBuilder<(MevSendBundle,), Option<EthBundleHash>>;
98
99 fn sim_mev_bundle(
113 &self,
114 bundle: MevSendBundle,
115 ) -> MevBuilder<(MevSendBundle,), Option<SimBundleResponse>>;
116}
117
118#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
119#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
120impl<N, P> MevApi<N> for P
121where
122 N: Network,
123 P: Provider<N>,
124{
125 fn call_bundle(
126 &self,
127 bundle: EthCallBundle,
128 ) -> MevBuilder<(EthCallBundle,), Option<EthCallBundleResponse>> {
129 MevBuilder::new_rpc(self.client().request("eth_callBundle", (bundle,)))
130 }
131
132 fn send_bundle(
133 &self,
134 bundle: EthSendBundle,
135 ) -> MevBuilder<(EthSendBundle,), Option<EthBundleHash>> {
136 MevBuilder::new_rpc(self.client().request("eth_sendBundle", (bundle,)))
137 }
138
139 fn cancel_bundle(&self, replacement_uuid: String) -> MevBuilder<(EthCancelBundle,), ()> {
140 MevBuilder::new_rpc(
141 self.client().request("eth_cancelBundle", (EthCancelBundle { replacement_uuid },)),
142 )
143 }
144
145 fn send_blobs(&self, blobs: EthSendBlobs) -> MevBuilder<(EthSendBlobs,), ()> {
146 MevBuilder::new_rpc(self.client().request("eth_sendBlobs", (blobs,)))
147 }
148
149 fn send_private_transaction(
150 &self,
151 private_tx: EthSendPrivateTransaction,
152 ) -> MevBuilder<(EthSendPrivateTransaction,), Option<TxHash>> {
153 MevBuilder::new_rpc(self.client().request("eth_sendPrivateTransaction", (private_tx,)))
154 }
155
156 fn send_private_raw_transaction(
157 &self,
158 encoded_tx: &[u8],
159 preferences: Option<PrivateTransactionPreferences>,
160 ) -> MevBuilder<(String, Option<PrivateTransactionPreferences>), Option<TxHash>> {
161 let rlp_hex = hex::encode_prefixed(encoded_tx);
162 MevBuilder::new_rpc(
163 self.client().request("eth_sendPrivateRawTransaction", (rlp_hex, preferences)),
164 )
165 }
166
167 fn cancel_private_transaction(
168 &self,
169 tx_hash: TxHash,
170 ) -> MevBuilder<(EthCancelPrivateTransaction,), bool> {
171 MevBuilder::new_rpc(
172 self.client().request(
173 "eth_cancelPrivateTransaction",
174 (EthCancelPrivateTransaction { tx_hash },),
175 ),
176 )
177 }
178
179 fn send_end_of_block_bundle(
180 &self,
181 bundle: EthSendEndOfBlockBundle,
182 ) -> MevBuilder<(EthSendEndOfBlockBundle,), Option<EthBundleHash>> {
183 MevBuilder::new_rpc(self.client().request("eth_sendEndOfBlockBundle", (bundle,)))
184 }
185
186 fn send_mev_bundle(
187 &self,
188 bundle: MevSendBundle,
189 ) -> MevBuilder<(MevSendBundle,), Option<EthBundleHash>> {
190 MevBuilder::new_rpc(self.client().request("mev_sendBundle", (bundle,)))
191 }
192
193 fn sim_mev_bundle(
194 &self,
195 bundle: MevSendBundle,
196 ) -> MevBuilder<(MevSendBundle,), Option<SimBundleResponse>> {
197 MevBuilder::new_rpc(self.client().request("mev_simBundle", (bundle,)))
198 }
199}