semioscan 0.10.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
// SPDX-FileCopyrightText: 2025 Semiotic AI, Inc.
//
// SPDX-License-Identifier: Apache-2.0

//! Strong types for configuration values
//!
//! These types ensure configuration values are not confused with
//! blockchain values (block numbers, gas amounts, etc.).

use serde::{Deserialize, Serialize};
use std::ops::{Add, AddAssign};

/// Maximum block range for RPC queries
///
/// This prevents overloading RPC nodes with queries that are too large.
/// Different chains have different limits based on their RPC infrastructure.
///
/// Typical values:
/// - Conservative: 2000 blocks (works on most chains)
/// - Moderate: 5000 blocks
/// - Generous: 10000 blocks (chains with robust RPC like Base)
///
/// # Examples
///
/// ```
/// use semioscan::MaxBlockRange;
///
/// let conservative = MaxBlockRange::DEFAULT;
/// assert_eq!(conservative.as_u64(), 2000);
///
/// let generous = MaxBlockRange::GENEROUS;
/// assert_eq!(generous.as_u64(), 10000);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct MaxBlockRange(u64);

impl MaxBlockRange {
    /// Conservative default (works on most chains)
    pub const DEFAULT: Self = Self(2000);

    /// Moderate range for chains with good RPC support
    pub const MODERATE: Self = Self(5000);

    /// For chains with generous RPC limits (e.g., Base)
    pub const GENEROUS: Self = Self(10000);

    /// Very conservative for rate-limited RPCs
    pub const CONSERVATIVE: Self = Self(1000);

    /// Create a new max block range
    ///
    /// # Examples
    ///
    /// ```
    /// use semioscan::MaxBlockRange;
    ///
    /// let range = MaxBlockRange::new(3000);
    /// assert_eq!(range.as_u64(), 3000);
    /// ```
    pub const fn new(blocks: u64) -> Self {
        Self(blocks)
    }

    /// Get the inner u64 value
    pub const fn as_u64(&self) -> u64 {
        self.0
    }

    /// Calculate number of chunks needed to cover a range
    ///
    /// # Examples
    ///
    /// ```
    /// use semioscan::MaxBlockRange;
    ///
    /// let range = MaxBlockRange::new(1000);
    /// let chunks = range.chunks_needed(0, 2500);
    /// assert_eq!(chunks, 3); // 0-999, 1000-1999, 2000-2500
    /// ```
    pub fn chunks_needed(&self, start: u64, end: u64) -> usize {
        if end < start {
            return 0;
        }
        let total_blocks = end - start + 1;
        total_blocks.div_ceil(self.0) as usize
    }

    /// Split a block range into chunks
    ///
    /// Returns an iterator of (start_block, end_block) tuples, where each
    /// chunk is at most `self.0` blocks in size.
    ///
    /// # Examples
    ///
    /// ```
    /// use semioscan::MaxBlockRange;
    ///
    /// let range = MaxBlockRange::new(1000);
    /// let chunks: Vec<_> = range.chunk_range(0, 2500).collect();
    ///
    /// assert_eq!(chunks.len(), 3);
    /// assert_eq!(chunks[0], (0, 999));
    /// assert_eq!(chunks[1], (1000, 1999));
    /// assert_eq!(chunks[2], (2000, 2500));
    /// ```
    pub fn chunk_range(&self, start: u64, end: u64) -> ChunkIterator {
        ChunkIterator {
            current: start,
            end,
            chunk_size: self.0,
        }
    }
}

impl From<u64> for MaxBlockRange {
    fn from(value: u64) -> Self {
        Self(value)
    }
}

impl std::fmt::Display for MaxBlockRange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} blocks", self.0)
    }
}

/// Iterator over block range chunks
///
/// Created by [`MaxBlockRange::chunk_range`]. Yields (start, end) tuples
/// representing block ranges.
#[derive(Debug, Clone)]
pub struct ChunkIterator {
    current: u64,
    end: u64,
    chunk_size: u64,
}

