semioscan 0.15.1

Production-grade Rust library for blockchain analytics: gas calculation, price extraction, and block window calculations for EVM chains
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
// SPDX-FileCopyrightText: 2025 Semiotic AI, Inc.
//
// SPDX-License-Identifier: Apache-2.0

//! Provider connection pooling for high-throughput scenarios
//!
//! This module provides thread-safe provider pooling that allows reusing
//! provider connections across multiple concurrent operations.
//!
//! # Overview
//!
//! The [`ProviderPool`] maintains a collection of providers indexed by chain,
//! enabling efficient connection reuse without creating new providers for each
//! operation. This is particularly useful for:
//!
//! - Multi-chain applications that query many chains concurrently
//! - High-throughput indexing or analytics workloads
//! - Long-running services that process blocks continuously
//!
//! # Examples
//!
//! ## Static Pool Initialization
//!
//! For applications that know their chains at startup, use a static pool:
//!
//! ```rust,ignore
//! use semioscan::provider::{ProviderPool, ProviderPoolBuilder};
//! use alloy_chains::NamedChain;
//! use std::sync::LazyLock;
//!
//! // Static pool initialized once on first access
//! static PROVIDERS: LazyLock<ProviderPool> = LazyLock::new(|| {
//!     ProviderPoolBuilder::new()
//!         .add_chain(NamedChain::Mainnet, "https://eth.llamarpc.com")
//!         .add_chain(NamedChain::Base, "https://mainnet.base.org")
//!         .with_rate_limit(10)
//!         .build()
//!         .expect("Failed to create provider pool")
//! });
//!
//! async fn get_block_number(chain: NamedChain) -> u64 {
//!     let provider = PROVIDERS.get(chain).expect("Chain not configured");
//!     provider.get_block_number().await.unwrap()
//! }
//! ```
//!
//! ## Dynamic Pool with Lazy Loading
//!
//! For applications that discover chains at runtime:
//!
//! ```rust,ignore
//! use semioscan::provider::ProviderPool;
//! use alloy_chains::NamedChain;
//!
//! let mut pool = ProviderPool::new();
//!
//! // Add providers as needed
//! pool.add(NamedChain::Mainnet, "https://eth.llamarpc.com", None)?;
//! pool.add(NamedChain::Base, "https://mainnet.base.org", Some(10))?;
//!
//! // Access providers
//! if let Some(provider) = pool.get(NamedChain::Mainnet) {
//!     let block = provider.get_block_number().await?;
//! }
//! ```
//!
//! ## With Preset Configurations
//!
//! ```rust,ignore
//! use semioscan::provider::{ProviderPool, ChainEndpoint};
//!
//! let endpoints = vec![
//!     ChainEndpoint::mainnet("https://eth.llamarpc.com"),
//!     ChainEndpoint::base("https://mainnet.base.org"),
//!     ChainEndpoint::optimism("https://mainnet.optimism.io"),
//! ];
//!
//! let pool = ProviderPool::from_endpoints(endpoints, Some(10))?;
//! ```

use alloy_chains::NamedChain;
use alloy_network::AnyNetwork;
use alloy_provider::RootProvider;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use tracing::{debug, info, warn};

use crate::config::policy::RpcPolicy;
use crate::errors::RpcError;

use super::config::ProviderConfig;
use super::factory::build_http_client;

/// Type alias for a pooled provider using `AnyNetwork`
pub type PooledProvider = Arc<RootProvider<AnyNetwork>>;

/// A thread-safe pool of providers indexed by chain
///
/// The pool uses read-write locks for efficient concurrent access:
/// - Multiple readers can access providers simultaneously
/// - Writers acquire exclusive access when adding new providers
///
/// # Thread Safety
///
/// The pool is safe to share across threads via `Arc<ProviderPool>` or
/// by storing it in a static variable with `LazyLock`.
#[derive(Debug, Default)]
pub struct ProviderPool {
    /// Map of chain to provider
    providers: RwLock<HashMap<NamedChain, PooledProvider>>,
    /// Default rate limit for new providers (requests per second)
    default_rate_limit: Option<u32>,
    /// Default per-request timeout for new providers.
    /// When neither this nor the per-endpoint `timeout` is set, the
    /// underlying transport's default timeout applies.
    default_timeout: Option<Duration>,
}

impl ProviderPool {
    /// Create a new empty provider pool
    #[must_use]
    pub fn new() -> Self {
        Self {
            providers: RwLock::new(HashMap::new()),
            default_rate_limit: None,
            default_timeout: None,
        }
    }

