diff --git a/crates/provider/src/lib.rs b/crates/provider/src/lib.rs index 682999ca153..565bc82048e 100644 --- a/crates/provider/src/lib.rs +++ b/crates/provider/src/lib.rs @@ -40,7 +40,8 @@ pub use heart::{PendingTransaction, PendingTransactionBuilder, PendingTransactio mod provider; pub use provider::{ - EthCall, FilterPollerBuilder, Provider, RootProvider, RpcWithBlock, SendableTx, WalletProvider, + builder, EthCall, FilterPollerBuilder, Provider, RootProvider, RpcWithBlock, SendableTx, + WalletProvider, }; pub mod utils; diff --git a/crates/provider/src/provider/mod.rs b/crates/provider/src/provider/mod.rs index 9ff0dd67e9c..0d8e939ed5a 100644 --- a/crates/provider/src/provider/mod.rs +++ b/crates/provider/src/provider/mod.rs @@ -2,7 +2,7 @@ mod call; pub use call::EthCall; mod root; -pub use root::RootProvider; +pub use root::{builder, RootProvider}; mod sendable; pub use sendable::SendableTx; diff --git a/crates/provider/src/provider/root.rs b/crates/provider/src/provider/root.rs index d1e24878d52..29883aacdfa 100644 --- a/crates/provider/src/provider/root.rs +++ b/crates/provider/src/provider/root.rs @@ -1,6 +1,7 @@ use crate::{ chain::ChainStreamPoller, heart::{Heartbeat, HeartbeatHandle}, + Identity, ProviderBuilder, }; use alloy_network::{Ethereum, Network}; use alloy_rpc_client::{BuiltInConnectionString, ClientBuilder, ClientRef, RpcClient, WeakClient}; @@ -36,6 +37,12 @@ impl fmt::Debug for RootProvider { } } +/// Helper function to directly access [`ProviderBuilder`] with minimal +/// generics. +pub fn builder() -> ProviderBuilder { + ProviderBuilder::default() +} + #[cfg(feature = "reqwest")] impl RootProvider, N> { /// Creates a new HTTP root provider from the given URL. diff --git a/crates/provider/src/provider/trait.rs b/crates/provider/src/provider/trait.rs index e03eb8f8d70..4d66fd9a6a6 100644 --- a/crates/provider/src/provider/trait.rs +++ b/crates/provider/src/provider/trait.rs @@ -2,8 +2,8 @@ use crate::{ utils::{self, Eip1559Estimation, EstimatorFunction}, - EthCall, PendingTransaction, PendingTransactionBuilder, PendingTransactionConfig, RootProvider, - RpcWithBlock, SendableTx, + EthCall, Identity, PendingTransaction, PendingTransactionBuilder, PendingTransactionConfig, + ProviderBuilder, RootProvider, RpcWithBlock, SendableTx, }; use alloy_eips::eip2718::Encodable2718; use alloy_json_rpc::{RpcError, RpcParam, RpcReturn}; @@ -70,6 +70,14 @@ pub trait Provider: /// Returns the root provider. fn root(&self) -> &RootProvider; + /// Returns the [`ProviderBuilder`](crate::ProviderBuilder) to build on. + fn builder() -> ProviderBuilder + where + Self: Sized, + { + ProviderBuilder::default() + } + /// Returns the RPC client used to send requests. /// /// NOTE: this method should not be overridden. @@ -948,7 +956,8 @@ impl Provider for RootProvider { #[cfg(test)] mod tests { use super::*; - use crate::{ProviderBuilder, WalletProvider}; + use crate::{builder, ProviderBuilder, WalletProvider}; + use alloy_network::AnyNetwork; use alloy_node_bindings::Anvil; use alloy_primitives::{address, b256, bytes}; use alloy_rpc_types_eth::request::TransactionRequest; @@ -957,6 +966,33 @@ mod tests { let _ = tracing_subscriber::fmt::try_init(); } + #[tokio::test] + async fn test_provider_builder() { + init_tracing(); + let provider = + RootProvider::::builder().with_recommended_fillers().on_anvil(); + let num = provider.get_block_number().await.unwrap(); + assert_eq!(0, num); + } + + #[tokio::test] + async fn test_builder_helper_fn() { + init_tracing(); + let provider = builder().with_recommended_fillers().on_anvil(); + let num = provider.get_block_number().await.unwrap(); + assert_eq!(0, num); + } + + #[tokio::test] + async fn test_builder_helper_fn_any_network() { + init_tracing(); + let anvil = Anvil::new().spawn(); + let provider = + builder::().with_recommended_fillers().on_http(anvil.endpoint_url()); + let num = provider.get_block_number().await.unwrap(); + assert_eq!(0, num); + } + #[cfg(feature = "reqwest")] #[tokio::test] async fn object_safety() {