openzeppelin_monitor/services/filter/filters/
mod.rs

1//! Block filtering implementations.
2//!
3//! Provides trait definition and implementations for filtering blocks
4//! across different blockchain types. Includes:
5//! - Generic BlockFilter trait
6//! - EVM-specific implementation
7//! - Stellar-specific implementation
8//! - Midnight-specific implementation
9//! - Solana-specific implementation
10
11pub mod evm {
12	pub mod evaluator;
13	pub mod filter;
14	pub mod helpers;
15}
16pub mod stellar {
17	pub mod evaluator;
18	pub mod filter;
19	pub mod helpers;
20}
21
22pub mod midnight {
23	pub mod filter;
24	pub mod helpers;
25}
26
27pub mod solana {
28	pub mod evaluator;
29	pub mod filter;
30	pub mod helpers;
31}
32
33use async_trait::async_trait;
34
35use crate::{
36	models::{BlockType, ContractSpec, Monitor, MonitorMatch, Network},
37	services::{blockchain::BlockFilterFactory, filter::error::FilterError},
38};
39
40/// Trait for filtering blockchain data
41///
42/// This trait must be implemented by all blockchain-specific clients to provide
43/// a way to filter blockchain data.
44#[async_trait]
45pub trait BlockFilter {
46	type Client;
47	async fn filter_block(
48		&self,
49		client: &Self::Client,
50		network: &Network,
51		block: &BlockType,
52		monitors: &[Monitor],
53		contract_specs: Option<&[(String, ContractSpec)]>,
54	) -> Result<Vec<MonitorMatch>, FilterError>;
55}
56
57#[async_trait]
58pub trait FilterServiceTrait: Send + Sync {
59	async fn filter_block<T: BlockFilterFactory<T> + Send + Sync + 'static>(
60		&self,
61		client: &T,
62		network: &Network,
63		block: &BlockType,
64		monitors: &[Monitor],
65		contract_specs: Option<&[(String, ContractSpec)]>,
66	) -> Result<Vec<MonitorMatch>, FilterError>;
67}
68
69/// Service for filtering blockchain data
70///
71/// This service provides a way to filter blockchain data based on a set of monitors.
72pub struct FilterService {}
73
74impl FilterService {
75	pub fn new() -> Self {
76		FilterService {}
77	}
78}
79
80impl Default for FilterService {
81	fn default() -> Self {
82		Self::new()
83	}
84}
85
86impl FilterService {
87	pub async fn filter_block<T: BlockFilterFactory<T>>(
88		&self,
89		client: &T,
90		network: &Network,
91		block: &BlockType,
92		monitors: &[Monitor],
93		contract_specs: Option<&[(String, ContractSpec)]>,
94	) -> Result<Vec<MonitorMatch>, FilterError> {
95		let filter = T::filter();
96		filter
97			.filter_block(client, network, block, monitors, contract_specs)
98			.await
99	}
100}
101
102#[async_trait]
103impl FilterServiceTrait for FilterService {
104	async fn filter_block<T: BlockFilterFactory<T> + Send + Sync + 'static>(
105		&self,
106		client: &T,
107		network: &Network,
108		block: &BlockType,
109		monitors: &[Monitor],
110		contract_specs: Option<&[(String, ContractSpec)]>,
111	) -> Result<Vec<MonitorMatch>, FilterError> {
112		let filter = T::filter();
113		filter
114			.filter_block(client, network, block, monitors, contract_specs)
115			.await
116	}
117}