openzeppelin_monitor/services/filter/filters/
mod.rs

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
//! Block filtering implementations.
//!
//! Provides trait definition and implementations for filtering blocks
//! across different blockchain types. Includes:
//! - Generic BlockFilter trait
//! - EVM-specific implementation
//! - Stellar-specific implementation

pub mod evm {
	pub mod filter;
	pub mod helpers;
}
pub mod stellar {
	pub mod filter;
	pub mod helpers;
}

use async_trait::async_trait;

use crate::{
	models::{BlockType, ContractSpec, Monitor, MonitorMatch, Network},
	services::{blockchain::BlockFilterFactory, filter::error::FilterError},
};
pub use evm::filter::EVMBlockFilter;
pub use stellar::filter::StellarBlockFilter;

/// Trait for filtering blockchain data
///
/// This trait must be implemented by all blockchain-specific clients to provide
/// a way to filter blockchain data.
#[async_trait]
pub trait BlockFilter {
	type Client;
	async fn filter_block(
		&self,
		client: &Self::Client,
		network: &Network,
		block: &BlockType,
		monitors: &[Monitor],
		contract_specs: Option<&[(String, ContractSpec)]>,
	) -> Result<Vec<MonitorMatch>, FilterError>;
}

/// Service for filtering blockchain data
///
/// This service provides a way to filter blockchain data based on a set of monitors.
pub struct FilterService {}

impl FilterService {
	pub fn new() -> Self {
		FilterService {}
	}
}

impl Default for FilterService {
	fn default() -> Self {
		Self::new()
	}
}

impl FilterService {
	pub async fn filter_block<T: BlockFilterFactory<T>>(
		&self,
		client: &T,
		network: &Network,
		block: &BlockType,
		monitors: &[Monitor],
		contract_specs: Option<&[(String, ContractSpec)]>,
	) -> Result<Vec<MonitorMatch>, FilterError> {
		let filter = T::filter();
		filter
			.filter_block(client, network, block, monitors, contract_specs)
			.await
	}
}