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(provider): expose ProviderBuilder via fn builder() #858

Merged
merged 4 commits into from
Jun 11, 2024
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
2 changes: 1 addition & 1 deletion crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ mod tests {
let my_state_builder = my_contract.myState();
assert_eq!(my_state_builder.calldata()[..], MyContract::myStateCall {}.abi_encode(),);
let result: MyContract::myStateReturn = my_state_builder.call().await.unwrap();
assert!(result._0);
assert!(result.myState);

let do_stuff_builder = my_contract.doStuff(U256::from(0x69), true);
assert_eq!(
Expand Down
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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be ethereum network like in ProviderBuilder::new?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would create cyclic deps

Copy link
Member

@DaniPopes DaniPopes Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How? It's right there in the imports in this file

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, I read that as it should be "in ethereum network", as in the network crate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be ethereum network like in ProviderBuilder::new?

Cannot set defaults in a free function.

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 @@ -51,6 +51,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()
}
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved

/// Returns the RPC client used to send requests.
///
/// NOTE: this method should not be overridden.
Expand Down Expand Up @@ -929,7 +937,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 @@ -938,6 +947,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