    /// Create a pool with default rate limit for new providers
    #[must_use]
    pub fn with_defaults(rate_limit: Option<u32>) -> Self {
        Self {
            providers: RwLock::new(HashMap::new()),
            default_rate_limit: rate_limit,
            default_timeout: None,
        }
    }

    /// Set the default per-request timeout used when an endpoint does not
    /// override its own.
    #[must_use]
    pub fn with_default_timeout(mut self, timeout: Duration) -> Self {
        self.default_timeout = Some(timeout);
        self
    }

    /// Create a pool from a list of chain endpoints
    ///
    /// # Errors
    ///
    /// Returns an error if any endpoint URL is invalid
    pub fn from_endpoints(
        endpoints: Vec<ChainEndpoint>,
        rate_limit: Option<u32>,
    ) -> Result<Self, RpcError> {
        let pool = Self::with_defaults(rate_limit);
        for endpoint in endpoints {
            pool.add_endpoint(&endpoint)?;
        }
        Ok(pool)
    }

    /// Add a provider for a specific chain
    ///
    /// If a provider already exists for this chain, it will be replaced.
    ///
    /// # Arguments
    ///
    /// * `chain` - The chain to add the provider for
    /// * `url` - The RPC endpoint URL
    /// * `rate_limit` - Optional rate limit in requests per second
    ///
    /// The per-request timeout is taken from the pool's default
    /// (see [`with_default_timeout`](Self::with_default_timeout)).
    /// Use [`add_endpoint`](Self::add_endpoint) to set a per-chain timeout
    /// or minimum-delay pacing.
    ///
    /// # Errors
    ///
    /// Returns an error if the URL is invalid
    pub fn add(
        &self,
        chain: NamedChain,
        url: &str,
        rate_limit: Option<u32>,
    ) -> Result<(), RpcError> {
        self.add_inner(chain, url, rate_limit, None, None)
    }

    /// Add a chain endpoint to the pool.
    ///
    /// Uses the endpoint's `rate_limit`, `timeout`, and `min_delay` when
    /// set, otherwise falls back to the pool defaults.
    ///
    /// # Errors
    ///
    /// Returns an error if the endpoint URL is invalid.
    pub fn add_endpoint(&self, endpoint: &ChainEndpoint) -> Result<(), RpcError> {
        self.add_inner(
            endpoint.chain,
            &endpoint.url,
            endpoint.rate_limit,
            endpoint.timeout,
            endpoint.min_delay,
        )
    }

    fn add_inner(
        &self,
        chain: NamedChain,
        url: &str,
        rate_limit: Option<u32>,
        timeout: Option<Duration>,
        min_delay: Option<Duration>,
    ) -> Result<(), RpcError> {
        let provider = create_pooled_provider(
            url,
            rate_limit.or(self.default_rate_limit),
            timeout.or(self.default_timeout),
            min_delay,
        )?;

        let mut providers = self.providers.write().map_err(|_| {
            RpcError::ProviderConnectionFailed("Provider pool lock poisoned".to_string())
        })?;

        if providers.contains_key(&chain) {
            debug!(chain = ?chain, "Replacing existing provider");
        } else {
            info!(chain = ?chain, url = url, "Added provider to pool");
        }

        providers.insert(chain, Arc::new(provider));
        Ok(())
    }

    /// Get a provider for a specific chain
    ///
    /// Returns `None` if no provider is configured for the chain.
    #[must_use]
    pub fn get(&self, chain: NamedChain) -> Option<PooledProvider> {
        self.providers
            .read()
            .ok()
            .and_then(|providers| providers.get(&chain).cloned())
    }

    /// Get a provider for a chain, or add it if not present
    ///
    /// This is useful for lazy initialization of providers.
    ///
    /// # Errors
    ///
    /// Returns an error if the URL is invalid and the provider needs to be created
    pub fn get_or_add(
        &self,
        chain: NamedChain,
        url: &str,
        rate_limit: Option<u32>,
    ) -> Result<PooledProvider, RpcError> {
        // Try read lock first for better concurrency
        if let Some(provider) = self.get(chain) {
            return Ok(provider);
        }

        // Provider not found, need to add it
        self.add(chain, url, rate_limit)?;
        self.get(chain).ok_or_else(|| {
            RpcError::ProviderConnectionFailed("Failed to retrieve newly added provider".into())
        })
    }

