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,
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
100#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
101#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
102impl<N, P> MevApi<N> for P
103where
104 N: Network,
105 P: Provider<N>,
106{
107 fn call_bundle(
108 &self,
109 bundle: EthCallBundle,
110 ) -> MevBuilder<(EthCallBundle,), Option<EthCallBundleResponse>> {
111 MevBuilder::new_rpc(self.client().request("eth_callBundle", (bundle,)))
112 }
113
114 fn send_bundle(
115 &self,
116 bundle: EthSendBundle,
117 ) -> MevBuilder<(EthSendBundle,), Option<EthBundleHash>> {
118 MevBuilder::new_rpc(self.client().request("eth_sendBundle", (bundle,)))
119 }
120
121 fn cancel_bundle(&self, replacement_uuid: String) -> MevBuilder<(EthCancelBundle,), ()> {
122 MevBuilder::new_rpc(
123 self.client().request("eth_cancelBundle", (EthCancelBundle { replacement_uuid },)),
124 )
125 }
126
127 fn send_blobs(&self, blobs: EthSendBlobs) -> MevBuilder<(EthSendBlobs,), ()> {
128 MevBuilder::new_rpc(self.client().request("eth_sendBlobs", (blobs,)))
129 }
130
131 fn send_private_transaction(
132 &self,
133 private_tx: EthSendPrivateTransaction,
134 ) -> MevBuilder<(EthSendPrivateTransaction,), Option<TxHash>> {
135 MevBuilder::new_rpc(self.client().request("eth_sendPrivateTransaction", (private_tx,)))
136 }
137
138 fn send_private_raw_transaction(
139 &self,
140 encoded_tx: &[u8],
141 preferences: Option<PrivateTransactionPreferences>,
142 ) -> MevBuilder<(String, Option<PrivateTransactionPreferences>), Option<TxHash>> {
143 let rlp_hex = hex::encode_prefixed(encoded_tx);
144 MevBuilder::new_rpc(
145 self.client().request("eth_sendPrivateRawTransaction", (rlp_hex, preferences)),
146 )
147 }
148
149 fn cancel_private_transaction(
150 &self,
151 tx_hash: TxHash,
152 ) -> MevBuilder<(EthCancelPrivateTransaction,), bool> {
153 MevBuilder::new_rpc(
154 self.client().request(
155 "eth_cancelPrivateTransaction",
156 (EthCancelPrivateTransaction { tx_hash },),
157 ),
158 )
159 }
160
161 fn send_end_of_block_bundle(
162 &self,
163 bundle: EthSendEndOfBlockBundle,
164 ) -> MevBuilder<(EthSendEndOfBlockBundle,), Option<EthBundleHash>> {
165 MevBuilder::new_rpc(self.client().request("eth_sendEndOfBlockBundle", (bundle,)))
166 }
167
168 fn send_mev_bundle(
169 &self,
170 bundle: MevSendBundle,
171 ) -> MevBuilder<(MevSendBundle,), Option<EthBundleHash>> {
172 MevBuilder::new_rpc(self.client().request("mev_sendBundle", (bundle,)))
173 }
174}