mod with_auth;
pub use self::with_auth::{
sign_flashbots_payload, verify_flashbots_signature, FlashbotsSignatureError, MevBuilder,
};
use crate::Provider;
use alloy_network::Network;
use alloy_primitives::{hex, TxHash};
use alloy_rpc_types_mev::{
EthBundleHash, EthCallBundle, EthCallBundleResponse, EthCancelBundle,
EthCancelPrivateTransaction, EthSendBlobs, EthSendBundle, EthSendEndOfBlockBundle,
EthSendPrivateTransaction, MevSendBundle, PrivateTransactionPreferences,
};
pub const FLASHBOTS_SIGNATURE_HEADER: &str = "x-flashbots-signature";
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
pub trait MevApi<N>: Send + Sync {
fn call_bundle(
&self,
bundle: EthCallBundle,
) -> MevBuilder<(EthCallBundle,), Option<EthCallBundleResponse>>;
fn send_bundle(
&self,
bundle: EthSendBundle,
) -> MevBuilder<(EthSendBundle,), Option<EthBundleHash>>;
fn cancel_bundle(&self, replacement_uuid: String) -> MevBuilder<(EthCancelBundle,), ()>;
fn send_blobs(&self, blobs: EthSendBlobs) -> MevBuilder<(EthSendBlobs,), ()>;
fn send_private_transaction(
&self,
private_tx: EthSendPrivateTransaction,
) -> MevBuilder<(EthSendPrivateTransaction,), Option<TxHash>>;
fn send_private_raw_transaction(
&self,
encoded_tx: &[u8],
preferences: Option<PrivateTransactionPreferences>,
) -> MevBuilder<(String, Option<PrivateTransactionPreferences>), Option<TxHash>>;
fn cancel_private_transaction(
&self,
tx_hash: TxHash,
) -> MevBuilder<(EthCancelPrivateTransaction,), bool>;
fn send_end_of_block_bundle(
&self,
bundle: EthSendEndOfBlockBundle,
) -> MevBuilder<(EthSendEndOfBlockBundle,), Option<EthBundleHash>>;
fn send_mev_bundle(
&self,
bundle: MevSendBundle,
) -> MevBuilder<(MevSendBundle,), Option<EthBundleHash>>;
}
#[cfg_attr(target_family = "wasm", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait::async_trait)]
impl<N, P> MevApi<N> for P
where
N: Network,
P: Provider<N>,
{
fn call_bundle(
&self,
bundle: EthCallBundle,
) -> MevBuilder<(EthCallBundle,), Option<EthCallBundleResponse>> {
MevBuilder::new_rpc(self.client().request("eth_callBundle", (bundle,)))
}
fn send_bundle(
&self,
bundle: EthSendBundle,
) -> MevBuilder<(EthSendBundle,), Option<EthBundleHash>> {
MevBuilder::new_rpc(self.client().request("eth_sendBundle", (bundle,)))
}
fn cancel_bundle(&self, replacement_uuid: String) -> MevBuilder<(EthCancelBundle,), ()> {
MevBuilder::new_rpc(
self.client().request("eth_cancelBundle", (EthCancelBundle { replacement_uuid },)),
)
}
fn send_blobs(&self, blobs: EthSendBlobs) -> MevBuilder<(EthSendBlobs,), ()> {
MevBuilder::new_rpc(self.client().request("eth_sendBlobs", (blobs,)))
}
fn send_private_transaction(
&self,
private_tx: EthSendPrivateTransaction,
) -> MevBuilder<(EthSendPrivateTransaction,), Option<TxHash>> {
MevBuilder::new_rpc(self.client().request("eth_sendPrivateTransaction", (private_tx,)))
}
fn send_private_raw_transaction(
&self,
encoded_tx: &[u8],
preferences: Option<PrivateTransactionPreferences>,
) -> MevBuilder<(String, Option<PrivateTransactionPreferences>), Option<TxHash>> {
let rlp_hex = hex::encode_prefixed(encoded_tx);
MevBuilder::new_rpc(
self.client().request("eth_sendPrivateRawTransaction", (rlp_hex, preferences)),
)
}
fn cancel_private_transaction(
&self,
tx_hash: TxHash,
) -> MevBuilder<(EthCancelPrivateTransaction,), bool> {
MevBuilder::new_rpc(
self.client().request(
"eth_cancelPrivateTransaction",
(EthCancelPrivateTransaction { tx_hash },),
),
)
}
fn send_end_of_block_bundle(
&self,
bundle: EthSendEndOfBlockBundle,
) -> MevBuilder<(EthSendEndOfBlockBundle,), Option<EthBundleHash>> {
MevBuilder::new_rpc(self.client().request("eth_sendEndOfBlockBundle", (bundle,)))
}
fn send_mev_bundle(
&self,
bundle: MevSendBundle,
) -> MevBuilder<(MevSendBundle,), Option<EthBundleHash>> {
MevBuilder::new_rpc(self.client().request("mev_sendBundle", (bundle,)))
}
}