    /// Remove a provider from the pool
    ///
    /// Returns the removed provider if it existed.
    pub fn remove(&self, chain: NamedChain) -> Option<PooledProvider> {
        self.providers
            .write()
            .ok()
            .and_then(|mut providers| providers.remove(&chain))
    }

    /// Check if a provider exists for a chain
    #[must_use]
    pub fn contains(&self, chain: NamedChain) -> bool {
        self.providers
            .read()
            .ok()
            .is_some_and(|providers| providers.contains_key(&chain))
    }

    /// Get the number of providers in the pool
    #[must_use]
    pub fn len(&self) -> usize {
        self.providers
            .read()
            .map(|providers| providers.len())
            .unwrap_or(0)
    }

    /// Check if the pool is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get all configured chains
    #[must_use]
    pub fn chains(&self) -> Vec<NamedChain> {
        self.providers
            .read()
            .map(|providers| providers.keys().copied().collect())
            .unwrap_or_default()
    }

    /// Clear all providers from the pool
    pub fn clear(&self) {
        if let Ok(mut providers) = self.providers.write() {
            providers.clear();
            info!("Cleared all providers from pool");
        }
    }
}

/// Builder for creating provider pools with common configurations
#[derive(Default)]
pub struct ProviderPoolBuilder {
    endpoints: Vec<ChainEndpoint>,
    default_rate_limit: Option<u32>,
    default_timeout: Option<Duration>,
    rpc_policy_timeouts: HashMap<NamedChain, Duration>,
    rpc_policy_min_delays: HashMap<NamedChain, Duration>,
}

impl ProviderPoolBuilder {
    /// Create a new builder
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a chain endpoint to the pool
    #[must_use]
    pub fn add_chain(mut self, chain: NamedChain, url: &str) -> Self {
        self.endpoints.push(ChainEndpoint::new(chain, url));
        self
    }

    /// Add a chain endpoint with a specific rate limit
    #[must_use]
    pub fn add_chain_with_rate_limit(
        mut self,
        chain: NamedChain,
        url: &str,
        rate_limit: u32,
    ) -> Self {
        self.endpoints
            .push(ChainEndpoint::new(chain, url).with_rate_limit(rate_limit));
        self
    }

    /// Add a chain endpoint with a specific per-request timeout
    #[must_use]
    pub fn add_chain_with_timeout(
        mut self,
        chain: NamedChain,
        url: &str,
        timeout: Duration,
    ) -> Self {
        self.endpoints
            .push(ChainEndpoint::new(chain, url).with_timeout(timeout));
        self
    }

    /// Add a fully-specified [`ChainEndpoint`] to the pool
    #[must_use]
    pub fn add_endpoint(mut self, endpoint: ChainEndpoint) -> Self {
        self.endpoints.push(endpoint);
        self
    }

    /// Set the default requests-per-second budget applied to every endpoint
    /// in the pool that does not carry its own [`ChainEndpoint::rate_limit`].
    ///
    /// **Interaction with [`with_rpc_policy`](Self::with_rpc_policy).** The
    /// per-second budget and a policy-supplied `rate_limit_delay` are not
    /// stackable on the wire — each endpoint installs at most one
    /// rate-limit layer. A chain that ends up with both a requests-per-second
    /// budget (from this setter or [`ChainEndpoint::with_rate_limit`]) and a
    /// minimum gap (from [`ChainEndpoint::with_min_delay`] or a policy
    /// `rate_limit_delay`) causes [`build`](Self::build) to return
    /// [`RpcError::ConflictingRateLimit`](crate::errors::RpcError::ConflictingRateLimit)
    /// rather than silently dropping one axis. If the per-chain minimum gap
    /// is the one that matters, set the per-second budget only on the
    /// endpoints that need it via [`ChainEndpoint::with_rate_limit`] instead
    /// of this pool-wide default, and the policy delay will apply to the
    /// remaining chains.
    #[must_use]
    pub fn with_rate_limit(mut self, requests_per_second: u32) -> Self {
        self.default_rate_limit = Some(requests_per_second);
        self
    }

