Skip to main content

iota_sdk_types/
events.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2025 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use super::{Address, Identifier, ObjectId, StructTag};
6
7/// Events emitted during the successful execution of a transaction
8///
9/// # BCS
10///
11/// The BCS serialized form for this type is defined by the following ABNF:
12///
13/// ```text
14/// transaction-events = vector event
15/// ```
16#[derive(Clone, Debug, Default, derive_more::Deref, derive_more::DerefMut, Eq, Hash, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
18#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
19#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
20pub struct TransactionEvents(pub Vec<Event>);
21
22impl crate::TreeDisplay for TransactionEvents {
23    fn fmt_tree(&self, w: &mut crate::TreeWriter<'_, '_>) -> std::fmt::Result {
24        w.header("Transaction Events")?;
25        w.children("Events", &self.0, true)
26    }
27}
28
29/// An event
30///
31/// # BCS
32///
33/// The BCS serialized form for this type is defined by the following ABNF:
34///
35/// ```text
36/// event = object-id identifier address struct-tag bytes
37/// ```
38#[derive(Clone, derive_more::Debug, Eq, Hash, PartialEq)]
39#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
40#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
41#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
42pub struct Event {
43    /// Package id of the top-level function invoked by a MoveCall command which
44    /// triggered this event to be emitted.
45    pub package_id: ObjectId,
46    /// Module name of the top-level function invoked by a MoveCall command
47    /// which triggered this event to be emitted.
48    pub module: Identifier,
49    /// Address of the account that sent the transaction where this event was
50    /// emitted.
51    pub sender: Address,
52    /// The type of the event emitted
53    #[cfg_attr(feature = "serde", serde(rename = "type"))]
54    pub struct_tag: StructTag,
55    /// BCS serialized bytes of the event
56    #[cfg_attr(
57        feature = "serde",
58        serde(with = "crate::_serde::ReadableBase64Encoded")
59    )]
60    #[debug("{:?}", <base64ct::Base64 as base64ct::Encoding>::encode_string(contents))]
61    pub contents: Vec<u8>,
62}
63
64impl Event {
65    fn is_system_epoch_info_event_type(&self, name: Identifier) -> bool {
66        self.struct_tag.address() == Address::SYSTEM
67            && *self.struct_tag.module() == Identifier::IOTA_SYSTEM_STATE_INNER_MODULE
68            && *self.struct_tag.name() == name
69    }
70
71    /// Checks if this is a `SystemEpochInfoEvent` of any version (V1 or V2).
72    pub fn is_system_epoch_info_event(&self) -> bool {
73        self.is_system_epoch_info_event_v1() || self.is_system_epoch_info_event_v2()
74    }
75
76    /// Checks if this is
77    /// `0x3::iota_system_state_inner::SystemEpochInfoEventV1`.
78    pub fn is_system_epoch_info_event_v1(&self) -> bool {
79        self.is_system_epoch_info_event_type(Identifier::SYSTEM_EPOCH_INFO_EVENT_V1)
80    }
81
82    /// Checks if this is
83    /// `0x3::iota_system_state_inner::SystemEpochInfoEventV2`.
84    pub fn is_system_epoch_info_event_v2(&self) -> bool {
85        self.is_system_epoch_info_event_type(Identifier::SYSTEM_EPOCH_INFO_EVENT_V2)
86    }
87}
88
89impl crate::TreeDisplay for Event {
90    fn fmt_tree(&self, w: &mut crate::TreeWriter<'_, '_>) -> std::fmt::Result {
91        w.header("Event")?;
92        w.leaf("Package ID", &self.package_id, false)?;
93        w.leaf("Module", &self.module, false)?;
94        w.leaf("Sender", &self.sender, false)?;
95        w.leaf("Struct Tag", &self.struct_tag, false)?;
96        w.leaf("Contents", &hex::encode(&self.contents), true)
97    }
98}
99
100crate::impl_tree_display!(TransactionEvents, Event);