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    FilledMakerOrders,
83    FilledTakerOrder,
84    LiquidatedPosition,
85    OrderbookPostReceipt,
86    PostedOrder,
87    RegisteredCollateralInfo<Otw>,
88    RegisteredMarketInfo<Otw>,
89    RejectedPositionFeesProposal,
90    RemovedRegisteredMarketInfo<Otw>,
91    ResettedPositionFees,
92    SetSubAccountUser,
93    SettledFunding,
94    UpdatedBaseOracleTolerance,
95    UpdatedClearingHouseVersion,
96    UpdatedCollateralOracleTolerance,
97    UpdatedCumFundings,
98    UpdatedFees,
99    UpdatedFunding,
100    UpdatedFundingParameters,
101    UpdatedLiquidationTolerance,
102    UpdatedMarginRatios,
103    UpdatedMaxPendingOrders,
104    UpdatedMinOrderUsdValue,
105    UpdatedOpenInterestAndFeesAccrued,
106    UpdatedPremiumTwap,
107    UpdatedSpreadTwap,
108    UpdatedSpreadTwapParameters,
109    WithdrewCollateral<Otw>,
110    WithdrewCollateralSubAccount,
111    WithdrewFees,
112    WithdrewInsuranceFund,
113});