    /// Set the default per-request timeout for all providers
    #[must_use]
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.default_timeout = Some(timeout);
        self
    }

    /// Apply per-chain settings from an [`RpcPolicy`].
    ///
    /// The policy is evaluated for every endpoint that has already been
    /// added to the builder; call this **after** all `add_chain*` /
    /// `add_endpoint` calls for the policy to take effect. Endpoints added
    /// after this call do **not** pick up the policy values — call
    /// `with_rpc_policy` again to refresh.
    ///
    /// For each axis the policy supplies (`rpc_timeout`, `rate_limit_delay`),
    /// a per-endpoint value always wins; the builder's `default_timeout`
    /// is the fallback when neither the endpoint nor the policy supplies
    /// a timeout for a chain. There is no pool-level default for the
    /// minimum-delay pacing axis, so a chain with no endpoint `min_delay`
    /// and no policy `rate_limit_delay` simply runs unpaced.
    ///
    /// **Interaction with [`with_rate_limit`](Self::with_rate_limit) and
    /// [`ChainEndpoint::with_rate_limit`].** A policy's `rate_limit_delay`
    /// and a requests-per-second budget on the same chain are not stackable
    /// on the wire: each endpoint installs at most one rate-limit layer.
    /// If `with_rate_limit(rps)` or an endpoint-level `rate_limit` is set
    /// on a chain that the policy also supplies a `rate_limit_delay` for,
    /// [`build`](Self::build) returns
    /// [`RpcError::ConflictingRateLimit`](crate::errors::RpcError::ConflictingRateLimit)
    /// rather than silently dropping one axis. To keep both knobs effective
    /// across the pool, move the per-second budget off this pool-wide
    /// default and onto the chains that need it via
    /// [`ChainEndpoint::with_rate_limit`], leaving the policy delay in place
    /// for the rest.
    #[must_use]
    pub fn with_rpc_policy<P: RpcPolicy>(mut self, policy: &P) -> Self {
        for endpoint in &self.endpoints {
            let cfg = policy.rpc_config(endpoint.chain);
            self.rpc_policy_timeouts
                .insert(endpoint.chain, cfg.rpc_timeout);
            if let Some(delay) = cfg.rate_limit_delay {
                self.rpc_policy_min_delays.insert(endpoint.chain, delay);
            }
        }
        self
    }

    /// Build the provider pool
    ///
    /// # Errors
    ///
    /// Returns an error if any endpoint URL is invalid, or if any chain
    /// ends up with both a requests-per-second budget and a minimum-delay
    /// gap set after composing endpoint, builder, and policy values
    /// ([`RpcError::ConflictingRateLimit`](crate::errors::RpcError::ConflictingRateLimit)).
    pub fn build(self) -> Result<ProviderPool, RpcError> {
        let pool = ProviderPool::with_defaults(self.default_rate_limit);
        let pool = match self.default_timeout {
            Some(t) => pool.with_default_timeout(t),
            None => pool,
        };

        for endpoint in &self.endpoints {
            let policy_timeout = self.rpc_policy_timeouts.get(&endpoint.chain).copied();
            let policy_min_delay = self.rpc_policy_min_delays.get(&endpoint.chain).copied();
            let effective = ChainEndpoint {
                chain: endpoint.chain,
                url: endpoint.url.clone(),
                rate_limit: endpoint.rate_limit.or(self.default_rate_limit),
                timeout: endpoint.timeout.or(policy_timeout),
                min_delay: endpoint.min_delay.or(policy_min_delay),
            };
            pool.add_endpoint(&effective)?;
        }

        Ok(pool)
    }
}

/// Configuration for a chain endpoint.
///
/// Construct via [`ChainEndpoint::new`] (or one of the preset constructors
/// such as [`ChainEndpoint::mainnet`]) and the `with_*` setters. The struct
/// is `#[non_exhaustive]` so additional optional fields can be added in
/// future releases without forcing struct-literal callers to update.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ChainEndpoint {
    /// The chain this endpoint serves
    pub chain: NamedChain,
    /// The RPC endpoint URL
    pub url: String,
    /// Optional rate limit override for this specific chain
    pub rate_limit: Option<u32>,
    /// Optional per-request timeout override for this specific chain.
    /// When `None`, the pool's default timeout (if any) is used.
    pub timeout: Option<Duration>,
    /// Optional minimum spacing between requests for this specific chain.
    /// When set, the underlying transport installs a minimum-delay layer
    /// that guarantees at least this gap between consecutive RPC calls.
    /// When `None`, the policy's per-chain `rate_limit_delay` (if any)
    /// supplies the value at build time.
    pub min_delay: Option<Duration>,
}

impl ChainEndpoint {
    /// Create a new chain endpoint
    #[must_use]
    pub fn new(chain: NamedChain, url: impl Into<String>) -> Self {
        Self {
            chain,
            url: url.into(),
            rate_limit: None,
            timeout: None,
            min_delay: None,
        }
    }

