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

//! Data types for combined gas and amount retrieval

use alloy_chains::NamedChain;
use alloy_primitives::{Address, BlockNumber, TxHash, U256};
use serde::{Deserialize, Serialize};

use crate::types::config::TransactionCount;
use crate::types::gas::{GasAmount, GasPrice};

/// Data for a single transaction including gas and transferred amount.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct GasAndAmountForTx {
    /// Transaction hash for the enriched transfer.
    pub tx_hash: TxHash,
    /// Block number containing the transaction.
    pub block_number: BlockNumber,
    /// L2 gas used by the transaction.
    pub gas_used: GasAmount,
    /// Effective L2 gas price charged for the transaction.
    pub effective_gas_price: GasPrice,
    /// Optional L1 data fee charged by L2 chains that expose it in the receipt.
    pub l1_fee: Option<U256>,
    /// Additional blob gas cost for EIP-4844 transactions.
    pub blob_gas_cost: U256,
    /// ERC-20 amount transferred by the decoded log this transaction matched.
    pub transferred_amount: U256,
}

impl GasAndAmountForTx {
    /// Calculates the total gas cost for this transaction, including L2 gas, L1 fee, and blob gas.
    #[must_use]
    pub fn total_gas_cost(&self) -> U256 {
        let l2_execution_cost = self.gas_used * self.effective_gas_price;
        let total_cost = l2_execution_cost.saturating_add(self.blob_gas_cost);
        total_cost.saturating_add(self.l1_fee.unwrap_or_default())
    }
}

/// Which follow-up RPC lookup failed while enriching a decoded transfer log.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum CombinedDataLookupStage {
    Transaction,
    Receipt,
}

impl CombinedDataLookupStage {
    #[must_use]
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::Transaction => "get_transaction_by_hash",
            Self::Receipt => "get_transaction_receipt",
        }
    }
}

/// Which pass produced a lookup error while enriching a decoded transfer log.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum CombinedDataLookupPass {
    Batch,
    SerialFallback,
}

/// Diagnostic details for one failed tx/receipt lookup attempt.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CombinedDataLookupAttempt {
    pub pass: CombinedDataLookupPass,
    pub stage: CombinedDataLookupStage,
    pub error: String,
    pub error_chain: Vec<String>,
    pub transport_error: Option<String>,
}

/// Structured metadata describing a decoded transfer that could not be fully enriched.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CombinedDataLookupFailure {
    pub tx_hash: TxHash,
    pub block_number: BlockNumber,
    pub transfer_value: U256,
    pub attempts: Vec<CombinedDataLookupAttempt>,
}

impl CombinedDataLookupFailure {
    #[must_use]
    pub fn final_attempt(&self) -> Option<&CombinedDataLookupAttempt> {
        self.attempts.last()
    }
}

/// Retrieval metadata for combined data calculations.
///
/// This exposes whether decoded transfers had to be skipped after bounded retry/fallback logic,
/// so callers can reject partial accounting results instead of silently persisting them.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct CombinedDataRetrievalMetadata {
    pub skipped_logs: usize,
    pub fallback_attempts: usize,
    pub fallback_recovered: usize,
    pub partial_failures: Vec<CombinedDataLookupFailure>,
}

impl CombinedDataRetrievalMetadata {
    #[must_use]
    pub fn has_partial_failures(&self) -> bool {
        !self.partial_failures.is_empty()
    }

    #[must_use]
    pub fn skipped_tx_hashes(&self) -> Vec<TxHash> {
        self.partial_failures
            .iter()
            .map(|failure| failure.tx_hash)
            .collect()
    }

    pub fn record_fallback_attempts(&mut self, attempts: usize) {
        self.fallback_attempts += attempts;
    }

    pub fn record_fallback_recovery(&mut self) {
        self.fallback_recovered += 1;
    }

    pub fn record_partial_failure(&mut self, failure: CombinedDataLookupFailure) {
        self.skipped_logs += 1;
        self.partial_failures.push(failure);
    }

