openzeppelin_monitor/models/core/
monitor.rs

1use serde::{Deserialize, Serialize};
2
3use crate::models::{blockchain::ContractSpec, ChainConfiguration};
4
5/// Configuration for monitoring specific blockchain activity.
6///
7/// A Monitor defines what blockchain activity to watch for through a combination of:
8/// - Network targets (which chains to monitor)
9/// - Contract addresses to watch
10/// - Conditions to match (functions, events, transactions)
11/// - Triggers conditions refers to a custom filter script that being executed apply extra filters
12///   to the matched transactions before triggering the notifications
13/// - Triggers to execute when conditions are met
14#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)]
15#[serde(deny_unknown_fields)]
16pub struct Monitor {
17	/// Unique name identifying this monitor
18	pub name: String,
19
20	/// List of network slugs this monitor should watch
21	pub networks: Vec<String>,
22
23	/// Whether this monitor is currently paused
24	pub paused: bool,
25
26	/// Contract addresses to monitor, optionally with their contract specs
27	pub addresses: Vec<AddressWithSpec>,
28
29	/// Conditions that should trigger this monitor
30	pub match_conditions: MatchConditions,
31
32	/// Conditions that should be met prior to triggering notifications
33	pub trigger_conditions: Vec<TriggerConditions>,
34
35	/// IDs of triggers to execute when conditions match
36	pub triggers: Vec<String>,
37
38	/// Chain-specific configurations
39	#[serde(default)]
40	pub chain_configurations: Vec<ChainConfiguration>,
41}
42
43/// Contract address with optional ABI for decoding transactions and events
44#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
45#[serde(deny_unknown_fields)]
46pub struct AddressWithSpec {
47	/// Contract address in the network's native format
48	pub address: String,
49
50	/// Optional contract spec for decoding contract interactions
51	pub contract_spec: Option<ContractSpec>,
52}
53
54/// Collection of conditions that can trigger a monitor
55#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Default)]
56#[serde(deny_unknown_fields)]
57pub struct MatchConditions {
58	/// Function calls to match
59	pub functions: Vec<FunctionCondition>,
60
61	/// Events to match
62	pub events: Vec<EventCondition>,
63
64	/// Transaction states to match
65	pub transactions: Vec<TransactionCondition>,
66}
67
68/// Condition for matching contract function calls
69#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
70#[serde(deny_unknown_fields)]
71pub struct FunctionCondition {
72	/// Function signature (e.g., "transfer(address,uint256)")
73	pub signature: String,
74
75	/// Optional expression to filter function parameters
76	pub expression: Option<String>,
77}
78
79/// Condition for matching contract events
80#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
81#[serde(deny_unknown_fields)]
82pub struct EventCondition {
83	/// Event signature (e.g., "Transfer(address,address,uint256)")
84	pub signature: String,
85
86	/// Optional expression to filter event parameters
87	pub expression: Option<String>,
88}
89
90/// Condition for matching transaction states
91#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
92#[serde(deny_unknown_fields)]
93pub struct TransactionCondition {
94	/// Required transaction status
95	pub status: TransactionStatus,
96
97	/// Optional expression to filter transaction properties
98	pub expression: Option<String>,
99}
100
101/// Possible transaction execution states
102#[derive(Debug, Copy, Clone, Deserialize, Serialize, PartialEq)]
103#[serde(deny_unknown_fields)]
104pub enum TransactionStatus {
105	/// Match any transaction status
106	Any,
107	/// Match only successful transactions
108	Success,
109	/// Match only failed transactions
110	Failure,
111}
112
113/// Conditions that should be met prior to triggering notifications
114#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
115#[serde(deny_unknown_fields)]
116pub struct TriggerConditions {
117	/// The path to the script
118	pub script_path: String,
119
120	/// The arguments of the script
121	#[serde(default)]
122	pub arguments: Option<Vec<String>>,
123
124	/// The language of the script
125	pub language: ScriptLanguage,
126
127	/// The timeout of the script
128	pub timeout_ms: u32,
129}
130/// The possible languages of the script
131#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Hash, Eq)]
132pub enum ScriptLanguage {
133	JavaScript,
134	Python,
135	Bash,
136}
137
138/// Static mapping of script languages to their file extensions
139pub const SCRIPT_LANGUAGE_EXTENSIONS: &[(&ScriptLanguage, &str)] = &[
140	(&ScriptLanguage::Python, "py"),
141	(&ScriptLanguage::JavaScript, "js"),
142	(&ScriptLanguage::Bash, "sh"),
143];