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