Skip to content

Commit

Permalink
feat: replace MevApiClient trait (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jul 26, 2023
1 parent 36611df commit d68cf97
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
12 changes: 6 additions & 6 deletions crates/mev-share-rpc-api/src/eth.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use jsonrpsee::proc_macros::rpc;

/// Eth bundle rpc interface.
///
Expand All @@ -10,26 +10,26 @@ use jsonrpsee::{core::RpcResult, proc_macros::rpc};
pub trait EthBundleApi {
/// `eth_sendBundle` can be used to send your bundles to the builder.
#[method(name = "sendBundle")]
async fn send_bundle(&self, request: ()) -> RpcResult<()>;
async fn send_bundle(&self, request: ()) -> jsonrpsee::core::RpcResult<()>;

/// `eth_callBundle` can be used to simulate a bundle against a specific block number, including
/// simulating a bundle at the top of the next block.
#[method(name = "callBundle")]
async fn call_bundle(&self, request: ()) -> RpcResult<()>;
async fn call_bundle(&self, request: ()) -> jsonrpsee::core::RpcResult<()>;

/// `eth_cancelBundle` is used to prevent a submitted bundle from being included on-chain. See [bundle cancellations](https://docs.flashbots.net/flashbots-auction/searchers/advanced/bundle-cancellations) for more information.
#[method(name = "cancelBundle")]
async fn cancel_bundle(&self, request: ()) -> RpcResult<()>;
async fn cancel_bundle(&self, request: ()) -> jsonrpsee::core::RpcResult<()>;

/// `eth_sendPrivateTransaction` is used to send a single transaction to Flashbots. Flashbots will attempt to build a block including the transaction for the next 25 blocks. See [Private Transactions](https://docs.flashbots.net/flashbots-auction/searchers/advanced/private-transaction) for more info.
#[method(name = "sendPrivateTransaction")]
async fn send_private_transaction(&self, request: ()) -> RpcResult<()>;
async fn send_private_transaction(&self, request: ()) -> jsonrpsee::core::RpcResult<()>;

/// The `eth_cancelPrivateTransaction` method stops private transactions from being submitted
/// for future blocks.
///
/// A transaction can only be cancelled if the request is signed by the same key as the
/// eth_sendPrivateTransaction call submitting the transaction in first place.
#[method(name = "cancelPrivateTransaction")]
async fn cancel_private_transaction(&self, request: ()) -> RpcResult<()>;
async fn cancel_private_transaction(&self, request: ()) -> jsonrpsee::core::RpcResult<()>;
}
6 changes: 5 additions & 1 deletion crates/mev-share-rpc-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ pub use clients::*;
#[cfg(feature = "client")]
#[doc(hidden)]
pub mod clients {
pub use crate::{auth::FlashbotsSignerLayer, auth::FlashbotsSigner, eth::EthBundleApiClient, mev::MevApiClient};
pub use crate::{
auth::{FlashbotsSigner, FlashbotsSignerLayer},
eth::EthBundleApiClient,
mev::MevApiClient,
};
}
68 changes: 42 additions & 26 deletions crates/mev-share-rpc-api/src/mev.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
use crate::{SendBundleRequest, SendBundleResponse, SimBundleOverrides, SimBundleResponse};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

/// Mev rpc interface.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "mev"))]
#[cfg_attr(all(feature = "client", feature = "server"), rpc(server, client, namespace = "mev"))]
#[cfg_attr(not(feature = "server"), rpc(client, namespace = "mev"))]
#[async_trait::async_trait]
pub trait MevApi {
/// Submitting bundles to the relay. It takes in a bundle and provides a bundle hash as a return
/// value.
#[method(name = "sendBundle")]
async fn send_bundle(&self, request: SendBundleRequest) -> RpcResult<SendBundleResponse>;
// re-export the rpc server trait
#[cfg(feature = "server")]
pub use rpc::MevApiServer;

/// Similar to `mev_sendBundle` but instead of submitting a bundle to the relay, it returns a
/// simulation result. Only fully matched bundles can be simulated.
#[method(name = "simBundle")]
async fn sim_bundle(
&self,
bundle: SendBundleRequest,
sim_overrides: SimBundleOverrides,
) -> RpcResult<SimBundleResponse>;
/// jsonrpsee generated code.
///
/// This hides the generated client trait which is replaced by the `MevApiClient` trait.
mod rpc {
use crate::{SendBundleRequest, SendBundleResponse, SimBundleOverrides, SimBundleResponse};
use jsonrpsee::proc_macros::rpc;

/// Mev rpc interface.
#[cfg_attr(not(feature = "client"), rpc(server, namespace = "mev"))]
#[cfg_attr(all(feature = "client", feature = "server"), rpc(server, client, namespace = "mev"))]
#[cfg_attr(not(feature = "server"), rpc(client, namespace = "mev"))]
#[async_trait::async_trait]
pub trait MevApi {
/// Submitting bundles to the relay. It takes in a bundle and provides a bundle hash as a
/// return value.
#[method(name = "sendBundle")]
async fn send_bundle(
&self,
request: SendBundleRequest,
) -> jsonrpsee::core::RpcResult<SendBundleResponse>;

/// Similar to `mev_sendBundle` but instead of submitting a bundle to the relay, it returns
/// a simulation result. Only fully matched bundles can be simulated.
#[method(name = "simBundle")]
async fn sim_bundle(
&self,
bundle: SendBundleRequest,
sim_overrides: SimBundleOverrides,
) -> jsonrpsee::core::RpcResult<SimBundleResponse>;
}
}

/// An object safe version of the `MevApiClient` trait.
#[cfg(feature = "client")]
#[async_trait::async_trait]
pub(crate) trait MevApiExt {
pub trait MevApiClient {
/// Submitting bundles to the relay. It takes in a bundle and provides a bundle hash as a return
/// value.
async fn send_bundle(
Expand All @@ -41,28 +56,29 @@ pub(crate) trait MevApiExt {
) -> Result<SimBundleResponse, jsonrpsee::core::Error>;
}

#[cfg(feature = "client")]
#[async_trait::async_trait]
impl<T> MevApiExt for T
impl<T> MevApiClient for T
where
T: MevApiClient + Sync,
T: rpc::MevApiClient + Sync,
{
async fn send_bundle(
&self,
request: SendBundleRequest,
) -> Result<SendBundleResponse, jsonrpsee::core::Error> {
MevApiClient::send_bundle(self, request).await
rpc::MevApiClient::send_bundle(self, request).await
}

async fn sim_bundle(
&self,
bundle: SendBundleRequest,
sim_overrides: SimBundleOverrides,
) -> Result<SimBundleResponse, jsonrpsee::core::Error> {
MevApiClient::sim_bundle(self, bundle, sim_overrides).await
rpc::MevApiClient::sim_bundle(self, bundle, sim_overrides).await
}
}

#[cfg(test)]
#[cfg(all(test, feature = "client"))]
mod tests {
use super::*;
use crate::FlashbotsSignerLayer;
Expand All @@ -71,7 +87,7 @@ mod tests {
use jsonrpsee::http_client::{transport, HttpClientBuilder};

struct Client {
inner: Box<dyn MevApiExt>,
inner: Box<dyn MevApiClient>,
}

#[allow(dead_code)]
Expand Down

0 comments on commit d68cf97

Please sign in to comment.