af_iperps/
event_instance.rs

1use af_move_type::otw::Otw;
2use af_move_type::{FromRawStructError, MoveInstance};
3use af_sui_types::StructTag;
4use derive_more::{Display, From, IsVariant, TryInto};
5
6#[derive(thiserror::Error, Debug)]
7pub enum FromRawEventError {
8    #[error(transparent)]
9    FromRawStruct(#[from] FromRawStructError),
10    #[error("Not a Perpetuals event name: {0}")]
11    Name(String),
12}
13
14/// Creates an `$Enum` enum with each `$variant` containing a [`MoveInstance<T>`] where `T` is a
15/// type in [`events`](crate::events).
16macro_rules! event_instance {
17    ($Enum:ident {
18        $($variant:ident$(<$($T:ident),+>)?),+ $(,)?
19    }) => {
20        /// A Perpetuals event instance of any kind.
21        // WARN: do not add serde to the below. Since the enum has to remain sorted, adding a
22        // variant may change the 'index' of the others, and some serialization formats (e.g., BCS)
23        // use the variants' indices; so backwards compatibility could be broken.
24        #[remain::sorted]
25        #[derive(Clone, Debug, Display, From, IsVariant, TryInto)]
26        #[non_exhaustive]
27        pub enum $Enum {
28            $(
29                $variant(MoveInstance<crate::events::$variant$(<$($T),+>)?>)
30            ),+
31        }
32
33        impl $Enum {
34            pub fn new(type_: StructTag, bcs: impl AsRef<[u8]>) -> Result<Self, FromRawEventError> {
35                let name = type_.name.to_string();
36                let name_str = name.as_str();
37                Ok(match name_str {
38                    $(
39                        stringify!($variant) => Self::$variant(MoveInstance::from_raw_struct(
40                            type_, bcs.as_ref()
41                        )?),
42                    )+
43                    name => return Err(FromRawEventError::Name(name.to_owned())),
44                })
45            }
46
47            pub fn struct_tag(&self) -> StructTag {
48                match self {
49                    $(
50                        Self::$variant(inner) => inner.type_.clone().into(),
51                    )+
52                }
53            }
54        }
55    };
56}
57
58event_instance!(EventInstance {
59    AcceptedPositionFeesProposal,
60    AllocatedCollateral,
61    AllocatedCollateralSubAccount,
62    CanceledOrder,
63    CanceledOrders,
64    CreatedAccount<Otw>,
65    CreatedClearingHouse,
66    CreatedMarginRatiosProposal,
67    CreatedOrderbook,
68    CreatedPosition,
69    CreatedPositionFeesProposal,
70    CreatedStopOrderTicket,
71    CreatedSubAccount,
72    DeallocatedCollateral,
73    DeallocatedCollateralSubAccount,
74    DeletedMarginRatiosProposal,
75    DeletedPositionFeesProposal,
76    DeletedStopOrderTicket,
77    DeletedSubAccount,
78    DepositedCollateral<Otw>,
79    DepositedCollateralSubAccount,
80    DonatedToInsuranceFund,
81    FilledMakerOrder,
82    FilledTakerOrder,
83    LiquidatedPosition,
84    OrderbookPostReceipt,
85    PostedOrder,
86    RegisteredCollateralInfo<Otw>,
87    RegisteredMarketInfo<Otw>,
88    RejectedPositionFeesProposal,
89    RemovedRegisteredMarketInfo<Otw>,
90    ResettedPositionFees,
91    SetSubAccountUser,
92    SettledFunding,
93    UpdatedBaseOracleTolerance,
94    UpdatedClearingHouseVersion,
95    UpdatedCollateralOracleTolerance,
96    UpdatedCumFundings,
97    UpdatedFees,
98    UpdatedFunding,
99    UpdatedFundingParameters,
100    UpdatedLiquidationTolerance,
101    UpdatedMarginRatios,
102    UpdatedMaxPendingOrders,
103    UpdatedMinOrderUsdValue,
104    UpdatedOpenInterestAndFeesAccrued,
105    UpdatedPremiumTwap,
106    UpdatedSpreadTwap,
107    UpdatedSpreadTwapParameters,
108    WithdrewCollateral<Otw>,
109    WithdrewCollateralSubAccount,
110    WithdrewFees,
111    WithdrewInsuranceFund,
112});