semioscan 0.11.3

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
// 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 tracing::{debug, info, warn};

use crate::errors::RpcError;
use crate::transport::RateLimitLayer;

/// 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>,
}

impl ProviderPool {
    /// Create a new empty provider pool
    #[must_use]
    pub fn new() -> Self {
        Self {
            providers: RwLock::new(HashMap::new()),
            default_rate_limit: 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,
        }
    }

    /// 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.chain,
                &endpoint.url,
                endpoint.rate_limit.or(rate_limit),
            )?;
        }
        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
    ///
    /// # Errors
    ///
    /// Returns an error if the URL is invalid
    pub fn add(
        &self,
        chain: NamedChain,
        url: &str,
        rate_limit: Option<u32>,
    ) -> Result<(), RpcError> {
        let provider = create_pooled_provider(url, rate_limit.or(self.default_rate_limit))?;

        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>,
}

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 {
            chain,
            url: url.to_string(),
            rate_limit: None,
        });
        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 {
            chain,
            url: url.to_string(),
            rate_limit: Some(rate_limit),
        });
        self
    }

    /// Set the default rate limit for all providers
    #[must_use]
    pub fn with_rate_limit(mut self, requests_per_second: u32) -> Self {
        self.default_rate_limit = Some(requests_per_second);
        self
    }

    /// Build the provider pool
    ///
    /// # Errors
    ///
    /// Returns an error if any endpoint URL is invalid
    pub fn build(self) -> Result<ProviderPool, RpcError> {
        let pool = ProviderPool::with_defaults(self.default_rate_limit);

        for endpoint in self.endpoints {
            pool.add(
                endpoint.chain,
                &endpoint.url,
                endpoint.rate_limit.or(self.default_rate_limit),
            )?;
        }

        Ok(pool)
    }
}

/// Configuration for a chain endpoint
#[derive(Debug, Clone)]
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>,
}

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,
        }
    }

    /// 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
    }

    /// 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
///
/// Returns a bare `RootProvider` without fillers, as fillers are typically
/// 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>,
) -> Result<RootProvider<AnyNetwork>, RpcError> {
    let parsed_url: url::Url = url.parse().map_err(|e| {
        warn!(url = url, error = ?e, "Invalid provider URL");
        RpcError::ProviderUrlInvalid(url.to_string())
    })?;

    // Build the RPC client with optional rate limiting
    let client = match rate_limit {
        Some(limit) => alloy_rpc_client::ClientBuilder::default()
            .layer(RateLimitLayer::per_second(limit))
            .http(parsed_url),
        None => alloy_rpc_client::ClientBuilder::default().http(parsed_url),
    };

    // Create a bare provider without fillers - fillers are application-specific
    // and should be added by consumers if needed
    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());
    }
}