    pub fn merge(&mut self, other: &CombinedDataRetrievalMetadata) {
        self.skipped_logs += other.skipped_logs;
        self.fallback_attempts += other.fallback_attempts;
        self.fallback_recovered += other.fallback_recovered;
        self.partial_failures
            .extend(other.partial_failures.iter().cloned());
    }
}

/// Aggregated result for combined data retrieval over a block range.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CombinedDataResult {
    pub chain: NamedChain,
    pub from_address: Address,
    pub to_address: Address,
    pub token_address: Address,
    pub total_l2_execution_cost: U256,
    pub total_blob_gas_cost: U256,
    pub total_l1_fee: U256,
    pub overall_total_gas_cost: U256,
    pub total_amount_transferred: U256,
    pub transaction_count: TransactionCount,
    pub transactions_data: Vec<GasAndAmountForTx>,
    #[serde(default)]
    pub retrieval_metadata: CombinedDataRetrievalMetadata,
}

impl CombinedDataResult {
    #[must_use]
    pub fn new(
        chain: NamedChain,
        from_address: Address,
        to_address: Address,
        token_address: Address,
    ) -> Self {
        Self {
            chain,
            from_address,
            to_address,
            token_address,
            total_l2_execution_cost: U256::ZERO,
            total_blob_gas_cost: U256::ZERO,
            total_l1_fee: U256::ZERO,
            overall_total_gas_cost: U256::ZERO,
            total_amount_transferred: U256::ZERO,
            transaction_count: TransactionCount::new(0),
            transactions_data: Vec::new(),
            retrieval_metadata: CombinedDataRetrievalMetadata::default(),
        }
    }

    pub fn add_transaction_data(&mut self, data: GasAndAmountForTx) {
        let l2_execution_cost = data.gas_used * data.effective_gas_price;
        self.total_l2_execution_cost = self
            .total_l2_execution_cost
            .saturating_add(l2_execution_cost);
        self.total_blob_gas_cost = self.total_blob_gas_cost.saturating_add(data.blob_gas_cost);
        self.total_l1_fee = self
            .total_l1_fee
            .saturating_add(data.l1_fee.unwrap_or_default());
        self.overall_total_gas_cost = self
            .overall_total_gas_cost
            .saturating_add(data.total_gas_cost());
        self.total_amount_transferred = self
            .total_amount_transferred
            .saturating_add(data.transferred_amount);
        self.transaction_count += TransactionCount::new(1);
        self.transactions_data.push(data);
    }

    /// Merge another result into this one (for combining results from multiple block ranges)
    pub fn merge(&mut self, other: &CombinedDataResult) {
        self.total_l2_execution_cost = self
            .total_l2_execution_cost
            .saturating_add(other.total_l2_execution_cost);
        self.total_blob_gas_cost = self
            .total_blob_gas_cost
            .saturating_add(other.total_blob_gas_cost);
        self.total_l1_fee = self.total_l1_fee.saturating_add(other.total_l1_fee);
        self.overall_total_gas_cost = self
            .overall_total_gas_cost
            .saturating_add(other.overall_total_gas_cost);
        self.total_amount_transferred = self
            .total_amount_transferred
            .saturating_add(other.total_amount_transferred);
        self.transaction_count += other.transaction_count;
        self.transactions_data
            .extend(other.transactions_data.iter().cloned());
        self.retrieval_metadata.merge(&other.retrieval_metadata);
    }

