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: default to Ethereum network in ProviderBuilder #304

Merged
merged 2 commits into from
Mar 13, 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
15 changes: 6 additions & 9 deletions crates/provider/src/builder.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -70,9 +70,8 @@ where
///
/// [`tower::ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html
#[derive(Debug)]
pub struct ProviderBuilder<L, N = ()> {
pub struct ProviderBuilder<L, N = Ethereum> {
layer: L,

network: PhantomData<N>,
}

Expand All @@ -99,25 +98,23 @@ impl<L, N> ProviderBuilder<L, N> {
/// [`tower::ServiceBuilder`]. The first layer added will be the first to
Copy link
Member

Choose a reason for hiding this comment

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

maybe add pub fn network<Other: Network>(self) -> ProviderBuilder<Stack<Inner, L>, Other> for lazy users or programmatic switching?

Copy link
Member Author

Choose a reason for hiding this comment

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

Already exists

/// 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.
///
/// ```rust,ignore
/// builder.network::<Arbitrum>()
/// ```
pub fn network<Net: Network>(self) -> ProviderBuilder<L, Net> {
ProviderBuilder { layer: self.layer, network: PhantomData }
}

Copy link
Member Author

@DaniPopes DaniPopes Mar 13, 2024

Choose a reason for hiding this comment

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

Just noticed docs need an update. cc @onbjerg we want Ethereum here by default right?

Copy link
Member

Choose a reason for hiding this comment

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

yes Ethereum by default

/// 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<Inner>(self, layer: Inner) -> ProviderBuilder<Stack<Inner, L>> {
pub fn layer<Inner>(self, layer: Inner) -> ProviderBuilder<Stack<Inner, L>, N> {
Copy link
Member Author

Choose a reason for hiding this comment

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

need to be careful here not to ignore N, previously this would reset it back to ()

ProviderBuilder { layer: Stack::new(layer, self.layer), network: PhantomData }
}

/// Add a signer layer to the stack being built.
///
/// See [`SignerLayer`].
pub fn signer<S>(self, signer: S) -> ProviderBuilder<Stack<SignerLayer<S>, L>> {
pub fn signer<S>(self, signer: S) -> ProviderBuilder<Stack<SignerLayer<S>, L>, N> {
self.layer(SignerLayer::new(signer))
}

/// 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::<Arbitrum>()
Expand Down
13 changes: 5 additions & 8 deletions crates/provider/src/layers/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ use tokio::sync::Mutex;
///
/// ```rs
/// # async fn test<T: Transport + Clone, S: NetworkSigner<Ethereum>>(transport: T, signer: S) {
/// let provider = ProviderBuilder::<_, Ethereum>::new()
/// let provider = ProviderBuilder::new()
/// .layer(ManagedNonceLayer)
/// .signer(EthereumSigner::from(signer)) // note the order!
/// .network::<Ethereum>()
/// .provider(RootProvider::new(transport));
///
/// provider.send_transaction(TransactionRequest::default()).await;
Expand Down Expand Up @@ -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;
Expand All @@ -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::<Ethereum>()
.provider(RootProvider::new(RpcClient::new(http, true)));

let tx = TransactionRequest {
Expand All @@ -175,14 +173,13 @@ mod tests {
let http = Http::<Client>::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::<Ethereum>()
.provider(RootProvider::new(RpcClient::new(http, true)));

let from = anvil.addresses()[0];
let tx = TransactionRequest {
from: Some(from),
value: Some(U256::from(100)),
Expand Down
11 changes: 3 additions & 8 deletions crates/provider/src/layers/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use std::marker::PhantomData;
///
/// ```rs
/// # async fn test<T: Transport + Clone, S: NetworkSigner<Ethereum>>(transport: T, signer: S) {
/// let provider = ProviderBuilder::<_, Ethereum>::new()
/// let provider = ProviderBuilder::new()
/// .signer(EthereumSigner::from(signer))
/// .network::<Ethereum>()
/// .provider(RootProvider::new(transport));
///
/// provider.send_transaction(TransactionRequest::default()).await;
Expand Down Expand Up @@ -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;
Expand All @@ -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<Ethereum>
// 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::<Ethereum>()
.provider(RootProvider::new(RpcClient::new(http, true)));

let tx = TransactionRequest {
Expand Down
Loading