alloy_provider/
builder.rs

1use crate::{
2    fillers::{
3        CachedNonceManager, ChainIdFiller, FillerControlFlow, GasFiller, JoinFill, NonceFiller,
4        NonceManager, RecommendedFillers, SimpleNonceManager, TxFiller, WalletFiller,
5    },
6    layers::{CallBatchLayer, ChainLayer},
7    provider::SendableTx,
8    Provider, RootProvider,
9};
10use alloy_chains::NamedChain;
11use alloy_network::{Ethereum, IntoWallet, Network};
12use alloy_primitives::ChainId;
13use alloy_rpc_client::{ClientBuilder, RpcClient};
14use alloy_transport::{TransportConnect, TransportError, TransportResult};
15use std::marker::PhantomData;
16
17/// A layering abstraction in the vein of [`tower::Layer`]
18///
19/// [`tower::Layer`]: https://docs.rs/tower/latest/tower/trait.Layer.html
20pub trait ProviderLayer<P: Provider<N>, N: Network = Ethereum> {
21    /// The provider constructed by this layer.
22    type Provider: Provider<N>;
23
24    /// Wrap the given provider in the layer's provider.
25    fn layer(&self, inner: P) -> Self::Provider;
26}
27
28/// An identity layer that does nothing.
29#[derive(Clone, Copy, Debug)]
30pub struct Identity;
31
32impl<N> TxFiller<N> for Identity
33where
34    N: Network,
35{
36    type Fillable = ();
37
38    fn status(&self, _tx: &<N as Network>::TransactionRequest) -> FillerControlFlow {
39        FillerControlFlow::Finished
40    }
41
42    fn fill_sync(&self, _tx: &mut SendableTx<N>) {}
43
44    async fn prepare<P>(
45        &self,
46        _provider: &P,
47        _tx: &N::TransactionRequest,
48    ) -> TransportResult<Self::Fillable> {
49        Ok(())
50    }
51
52    async fn fill(
53        &self,
54        _to_fill: Self::Fillable,
55        tx: SendableTx<N>,
56    ) -> TransportResult<SendableTx<N>> {
57        Ok(tx)
58    }
59}
60
61impl<P, N> ProviderLayer<P, N> for Identity
62where
63    N: Network,
64    P: Provider<N>,
65{
66    type Provider = P;
67
68    fn layer(&self, inner: P) -> Self::Provider {
69        inner
70    }
71}
72
73/// A stack of two providers.
74#[derive(Debug)]
75pub struct Stack<Inner, Outer> {
76    inner: Inner,
77    outer: Outer,
78}
79
80impl<Inner, Outer> Stack<Inner, Outer> {
81    /// Create a new `Stack`.
82    pub const fn new(inner: Inner, outer: Outer) -> Self {
83        Self { inner, outer }
84    }
85}
86
87impl<P, N, Inner, Outer> ProviderLayer<P, N> for Stack<Inner, Outer>
88where
89    N: Network,
90    P: Provider<N>,
91    Inner: ProviderLayer<P, N>,
92    Outer: ProviderLayer<Inner::Provider, N>,
93{
94    type Provider = Outer::Provider;
95
96    fn layer(&self, provider: P) -> Self::Provider {
97        let inner = self.inner.layer(provider);
98
99        self.outer.layer(inner)
100    }
101}
102
103/// A builder for constructing a [`Provider`] from various layers.
104///
105/// This type is similar to [`tower::ServiceBuilder`], with extra complication
106/// around maintaining the network and transport types.
107///
108/// The [`ProviderBuilder`] can be instantiated in two ways, using `ProviderBuilder::new()` or
109/// `ProviderBuilder::default()`.
110///
111/// `ProviderBuilder::new()` will create a new [`ProviderBuilder`] with the [`RecommendedFillers`]
112/// enabled, whereas `ProviderBuilder::default()` will instantiate it in its vanilla
113/// [`ProviderBuilder`] form i.e with no fillers enabled.
114///
115/// [`tower::ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html
116#[derive(Debug)]
117pub struct ProviderBuilder<L, F, N = Ethereum> {
118    layer: L,
119    filler: F,
120    network: PhantomData<fn() -> N>,
121}
122
123impl
124    ProviderBuilder<
125        Identity,
126        JoinFill<Identity, <Ethereum as RecommendedFillers>::RecommendedFillers>,
127        Ethereum,
128    >
129{
130    /// Create a new [`ProviderBuilder`] with the recommended filler enabled.
131    ///
132    /// Recommended fillers are preconfigured set of fillers that handle gas estimation, nonce
133    /// management, and chain-id fetching.
134    ///
135    /// Building a provider with this setting enabled will return a [`crate::fillers::FillProvider`]
136    /// with [`crate::utils::JoinedRecommendedFillers`].
137    ///
138    /// You can opt-out of using these fillers by using the `.disable_recommended_fillers()` method.
139    pub fn new() -> Self {
140        ProviderBuilder::default().with_recommended_fillers()
141    }
142
143    /// Opt-out of the recommended fillers by resetting the fillers stack in the
144    /// [`ProviderBuilder`].
145    ///
146    /// This is equivalent to creating the builder using `ProviderBuilder::default()`.
147    pub fn disable_recommended_fillers(self) -> ProviderBuilder<Identity, Identity, Ethereum> {
148        ProviderBuilder { layer: self.layer, filler: Identity, network: self.network }
149    }
150}
151
152impl<N> Default for ProviderBuilder<Identity, Identity, N> {
153    fn default() -> Self {
154        Self { layer: Identity, filler: Identity, network: PhantomData }
155    }
156}
157
158impl ProviderBuilder<Identity, Identity, Ethereum> {
159    /// Create a new [`ProviderBuilder`] with the [`RecommendedFillers`] for the provided
160    /// [`Network`].
161    pub fn new_with_network<Net: RecommendedFillers>(
162    ) -> ProviderBuilder<Identity, JoinFill<Identity, Net::RecommendedFillers>, Net> {
163        ProviderBuilder {
164            layer: Identity,
165            filler: JoinFill::new(Identity, Net::recommended_fillers()),
166            network: PhantomData,
167        }
168    }
169}
170
171impl<L, N: Network> ProviderBuilder<L, Identity, N> {
172    /// Add preconfigured set of layers handling gas estimation, nonce
173    /// management, and chain-id fetching.
174    pub fn with_recommended_fillers(
175        self,
176    ) -> ProviderBuilder<L, JoinFill<Identity, N::RecommendedFillers>, N>
177    where
178        N: RecommendedFillers,
179    {
180        self.filler(N::recommended_fillers())
181    }
182}
183
184impl<L, F, N> ProviderBuilder<L, F, N> {
185    /// Add a layer to the stack being built. This is similar to
186    /// [`tower::ServiceBuilder::layer`].
187    ///
188    /// ## Note:
189    ///
190    /// Layers are added in outer-to-inner order, as in
191    /// [`tower::ServiceBuilder`]. The first layer added will be the first to
192    /// see the request.
193    ///
194    /// [`tower::ServiceBuilder::layer`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html#method.layer
195    /// [`tower::ServiceBuilder`]: https://docs.rs/tower/latest/tower/struct.ServiceBuilder.html
196    pub fn layer<Inner>(self, layer: Inner) -> ProviderBuilder<Stack<Inner, L>, F, N> {
197        ProviderBuilder {
198            layer: Stack::new(layer, self.layer),
199            filler: self.filler,
200            network: PhantomData,
201        }
202    }
203
204    /// Add a transaction filler to the stack being built. Transaction fillers
205    /// are used to fill in missing fields on transactions before they are sent,
206    /// and are all joined to form the outermost layer of the stack.
207    pub fn filler<F2>(self, filler: F2) -> ProviderBuilder<L, JoinFill<F, F2>, N> {
208        ProviderBuilder {
209            layer: self.layer,
210            filler: JoinFill::new(self.filler, filler),
211            network: PhantomData,
212        }
213    }
214
215    /// Change the network.
216    ///
217    /// By default, the network is `Ethereum`. This method must be called to configure a different
218    /// network.
219    ///
220    /// ```ignore
221    /// builder.network::<Arbitrum>()
222    /// ```
223    pub fn network<Net: Network>(self) -> ProviderBuilder<L, F, Net> {
224        ProviderBuilder { layer: self.layer, filler: self.filler, network: PhantomData }
225    }
226
227    /// Add a chain layer to the stack being built. The layer will set
228    /// the client's poll interval based on the average block time for this chain.
229    ///
230    /// Does nothing to the client with a local transport.
231    pub fn with_chain(self, chain: NamedChain) -> ProviderBuilder<Stack<ChainLayer, L>, F, N> {
232        self.layer(ChainLayer::new(chain))
233    }
234
235    // --- Fillers ---
236
237    /// Add gas estimation to the stack being built.
238    ///
239    /// See [`GasFiller`] for more information.
240    pub fn with_gas_estimation(self) -> ProviderBuilder<L, JoinFill<F, GasFiller>, N> {
241        self.filler(GasFiller)
242    }
243
244    /// Add nonce management to the stack being built.
245    ///
246    /// See [`NonceFiller`] for more information.
247    pub fn with_nonce_management<M: NonceManager>(
248        self,
249        nonce_manager: M,
250    ) -> ProviderBuilder<L, JoinFill<F, NonceFiller<M>>, N> {
251        self.filler(NonceFiller::new(nonce_manager))
252    }
253
254    /// Add simple nonce management to the stack being built.
255    ///
256    /// See [`SimpleNonceManager`] for more information.
257    pub fn with_simple_nonce_management(
258        self,
259    ) -> ProviderBuilder<L, JoinFill<F, NonceFiller<SimpleNonceManager>>, N> {
260        self.with_nonce_management(SimpleNonceManager::default())
261    }
262
263    /// Add cached nonce management to the stack being built.
264    ///
265    /// See [`CachedNonceManager`] for more information.
266    pub fn with_cached_nonce_management(
267        self,
268    ) -> ProviderBuilder<L, JoinFill<F, NonceFiller<CachedNonceManager>>, N> {
269        self.with_nonce_management(CachedNonceManager::default())
270    }
271
272    /// Add a chain ID filler to the stack being built. The filler will attempt
273    /// to fetch the chain ID from the provider using
274    /// [`Provider::get_chain_id`]. the first time a transaction is prepared,
275    /// and will cache it for future transactions.
276    pub fn fetch_chain_id(self) -> ProviderBuilder<L, JoinFill<F, ChainIdFiller>, N> {
277        self.filler(ChainIdFiller::default())
278    }
279
280    /// Add a specific chain ID to the stack being built. The filler will
281    /// fill transactions with the provided chain ID, regardless of the chain ID
282    /// that the provider reports via [`Provider::get_chain_id`].
283    pub fn with_chain_id(
284        self,
285        chain_id: ChainId,
286    ) -> ProviderBuilder<L, JoinFill<F, ChainIdFiller>, N> {
287        self.filler(ChainIdFiller::new(Some(chain_id)))
288    }
289
290    /// Add a wallet layer to the stack being built.
291    ///
292    /// See [`WalletFiller`].
293    pub fn wallet<W: IntoWallet<N>>(
294        self,
295        wallet: W,
296    ) -> ProviderBuilder<L, JoinFill<F, WalletFiller<W::NetworkWallet>>, N>
297    where
298        N: Network,
299    {
300        self.filler(WalletFiller::new(wallet.into_wallet()))
301    }
302
303    // --- Layers ---
304
305    /// Aggregate multiple `eth_call` requests into a single batch request using Multicall3.
306    ///
307    /// See [`CallBatchLayer`] for more information.
308    pub fn with_call_batching(self) -> ProviderBuilder<Stack<CallBatchLayer, L>, F, N> {
309        self.layer(CallBatchLayer::new())
310    }
311
312    // --- Build to Provider ---
313
314    /// Finish the layer stack by providing a root [`Provider`], outputting
315    /// the final [`Provider`] type with all stack components.
316    pub fn connect_provider<P>(self, provider: P) -> F::Provider
317    where
318        L: ProviderLayer<P, N>,
319        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
320        P: Provider<N>,
321        N: Network,
322    {
323        let Self { layer, filler, network: PhantomData } = self;
324        let stack = Stack::new(layer, filler);
325        stack.layer(provider)
326    }
327
328    /// Finish the layer stack by providing a root [`Provider`], outputting
329    /// the final [`Provider`] type with all stack components.
330    #[deprecated(since = "0.12.6", note = "use `connect_provider` instead")]
331    pub fn on_provider<P>(self, provider: P) -> F::Provider
332    where
333        L: ProviderLayer<P, N>,
334        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
335        P: Provider<N>,
336        N: Network,
337    {
338        let Self { layer, filler, network: PhantomData } = self;
339        let stack = Stack::new(layer, filler);
340        stack.layer(provider)
341    }
342
343    /// Finish the layer stack by providing a root [`RpcClient`], outputting
344    /// the final [`Provider`] type with all stack components.
345    ///
346    /// This is a convenience function for
347    /// `ProviderBuilder::on_provider(RootProvider::new(client))`.
348    pub fn connect_client(self, client: RpcClient) -> F::Provider
349    where
350        L: ProviderLayer<RootProvider<N>, N>,
351        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
352        N: Network,
353    {
354        self.connect_provider(RootProvider::new(client))
355    }
356
357    /// Finish the layer stack by providing a root [`RpcClient`], outputting
358    /// the final [`Provider`] type with all stack components.
359    ///
360    /// This is a convenience function for
361    /// `ProviderBuilder::on_provider(RootProvider::new(client))`.
362    #[deprecated(since = "0.12.6", note = "use `connect_client` instead")]
363    pub fn on_client(self, client: RpcClient) -> F::Provider
364    where
365        L: ProviderLayer<RootProvider<N>, N>,
366        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
367        N: Network,
368    {
369        self.connect_provider(RootProvider::new(client))
370    }
371
372    /// Finish the layer stack by providing a [`RpcClient`] that mocks responses, outputting
373    /// the final [`Provider`] type with all stack components.
374    ///
375    /// This is a convenience function for
376    /// `ProviderBuilder::on_client(RpcClient::mocked(asserter))`.
377    pub fn connect_mocked_client(self, asserter: alloy_transport::mock::Asserter) -> F::Provider
378    where
379        L: ProviderLayer<RootProvider<N>, N>,
380        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
381        N: Network,
382    {
383        self.connect_client(RpcClient::mocked(asserter))
384    }
385
386    /// Finish the layer stack by providing a [`RpcClient`] that mocks responses, outputting
387    /// the final [`Provider`] type with all stack components.
388    ///
389    /// This is a convenience function for
390    /// `ProviderBuilder::on_client(RpcClient::mocked(asserter))`.
391    #[deprecated(since = "0.12.6", note = "use `connect_mocked_client` instead")]
392    pub fn on_mocked_client(self, asserter: alloy_transport::mock::Asserter) -> F::Provider
393    where
394        L: ProviderLayer<RootProvider<N>, N>,
395        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
396        N: Network,
397    {
398        self.connect_client(RpcClient::mocked(asserter))
399    }
400
401    /// Finish the layer stack by providing a connection string for a built-in
402    /// transport type, outputting the final [`Provider`] type with all stack
403    /// components.
404    #[doc(alias = "on_builtin")]
405    pub async fn connect(self, s: &str) -> Result<F::Provider, TransportError>
406    where
407        L: ProviderLayer<RootProvider<N>, N>,
408        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
409        N: Network,
410    {
411        let client = ClientBuilder::default().connect(s).await?;
412        Ok(self.connect_client(client))
413    }
414
415    /// Finish the layer stack by providing a [`TransportConnect`] instance.
416    pub async fn connect_with<C>(self, connect: &C) -> Result<F::Provider, TransportError>
417    where
418        L: ProviderLayer<RootProvider<N>, N>,
419        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
420        N: Network,
421        C: TransportConnect,
422    {
423        connect
424            .get_transport()
425            .await
426            .map(|t| RpcClient::new(t, connect.is_local()))
427            .map(|client| self.connect_client(client))
428    }
429
430    /// Finish the layer stack by providing a [`PubSubConnect`] instance,
431    /// producing a [`Provider`] with pubsub capabilities.
432    ///
433    /// [`PubSubConnect`]: alloy_pubsub::PubSubConnect
434    #[cfg(feature = "pubsub")]
435    pub async fn connect_pubsub_with<C>(self, connect: C) -> Result<F::Provider, TransportError>
436    where
437        L: ProviderLayer<RootProvider<N>, N>,
438        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
439        N: Network,
440        C: alloy_pubsub::PubSubConnect,
441    {
442        ClientBuilder::default().pubsub(connect).await.map(|client| self.connect_client(client))
443    }
444
445    /// Finish the layer stack by providing a connection string for a built-in
446    /// transport type, outputting the final [`Provider`] type with all stack
447    /// components.
448    #[deprecated = "use `connect` instead"]
449    #[doc(hidden)]
450    pub async fn on_builtin(self, s: &str) -> Result<F::Provider, TransportError>
451    where
452        L: ProviderLayer<RootProvider<N>, N>,
453        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
454        N: Network,
455    {
456        self.connect(s).await
457    }
458
459    /// Build this provider with a websocket connection.
460    #[cfg(feature = "ws")]
461    pub async fn connect_ws(
462        self,
463        connect: alloy_transport_ws::WsConnect,
464    ) -> Result<F::Provider, TransportError>
465    where
466        L: ProviderLayer<RootProvider<N>, N>,
467        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
468        N: Network,
469    {
470        let client = ClientBuilder::default().ws(connect).await?;
471        Ok(self.connect_client(client))
472    }
473
474    /// Build this provider with a websocket connection.
475    #[cfg(feature = "ws")]
476    #[deprecated(since = "0.12.6", note = "use `connect_ws` instead")]
477    pub async fn on_ws(
478        self,
479        connect: alloy_transport_ws::WsConnect,
480    ) -> Result<F::Provider, TransportError>
481    where
482        L: ProviderLayer<RootProvider<N>, N>,
483        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
484        N: Network,
485    {
486        let client = ClientBuilder::default().ws(connect).await?;
487        Ok(self.connect_client(client))
488    }
489
490    /// Build this provider with an IPC connection.
491    #[cfg(feature = "ipc")]
492    pub async fn connect_ipc<T>(
493        self,
494        connect: alloy_transport_ipc::IpcConnect<T>,
495    ) -> Result<F::Provider, TransportError>
496    where
497        alloy_transport_ipc::IpcConnect<T>: alloy_pubsub::PubSubConnect,
498        L: ProviderLayer<RootProvider<N>, N>,
499        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
500        N: Network,
501    {
502        let client = ClientBuilder::default().ipc(connect).await?;
503        Ok(self.connect_client(client))
504    }
505
506    /// Build this provider with an IPC connection.
507    #[cfg(feature = "ipc")]
508    #[deprecated(since = "0.12.6", note = "use `connect_ipc` instead")]
509    pub async fn on_ipc<T>(
510        self,
511        connect: alloy_transport_ipc::IpcConnect<T>,
512    ) -> Result<F::Provider, TransportError>
513    where
514        alloy_transport_ipc::IpcConnect<T>: alloy_pubsub::PubSubConnect,
515        L: ProviderLayer<RootProvider<N>, N>,
516        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
517        N: Network,
518    {
519        let client = ClientBuilder::default().ipc(connect).await?;
520        Ok(self.connect_client(client))
521    }
522
523    /// Build this provider with an Reqwest HTTP transport.
524    #[cfg(any(test, feature = "reqwest"))]
525    pub fn connect_http(self, url: reqwest::Url) -> F::Provider
526    where
527        L: ProviderLayer<crate::RootProvider<N>, N>,
528        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
529        N: Network,
530    {
531        let client = ClientBuilder::default().http(url);
532        self.connect_client(client)
533    }
534
535    /// Build this provider with a pre-built Reqwest client.
536    #[cfg(any(test, feature = "reqwest"))]
537    pub fn connect_reqwest<C>(self, client: C, url: reqwest::Url) -> F::Provider
538    where
539        L: ProviderLayer<crate::RootProvider<N>, N>,
540        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
541        N: Network,
542        C: Into<reqwest::Client>,
543    {
544        let client = ClientBuilder::default().http_with_client(client.into(), url);
545        self.connect_client(client)
546    }
547
548    /// Build this provider with a provided Reqwest client builder.
549    #[cfg(any(test, feature = "reqwest"))]
550    pub fn with_reqwest<B>(self, url: reqwest::Url, builder: B) -> F::Provider
551    where
552        L: ProviderLayer<crate::RootProvider<N>, N>,
553        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
554        N: Network,
555        B: FnOnce(reqwest::ClientBuilder) -> reqwest::Client,
556    {
557        self.connect_reqwest(builder(reqwest::ClientBuilder::default()), url)
558    }
559
560    /// Build this provider with an Reqwest HTTP transport.
561    #[cfg(any(test, feature = "reqwest"))]
562    #[deprecated(since = "0.12.6", note = "use `connect_http` instead")]
563    pub fn on_http(self, url: reqwest::Url) -> F::Provider
564    where
565        L: ProviderLayer<RootProvider<N>, N>,
566        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
567        N: Network,
568    {
569        let client = ClientBuilder::default().http(url);
570        self.connect_client(client)
571    }
572
573    /// Build this provider with an Hyper HTTP transport.
574    #[cfg(feature = "hyper")]
575    pub fn connect_hyper_http(self, url: url::Url) -> F::Provider
576    where
577        L: ProviderLayer<crate::RootProvider<N>, N>,
578        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
579        N: Network,
580    {
581        let client = ClientBuilder::default().hyper_http(url);
582        self.connect_client(client)
583    }
584
585    /// Build this provider with an Hyper HTTP transport.
586    #[cfg(feature = "hyper")]
587    #[deprecated(since = "0.12.6", note = "use `connect_hyper_http` instead")]
588    pub fn on_hyper_http(self, url: url::Url) -> F::Provider
589    where
590        L: ProviderLayer<RootProvider<N>, N>,
591        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
592        N: Network,
593    {
594        let client = ClientBuilder::default().hyper_http(url);
595        self.connect_client(client)
596    }
597}
598
599#[cfg(any(test, feature = "anvil-node"))]
600type JoinedEthereumWalletFiller<F> = JoinFill<F, WalletFiller<alloy_network::EthereumWallet>>;
601
602#[cfg(any(test, feature = "anvil-node"))]
603type AnvilProviderResult<T> = Result<T, alloy_node_bindings::NodeError>;
604
605#[cfg(any(test, feature = "anvil-node"))]
606impl<L, F, N: Network> ProviderBuilder<L, F, N> {
607    /// Build this provider with anvil, using the BoxTransport.
608    pub fn connect_anvil(self) -> F::Provider
609    where
610        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
611        L: crate::builder::ProviderLayer<
612            crate::layers::AnvilProvider<crate::provider::RootProvider<N>, N>,
613            N,
614        >,
615    {
616        self.connect_anvil_with_config(std::convert::identity)
617    }
618
619    /// Build this provider with anvil, using the BoxTransport.
620    #[deprecated(since = "0.12.6", note = "use `connect_anvil` instead")]
621    pub fn on_anvil(self) -> F::Provider
622    where
623        L: ProviderLayer<crate::layers::AnvilProvider<RootProvider<N>, N>, N>,
624        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
625    {
626        self.connect_anvil_with_config(std::convert::identity)
627    }
628
629    /// Build this provider with anvil, using the BoxTransport. This
630    /// function configures a wallet backed by anvil keys, and is intended for
631    /// use in tests.
632    pub fn connect_anvil_with_wallet(
633        self,
634    ) -> <JoinedEthereumWalletFiller<F> as ProviderLayer<L::Provider, N>>::Provider
635    where
636        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
637        L: crate::builder::ProviderLayer<
638            crate::layers::AnvilProvider<crate::provider::RootProvider<N>, N>,
639            N,
640        >,
641        alloy_network::EthereumWallet: alloy_network::NetworkWallet<N>,
642    {
643        self.connect_anvil_with_wallet_and_config(std::convert::identity)
644            .expect("failed to build provider")
645    }
646
647    /// Build this provider with anvil, using the BoxTransport. This
648    /// function configures a wallet backed by anvil keys, and is intended for
649    /// use in tests.
650    #[deprecated(since = "0.12.6", note = "use `connect_anvil_with_wallet` instead")]
651    pub fn on_anvil_with_wallet(
652        self,
653    ) -> <JoinedEthereumWalletFiller<F> as ProviderLayer<L::Provider, N>>::Provider
654    where
655        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
656        L: crate::builder::ProviderLayer<
657            crate::layers::AnvilProvider<crate::provider::RootProvider<N>, N>,
658            N,
659        >,
660        alloy_network::EthereumWallet: alloy_network::NetworkWallet<N>,
661    {
662        self.connect_anvil_with_wallet_and_config(std::convert::identity)
663            .expect("failed to build provider")
664    }
665
666    /// Build this provider with anvil, using the BoxTransport. The
667    /// given function is used to configure the anvil instance.
668    pub fn connect_anvil_with_config(
669        self,
670        f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
671    ) -> F::Provider
672    where
673        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
674        L: crate::builder::ProviderLayer<
675            crate::layers::AnvilProvider<crate::provider::RootProvider<N>, N>,
676            N,
677        >,
678    {
679        let anvil_layer = crate::layers::AnvilLayer::from(f(Default::default()));
680        let url = anvil_layer.endpoint_url();
681
682        let rpc_client = ClientBuilder::default().http(url);
683
684        self.layer(anvil_layer).connect_client(rpc_client)
685    }
686
687    /// Build this provider with anvil, using the BoxTransport. The
688    /// given function is used to configure the anvil instance.
689    #[deprecated(since = "0.12.6", note = "use `connect_anvil_with_config` instead")]
690    pub fn on_anvil_with_config(
691        self,
692        f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
693    ) -> F::Provider
694    where
695        L: ProviderLayer<crate::layers::AnvilProvider<RootProvider<N>, N>, N>,
696        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
697    {
698        let anvil_layer = crate::layers::AnvilLayer::from(f(Default::default()));
699        let url = anvil_layer.endpoint_url();
700
701        let rpc_client = ClientBuilder::default().http(url);
702
703        self.layer(anvil_layer).connect_client(rpc_client)
704    }
705
706    /// Build this provider with anvil, using the BoxTransport.
707    /// This calls `try_on_anvil_with_wallet_and_config` and panics on error.
708    pub fn connect_anvil_with_wallet_and_config(
709        self,
710        f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
711    ) -> AnvilProviderResult<
712        <JoinedEthereumWalletFiller<F> as ProviderLayer<L::Provider, N>>::Provider,
713    >
714    where
715        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
716        L: crate::builder::ProviderLayer<
717            crate::layers::AnvilProvider<crate::provider::RootProvider<N>, N>,
718            N,
719        >,
720        alloy_network::EthereumWallet: alloy_network::NetworkWallet<N>,
721    {
722        let anvil_layer = crate::layers::AnvilLayer::from(f(Default::default()));
723        let url = anvil_layer.endpoint_url();
724
725        let wallet = anvil_layer
726            .instance()
727            .wallet()
728            .ok_or(alloy_node_bindings::NodeError::NoKeysAvailable)?;
729
730        let rpc_client = ClientBuilder::default().http(url);
731
732        Ok(self.wallet(wallet).layer(anvil_layer).connect_client(rpc_client))
733    }
734
735    /// Build this provider with anvil, using the BoxTransport.
736    /// This calls `try_on_anvil_with_wallet_and_config` and panics on error.
737    #[deprecated(since = "0.12.6", note = "use `connect_anvil_with_wallet_and_config` instead")]
738    pub fn on_anvil_with_wallet_and_config(
739        self,
740        f: impl FnOnce(alloy_node_bindings::Anvil) -> alloy_node_bindings::Anvil,
741    ) -> AnvilProviderResult<
742        <JoinedEthereumWalletFiller<F> as ProviderLayer<L::Provider, N>>::Provider,
743    >
744    where
745        F: TxFiller<N> + ProviderLayer<L::Provider, N>,
746        L: crate::builder::ProviderLayer<
747            crate::layers::AnvilProvider<crate::provider::RootProvider<N>, N>,
748            N,
749        >,
750        alloy_network::EthereumWallet: alloy_network::NetworkWallet<N>,
751    {
752        let anvil_layer = crate::layers::AnvilLayer::from(f(Default::default()));
753        let url = anvil_layer.endpoint_url();
754
755        let wallet = anvil_layer
756            .instance()
757            .wallet()
758            .ok_or(alloy_node_bindings::NodeError::NoKeysAvailable)?;
759
760        let rpc_client = ClientBuilder::default().http(url);
761
762        Ok(self.wallet(wallet).layer(anvil_layer).connect_client(rpc_client))
763    }
764}
765
766#[cfg(test)]
767mod tests {
768    use super::*;
769    use crate::Provider;
770    use alloy_network::AnyNetwork;
771
772    #[tokio::test]
773    async fn basic() {
774        let provider = ProviderBuilder::new()
775            .with_cached_nonce_management()
776            .with_call_batching()
777            .connect_http("http://localhost:8545".parse().unwrap());
778        let _ = provider.get_account(Default::default());
779        let provider = provider.erased();
780        let _ = provider.get_account(Default::default());
781    }
782
783    #[tokio::test]
784    #[cfg(feature = "reqwest")]
785    async fn test_connect_reqwest() {
786        let provider = ProviderBuilder::new()
787            .with_cached_nonce_management()
788            .with_call_batching()
789            .connect_reqwest(
790                reqwest::Client::new(),
791                reqwest::Url::parse("http://localhost:8545").unwrap(),
792            );
793        let _ = provider.get_account(Default::default());
794        let provider = provider.erased();
795        let _ = provider.get_account(Default::default());
796    }
797
798    #[tokio::test]
799    #[cfg(feature = "reqwest")]
800    async fn test_with_reqwest() {
801        let provider = ProviderBuilder::new()
802            .with_cached_nonce_management()
803            .with_call_batching()
804            .with_reqwest(reqwest::Url::parse("http://localhost:8545").unwrap(), |builder| {
805                builder
806                    .user_agent("alloy/test")
807                    .timeout(std::time::Duration::from_secs(10))
808                    .build()
809                    .expect("failed to build reqwest client")
810            });
811        let _ = provider.get_account(Default::default());
812        let provider = provider.erased();
813        let _ = provider.get_account(Default::default());
814    }
815
816    #[tokio::test]
817    async fn compile_with_network() {
818        let p = ProviderBuilder::new_with_network::<AnyNetwork>().connect_anvil();
819        let num = p.get_block_number().await.unwrap();
820        assert_eq!(num, 0);
821    }
822}