oasis_contract_sdk/
event.rs

1//! Contract event trait.
2use crate::types;
3
4/// An event emitted by the contract.
5///
6/// This trait can be derived:
7/// ```
8/// # #[cfg(feature = "oasis-contract-sdk-macros")]
9/// # mod example {
10/// # use oasis_contract_sdk_macros::Event;
11/// #[derive(Clone, Debug, cbor::Encode, Event)]
12/// #[cbor(untagged)]
13/// #[sdk_event(autonumber)]
14/// enum MyEvent {
15///    Greeting(String),      // autonumbered to 0
16///    #[sdk_event(code = 2)] // manually numbered to 2 (`code` is required if not autonumbering)
17///    DontPanic,
18///    Salutation {           // autonumbered to 1
19///        plural: bool,
20///    }
21/// }
22/// # }
23/// ```
24pub trait Event: Sized + cbor::Encode {
25    /// Name of the module that emitted the event.
26    fn module_name(&self) -> &str;
27
28    /// Code uniquely identifying the event.
29    fn code(&self) -> u32;
30
31    /// Converts an event into the raw event type that can be emitted from the contract.
32    fn into_raw(self) -> types::event::Event {
33        types::event::Event {
34            module: self.module_name().to_string(),
35            code: self.code(),
36            data: cbor::to_vec(self),
37        }
38    }
39}
40
41impl Event for types::event::Event {
42    fn module_name(&self) -> &str {
43        &self.module
44    }
45
46    fn code(&self) -> u32 {
47        self.code
48    }
49
50    fn into_raw(self) -> types::event::Event {
51        self
52    }
53}