    /// Create a new chain endpoint with rate limiting
    #[must_use]
    pub fn with_rate_limit(mut self, rate_limit: u32) -> Self {
        self.rate_limit = Some(rate_limit);
        self
    }

    /// Override the per-request timeout for this chain
    #[must_use]
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Override the minimum spacing between requests for this chain.
    ///
    /// Mirrors [`ProviderConfig::with_min_delay`](crate::provider::ProviderConfig::with_min_delay):
    /// the underlying transport installs a layer that guarantees at least
    /// `delay` between consecutive RPC calls.
    ///
    /// # Panics
    ///
    /// Panics if `delay` is [`Duration::ZERO`]. See
    /// [`ProviderConfig::with_min_delay`](crate::provider::ProviderConfig::with_min_delay)
    /// for the reasoning and how to express "no pacing" for an endpoint
    /// instead.
    #[must_use]
    #[track_caller]
    pub fn with_min_delay(mut self, delay: Duration) -> Self {
        super::assert_nonzero_min_delay(delay);
        self.min_delay = Some(delay);
        self
    }

    /// Create an Ethereum mainnet endpoint
    #[must_use]
    pub fn mainnet(url: impl Into<String>) -> Self {
        Self::new(NamedChain::Mainnet, url)
    }

    /// Create a Base mainnet endpoint
    #[must_use]
    pub fn base(url: impl Into<String>) -> Self {
        Self::new(NamedChain::Base, url)
    }

    /// Create an Optimism mainnet endpoint
    #[must_use]
    pub fn optimism(url: impl Into<String>) -> Self {
        Self::new(NamedChain::Optimism, url)
    }

    /// Create an Arbitrum One endpoint
    #[must_use]
    pub fn arbitrum(url: impl Into<String>) -> Self {
        Self::new(NamedChain::Arbitrum, url)
    }

    /// Create a Polygon mainnet endpoint
    #[must_use]
    pub fn polygon(url: impl Into<String>) -> Self {
        Self::new(NamedChain::Polygon, url)
    }

    /// Create a Sepolia testnet endpoint
    #[must_use]
    pub fn sepolia(url: impl Into<String>) -> Self {
        Self::new(NamedChain::Sepolia, url)
    }
}

