event_scanner/event_scanner/scanner/
live.rs1use alloy::network::Network;
2
3use super::common::{ConsumerMode, handle_stream};
4use crate::{
5 EventScannerBuilder, ScannerError,
6 event_scanner::{EventScanner, scanner::Live},
7};
8
9impl EventScannerBuilder<Live> {
10 #[must_use]
11 pub fn block_confirmations(mut self, confirmations: u64) -> Self {
12 self.config.block_confirmations = confirmations;
13 self
14 }
15}
16
17impl<N: Network> EventScanner<Live, N> {
18 pub async fn start(self) -> Result<(), ScannerError> {
32 let client = self.block_range_scanner.run()?;
33 let stream = client.stream_live(self.config.block_confirmations).await?;
34
35 let provider = self.block_range_scanner.provider().clone();
36 let listeners = self.listeners.clone();
37
38 tokio::spawn(async move {
39 handle_stream(stream, &provider, &listeners, ConsumerMode::Stream).await;
40 });
41
42 Ok(())
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_live_scanner_builder_pattern() {
52 let builder = EventScannerBuilder::live().max_block_range(25).block_confirmations(5);
53
54 assert_eq!(builder.block_range_scanner.max_block_range, 25);
55 assert_eq!(builder.config.block_confirmations, 5);
56 }
57
58 #[test]
59 fn test_live_scanner_builder_with_zero_confirmations() {
60 let builder = EventScannerBuilder::live().block_confirmations(0).max_block_range(100);
61
62 assert_eq!(builder.config.block_confirmations, 0);
63 assert_eq!(builder.block_range_scanner.max_block_range, 100);
64 }
65
66 #[test]
67 fn test_live_scanner_builder_last_call_wins() {
68 let builder = EventScannerBuilder::live()
69 .max_block_range(25)
70 .max_block_range(55)
71 .max_block_range(105)
72 .block_confirmations(2)
73 .block_confirmations(4)
74 .block_confirmations(8);
75
76 assert_eq!(builder.block_range_scanner.max_block_range, 105);
77 assert_eq!(builder.config.block_confirmations, 8);
78 }
79}