ac_node_api/events/event_details.rs
1// This file bases on subxt (Parity Technologies (UK))
2// https://github.com/paritytech/subxt/
3// And was adapted by Supercomputing Systems AG.
4
5// Copyright 2019-2023 Parity Technologies (UK) Ltd.
6// This file is dual-licensed as Apache-2.0 or GPL-3.0.
7// see LICENSE for license details.
8
9//! A representation of a block of events.
10
11use crate::{
12 error::{DispatchError, Error},
13 events::{EventMetadataDetails, RawEventDetails, RootEvent},
14 Metadata, Phase, StaticEvent,
15};
16use alloc::sync::Arc;
17use codec::{Decode, Encode};
18use scale_value::Composite;
19
20/// The event details with the associated metadata.
21// Based on subxt EventDetails.
22// https://github.com/paritytech/subxt/blob/8413c4d2dd625335b9200dc2289670accdf3391a/subxt/src/events/events_type.rs#L197-L216
23#[derive(Debug, Clone, Encode, Decode)]
24pub struct EventDetails<Hash: Encode + Decode> {
25 inner: RawEventDetails<Hash>,
26 metadata: Metadata,
27}
28
29impl<Hash: Encode + Decode> EventDetails<Hash> {
30 // Attempt to dynamically decode a single event from our events input.
31 pub(crate) fn decode_from(
32 metadata: Metadata,
33 all_bytes: Arc<[u8]>,
34 start_idx: usize,
35 index: u32,
36 ) -> Result<Self, Error> {
37 let inner = RawEventDetails::decode_from(&metadata, all_bytes, start_idx, index)?;
38 Ok(EventDetails { inner, metadata })
39 }
40
41 /// When was the event produced?
42 pub fn phase(&self) -> Phase {
43 self.inner.phase()
44 }
45
46 /// What index is this event in the stored events for this block.
47 pub fn index(&self) -> u32 {
48 self.inner.index()
49 }
50
51 /// The index of the pallet that the event originated from.
52 pub fn pallet_index(&self) -> u8 {
53 // Note: never panics; we expect these bytes to exist
54 // in order that the EventDetails could be created.
55 self.inner.pallet_index()
56 }
57
58 /// The index of the event variant that the event originated from.
59 pub fn variant_index(&self) -> u8 {
60 // Note: never panics; we expect these bytes to exist
61 // in order that the EventDetails could be created.
62 self.inner.variant_index()
63 }
64
65 /// The name of the pallet from whence the Event originated.
66 pub fn pallet_name(&self) -> &str {
67 self.inner.pallet_name()
68 }
69
70 /// The name of the event (ie the name of the variant that it corresponds to).
71 pub fn variant_name(&self) -> &str {
72 self.inner.variant_name()
73 }
74
75 /// Fetch details from the metadata for this event.
76 pub fn event_metadata(&self) -> EventMetadataDetails {
77 self.inner.event_metadata_unchecked(&self.metadata)
78 }
79
80 /// Return _all_ of the bytes representing this event, which include, in order:
81 /// - The phase.
82 /// - Pallet and event index.
83 /// - Event fields.
84 /// - Event Topics.
85 pub fn bytes(&self) -> &[u8] {
86 self.inner.bytes()
87 }
88
89 /// Return the bytes representing the fields stored in this event.
90 pub fn field_bytes(&self) -> &[u8] {
91 self.inner.field_bytes()
92 }
93
94 /// Decode and provide the event fields back in the form of a [`scale_value::Composite`]
95 /// type which represents the named or unnamed fields that were present in the event.
96 pub fn field_values(&self) -> Result<Composite<u32>, Error> {
97 self.inner.field_values_unchecked(&self.metadata)
98 }
99
100 /// Attempt to decode these [`EventDetails`] into a specific static event.
101 /// This targets the fields within the event directly. You can also attempt to
102 /// decode the entirety of the event type (including the pallet and event
103 /// variants) using [`EventDetails::as_root_event()`].
104 pub fn as_event<E: StaticEvent>(&self) -> Result<Option<E>, Error> {
105 self.inner.as_event()
106 }
107
108 /// Attempt to decode these [`EventDetails`] into a root event type (which includes
109 /// the pallet and event enum variants as well as the event fields). A compatible
110 /// type for this is exposed via static codegen as a root level `Event` type.
111 pub fn as_root_event<E: RootEvent>(&self) -> Result<E, Error> {
112 self.inner.as_root_event_unchecked(&self.metadata)
113 }
114
115 /// Return the topics associated with this event.
116 pub fn topics(&self) -> &[Hash] {
117 self.inner.topics()
118 }
119
120 /// Consume original struct and return only the raw portion without metadata.
121 pub fn to_raw(self) -> RawEventDetails<Hash> {
122 self.inner
123 }
124}
125
126impl<Hash: Encode + Decode> EventDetails<Hash> {
127 /// Checks if the extrinsic has failed.
128 pub fn has_failed(&self) -> bool {
129 self.inner.has_failed()
130 }
131
132 /// Returns the dispatch error of the failed extrinsic, if it has failed.
133 pub fn get_associated_dispatch_error(&self) -> Option<DispatchError> {
134 self.inner.get_associated_dispatch_error(&self.metadata)
135 }
136
137 /// Checks if the event represents a code update (runtime update).
138 pub fn is_code_update(&self) -> bool {
139 self.inner.is_code_update()
140 }
141}