From 9a33128261e4c1c48d7e124758758aa218024b20 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 13 Mar 2024 22:05:24 +0100 Subject: [PATCH 1/2] feat: default to Ethereum network in `ProviderBuilder` --- crates/provider/src/builder.rs | 10 ++++------ crates/provider/src/layers/nonce.rs | 13 +++++-------- crates/provider/src/layers/signer.rs | 11 +++-------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/crates/provider/src/builder.rs b/crates/provider/src/builder.rs index a78df9bea45..7141e18fd45 100644 --- a/crates/provider/src/builder.rs +++ b/crates/provider/src/builder.rs @@ -1,5 +1,5 @@ use crate::{layers::SignerLayer, Provider, RootProvider}; -use alloy_network::Network; +use alloy_network::{Ethereum, Network}; use alloy_rpc_client::RpcClient; use alloy_transport::Transport; use std::marker::PhantomData; @@ -70,9 +70,8 @@ where /// /// [`tower::ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html #[derive(Debug)] -pub struct ProviderBuilder { +pub struct ProviderBuilder { layer: L, - network: PhantomData, } @@ -99,17 +98,16 @@ impl ProviderBuilder { /// [`tower::ServiceBuilder`]. The first layer added will be the first to /// see the request. /// - /// /// [`tower::ServiceBuilder::layer`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html#method.layer /// [`tower::ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html - pub fn layer(self, layer: Inner) -> ProviderBuilder> { + pub fn layer(self, layer: Inner) -> ProviderBuilder, N> { ProviderBuilder { layer: Stack::new(layer, self.layer), network: PhantomData } } /// Add a signer layer to the stack being built. /// /// See [`SignerLayer`]. - pub fn signer(self, signer: S) -> ProviderBuilder, L>> { + pub fn signer(self, signer: S) -> ProviderBuilder, L>, N> { self.layer(SignerLayer::new(signer)) } diff --git a/crates/provider/src/layers/nonce.rs b/crates/provider/src/layers/nonce.rs index e6801b91d5f..f227a0d2d07 100644 --- a/crates/provider/src/layers/nonce.rs +++ b/crates/provider/src/layers/nonce.rs @@ -26,10 +26,9 @@ use tokio::sync::Mutex; /// /// ```rs /// # async fn test>(transport: T, signer: S) { -/// let provider = ProviderBuilder::<_, Ethereum>::new() +/// let provider = ProviderBuilder::new() /// .layer(ManagedNonceLayer) /// .signer(EthereumSigner::from(signer)) // note the order! -/// .network::() /// .provider(RootProvider::new(transport)); /// /// provider.send_transaction(TransactionRequest::default()).await; @@ -134,7 +133,7 @@ where mod tests { use super::*; use crate::ProviderBuilder; - use alloy_network::{Ethereum, EthereumSigner}; + use alloy_network::EthereumSigner; use alloy_node_bindings::Anvil; use alloy_primitives::{address, U256}; use alloy_rpc_client::RpcClient; @@ -150,10 +149,9 @@ mod tests { let wallet = alloy_signer::Wallet::from(anvil.keys()[0].clone()); - let provider = ProviderBuilder::<_, Ethereum>::new() + let provider = ProviderBuilder::new() .layer(ManagedNonceLayer) .signer(EthereumSigner::from(wallet)) - .network::() .provider(RootProvider::new(RpcClient::new(http, true))); let tx = TransactionRequest { @@ -175,14 +173,13 @@ mod tests { let http = Http::::new(url); let wallet = alloy_signer::Wallet::from(anvil.keys()[0].clone()); - let from = anvil.addresses()[0]; - let provider = ProviderBuilder::<_, Ethereum>::new() + let provider = ProviderBuilder::new() .layer(ManagedNonceLayer) .signer(EthereumSigner::from(wallet)) - .network::() .provider(RootProvider::new(RpcClient::new(http, true))); + let from = anvil.addresses()[0]; let tx = TransactionRequest { from: Some(from), value: Some(U256::from(100)), diff --git a/crates/provider/src/layers/signer.rs b/crates/provider/src/layers/signer.rs index 020c5eee6a6..fd441e4d80a 100644 --- a/crates/provider/src/layers/signer.rs +++ b/crates/provider/src/layers/signer.rs @@ -17,9 +17,8 @@ use std::marker::PhantomData; /// /// ```rs /// # async fn test>(transport: T, signer: S) { -/// let provider = ProviderBuilder::<_, Ethereum>::new() +/// let provider = ProviderBuilder::new() /// .signer(EthereumSigner::from(signer)) -/// .network::() /// .provider(RootProvider::new(transport)); /// /// provider.send_transaction(TransactionRequest::default()).await; @@ -99,7 +98,7 @@ where #[cfg(test)] mod tests { use crate::{Provider, ProviderBuilder, RootProvider}; - use alloy_network::{Ethereum, EthereumSigner}; + use alloy_network::EthereumSigner; use alloy_node_bindings::Anvil; use alloy_primitives::{address, b256, U256, U64}; use alloy_rpc_client::RpcClient; @@ -115,12 +114,8 @@ mod tests { let wallet = alloy_signer::Wallet::from(anvil.keys()[0].clone()); - // can we somehow remove the need for <_, Ethereum>? we NEED to call .network - // note: we need to 1) add <_, Ethereum> 2) layer things, and then 3) call .network before - // we can call provider - let provider = ProviderBuilder::<_, Ethereum>::new() + let provider = ProviderBuilder::new() .signer(EthereumSigner::from(wallet)) - .network::() .provider(RootProvider::new(RpcClient::new(http, true))); let tx = TransactionRequest { From 5d137120eace3a794f4caf7b71449886c42d3537 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 14 Mar 2024 00:32:24 +0100 Subject: [PATCH 2/2] docs --- crates/provider/src/builder.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/provider/src/builder.rs b/crates/provider/src/builder.rs index 7141e18fd45..98c7591a5dd 100644 --- a/crates/provider/src/builder.rs +++ b/crates/provider/src/builder.rs @@ -113,9 +113,8 @@ impl ProviderBuilder { /// Change the network. /// - /// By default, the network is invalid, and contains the unit type `()`. - /// This method MUST be called before the provider is built. The `client` - /// and `provider` methods only exist when the network is valid. + /// By default, the network is `Ethereum`. This method must be called to configure a different + /// network. /// /// ```rust,ignore /// builder.network::()