Skip to content

Commit

Permalink
feat(provider): MockProvider (#2137)
Browse files Browse the repository at this point in the history
* feat(`providers`): MockProvider

* RpcWithBlock demo

* nit

* fix

* fix

* more provider trait methods

* fix

* fix

* nit

* fix
  • Loading branch information
yash-atreya authored Mar 6, 2025
1 parent 4445d9a commit 76735d0
Show file tree
Hide file tree
Showing 3 changed files with 491 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crates/provider/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(all(not(target_arch = "wasm32"), any(test, feature = "reqwest", feature = "hyper")))]
use crate::layers::{Asserter, MockLayer, MockProvider};
use crate::{
fillers::{
CachedNonceManager, ChainIdFiller, FillerControlFlow, GasFiller, JoinFill, NonceFiller,
Expand Down Expand Up @@ -145,6 +147,25 @@ impl
pub fn disable_recommended_fillers(self) -> ProviderBuilder<Identity, Identity, Ethereum> {
ProviderBuilder { layer: self.layer, filler: Identity, network: self.network }
}

/// Create a new [`MockProvider`] for testing purposes.
///
/// Sets the dummy RPC_URL to `http://localhost:8545`.
#[cfg(all(not(target_arch = "wasm32"), any(test, feature = "reqwest", feature = "hyper")))]
pub fn mocked() -> (MockProvider<RootProvider, Ethereum>, Asserter) {
let asserter = Asserter::new();
let layer = MockLayer::new(asserter.clone());

let builder = ProviderBuilder::<_, _, Ethereum>::default().layer(layer);

#[cfg(any(test, feature = "reqwest"))]
let mock_provider = builder.on_http("http://localhost:8545".parse().unwrap());

#[cfg(all(feature = "hyper", not(feature = "reqwest")))]
let mock_provider = builder.on_hyper_http("http://localhost:8545".parse().unwrap());

(mock_provider, asserter)
}
}

impl<N> Default for ProviderBuilder<Identity, Identity, N> {
Expand Down
Loading

0 comments on commit 76735d0

Please sign in to comment.