Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MevApiExt trait #21

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ hyper = "0.14"

## async
futures-util = "0.3"
async-trait = "0.1"

## misc
serde_json = "1.0"
Expand Down
1 change: 1 addition & 0 deletions crates/mev-share-rpc-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ http.workspace = true
hyper = { workspace = true, features = ["stream"] }
tower.workspace = true
futures-util.workspace = true
async-trait.workspace = true


[dev-dependencies]
Expand Down
81 changes: 79 additions & 2 deletions crates/mev-share-rpc-api/src/mev.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

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"))]
Expand All @@ -22,3 +21,81 @@ pub trait MevApi {
sim_overrides: SimBundleOverrides,
) -> RpcResult<SimBundleResponse>;
}

/// An object safe version of the `MevApiClient` trait.
#[async_trait::async_trait]
pub(crate) trait MevApiExt {
/// Submitting bundles to the relay. It takes in a bundle and provides a bundle hash as a return
/// value.
async fn send_bundle(
&self,
request: SendBundleRequest,
) -> Result<SendBundleResponse, jsonrpsee::core::Error>;

/// 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.
async fn sim_bundle(
&self,
bundle: SendBundleRequest,
sim_overrides: SimBundleOverrides,
) -> Result<SimBundleResponse, jsonrpsee::core::Error>;
}

#[async_trait::async_trait]
impl<T> MevApiExt for T
where
T: MevApiClient + Sync,
{
async fn send_bundle(
&self,
request: SendBundleRequest,
) -> Result<SendBundleResponse, jsonrpsee::core::Error> {
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
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::FlashbotsSignerLayer;
use ethers_core::rand::thread_rng;
use ethers_signers::LocalWallet;
use jsonrpsee::http_client::{transport, HttpClientBuilder};

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

#[allow(dead_code)]
async fn assert_mev_api_box() {
let fb_signer = LocalWallet::new(&mut thread_rng());
let http = HttpClientBuilder::default()
.set_middleware(
tower::ServiceBuilder::new()
.map_err(transport::Error::Http)
.layer(FlashbotsSignerLayer::new(fb_signer.clone())),
)
.build("http://localhost:3030")
.unwrap();
let client = Client { inner: Box::new(http) };
client
.inner
.send_bundle(SendBundleRequest {
protocol_version: Default::default(),
inclusion: Default::default(),
bundle_body: vec![],
validity: None,
privacy: None,
})
.await
.unwrap();
}
}
2 changes: 1 addition & 1 deletion crates/mev-share-rpc-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,6 @@ mod tests {
}
"#;
let actual: SimBundleResponse = serde_json::from_str(expected).unwrap();
assert_eq!(actual.success, true);
assert!(actual.success);
}
}