impl Iterator for ChunkIterator {
    type Item = (u64, u64);

    fn next(&mut self) -> Option<Self::Item> {
        if self.current > self.end {
            return None;
        }

        let chunk_start = self.current;
        let chunk_end = (self.current + self.chunk_size - 1).min(self.end);

        self.current = chunk_end + 1;

        Some((chunk_start, chunk_end))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.current > self.end {
            (0, Some(0))
        } else {
            let remaining_blocks = self.end - self.current + 1;
            let chunks = remaining_blocks.div_ceil(self.chunk_size) as usize;
            (chunks, Some(chunks))
        }
    }
}

impl ExactSizeIterator for ChunkIterator {}

/// Represents a count of blockchain transactions
///
/// This type prevents confusion between transaction counts and other numeric values.
/// Using `usize` internally ensures counts cannot be negative.
///
/// # Examples
///
/// ```
/// use semioscan::TransactionCount;
///
/// let count = TransactionCount::new(5);
/// assert_eq!(count.as_usize(), 5);
///
/// let total = count + TransactionCount::new(3);
/// assert_eq!(total.as_usize(), 8);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct TransactionCount(usize);

impl TransactionCount {
    /// Zero transactions
    pub const ZERO: Self = Self(0);

    /// Create a new transaction count
    ///
    /// # Examples
    ///
    /// ```
    /// use semioscan::TransactionCount;
    ///
    /// let count = TransactionCount::new(10);
    /// assert_eq!(count.as_usize(), 10);
    /// ```
    pub const fn new(count: usize) -> Self {
        Self(count)
    }

    /// Get the inner usize value
    pub const fn as_usize(&self) -> usize {
        self.0
    }

    /// Increment the count by one
    ///
    /// Uses saturating addition to prevent overflow.
    pub fn increment(&mut self) {
        self.0 = self.0.saturating_add(1);
    }

    /// Check if count is zero
    pub fn is_zero(&self) -> bool {
        self.0 == 0
    }
}

impl From<usize> for TransactionCount {
    fn from(value: usize) -> Self {
        Self(value)
    }
}

impl Add for TransactionCount {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_add(rhs.0))
    }
}

impl AddAssign for TransactionCount {
    fn add_assign(&mut self, rhs: Self) {
        self.0 = self.0.saturating_add(rhs.0);
    }
}

impl std::fmt::Display for TransactionCount {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} transactions", self.0)
    }
}

/// Represents a count of blocks (not a block number)
///
/// This type prevents confusion between block counts (a quantity of blocks)
/// and block numbers (a position in the blockchain). For example, a block
/// window from block 100 to 110 has a count of 11 blocks, not 10.
///
/// # Examples
///
/// ```
/// use semioscan::BlockCount;
///
/// let count = BlockCount::new(100);
/// assert_eq!(count.as_u64(), 100);
///
/// let total = count + BlockCount::new(50);
/// assert_eq!(total.as_u64(), 150);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct BlockCount(u64);

impl BlockCount {
    /// Zero blocks
    pub const ZERO: Self = Self(0);

    /// Create a new block count
    ///
    /// # Examples
    ///
    /// ```
    /// use semioscan::BlockCount;
    ///
    /// let count = BlockCount::new(1000);
    /// assert_eq!(count.as_u64(), 1000);
    /// ```
    pub const fn new(count: u64) -> Self {
        Self(count)
    }

    /// Get the inner u64 value
    pub const fn as_u64(&self) -> u64 {
        self.0
    }

    /// Check if count is zero
    pub fn is_zero(&self) -> bool {
        self.0 == 0
    }
}

impl From<u64> for BlockCount {
    fn from(value: u64) -> Self {
        Self(value)
    }
}

impl Add for BlockCount {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_add(rhs.0))
    }
}

