Skip to content

Commit

Permalink
feat(provider): expose ProviderBuilder via fn builder() (#858)
Browse files Browse the repository at this point in the history
* feat(provider): expose `ProviderBuilder` via `fn builder()`

* feat(provider): free builder<N>() fn

* nit

* AnyNetwork test for builder fn
  • Loading branch information
yash-atreya authored Jun 11, 2024
1 parent 7578618 commit a096e6e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
3 changes: 2 additions & 1 deletion crates/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions crates/provider/src/provider/root.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -36,6 +37,12 @@ impl<T: fmt::Debug, N> fmt::Debug for RootProvider<T, N> {
}
}

/// Helper function to directly access [`ProviderBuilder`] with minimal
/// generics.
pub fn builder<N: Network>() -> ProviderBuilder<Identity, Identity, N> {
ProviderBuilder::default()
}

#[cfg(feature = "reqwest")]
impl<N: Network> RootProvider<Http<reqwest::Client>, N> {
/// Creates a new HTTP root provider from the given URL.
Expand Down
42 changes: 39 additions & 3 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -70,6 +70,14 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
/// Returns the root provider.
fn root(&self) -> &RootProvider<T, N>;

/// Returns the [`ProviderBuilder`](crate::ProviderBuilder) to build on.
fn builder() -> ProviderBuilder<Identity, Identity, N>
where
Self: Sized,
{
ProviderBuilder::default()
}

/// Returns the RPC client used to send requests.
///
/// NOTE: this method should not be overridden.
Expand Down Expand Up @@ -948,7 +956,8 @@ impl<T: Transport + Clone, N: Network> Provider<T, N> for RootProvider<T, N> {
#[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;
Expand All @@ -957,6 +966,33 @@ mod tests {
let _ = tracing_subscriber::fmt::try_init();
}

#[tokio::test]
async fn test_provider_builder() {
init_tracing();
let provider =
RootProvider::<BoxTransport, Ethereum>::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::<AnyNetwork>().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() {
Expand Down

0 comments on commit a096e6e

Please sign in to comment.