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

fix(provider): use BoxTransport in on_anvil_* #1693

Merged
merged 3 commits into from
Nov 26, 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
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