impl AddAssign for BlockCount {
    fn add_assign(&mut self, rhs: Self) {
        self.0 = self.0.saturating_add(rhs.0);
    }
}

impl std::fmt::Display for BlockCount {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0 == 1 {
            write!(f, "1 block")
        } else {
            write!(f, "{} blocks", self.0)
        }
    }
}

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

    #[test]
    fn test_max_block_range_creation() {
        let range = MaxBlockRange::new(2000);
        assert_eq!(range.as_u64(), 2000);
    }

    #[test]
    fn test_max_block_range_constants() {
        assert_eq!(MaxBlockRange::CONSERVATIVE.as_u64(), 1000);
        assert_eq!(MaxBlockRange::DEFAULT.as_u64(), 2000);
        assert_eq!(MaxBlockRange::MODERATE.as_u64(), 5000);
        assert_eq!(MaxBlockRange::GENEROUS.as_u64(), 10000);
    }

    #[test]
    fn test_chunks_needed() {
        let range = MaxBlockRange::new(1000);

        // Exactly one chunk
        assert_eq!(range.chunks_needed(0, 999), 1);

        // Two chunks
        assert_eq!(range.chunks_needed(0, 1000), 2);

        // Three chunks with partial last chunk
        assert_eq!(range.chunks_needed(0, 2500), 3);

        // Empty range
        assert_eq!(range.chunks_needed(100, 50), 0);

        // Single block
        assert_eq!(range.chunks_needed(100, 100), 1);
    }

    #[test]
    fn test_chunk_range_exact_multiple() {
        let range = MaxBlockRange::new(1000);
        let chunks: Vec<_> = range.chunk_range(0, 2999).collect();

        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0], (0, 999));
        assert_eq!(chunks[1], (1000, 1999));
        assert_eq!(chunks[2], (2000, 2999));
    }

    #[test]
    fn test_chunk_range_partial_last_chunk() {
        let range = MaxBlockRange::new(1000);
        let chunks: Vec<_> = range.chunk_range(0, 2500).collect();

        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0], (0, 999));
        assert_eq!(chunks[1], (1000, 1999));
        assert_eq!(chunks[2], (2000, 2500));
    }

    #[test]
    fn test_chunk_range_single_chunk() {
        let range = MaxBlockRange::new(1000);
        let chunks: Vec<_> = range.chunk_range(0, 500).collect();

        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0], (0, 500));
    }

    #[test]
    fn test_chunk_range_single_block() {
        let range = MaxBlockRange::new(1000);
        let chunks: Vec<_> = range.chunk_range(100, 100).collect();

        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0], (100, 100));
    }

    #[test]
    fn test_chunk_range_empty() {
        let range = MaxBlockRange::new(1000);
        let chunks: Vec<_> = range.chunk_range(100, 50).collect();

        assert_eq!(chunks.len(), 0);
    }

    #[test]
    fn test_chunk_range_non_zero_start() {
        let range = MaxBlockRange::new(1000);
        let chunks: Vec<_> = range.chunk_range(5000, 7500).collect();

        assert_eq!(chunks.len(), 3);
        assert_eq!(chunks[0], (5000, 5999));
        assert_eq!(chunks[1], (6000, 6999));
        assert_eq!(chunks[2], (7000, 7500));
    }

    #[test]
    fn test_chunk_iterator_size_hint() {
        let range = MaxBlockRange::new(1000);
        let mut iter = range.chunk_range(0, 2500);

        assert_eq!(iter.size_hint(), (3, Some(3)));

        iter.next();
        assert_eq!(iter.size_hint(), (2, Some(2)));

        iter.next();
        assert_eq!(iter.size_hint(), (1, Some(1)));

        iter.next();
        assert_eq!(iter.size_hint(), (0, Some(0)));
    }

    #[test]
    fn test_display() {
        let range = MaxBlockRange::new(2000);
        assert_eq!(format!("{}", range), "2000 blocks");
    }

    #[test]
    fn test_serialization() {
        let range = MaxBlockRange::new(2000);
        let json = serde_json::to_string(&range).unwrap();
        let deserialized: MaxBlockRange = serde_json::from_str(&json).unwrap();
        assert_eq!(range, deserialized);
    }

    #[test]
    fn test_conversions() {
        let u64_val = 2000u64;
        let range: MaxBlockRange = u64_val.into();
        let back: u64 = range.as_u64();
        assert_eq!(u64_val, back);
    }

    #[test]
    fn test_ordering() {
        let small = MaxBlockRange::CONSERVATIVE;
        let medium = MaxBlockRange::DEFAULT;
        let large = MaxBlockRange::GENEROUS;

        assert!(small < medium);
        assert!(medium < large);
        assert!(small < large);
    }

    #[test]
    fn test_real_world_scenario() {
        // Simulate scanning 1 day of blocks on Arbitrum (≈7200 blocks per hour × 24 hours)
        let daily_blocks = 7200 * 24; // ≈172,800 blocks
        let range = MaxBlockRange::MODERATE; // 5000 blocks per query

        let chunks: Vec<_> = range.chunk_range(1000000, 1000000 + daily_blocks).collect();

        // Should need about 35 chunks (172800 / 5000 ≈ 34.56)
        assert_eq!(chunks.len(), 35);

        // Verify first and last chunk
        assert_eq!(chunks[0].0, 1000000);
        assert_eq!(chunks[34].1, 1000000 + daily_blocks);

        // Verify no gaps between chunks
        for i in 0..chunks.len() - 1 {
            assert_eq!(chunks[i].1 + 1, chunks[i + 1].0);
        }
    }

    // TransactionCount tests
    #[test]
    fn test_transaction_count_creation() {
        let count = TransactionCount::new(5);
        assert_eq!(count.as_usize(), 5);
    }

    #[test]
    fn test_transaction_count_zero() {
        assert!(TransactionCount::ZERO.is_zero());
        assert_eq!(TransactionCount::ZERO.as_usize(), 0);
    }

    #[test]
    fn test_transaction_count_addition() {
        let a = TransactionCount::new(5);
        let b = TransactionCount::new(3);
        let sum = a + b;
        assert_eq!(sum.as_usize(), 8);
    }

    #[test]
    fn test_transaction_count_increment() {
        let mut count = TransactionCount::new(5);
        count.increment();
        assert_eq!(count.as_usize(), 6);
    }

    #[test]
    fn test_transaction_count_saturating_addition() {
        let max_count = TransactionCount::new(usize::MAX);
        let small_count = TransactionCount::new(1);
        let result = max_count + small_count;
        assert_eq!(result.as_usize(), usize::MAX);
    }

    #[test]
    fn test_transaction_count_saturating_increment() {
        let mut count = TransactionCount::new(usize::MAX);
        count.increment();
        assert_eq!(count.as_usize(), usize::MAX);
    }

    #[test]
    fn test_transaction_count_display() {
        let count = TransactionCount::new(42);
        assert_eq!(format!("{}", count), "42 transactions");
    }

    #[test]
    fn test_transaction_count_serialization() {
        let count = TransactionCount::new(10);
        let json = serde_json::to_string(&count).unwrap();
        let deserialized: TransactionCount = serde_json::from_str(&json).unwrap();
        assert_eq!(count, deserialized);
    }

    #[test]
    fn test_transaction_count_conversions() {
        let usize_val = 42usize;
        let count: TransactionCount = usize_val.into();
        let back: usize = count.as_usize();
        assert_eq!(usize_val, back);
    }

    #[test]
    fn test_transaction_count_ordering() {
        let small = TransactionCount::new(5);
        let medium = TransactionCount::new(10);
        let large = TransactionCount::new(20);

        assert!(small < medium);
        assert!(medium < large);
        assert!(small < large);
    }
}