Skip to content

Commit

Permalink
fix(provider): use BoxTransport in on_anvil_* (#1693)
Browse files Browse the repository at this point in the history
* fix(`provider`): use BoxTransport in `on_anvil_*`

* fix: on_anvil_with_wallet

* docs
  • Loading branch information
yash-atreya authored Nov 26, 2024
1 parent c5d429f commit 36fd778
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 60 deletions.
6 changes: 2 additions & 4 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,6 @@ mod tests {
};
use alloy_rpc_types_eth::AccessListItem;
use alloy_sol_types::sol;
use alloy_transport_http::Http;
use reqwest::Client;

#[test]
fn empty_constructor() {
Expand Down Expand Up @@ -624,8 +622,8 @@ mod tests {
/// Creates a new call_builder to test field modifications, taken from [call_encoding]
#[allow(clippy::type_complexity)]
fn build_call_builder() -> CallBuilder<
Http<Client>,
AnvilProvider<RootProvider<Http<Client>>, Http<Client>>,
alloy_transport::BoxTransport,
AnvilProvider<RootProvider<alloy_transport::BoxTransport>, alloy_transport::BoxTransport>,
PhantomData<MyContract::doStuffCall>,
> {
let provider = ProviderBuilder::new().on_anvil();
Expand Down
83 changes: 29 additions & 54 deletions crates/provider/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,113 +373,86 @@ type AnvilProviderResult<T> = Result<T, alloy_node_bindings::NodeError>;
// `reqwest` feature is enabled.
#[cfg(any(test, feature = "anvil-node"))]
impl<L, F> ProviderBuilder<L, F, Ethereum> {
/// Build this provider with anvil, using an Reqwest HTTP transport.
/// Build this provider with anvil, using the BoxTransport.
pub fn on_anvil(self) -> F::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Ethereum> + ProviderLayer<L::Provider, BoxTransport, Ethereum>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
alloy_transport_http::Http<reqwest::Client>,
>,
alloy_transport_http::Http<reqwest::Client>,
crate::layers::AnvilProvider<crate::provider::RootProvider<BoxTransport>, BoxTransport>,
BoxTransport,
>,
{
self.on_anvil_with_config(std::convert::identity)
}

/// Build this provider with anvil, using an Reqwest HTTP transport. This
/// Build this provider with anvil, using the BoxTransport. This
/// function configures a wallet backed by anvil keys, and is intended for
/// use in tests.
pub fn on_anvil_with_wallet(
self,
) -> <JoinedEthereumWalletFiller<F> as ProviderLayer<
L::Provider,
alloy_transport_http::Http<reqwest::Client>,
>>::Provider
) -> <JoinedEthereumWalletFiller<F> as ProviderLayer<L::Provider, BoxTransport>>::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Ethereum> + ProviderLayer<L::Provider, BoxTransport, Ethereum>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
alloy_transport_http::Http<reqwest::Client>,
>,
alloy_transport_http::Http<reqwest::Client>,
crate::layers::AnvilProvider<crate::provider::RootProvider<BoxTransport>, BoxTransport>,
BoxTransport,
>,
{
self.on_anvil_with_wallet_and_config(std::convert::identity)
}

/// Build this provider with anvil, using an Reqwest HTTP transport. The
/// Build this provider with anvil, using the BoxTransport. The
/// given function is used to configure the anvil instance.
pub fn on_anvil_with_config(
self,
f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
) -> F::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Ethereum> + ProviderLayer<L::Provider, BoxTransport, Ethereum>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
alloy_transport_http::Http<reqwest::Client>,
>,
alloy_transport_http::Http<reqwest::Client>,
crate::layers::AnvilProvider<crate::provider::RootProvider<BoxTransport>, BoxTransport>,
BoxTransport,
>,
{
let anvil_layer = crate::layers::AnvilLayer::from(f(Default::default()));
let url = anvil_layer.endpoint_url();

self.layer(anvil_layer).on_http(url)
let rpc_client = ClientBuilder::default().http(url).boxed();

self.layer(anvil_layer).on_client(rpc_client)
}

/// Build this provider with anvil, using an Reqwest HTTP transport.
/// Build this provider with anvil, using the BoxTransport.
/// This calls `try_on_anvil_with_wallet_and_config` and panics on error.
pub fn on_anvil_with_wallet_and_config(
self,
f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
) -> <JoinedEthereumWalletFiller<F> as ProviderLayer<
L::Provider,
alloy_transport_http::Http<reqwest::Client>,
>>::Provider
) -> <JoinedEthereumWalletFiller<F> as ProviderLayer<L::Provider, BoxTransport>>::Provider
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Ethereum> + ProviderLayer<L::Provider, BoxTransport, Ethereum>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
alloy_transport_http::Http<reqwest::Client>,
>,
alloy_transport_http::Http<reqwest::Client>,
crate::layers::AnvilProvider<crate::provider::RootProvider<BoxTransport>, BoxTransport>,
BoxTransport,
>,
{
self.try_on_anvil_with_wallet_and_config(f).unwrap()
}

/// Build this provider with anvil, using an Reqwest HTTP transport. The
/// Build this provider with anvil, using the BoxTransport. The
/// given function is used to configure the anvil instance. This
/// function configures a wallet backed by anvil keys, and is intended for
/// use in tests.
pub fn try_on_anvil_with_wallet_and_config(
self,
f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
) -> AnvilProviderResult<
<JoinedEthereumWalletFiller<F> as ProviderLayer<
L::Provider,
alloy_transport_http::Http<reqwest::Client>,
>>::Provider,
<JoinedEthereumWalletFiller<F> as ProviderLayer<L::Provider, BoxTransport>>::Provider,
>
where
F: TxFiller<Ethereum>
+ ProviderLayer<L::Provider, alloy_transport_http::Http<reqwest::Client>, Ethereum>,
F: TxFiller<Ethereum> + ProviderLayer<L::Provider, BoxTransport, Ethereum>,
L: crate::builder::ProviderLayer<
crate::layers::AnvilProvider<
crate::provider::RootProvider<alloy_transport_http::Http<reqwest::Client>>,
alloy_transport_http::Http<reqwest::Client>,
>,
alloy_transport_http::Http<reqwest::Client>,
crate::layers::AnvilProvider<crate::provider::RootProvider<BoxTransport>, BoxTransport>,
BoxTransport,
>,
{
use alloy_signer::Signer;
Expand All @@ -499,7 +472,9 @@ impl<L, F> ProviderBuilder<L, F, Ethereum> {
wallet.register_signer(alloy_signer_local::LocalSigner::from(key.clone()))
}

Ok(self.wallet(wallet).layer(anvil_layer).on_http(url))
let rpc_client = ClientBuilder::default().http(url).boxed();

Ok(self.wallet(wallet).layer(anvil_layer).on_client(rpc_client))
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ mod tests {

// These blocks are not necessary.
{
let refdyn = &provider as &dyn Provider<alloy_transport_http::Http<reqwest::Client>, _>;
let refdyn = &provider as &dyn Provider<alloy_transport::BoxTransport, _>;
let num = refdyn.get_block_number().await.unwrap();
assert_eq!(0, num);
}
Expand All @@ -1332,7 +1332,7 @@ mod tests {

// Note the `Http` arg, vs no arg (defaulting to `BoxedTransport`) below.
{
let refdyn = &provider as &dyn Provider<alloy_transport_http::Http<reqwest::Client>, _>;
let refdyn = &provider as &dyn Provider<alloy_transport::BoxTransport, _>;
let num = refdyn.get_block_number().await.unwrap();
assert_eq!(0, num);
}
Expand Down

0 comments on commit 36fd778

Please sign in to comment.