af_oracle/
event_instance.rs

1use 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
13/// Creates an `$Enum` enum with each `$variant` containing a [`MoveInstance<T>`] where `T` is a
14/// type in [`events`](crate::events).
15macro_rules! event_instance {
16    ($Enum:ident {
17        $($variant:ident),+ $(,)?
18    }) => {
19        /// An AfOracle event instance of any kind.
20        // WARN: do not add serde to the below. Since the enum has to remain sorted, adding a
21        // variant may change the 'index' of the others, and some serialization formats (e.g., BCS)
22        // use the variants' indices; so backwards compatibility could be broken.
23        #[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});