    #[must_use]
    pub fn is_partial(&self) -> bool {
        self.retrieval_metadata.has_partial_failures()
    }
}

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

    fn create_test_tx(
        gas_used: u64,
        gas_price: u64,
        l1_fee: Option<u64>,
        blob_gas_cost: u64,
        transferred_amount: u64,
    ) -> GasAndAmountForTx {
        GasAndAmountForTx {
            tx_hash: TxHash::ZERO,
            block_number: 1000,
            gas_used: GasAmount::from(gas_used),
            effective_gas_price: GasPrice::from(gas_price),
            l1_fee: l1_fee.map(U256::from),
            blob_gas_cost: U256::from(blob_gas_cost),
            transferred_amount: U256::from(transferred_amount),
        }
    }

    #[test]
    fn test_total_gas_cost_basic() {
        // Test basic calculation with L2 gas only
        let tx = create_test_tx(
            21000, // gas used
            50,    // gas price
            None,  // no L1 fee
            0,     // no blob gas
            1000,  // transferred amount
        );

        let total = tx.total_gas_cost();
        let expected = U256::from(21000 * 50); // gas_used * gas_price

        assert_eq!(total, expected, "Should calculate L2 gas cost correctly");
    }

    #[test]
    fn test_total_gas_cost_with_l1_fee() {
        // Test calculation including L1 fee
        let tx = create_test_tx(
            50000,        // gas used
            100,          // gas price
            Some(200000), // L1 fee
            0,            // no blob gas
            5000,
        );

        let total = tx.total_gas_cost();
        let expected = U256::from(50000 * 100 + 200000); // (gas_used * gas_price) + l1_fee

        assert_eq!(
            total, expected,
            "Should include L1 fee in total gas cost calculation"
        );
    }

    #[test]
    fn test_total_gas_cost_with_blob_gas() {
        // Test calculation including blob gas cost
        let tx = create_test_tx(
            30000,  // gas used
            75,     // gas price
            None,   // no L1 fee
            100000, // blob gas cost
            2500,
        );

        let total = tx.total_gas_cost();
        let expected = U256::from(30000 * 75 + 100000); // (gas_used * gas_price) + blob_gas_cost

        assert_eq!(
            total, expected,
            "Should include blob gas cost in total calculation"
        );
    }

    #[test]
    fn test_total_gas_cost_all_components() {
        // Test calculation with all components: L2 gas, L1 fee, and blob gas
        let tx = create_test_tx(
            100000,       // gas used
            200,          // gas price
            Some(500000), // L1 fee
            300000,       // blob gas cost
            10000,
        );

        let total = tx.total_gas_cost();
        let expected = U256::from(100000 * 200 + 500000 + 300000);

        assert_eq!(
            total, expected,
            "Should correctly sum all gas cost components"
        );
    }

    #[test]
    fn test_total_gas_cost_large_values() {
        // Test with large values to ensure no overflow
        let large_gas = u64::MAX / 2;
        let large_price = 100;

        let tx = create_test_tx(large_gas, large_price, Some(1_000_000), 500_000, 100_000);

        let total = tx.total_gas_cost();
        let expected_l2_cost = U256::from(large_gas) * U256::from(large_price);
        let expected = expected_l2_cost + U256::from(1_000_000) + U256::from(500_000);

        assert_eq!(total, expected, "Should handle large values correctly");
    }

    #[test]
    fn test_total_gas_cost_saturating_arithmetic() {
        // Test that the calculation uses saturating arithmetic
        // This test verifies the function doesn't panic on large inputs
        let max_gas = u64::MAX;
        let max_price = u64::MAX;

        let tx = create_test_tx(max_gas, max_price, Some(u64::MAX), u64::MAX, 1000);

        // Should not panic - saturating_mul and saturating_add prevent overflow
        let total = tx.total_gas_cost();

        // The result should be saturated at U256::MAX
        assert!(
            total > U256::ZERO,
            "Should produce non-zero result even with overflow"
        );
    }

    #[test]
    fn test_clone_and_equality() {
        // Test that GasAndAmountForTx implements Clone and PartialEq correctly
        let tx1 = create_test_tx(21000, 50, None, 0, 1000);
        let tx2 = tx1.clone();

        assert_eq!(tx1, tx2, "Cloned transactions should be equal");
        assert_eq!(
            tx1.total_gas_cost(),
            tx2.total_gas_cost(),
            "Total costs should match"
        );
    }

    #[test]
    fn test_debug_representation() {
        // Test that Debug trait is implemented
        let tx = create_test_tx(21000, 50, Some(1000), 500, 2000);

        let debug_str = format!("{:?}", tx);

        // Should contain key fields
        assert!(
            debug_str.contains("gas_used"),
            "Debug output should include gas_used"
        );
        assert!(
            debug_str.contains("tx_hash"),
            "Debug output should include tx_hash"
        );
    }

    #[test]
    fn test_combined_result_is_partial_when_metadata_has_failures() {
        let mut result = CombinedDataResult::new(
            NamedChain::Mainnet,
            Address::ZERO,
            Address::ZERO,
            Address::ZERO,
        );

        result
            .retrieval_metadata
            .record_partial_failure(CombinedDataLookupFailure {
                tx_hash: TxHash::repeat_byte(0x11),
                block_number: 123,
                transfer_value: U256::from(42_u64),
                attempts: vec![CombinedDataLookupAttempt {
                    pass: CombinedDataLookupPass::Batch,
                    stage: CombinedDataLookupStage::Transaction,
                    error: "RPC error".to_string(),
                    error_chain: vec!["RPC error".to_string(), "inner transport".to_string()],
                    transport_error: Some("inner transport".to_string()),
                }],
            });

        assert!(result.is_partial());
        assert!(result.retrieval_metadata.has_partial_failures());
        assert_eq!(
            result.retrieval_metadata.skipped_tx_hashes(),
            vec![TxHash::repeat_byte(0x11)]
        );
    }

    #[test]
    fn test_metadata_has_partial_failures_tracks_failure_entries_not_skip_counter() {
        let metadata = CombinedDataRetrievalMetadata {
            skipped_logs: 0,
            fallback_attempts: 0,
            fallback_recovered: 0,
            partial_failures: vec![CombinedDataLookupFailure {
                tx_hash: TxHash::repeat_byte(0x22),
                block_number: 456,
                transfer_value: U256::from(7_u64),
                attempts: vec![CombinedDataLookupAttempt {
                    pass: CombinedDataLookupPass::Batch,
                    stage: CombinedDataLookupStage::Receipt,
                    error: "missing receipt".to_string(),
                    error_chain: vec!["missing receipt".to_string()],
                    transport_error: None,
                }],
            }],
        };

        assert!(metadata.has_partial_failures());
    }

    #[test]
    fn test_combined_result_merge_includes_retrieval_metadata() {
        let mut left = CombinedDataResult::new(
            NamedChain::Mainnet,
            Address::ZERO,
            Address::ZERO,
            Address::ZERO,
        );
        let mut right = CombinedDataResult::new(
            NamedChain::Mainnet,
            Address::ZERO,
            Address::ZERO,
            Address::ZERO,
        );

        left.retrieval_metadata.fallback_attempts = 1;
        left.retrieval_metadata.fallback_recovered = 1;
        right
            .retrieval_metadata
            .record_partial_failure(CombinedDataLookupFailure {
                tx_hash: TxHash::repeat_byte(0x22),
                block_number: 456,
                transfer_value: U256::from(99_u64),
                attempts: vec![CombinedDataLookupAttempt {
                    pass: CombinedDataLookupPass::SerialFallback,
                    stage: CombinedDataLookupStage::Receipt,
                    error: "missing receipt".to_string(),
                    error_chain: vec!["missing receipt".to_string()],
                    transport_error: None,
                }],
            });

        left.merge(&right);

        assert_eq!(left.retrieval_metadata.fallback_attempts, 1);
        assert_eq!(left.retrieval_metadata.fallback_recovered, 1);
        assert_eq!(left.retrieval_metadata.skipped_logs, 1);
        assert_eq!(left.retrieval_metadata.partial_failures.len(), 1);
        assert_eq!(
            left.retrieval_metadata.skipped_tx_hashes(),
            vec![TxHash::repeat_byte(0x22)]
        );
    }
}