1use serde::{Deserialize, Serialize};
12use serde_json::Value;
13use std::ops::{Deref, DerefMut};
14
15#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
22pub enum Phase {
23 ApplyExtrinsic(u32),
26 Finalization,
28 Initialization,
30}
31
32impl Default for Phase {
33 fn default() -> Self {
34 Self::ApplyExtrinsic(0)
35 }
36}
37
38#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Default)]
43pub struct Topics {
44 pub topics: Vec<String>,
46}
47
48#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
53pub struct TxAppliedDetails {
54 pub phase: Phase,
56 pub topics: Topics,
58 pub tx_hash: String,
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
67pub struct MaintainDetails {
68 pub phase: Phase,
70 pub topics: Topics,
72 pub address: String,
74 pub tx_hash: String,
76}
77
78#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
83pub struct DeploymentDetails {
84 pub phase: Phase,
86 pub topics: Topics,
88 pub address: String,
90 pub tx_hash: String,
92}
93
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
99pub struct CallDetails {
100 pub phase: Phase,
102 pub topics: Topics,
104 pub address: String,
106 pub tx_hash: String,
108}
109
110#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
115pub struct ClaimMintDetails {
116 pub phase: Phase,
118 pub topics: Topics,
120 pub coin_type: String,
122 pub value: u128,
124 pub tx_hash: String,
126}
127
128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
133pub struct PayoutDetails {
134 pub phase: Phase,
136 pub topics: Topics,
138 pub amount: u128,
140 pub receiver: String,
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize)]
150pub enum EventType {
151 MidnightCallContract(CallDetails),
154 MidnightDeployContract(DeploymentDetails),
157 MidnightTxApplied(TxAppliedDetails),
160 MidnightOnlyGuaranteedTxApplied(TxAppliedDetails),
163 MidnightMaintainContract(MaintainDetails),
166 MidnightPayoutMinted(PayoutDetails),
169 MidnightClaimMint(ClaimMintDetails),
172 Unknown(String),
176}
177
178impl Default for EventType {
179 fn default() -> Self {
180 Self::Unknown(format!(
181 "Unknown event type: {}",
182 std::any::type_name::<Self>()
183 ))
184 }
185}
186
187#[derive(Deserialize, Serialize, Debug, Clone)]
193pub struct Event(pub EventType);
194
195impl Event {
197 pub fn is_tx_applied(&self) -> bool {
199 matches!(self.0, EventType::MidnightTxApplied(_))
200 }
201
202 pub fn is_only_guaranteed_tx_applied(&self) -> bool {
204 matches!(self.0, EventType::MidnightOnlyGuaranteedTxApplied(_))
205 }
206
207 pub fn is_success(&self) -> bool {
212 self.is_tx_applied() || self.is_only_guaranteed_tx_applied()
213 }
214
215 pub fn get_tx_hash(&self) -> Option<String> {
219 match &self.0 {
220 EventType::MidnightTxApplied(details) => Some(details.tx_hash.clone()),
221 EventType::MidnightOnlyGuaranteedTxApplied(details) => Some(details.tx_hash.clone()),
222 EventType::MidnightCallContract(details) => Some(details.tx_hash.clone()),
223 EventType::MidnightDeployContract(details) => Some(details.tx_hash.clone()),
224 EventType::MidnightMaintainContract(details) => Some(details.tx_hash.clone()),
225 EventType::MidnightClaimMint(details) => Some(details.tx_hash.clone()),
226 EventType::MidnightPayoutMinted(_) => None,
227 EventType::Unknown(_) => None,
228 }
229 }
230
231 pub fn get_topics(&self) -> Option<Vec<String>> {
235 match &self.0 {
236 EventType::MidnightTxApplied(details) => Some(details.topics.topics.clone()),
237 EventType::MidnightOnlyGuaranteedTxApplied(details) => {
238 Some(details.topics.topics.clone())
239 }
240 EventType::MidnightCallContract(details) => Some(details.topics.topics.clone()),
241 EventType::MidnightDeployContract(details) => Some(details.topics.topics.clone()),
242 EventType::MidnightMaintainContract(details) => Some(details.topics.topics.clone()),
243 EventType::MidnightPayoutMinted(details) => Some(details.topics.topics.clone()),
244 EventType::MidnightClaimMint(details) => Some(details.topics.topics.clone()),
245 EventType::Unknown(_) => None,
246 }
247 }
248
249 pub fn get_phase(&self) -> Option<Phase> {
253 match &self.0 {
254 EventType::MidnightTxApplied(details) => Some(details.phase.clone()),
255 EventType::MidnightOnlyGuaranteedTxApplied(details) => Some(details.phase.clone()),
256 EventType::MidnightCallContract(details) => Some(details.phase.clone()),
257 EventType::MidnightDeployContract(details) => Some(details.phase.clone()),
258 EventType::MidnightMaintainContract(details) => Some(details.phase.clone()),
259 EventType::MidnightPayoutMinted(details) => Some(details.phase.clone()),
260 EventType::MidnightClaimMint(details) => Some(details.phase.clone()),
261 EventType::Unknown(_) => None,
262 }
263 }
264}
265
266impl Deref for Event {
268 type Target = EventType;
269
270 fn deref(&self) -> &Self::Target {
271 &self.0
272 }
273}
274
275impl DerefMut for Event {
276 fn deref_mut(&mut self) -> &mut Self::Target {
277 &mut self.0
278 }
279}
280
281impl From<EventType> for Event {
282 fn from(event_type: EventType) -> Self {
283 Self(event_type)
284 }
285}
286
287impl From<Event> for EventType {
288 fn from(event: Event) -> Self {
289 event.0
290 }
291}
292
293impl From<Value> for Event {
294 fn from(value: Value) -> Self {
295 match serde_json::from_value::<EventType>(value) {
296 Ok(event_type) => Event(event_type),
297 Err(e) => Event(EventType::Unknown(format!(
298 "Failed to deserialize event: {}",
299 e.to_string().split(",").next().unwrap_or_default()
300 ))),
301 }
302 }
303}
304
305#[cfg(test)]
306mod tests {
307 use super::*;
308 use serde_json::json;
309
310 #[test]
311 fn test_tx_applied_details() {
312 let details = TxAppliedDetails {
313 phase: Phase::default(),
314 topics: Topics::default(),
315 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
316 .to_string(),
317 };
318 assert_eq!(
319 details.tx_hash,
320 "0x0000000000000000000000000000000000000000000000000000000000000000"
321 );
322 }
323
324 #[test]
325 fn test_maintain_details() {
326 let details = MaintainDetails {
327 phase: Phase::default(),
328 topics: Topics::default(),
329 address: "0x123".to_string(),
330 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
331 .to_string(),
332 };
333 assert_eq!(
334 details.tx_hash,
335 "0x0000000000000000000000000000000000000000000000000000000000000000"
336 );
337 assert_eq!(details.address, "0x123");
338 }
339
340 #[test]
341 fn test_deployment_details() {
342 let details = DeploymentDetails {
343 phase: Phase::default(),
344 topics: Topics::default(),
345 address: "0x123".to_string(),
346 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
347 .to_string(),
348 };
349 assert_eq!(
350 details.tx_hash,
351 "0x0000000000000000000000000000000000000000000000000000000000000000"
352 );
353 assert_eq!(details.address, "0x123");
354 }
355
356 #[test]
357 fn test_call_details() {
358 let details = CallDetails {
359 phase: Phase::default(),
360 topics: Topics::default(),
361 address: "0x123".to_string(),
362 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
363 .to_string(),
364 };
365 assert_eq!(
366 details.tx_hash,
367 "0x0000000000000000000000000000000000000000000000000000000000000000"
368 );
369 assert_eq!(details.address, "0x123");
370 }
371
372 #[test]
373 fn test_claim_mint_details() {
374 let details = ClaimMintDetails {
375 phase: Phase::default(),
376 topics: Topics::default(),
377 coin_type: "0x123".to_string(),
378 value: 100u128,
379 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
380 .to_string(),
381 };
382 assert_eq!(
383 details.tx_hash,
384 "0x0000000000000000000000000000000000000000000000000000000000000000"
385 );
386 assert_eq!(details.coin_type, "0x123");
387 assert_eq!(details.value, 100u128);
388 }
389
390 #[test]
391 fn test_payout_details() {
392 let details = PayoutDetails {
393 phase: Phase::default(),
394 topics: Topics::default(),
395 amount: 100u128,
396 receiver: "0x123".to_string(),
397 };
398 assert_eq!(details.amount, 100u128);
399 assert_eq!(details.receiver, "0x123");
400 }
401
402 #[test]
403 fn test_event_type_contract_call() {
404 let call_details = CallDetails {
405 phase: Phase::default(),
406 topics: Topics::default(),
407 address: "0x123".to_string(),
408 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
409 .to_string(),
410 };
411 let event_type = EventType::MidnightCallContract(call_details);
412
413 match event_type {
414 EventType::MidnightCallContract(details) => {
415 assert_eq!(
416 details.tx_hash,
417 "0x0000000000000000000000000000000000000000000000000000000000000000"
418 );
419 assert_eq!(details.address, "0x123");
420 }
421 _ => panic!("Expected MidnightCallContract event type"),
422 }
423 }
424
425 #[test]
426 fn test_event_type_contract_deploy() {
427 let deploy_details = DeploymentDetails {
428 phase: Phase::default(),
429 topics: Topics::default(),
430 address: "0x123".to_string(),
431 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
432 .to_string(),
433 };
434 let event_type = EventType::MidnightDeployContract(deploy_details);
435
436 match event_type {
437 EventType::MidnightDeployContract(details) => {
438 assert_eq!(
439 details.tx_hash,
440 "0x0000000000000000000000000000000000000000000000000000000000000000"
441 );
442 assert_eq!(details.address, "0x123");
443 }
444 _ => panic!("Expected MidnightDeployContract event type"),
445 }
446 }
447
448 #[test]
449 fn test_event_type_tx_applied() {
450 let tx_details = TxAppliedDetails {
451 phase: Phase::default(),
452 topics: Topics::default(),
453 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
454 .to_string(),
455 };
456 let event_type = EventType::MidnightTxApplied(tx_details);
457
458 match event_type {
459 EventType::MidnightTxApplied(details) => {
460 assert_eq!(
461 details.tx_hash,
462 "0x0000000000000000000000000000000000000000000000000000000000000000"
463 );
464 }
465 _ => panic!("Expected MidnightTxApplied event type"),
466 }
467 }
468
469 #[test]
470 fn test_event_deref() {
471 let tx_details = TxAppliedDetails {
472 phase: Phase::default(),
473 topics: Topics::default(),
474 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
475 .to_string(),
476 };
477 let event_type = EventType::MidnightTxApplied(tx_details);
478 let event = Event(event_type);
479
480 match &*event {
481 EventType::MidnightTxApplied(details) => {
482 assert_eq!(
483 details.tx_hash,
484 "0x0000000000000000000000000000000000000000000000000000000000000000"
485 );
486 }
487 _ => panic!("Expected MidnightTxApplied event type"),
488 }
489 }
490
491 #[test]
492 fn test_event_from_event_type() {
493 let tx_details = TxAppliedDetails {
494 phase: Phase::default(),
495 topics: Topics::default(),
496 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
497 .to_string(),
498 };
499 let event_type = EventType::MidnightTxApplied(tx_details);
500 let event = Event::from(event_type);
501
502 match &*event {
503 EventType::MidnightTxApplied(details) => {
504 assert_eq!(
505 details.tx_hash,
506 "0x0000000000000000000000000000000000000000000000000000000000000000"
507 );
508 }
509 _ => panic!("Expected MidnightTxApplied event type"),
510 }
511 }
512
513 #[test]
514 fn test_event_type_from_event() {
515 let tx_details = TxAppliedDetails {
516 phase: Phase::default(),
517 topics: Topics::default(),
518 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
519 .to_string(),
520 };
521 let event_type = EventType::MidnightTxApplied(tx_details);
522 let event = Event(event_type);
523 let converted_event_type = EventType::from(event);
524
525 match converted_event_type {
526 EventType::MidnightTxApplied(details) => {
527 assert_eq!(
528 details.tx_hash,
529 "0x0000000000000000000000000000000000000000000000000000000000000000"
530 );
531 }
532 _ => panic!("Expected MidnightTxApplied event type"),
533 }
534 }
535
536 #[test]
537 fn test_event_serialization() {
538 let tx_details = TxAppliedDetails {
539 phase: Phase::default(),
540 topics: Topics::default(),
541 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
542 .to_string(),
543 };
544 let event_type = EventType::MidnightTxApplied(tx_details);
545 let event = Event(event_type);
546
547 let serialized = serde_json::to_string(&event).unwrap();
548 let deserialized: Event = serde_json::from_str(&serialized).unwrap();
549
550 match &*deserialized {
551 EventType::MidnightTxApplied(details) => {
552 assert_eq!(
553 details.tx_hash,
554 "0x0000000000000000000000000000000000000000000000000000000000000000"
555 );
556 }
557 _ => panic!("Expected MidnightTxApplied event type"),
558 }
559 }
560
561 #[test]
562 fn test_event_from_value() {
563 let valid_json = json!({
565 "MidnightTxApplied": {
566 "phase": {
567 "ApplyExtrinsic": 0
568 },
569 "topics": { "topics": [] },
570 "tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000000"
571 }
572 });
573 let event = Event::from(valid_json);
574 match &*event {
575 EventType::MidnightTxApplied(details) => {
576 assert_eq!(
577 details.tx_hash,
578 "0x0000000000000000000000000000000000000000000000000000000000000000"
579 );
580 assert_eq!(details.phase, Phase::ApplyExtrinsic(0));
581 assert_eq!(details.topics.topics, Vec::<String>::new());
582 }
583 _ => panic!("Expected MidnightTxApplied event type"),
584 }
585
586 let invalid_json = json!({
588 "InvalidType": {
589 "some_field": "value"
590 }
591 });
592 let event = Event::from(invalid_json);
593 match &*event {
594 EventType::Unknown(_) => (),
595 _ => panic!("Expected Unknown event type"),
596 }
597
598 let malformed_json = json!({
600 "MidnightTxApplied": "not_an_object"
601 });
602 let event = Event::from(malformed_json);
603 match &*event {
604 EventType::Unknown(_) => (),
605 _ => panic!("Expected Unknown event type"),
606 }
607 }
608
609 #[test]
610 fn test_event_get_tx_hash() {
611 let tx_details = TxAppliedDetails {
612 phase: Phase::default(),
613 topics: Topics::default(),
614 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
615 .to_string(),
616 };
617 let event_type = EventType::MidnightTxApplied(tx_details);
618 let event = Event(event_type);
619 assert_eq!(
620 event.get_tx_hash(),
621 Some("0x0000000000000000000000000000000000000000000000000000000000000000".to_string())
622 );
623
624 let call_details = CallDetails {
625 phase: Phase::default(),
626 topics: Topics::default(),
627 address: "0x123".to_string(),
628 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
629 .to_string(),
630 };
631 let event_type = EventType::MidnightCallContract(call_details);
632 let event = Event(event_type);
633 assert_eq!(
634 event.get_tx_hash(),
635 Some("0x0000000000000000000000000000000000000000000000000000000000000000".to_string())
636 );
637
638 let deploy_details = DeploymentDetails {
639 phase: Phase::default(),
640 topics: Topics::default(),
641 address: "0x123".to_string(),
642 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
643 .to_string(),
644 };
645 let event_type = EventType::MidnightDeployContract(deploy_details);
646 let event = Event(event_type);
647 assert_eq!(
648 event.get_tx_hash(),
649 Some("0x0000000000000000000000000000000000000000000000000000000000000000".to_string())
650 );
651
652 let maintain_details = MaintainDetails {
653 phase: Phase::default(),
654 topics: Topics::default(),
655 address: "0x123".to_string(),
656 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
657 .to_string(),
658 };
659 let event_type = EventType::MidnightMaintainContract(maintain_details);
660 let event = Event(event_type);
661 assert_eq!(
662 event.get_tx_hash(),
663 Some("0x0000000000000000000000000000000000000000000000000000000000000000".to_string())
664 );
665
666 let claim_mint_details = ClaimMintDetails {
667 phase: Phase::default(),
668 topics: Topics::default(),
669 coin_type: "0x123".to_string(),
670 value: 100u128,
671 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
672 .to_string(),
673 };
674 let event_type = EventType::MidnightClaimMint(claim_mint_details);
675 let event = Event(event_type);
676 assert_eq!(
677 event.get_tx_hash(),
678 Some("0x0000000000000000000000000000000000000000000000000000000000000000".to_string())
679 );
680
681 let payout_details = PayoutDetails {
682 phase: Phase::default(),
683 topics: Topics::default(),
684 amount: 100u128,
685 receiver: "0x123".to_string(),
686 };
687 let event_type = EventType::MidnightPayoutMinted(payout_details);
688 let event = Event(event_type);
689 assert_eq!(event.get_tx_hash(), None);
690
691 let unknown_event = Event(EventType::Unknown("unknown".to_string()));
692 assert_eq!(unknown_event.get_tx_hash(), None);
693 }
694
695 #[test]
696 fn test_event_type_checks() {
697 let tx_details = TxAppliedDetails {
699 phase: Phase::default(),
700 topics: Topics::default(),
701 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
702 .to_string(),
703 };
704 let event = Event(EventType::MidnightTxApplied(tx_details));
705 assert!(event.is_tx_applied());
706 assert!(!event.is_only_guaranteed_tx_applied());
707 assert!(event.is_success());
708
709 let tx_details = TxAppliedDetails {
711 phase: Phase::default(),
712 topics: Topics::default(),
713 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
714 .to_string(),
715 };
716 let event = Event(EventType::MidnightOnlyGuaranteedTxApplied(tx_details));
717 assert!(!event.is_tx_applied());
718 assert!(event.is_only_guaranteed_tx_applied());
719 assert!(event.is_success());
720
721 let call_details = CallDetails {
723 phase: Phase::default(),
724 topics: Topics::default(),
725 address: "0x123".to_string(),
726 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
727 .to_string(),
728 };
729 let event = Event(EventType::MidnightCallContract(call_details));
730 assert!(!event.is_tx_applied());
731 assert!(!event.is_only_guaranteed_tx_applied());
732 assert!(!event.is_success());
733
734 let deploy_details = DeploymentDetails {
735 phase: Phase::default(),
736 topics: Topics::default(),
737 address: "0x123".to_string(),
738 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
739 .to_string(),
740 };
741 let event = Event(EventType::MidnightDeployContract(deploy_details));
742 assert!(!event.is_tx_applied());
743 assert!(!event.is_only_guaranteed_tx_applied());
744 assert!(!event.is_success());
745
746 let maintain_details = MaintainDetails {
747 phase: Phase::default(),
748 topics: Topics::default(),
749 address: "0x123".to_string(),
750 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
751 .to_string(),
752 };
753 let event = Event(EventType::MidnightMaintainContract(maintain_details));
754 assert!(!event.is_tx_applied());
755 assert!(!event.is_only_guaranteed_tx_applied());
756 assert!(!event.is_success());
757
758 let payout_details = PayoutDetails {
759 phase: Phase::default(),
760 topics: Topics::default(),
761 amount: 100u128,
762 receiver: "0x123".to_string(),
763 };
764 let event = Event(EventType::MidnightPayoutMinted(payout_details));
765 assert!(!event.is_tx_applied());
766 assert!(!event.is_only_guaranteed_tx_applied());
767 assert!(!event.is_success());
768
769 let claim_mint_details = ClaimMintDetails {
770 phase: Phase::default(),
771 topics: Topics::default(),
772 coin_type: "0x123".to_string(),
773 value: 100u128,
774 tx_hash: "0x0000000000000000000000000000000000000000000000000000000000000000"
775 .to_string(),
776 };
777 let event = Event(EventType::MidnightClaimMint(claim_mint_details));
778 assert!(!event.is_tx_applied());
779 assert!(!event.is_only_guaranteed_tx_applied());
780 assert!(!event.is_success());
781
782 let event = Event(EventType::Unknown("unknown".to_string()));
783 assert!(!event.is_tx_applied());
784 assert!(!event.is_only_guaranteed_tx_applied());
785 assert!(!event.is_success());
786 }
787
788 #[test]
789 fn test_event_get_topics() {
790 let topics = vec!["topic1".to_string(), "topic2".to_string()];
791 let topics_struct = Topics {
792 topics: topics.clone(),
793 };
794
795 let tx_details = TxAppliedDetails {
797 phase: Phase::default(),
798 topics: topics_struct.clone(),
799 tx_hash: "0x123".to_string(),
800 };
801 let event = Event(EventType::MidnightTxApplied(tx_details));
802 assert_eq!(event.get_topics(), Some(topics.clone()));
803
804 let tx_details = TxAppliedDetails {
806 phase: Phase::default(),
807 topics: topics_struct.clone(),
808 tx_hash: "0x123".to_string(),
809 };
810 let event = Event(EventType::MidnightOnlyGuaranteedTxApplied(tx_details));
811 assert_eq!(event.get_topics(), Some(topics.clone()));
812
813 let call_details = CallDetails {
815 phase: Phase::default(),
816 topics: topics_struct.clone(),
817 address: "0x123".to_string(),
818 tx_hash: "0x456".to_string(),
819 };
820 let event = Event(EventType::MidnightCallContract(call_details));
821 assert_eq!(event.get_topics(), Some(topics.clone()));
822
823 let deploy_details = DeploymentDetails {
825 phase: Phase::default(),
826 topics: topics_struct.clone(),
827 address: "0x123".to_string(),
828 tx_hash: "0x456".to_string(),
829 };
830 let event = Event(EventType::MidnightDeployContract(deploy_details));
831 assert_eq!(event.get_topics(), Some(topics.clone()));
832
833 let maintain_details = MaintainDetails {
835 phase: Phase::default(),
836 topics: topics_struct.clone(),
837 address: "0x123".to_string(),
838 tx_hash: "0x456".to_string(),
839 };
840 let event = Event(EventType::MidnightMaintainContract(maintain_details));
841 assert_eq!(event.get_topics(), Some(topics.clone()));
842
843 let payout_details = PayoutDetails {
845 phase: Phase::default(),
846 topics: topics_struct.clone(),
847 amount: 100u128,
848 receiver: "0x123".to_string(),
849 };
850 let event = Event(EventType::MidnightPayoutMinted(payout_details));
851 assert_eq!(event.get_topics(), Some(topics.clone()));
852
853 let claim_mint_details = ClaimMintDetails {
855 phase: Phase::default(),
856 topics: topics_struct,
857 coin_type: "ETH".to_string(),
858 value: 100u128,
859 tx_hash: "0x456".to_string(),
860 };
861 let event = Event(EventType::MidnightClaimMint(claim_mint_details));
862 assert_eq!(event.get_topics(), Some(topics));
863
864 let event = Event(EventType::Unknown("unknown".to_string()));
866 assert_eq!(event.get_topics(), None);
867 }
868
869 #[test]
870 fn test_event_get_phase() {
871 let phase = Phase::ApplyExtrinsic(1);
872
873 let tx_details = TxAppliedDetails {
875 phase: phase.clone(),
876 topics: Topics::default(),
877 tx_hash: "0x123".to_string(),
878 };
879 let event = Event(EventType::MidnightTxApplied(tx_details));
880 assert_eq!(event.get_phase(), Some(phase.clone()));
881
882 let tx_details = TxAppliedDetails {
884 phase: phase.clone(),
885 topics: Topics::default(),
886 tx_hash: "0x123".to_string(),
887 };
888 let event = Event(EventType::MidnightOnlyGuaranteedTxApplied(tx_details));
889 assert_eq!(event.get_phase(), Some(phase.clone()));
890
891 let call_details = CallDetails {
893 phase: phase.clone(),
894 topics: Topics::default(),
895 address: "0x123".to_string(),
896 tx_hash: "0x456".to_string(),
897 };
898 let event = Event(EventType::MidnightCallContract(call_details));
899 assert_eq!(event.get_phase(), Some(phase.clone()));
900
901 let deploy_details = DeploymentDetails {
903 phase: phase.clone(),
904 topics: Topics::default(),
905 address: "0x123".to_string(),
906 tx_hash: "0x456".to_string(),
907 };
908 let event = Event(EventType::MidnightDeployContract(deploy_details));
909 assert_eq!(event.get_phase(), Some(phase.clone()));
910
911 let maintain_details = MaintainDetails {
913 phase: phase.clone(),
914 topics: Topics::default(),
915 address: "0x123".to_string(),
916 tx_hash: "0x456".to_string(),
917 };
918 let event = Event(EventType::MidnightMaintainContract(maintain_details));
919 assert_eq!(event.get_phase(), Some(phase.clone()));
920
921 let payout_details = PayoutDetails {
923 phase: phase.clone(),
924 topics: Topics::default(),
925 amount: 100u128,
926 receiver: "0x123".to_string(),
927 };
928 let event = Event(EventType::MidnightPayoutMinted(payout_details));
929 assert_eq!(event.get_phase(), Some(phase.clone()));
930
931 let claim_mint_details = ClaimMintDetails {
933 phase: phase.clone(),
934 topics: Topics::default(),
935 coin_type: "ETH".to_string(),
936 value: 100u128,
937 tx_hash: "0x456".to_string(),
938 };
939 let event = Event(EventType::MidnightClaimMint(claim_mint_details));
940 assert_eq!(event.get_phase(), Some(phase));
941
942 let event = Event(EventType::Unknown("unknown".to_string()));
944 assert_eq!(event.get_phase(), None);
945 }
946
947 #[test]
948 fn test_event_type_default() {
949 let default_event_type = EventType::default();
950 match default_event_type {
951 EventType::Unknown(message) => {
952 assert!(message.starts_with("Unknown event type: "));
953 assert!(message.contains("EventType"));
954 }
955 _ => panic!("Expected Unknown event type"),
956 }
957 }
958
959 #[test]
960 fn test_event_deref_mut() {
961 let mut event = Event(EventType::Unknown("original".to_string()));
962
963 *event = EventType::Unknown("modified".to_string());
965
966 match &*event {
967 EventType::Unknown(message) => {
968 assert_eq!(message, "modified");
969 }
970 _ => panic!("Expected Unknown event type"),
971 }
972 }
973}