event_scanner/block_range_scanner/
builder.rs1use alloy::network::Network;
2
3use robust_provider::IntoRobustProvider;
4
5use crate::{
6 ScannerError,
7 block_range_scanner::{
8 DEFAULT_MAX_BLOCK_RANGE, DEFAULT_STREAM_BUFFER_CAPACITY, RingBufferCapacity,
9 scanner::BlockRangeScanner,
10 },
11};
12
13#[derive(Clone, Debug)]
15pub struct BlockRangeScannerBuilder {
16 pub max_block_range: u64,
18 pub past_blocks_storage_capacity: RingBufferCapacity,
22 pub buffer_capacity: usize,
23}
24
25impl Default for BlockRangeScannerBuilder {
26 fn default() -> Self {
27 Self::new()
28 }
29}
30
31impl BlockRangeScannerBuilder {
32 #[must_use]
34 pub fn new() -> Self {
35 Self {
36 max_block_range: DEFAULT_MAX_BLOCK_RANGE,
37 past_blocks_storage_capacity: RingBufferCapacity::Limited(10),
38 buffer_capacity: DEFAULT_STREAM_BUFFER_CAPACITY,
39 }
40 }
41
42 #[must_use]
48 pub fn max_block_range(mut self, max_block_range: u64) -> Self {
49 self.max_block_range = max_block_range;
50 self
51 }
52
53 #[must_use]
57 pub fn past_blocks_storage_capacity(
58 mut self,
59 past_blocks_storage_capacity: RingBufferCapacity,
60 ) -> Self {
61 self.past_blocks_storage_capacity = past_blocks_storage_capacity;
62 self
63 }
64
65 #[must_use]
74 pub fn buffer_capacity(mut self, buffer_capacity: usize) -> Self {
75 self.buffer_capacity = buffer_capacity;
76 self
77 }
78
79 pub async fn connect<N: Network>(
85 self,
86 provider: impl IntoRobustProvider<N>,
87 ) -> Result<BlockRangeScanner<N>, ScannerError> {
88 if self.max_block_range == 0 {
89 return Err(ScannerError::InvalidMaxBlockRange);
90 }
91 if self.buffer_capacity == 0 {
92 return Err(ScannerError::InvalidBufferCapacity);
93 }
94 let provider = provider.into_robust_provider().await?;
95 Ok(BlockRangeScanner::new(
96 provider,
97 self.max_block_range,
98 self.past_blocks_storage_capacity,
99 self.buffer_capacity,
100 ))
101 }
102}