diff --git a/Cargo.lock b/Cargo.lock index 3dc1ac0975..e532b8c0d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -403,6 +403,7 @@ dependencies = [ "alloy-rpc-types-eth", "alloy-transport", "alloy-transport-http", + "alloy-transport-ipc", "alloy-transport-ws", "async-stream", "async-trait", @@ -475,6 +476,7 @@ dependencies = [ "alloy-pubsub", "alloy-transport", "alloy-transport-http", + "alloy-transport-ipc", "alloy-transport-ws", "futures", "pin-project", @@ -8835,7 +8837,9 @@ name = "reth-rpc-builder" version = "1.1.5" dependencies = [ "alloy-eips", + "alloy-network", "alloy-primitives", + "alloy-provider", "alloy-rpc-types-engine", "alloy-rpc-types-eth", "alloy-rpc-types-trace", diff --git a/crates/rpc/rpc-builder/Cargo.toml b/crates/rpc/rpc-builder/Cargo.toml index 79ff504dfc..4ed3049376 100644 --- a/crates/rpc/rpc-builder/Cargo.toml +++ b/crates/rpc/rpc-builder/Cargo.toml @@ -48,6 +48,8 @@ thiserror.workspace = true tracing.workspace = true tokio-util = { workspace = true } tokio = { workspace = true, features = ["rt", "rt-multi-thread"] } +alloy-provider = { workspace = true, features = ["ws", "ipc"] } +alloy-network.workspace = true [dev-dependencies] reth-primitives-traits.workspace = true diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 7b422f3eed..0b45b24313 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -199,6 +199,7 @@ use std::{ }; use crate::{auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcRequestMetrics}; +use alloy_provider::{fillers::RecommendedFillers, Provider, ProviderBuilder}; use error::{ConflictingModules, RpcError, ServerKind}; use eth::DynEthApiBuilder; use http::{header::AUTHORIZATION, HeaderMap}; @@ -2331,6 +2332,78 @@ impl RpcServerHandle { let client = builder.build(url).await.expect("failed to create ws client"); Some(client) } + + /// Returns a new [`alloy_network::Ethereum`] http provider with its recommended fillers. + pub fn eth_http_provider( + &self, + ) -> Option + Clone + Unpin + 'static> { + self.new_http_provider_for() + } + + /// Returns an http provider from the rpc server handle for the + /// specified [`alloy_network::Network`]. + /// + /// This installs the recommended fillers: [`RecommendedFillers`] + pub fn new_http_provider_for(&self) -> Option + Clone + Unpin + 'static> + where + N: RecommendedFillers, + { + let rpc_url = self.http_url()?; + let provider = ProviderBuilder::default() + .with_recommended_fillers() + .on_http(rpc_url.parse().expect("valid url")); + Some(provider) + } + + /// Returns a new [`alloy_network::Ethereum`] websocket provider with its recommended fillers. + pub async fn eth_ws_provider( + &self, + ) -> Option + Clone + Unpin + 'static> { + self.new_ws_provider_for().await + } + + /// Returns an ws provider from the rpc server handle for the + /// specified [`alloy_network::Network`]. + /// + /// This installs the recommended fillers: [`RecommendedFillers`] + pub async fn new_ws_provider_for(&self) -> Option + Clone + Unpin + 'static> + where + N: RecommendedFillers, + { + let rpc_url = self.ws_url()?; + let provider = ProviderBuilder::default() + .with_recommended_fillers() + .on_builtin(&rpc_url) + .await + .expect("failed to create ws client"); + Some(provider) + } + + /// Returns a new [`alloy_network::Ethereum`] ipc provider with its recommended fillers. + pub async fn eth_ipc_provider( + &self, + ) -> Option + Clone + Unpin + 'static> { + self.new_ws_provider_for().await + } + + /// Returns an ipc provider from the rpc server handle for the + /// specified [`alloy_network::Network`]. + /// + /// This installs the recommended fillers: [`RecommendedFillers`] + pub async fn new_ipc_provider_for( + &self, + ) -> Option + Clone + Unpin + 'static> + where + N: RecommendedFillers, + { + let rpc_url = self.ipc_endpoint()?; + let provider = ProviderBuilder::default() + .with_recommended_fillers() + .on_builtin(&rpc_url) + .await + .expect("failed to create ipc client"); + Some(provider) + } } #[cfg(test)]