openzeppelin_monitor/utils/tests/builders/midnight/
event.rs1use crate::models::{
2 MidnightCallDetails, MidnightClaimMintDetails, MidnightDeploymentDetails, MidnightEvent,
3 MidnightEventType, MidnightMaintainDetails, MidnightPayoutDetails, MidnightPhase,
4 MidnightTopics, MidnightTxAppliedDetails,
5};
6
7#[derive(Debug)]
9pub struct EventBuilder {
10 event: MidnightEvent,
11}
12
13impl Default for EventBuilder {
14 fn default() -> Self {
16 Self {
17 event: MidnightEvent::from(MidnightEventType::Unknown(
18 "Unknown event type".to_string(),
19 )),
20 }
21 }
22}
23
24impl EventBuilder {
25 pub fn new() -> Self {
27 Self::default()
28 }
29
30 pub fn event_type(mut self, event_type: MidnightEventType) -> Self {
32 self.event = MidnightEvent::from(event_type);
33 self
34 }
35
36 pub fn tx_applied(mut self, tx_hash: String) -> Self {
38 self.event = MidnightEvent::from(MidnightEventType::MidnightTxApplied(
39 MidnightTxAppliedDetails {
40 phase: MidnightPhase::default(),
41 topics: MidnightTopics::default(),
42 tx_hash,
43 },
44 ));
45 self
46 }
47
48 pub fn only_guaranteed_tx_applied(mut self, tx_hash: String) -> Self {
50 self.event = MidnightEvent::from(MidnightEventType::MidnightOnlyGuaranteedTxApplied(
51 MidnightTxAppliedDetails {
52 phase: MidnightPhase::default(),
53 topics: MidnightTopics::default(),
54 tx_hash,
55 },
56 ));
57 self
58 }
59
60 pub fn call_contract(mut self, address: String, tx_hash: String) -> Self {
62 self.event = MidnightEvent::from(MidnightEventType::MidnightCallContract(
63 MidnightCallDetails {
64 phase: MidnightPhase::default(),
65 topics: MidnightTopics::default(),
66 address,
67 tx_hash,
68 },
69 ));
70 self
71 }
72
73 pub fn deploy_contract(mut self, address: String, tx_hash: String) -> Self {
75 self.event = MidnightEvent::from(MidnightEventType::MidnightDeployContract(
76 MidnightDeploymentDetails {
77 phase: MidnightPhase::default(),
78 topics: MidnightTopics::default(),
79 address,
80 tx_hash,
81 },
82 ));
83 self
84 }
85
86 pub fn maintain_contract(mut self, address: String, tx_hash: String) -> Self {
88 self.event = MidnightEvent::from(MidnightEventType::MidnightMaintainContract(
89 MidnightMaintainDetails {
90 phase: MidnightPhase::default(),
91 topics: MidnightTopics::default(),
92 address,
93 tx_hash,
94 },
95 ));
96 self
97 }
98
99 pub fn payout_minted(mut self, amount: u128, receiver: String) -> Self {
101 self.event = MidnightEvent::from(MidnightEventType::MidnightPayoutMinted(
102 MidnightPayoutDetails {
103 phase: MidnightPhase::default(),
104 topics: MidnightTopics::default(),
105 amount,
106 receiver,
107 },
108 ));
109 self
110 }
111
112 pub fn claim_mint(mut self, coin_type: String, value: u128, tx_hash: String) -> Self {
114 self.event = MidnightEvent::from(MidnightEventType::MidnightClaimMint(
115 MidnightClaimMintDetails {
116 phase: MidnightPhase::default(),
117 topics: MidnightTopics::default(),
118 coin_type,
119 value,
120 tx_hash,
121 },
122 ));
123 self
124 }
125
126 pub fn topics(mut self, topics: Vec<String>) -> Self {
128 match &mut self.event {
129 MidnightEvent(event_type) => match event_type {
130 MidnightEventType::MidnightTxApplied(details) => {
131 details.topics = MidnightTopics { topics }
132 }
133 MidnightEventType::MidnightOnlyGuaranteedTxApplied(details) => {
134 details.topics = MidnightTopics { topics }
135 }
136 MidnightEventType::MidnightCallContract(details) => {
137 details.topics = MidnightTopics { topics }
138 }
139 MidnightEventType::MidnightDeployContract(details) => {
140 details.topics = MidnightTopics { topics }
141 }
142 MidnightEventType::MidnightMaintainContract(details) => {
143 details.topics = MidnightTopics { topics }
144 }
145 MidnightEventType::MidnightPayoutMinted(details) => {
146 details.topics = MidnightTopics { topics }
147 }
148 MidnightEventType::MidnightClaimMint(details) => {
149 details.topics = MidnightTopics { topics }
150 }
151 MidnightEventType::Unknown(_) => (),
152 },
153 }
154 self
155 }
156
157 pub fn phase(mut self, phase: MidnightPhase) -> Self {
159 match &mut self.event {
160 MidnightEvent(event_type) => match event_type {
161 MidnightEventType::MidnightTxApplied(details) => details.phase = phase,
162 MidnightEventType::MidnightOnlyGuaranteedTxApplied(details) => {
163 details.phase = phase
164 }
165 MidnightEventType::MidnightCallContract(details) => details.phase = phase,
166 MidnightEventType::MidnightDeployContract(details) => details.phase = phase,
167 MidnightEventType::MidnightMaintainContract(details) => details.phase = phase,
168 MidnightEventType::MidnightPayoutMinted(details) => details.phase = phase,
169 MidnightEventType::MidnightClaimMint(details) => details.phase = phase,
170 MidnightEventType::Unknown(_) => (),
171 },
172 }
173 self
174 }
175
176 pub fn build(self) -> MidnightEvent {
178 self.event
179 }
180}
181
182#[cfg(test)]
183mod tests {
184 use super::*;
185
186 #[test]
187 fn test_builder_default() {
188 let event = EventBuilder::new().build();
189 match &*event {
190 MidnightEventType::Unknown(_) => (),
191 _ => panic!("Expected Unknown event type"),
192 }
193 }
194
195 #[test]
196 fn test_builder_tx_applied() {
197 let tx_hash = "0x123".to_string();
198 let event = EventBuilder::new().tx_applied(tx_hash.clone()).build();
199 assert!(event.is_tx_applied());
200 assert_eq!(event.get_tx_hash(), Some(tx_hash));
201 }
202
203 #[test]
204 fn test_builder_only_guaranteed_tx_applied() {
205 let tx_hash = "0x123".to_string();
206 let event = EventBuilder::new()
207 .only_guaranteed_tx_applied(tx_hash.clone())
208 .build();
209 assert!(event.is_only_guaranteed_tx_applied());
210 assert_eq!(event.get_tx_hash(), Some(tx_hash));
211 }
212
213 #[test]
214 fn test_builder_call_contract() {
215 let address = "0x123".to_string();
216 let tx_hash = "0x456".to_string();
217 let event = EventBuilder::new()
218 .call_contract(address.clone(), tx_hash.clone())
219 .build();
220 assert_eq!(event.get_tx_hash(), Some(tx_hash));
221 }
222
223 #[test]
224 fn test_builder_deploy_contract() {
225 let address = "0x123".to_string();
226 let tx_hash = "0x456".to_string();
227 let event = EventBuilder::new()
228 .deploy_contract(address.clone(), tx_hash.clone())
229 .build();
230 assert_eq!(event.get_tx_hash(), Some(tx_hash));
231 }
232
233 #[test]
234 fn test_builder_maintain_contract() {
235 let address = "0x123".to_string();
236 let tx_hash = "0x456".to_string();
237 let event = EventBuilder::new()
238 .maintain_contract(address.clone(), tx_hash.clone())
239 .build();
240 assert_eq!(event.get_tx_hash(), Some(tx_hash));
241 }
242
243 #[test]
244 fn test_builder_payout_minted() {
245 let amount = 100u128;
246 let receiver = "0x123".to_string();
247 let event = EventBuilder::new()
248 .payout_minted(amount, receiver.clone())
249 .build();
250 assert_eq!(event.get_tx_hash(), None);
251 }
252
253 #[test]
254 fn test_builder_claim_mint() {
255 let coin_type = "ETH".to_string();
256 let value = 100u128;
257 let tx_hash = "0x123".to_string();
258 let event = EventBuilder::new()
259 .claim_mint(coin_type.clone(), value, tx_hash.clone())
260 .build();
261 assert_eq!(event.get_tx_hash(), Some(tx_hash));
262 }
263
264 #[test]
265 fn test_builder_with_topics() {
266 let topics = vec!["topic1".to_string(), "topic2".to_string()];
267 let event = EventBuilder::new()
268 .tx_applied("0x123".to_string())
269 .topics(topics.clone())
270 .build();
271 match &*event {
272 MidnightEventType::MidnightTxApplied(details) => {
273 assert_eq!(details.topics, MidnightTopics { topics });
274 }
275 _ => panic!("Expected MidnightTxApplied event type"),
276 }
277 }
278
279 #[test]
280 fn test_builder_with_phase() {
281 let phase = MidnightPhase::ApplyExtrinsic(1);
282 let event = EventBuilder::new()
283 .tx_applied("0x123".to_string())
284 .phase(phase.clone())
285 .build();
286 match &*event {
287 MidnightEventType::MidnightTxApplied(details) => {
288 assert_eq!(details.phase, phase);
289 }
290 _ => panic!("Expected MidnightTxApplied event type"),
291 }
292 }
293}