/// Create a pooled provider with optional rate limiting and pacing.
///
/// Routes through the shared HTTP client builder so the
/// `(rate_limit_per_second, min_delay, timeout)` dispatch matrix matches
/// every other HTTP provider this crate hands out. Returns a bare
/// `RootProvider` without fillers, as fillers are application-specific
/// and should be added by the consumer if needed.
///
/// Note: RPC request/response logging is handled natively by alloy's transport
/// layer at DEBUG/TRACE level.
fn create_pooled_provider(
    url: &str,
    rate_limit: Option<u32>,
    timeout: Option<Duration>,
    min_delay: Option<Duration>,
) -> Result<RootProvider<AnyNetwork>, RpcError> {
    let mut config = ProviderConfig::new(url).with_rate_limit_opt(rate_limit);
    if let Some(t) = timeout {
        config = config.with_timeout(t);
    }
    if let Some(d) = min_delay {
        config = config.with_min_delay(d);
    }

    let client = build_http_client(config).inspect_err(|e| {
        warn!(url = url, error = ?e, "Failed to build pooled provider");
    })?;

    Ok(RootProvider::<AnyNetwork>::new(client))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pool_new() {
        let pool = ProviderPool::new();
        assert!(pool.is_empty());
        assert_eq!(pool.len(), 0);
    }

    #[test]
    fn test_pool_with_defaults() {
        let pool = ProviderPool::with_defaults(Some(10));
        assert_eq!(pool.default_rate_limit, Some(10));
    }

    #[test]
    fn test_chain_endpoint_constructors() {
        let endpoint = ChainEndpoint::mainnet("https://eth.llamarpc.com");
        assert_eq!(endpoint.chain, NamedChain::Mainnet);
        assert_eq!(endpoint.url, "https://eth.llamarpc.com");
        assert!(endpoint.rate_limit.is_none());

        let endpoint = ChainEndpoint::base("https://mainnet.base.org").with_rate_limit(5);
        assert_eq!(endpoint.chain, NamedChain::Base);
        assert_eq!(endpoint.rate_limit, Some(5));
    }

    #[test]
    fn test_pool_builder() {
        let builder = ProviderPoolBuilder::new()
            .add_chain(NamedChain::Mainnet, "https://eth.llamarpc.com")
            .add_chain_with_rate_limit(NamedChain::Base, "https://mainnet.base.org", 5)
            .with_rate_limit(10);

        assert_eq!(builder.endpoints.len(), 2);
        assert_eq!(builder.default_rate_limit, Some(10));
    }

    #[test]
    fn test_pool_contains_and_chains() {
        let pool = ProviderPool::new();

        // Add a provider (using a valid URL format)
        let result = pool.add(NamedChain::Mainnet, "https://eth.llamarpc.com", None);
        assert!(result.is_ok());

        assert!(pool.contains(NamedChain::Mainnet));
        assert!(!pool.contains(NamedChain::Base));

        let chains = pool.chains();
        assert_eq!(chains.len(), 1);
        assert!(chains.contains(&NamedChain::Mainnet));
    }

    #[test]
    fn test_pool_remove() {
        let pool = ProviderPool::new();
        pool.add(NamedChain::Mainnet, "https://eth.llamarpc.com", None)
            .unwrap();

        assert!(pool.contains(NamedChain::Mainnet));

        let removed = pool.remove(NamedChain::Mainnet);
        assert!(removed.is_some());
        assert!(!pool.contains(NamedChain::Mainnet));

        // Removing again should return None
        let removed_again = pool.remove(NamedChain::Mainnet);
        assert!(removed_again.is_none());
    }

    #[test]
    fn test_pool_clear() {
        let pool = ProviderPool::new();
        pool.add(NamedChain::Mainnet, "https://eth.llamarpc.com", None)
            .unwrap();
        pool.add(NamedChain::Base, "https://mainnet.base.org", None)
            .unwrap();

        assert_eq!(pool.len(), 2);

        pool.clear();
        assert!(pool.is_empty());
    }

    #[test]
    fn test_invalid_url() {
        let pool = ProviderPool::new();
        let result = pool.add(NamedChain::Mainnet, "not a valid url", None);
        assert!(result.is_err());
    }

    /// Zero-duration `min_delay` is rejected at the endpoint builder call
    /// site. Pins the contract documented on
    /// [`ChainEndpoint::with_min_delay`]'s `# Panics` section: the
    /// token-bucket layer it ultimately feeds cannot represent a zero
    /// period, so the setter fails fast at the operator's call line
    /// rather than letting `Some(Duration::ZERO)` propagate to a later
    /// pool-build panic with a useless backtrace.
    #[test]
    #[should_panic(expected = "min_delay must be > 0")]
    fn test_chain_endpoint_with_min_delay_rejects_zero() {
        let _ = ChainEndpoint::new(NamedChain::Mainnet, "https://eth.llamarpc.com")
            .with_min_delay(Duration::ZERO);
    }

    #[test]
    fn with_rpc_policy_only_covers_chains_added_before_it() {
        use crate::config::policy::RpcConfig;

        struct FixedPolicy {
            timeout: Duration,
            rate_limit_delay: Option<Duration>,
        }
        impl RpcPolicy for FixedPolicy {
            fn rpc_config(&self, _: NamedChain) -> RpcConfig {
                RpcConfig {
                    rpc_timeout: self.timeout,
                    rate_limit_delay: self.rate_limit_delay,
                }
            }
        }

        let policy = FixedPolicy {
            timeout: Duration::from_secs(5),
            rate_limit_delay: Some(Duration::from_millis(250)),
        };

        let before = ProviderPoolBuilder::new()
            .add_chain(NamedChain::Mainnet, "http://localhost:8545")
            .with_rpc_policy(&policy);
        assert_eq!(
            before
                .rpc_policy_timeouts
                .get(&NamedChain::Mainnet)
                .copied(),
            Some(Duration::from_secs(5)),
            "policy timeout must apply when chain is added before with_rpc_policy",
        );
        assert_eq!(
            before
                .rpc_policy_min_delays
                .get(&NamedChain::Mainnet)
                .copied(),
            Some(Duration::from_millis(250)),
            "policy rate_limit_delay must apply when chain is added before with_rpc_policy",
        );

        let after = ProviderPoolBuilder::new()
            .with_rpc_policy(&policy)
            .add_chain(NamedChain::Mainnet, "http://localhost:8545");
        assert!(
            !after.rpc_policy_timeouts.contains_key(&NamedChain::Mainnet),
            "policy timeout must not apply when chain is added after with_rpc_policy",
        );
        assert!(
            !after
                .rpc_policy_min_delays
                .contains_key(&NamedChain::Mainnet),
            "policy rate_limit_delay must not apply when chain is added after with_rpc_policy",
        );
    }
}