af_oracle/
event_instance.rs1use af_move_type::{FromRawStructError, MoveInstance};
2use af_sui_types::StructTag;
3use derive_more::{Display, From, IsVariant, TryInto};
4
5#[derive(thiserror::Error, Debug)]
6pub enum FromRawEventError {
7 #[error(transparent)]
8 FromRawStruct(#[from] FromRawStructError),
9 #[error("Not an Oracle event name: {0}")]
10 Name(String),
11}
12
13macro_rules! event_instance {
16 ($Enum:ident {
17 $($variant:ident),+ $(,)?
18 }) => {
19 #[remain::sorted]
24 #[derive(Clone, Debug, Display, From, IsVariant, TryInto)]
25 #[non_exhaustive]
26 pub enum $Enum {
27 $(
28 $variant(MoveInstance<crate::events::$variant>)
29 ),+
30 }
31
32 impl $Enum {
33 pub fn new(type_: StructTag, bcs: impl AsRef<[u8]>) -> Result<Self, FromRawEventError> {
34 let name = type_.name.to_string();
35 let name_str = name.as_str();
36 Ok(match name_str {
37 $(
38 stringify!($variant) => Self::$variant(MoveInstance::from_raw_struct(
39 type_, bcs.as_ref()
40 )?),
41 )+
42 name => return Err(FromRawEventError::Name(name.to_owned())),
43 })
44 }
45
46 pub fn struct_tag(&self) -> StructTag {
47 match self {
48 $(
49 Self::$variant(inner) => inner.type_.clone().into(),
50 )+
51 }
52 }
53 }
54 };
55}
56
57event_instance!(EventInstance {
58 AddedAuthorization,
59 CreatedPriceFeed,
60 CreatedPriceFeedStorage,
61 RemovedAuthorization,
62 RemovedPriceFeed,
63 UpdatedPriceFeed,
64 UpdatedPriceFeedTimeTolerance,
65});