added helper function new_alloy_provider (#13579)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
abdaze
2025-01-31 20:17:31 +05:30
committed by GitHub
parent 2d3bbb327d
commit fe160e74df
3 changed files with 79 additions and 0 deletions

4
Cargo.lock generated
View File

@ -403,6 +403,7 @@ dependencies = [
"alloy-rpc-types-eth", "alloy-rpc-types-eth",
"alloy-transport", "alloy-transport",
"alloy-transport-http", "alloy-transport-http",
"alloy-transport-ipc",
"alloy-transport-ws", "alloy-transport-ws",
"async-stream", "async-stream",
"async-trait", "async-trait",
@ -475,6 +476,7 @@ dependencies = [
"alloy-pubsub", "alloy-pubsub",
"alloy-transport", "alloy-transport",
"alloy-transport-http", "alloy-transport-http",
"alloy-transport-ipc",
"alloy-transport-ws", "alloy-transport-ws",
"futures", "futures",
"pin-project", "pin-project",
@ -8835,7 +8837,9 @@ name = "reth-rpc-builder"
version = "1.1.5" version = "1.1.5"
dependencies = [ dependencies = [
"alloy-eips", "alloy-eips",
"alloy-network",
"alloy-primitives", "alloy-primitives",
"alloy-provider",
"alloy-rpc-types-engine", "alloy-rpc-types-engine",
"alloy-rpc-types-eth", "alloy-rpc-types-eth",
"alloy-rpc-types-trace", "alloy-rpc-types-trace",

View File

@ -48,6 +48,8 @@ thiserror.workspace = true
tracing.workspace = true tracing.workspace = true
tokio-util = { workspace = true } tokio-util = { workspace = true }
tokio = { workspace = true, features = ["rt", "rt-multi-thread"] } tokio = { workspace = true, features = ["rt", "rt-multi-thread"] }
alloy-provider = { workspace = true, features = ["ws", "ipc"] }
alloy-network.workspace = true
[dev-dependencies] [dev-dependencies]
reth-primitives-traits.workspace = true reth-primitives-traits.workspace = true

View File

@ -199,6 +199,7 @@ use std::{
}; };
use crate::{auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcRequestMetrics}; use crate::{auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcRequestMetrics};
use alloy_provider::{fillers::RecommendedFillers, Provider, ProviderBuilder};
use error::{ConflictingModules, RpcError, ServerKind}; use error::{ConflictingModules, RpcError, ServerKind};
use eth::DynEthApiBuilder; use eth::DynEthApiBuilder;
use http::{header::AUTHORIZATION, HeaderMap}; use http::{header::AUTHORIZATION, HeaderMap};
@ -2331,6 +2332,78 @@ impl RpcServerHandle {
let client = builder.build(url).await.expect("failed to create ws client"); let client = builder.build(url).await.expect("failed to create ws client");
Some(client) Some(client)
} }
/// Returns a new [`alloy_network::Ethereum`] http provider with its recommended fillers.
pub fn eth_http_provider(
&self,
) -> Option<impl Provider<alloy_network::Ethereum> + 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<N>(&self) -> Option<impl Provider<N> + Clone + Unpin + 'static>
where
N: RecommendedFillers<RecommendedFillers: Unpin>,
{
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<impl Provider<alloy_network::Ethereum> + 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<N>(&self) -> Option<impl Provider<N> + Clone + Unpin + 'static>
where
N: RecommendedFillers<RecommendedFillers: Unpin>,
{
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<impl Provider<alloy_network::Ethereum> + 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<N>(
&self,
) -> Option<impl Provider<N> + Clone + Unpin + 'static>
where
N: RecommendedFillers<RecommendedFillers: Unpin>,
{
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)] #[cfg(test)]