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: convenience functions for nonce and gas on ProviderBuilder #378

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 25 additions & 1 deletion crates/provider/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{layers::SignerLayer, Provider, RootProvider};
use crate::{
layers::{GasEstimatorLayer, ManagedNonceLayer, SignerLayer},
Provider, RootProvider,
};
use alloy_network::{Ethereum, Network};
use alloy_rpc_client::RpcClient;
use alloy_transport::Transport;
Expand Down Expand Up @@ -111,6 +114,27 @@ impl<L, N> ProviderBuilder<L, N> {
self.layer(SignerLayer::new(signer))
}

/// Add gas estimation to the stack being built.
///
/// See [`GasEstimatorLayer`]
pub fn with_gas_estimation(self) -> ProviderBuilder<Stack<GasEstimatorLayer, L>, N> {
self.layer(GasEstimatorLayer)
}

/// Add nonce management to the stack being built.
///
/// See [`ManagedNonceLayer`]
pub fn with_nonce_management(self) -> ProviderBuilder<Stack<ManagedNonceLayer, L>, N> {
self.layer(ManagedNonceLayer)
}

/// Add preconfigured set of layers handling gas estimation and nonce management
pub fn with_recommended_layers(
self,
) -> ProviderBuilder<Stack<ManagedNonceLayer, Stack<GasEstimatorLayer, L>>, N> {
self.with_gas_estimation().with_nonce_management()
}

/// Change the network.
///
/// By default, the network is `Ethereum`. This method must be called to configure a different
Expand Down
14 changes: 7 additions & 7 deletions crates/provider/src/layers/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::{layers::ManagedNonceLayer, ProviderBuilder};
use crate::ProviderBuilder;
use alloy_network::EthereumSigner;
use alloy_node_bindings::Anvil;
use alloy_primitives::{address, U128};
Expand All @@ -199,8 +199,8 @@ mod tests {
let wallet = alloy_signer_wallet::Wallet::from(anvil.keys()[0].clone());

let provider = ProviderBuilder::new()
.layer(ManagedNonceLayer)
.layer(GasEstimatorLayer)
.with_nonce_management()
.with_gas_estimation()
.signer(EthereumSigner::from(wallet))
.provider(RootProvider::new(RpcClient::new(http, true)));

Expand Down Expand Up @@ -233,8 +233,8 @@ mod tests {
let wallet = alloy_signer_wallet::Wallet::from(anvil.keys()[0].clone());

let provider = ProviderBuilder::new()
.layer(ManagedNonceLayer)
.layer(GasEstimatorLayer)
.with_nonce_management()
.with_gas_estimation()
.signer(EthereumSigner::from(wallet))
.provider(RootProvider::new(RpcClient::new(http, true)));

Expand Down Expand Up @@ -265,8 +265,8 @@ mod tests {
let wallet = alloy_signer_wallet::Wallet::from(anvil.keys()[0].clone());

let provider = ProviderBuilder::new()
.layer(ManagedNonceLayer)
.layer(GasEstimatorLayer)
.with_nonce_management()
.with_gas_estimation()
.signer(EthereumSigner::from(wallet))
.provider(RootProvider::new(RpcClient::new(http, true)));

Expand Down
4 changes: 2 additions & 2 deletions crates/provider/src/layers/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod tests {
let wallet = alloy_signer_wallet::Wallet::from(anvil.keys()[0].clone());

let provider = ProviderBuilder::new()
.layer(ManagedNonceLayer)
.with_nonce_management()
.signer(EthereumSigner::from(wallet))
.provider(RootProvider::new(RpcClient::new(http, true)));

Expand All @@ -176,7 +176,7 @@ mod tests {
let wallet = alloy_signer_wallet::Wallet::from(anvil.keys()[0].clone());

let provider = ProviderBuilder::new()
.layer(ManagedNonceLayer)
.with_nonce_management()
.signer(EthereumSigner::from(wallet))
.provider(RootProvider::new(RpcClient::new(http, true)));

Expand Down
Loading