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    CreatedMarketPositionSubAccount,
68    CreatedOrderbook,
69    CreatedPosition,
70    CreatedPositionFeesProposal,
71    CreatedStopOrderTicket,
72    CreatedSubAccount,
73    DeallocatedCollateral,
74    DeallocatedCollateralSubAccount,
75    DeletedMarginRatiosProposal,
76    DeletedPositionFeesProposal,
77    DeletedStopOrderTicket,
78    DeletedSubAccount,
79    DepositedCollateral<Otw>,
80    DepositedCollateralSubAccount,
81    DonatedToInsuranceFund,
82    FilledMakerOrder,
83    FilledMakerOrders,
84    FilledTakerOrder,
85    LiquidatedPosition,
86    OrderbookPostReceipt,
87    PerfomedLiquidation,
88    PostedOrder,
89    RegisteredCollateralInfo<Otw>,
90    RegisteredMarketInfo<Otw>,
91    RejectedPositionFeesProposal,
92    RemovedRegisteredMarketInfo<Otw>,
93    ResettedPositionFees,
94    SetSubAccountUser,
95    SettledFunding,
96    UpdatedBaseOracleTolerance,
97    UpdatedClearingHouseVersion,
98    UpdatedCollateralOracleTolerance,
99    UpdatedCumFundings,
100    UpdatedFees,
101    UpdatedFunding,
102    UpdatedFundingParameters,
103    UpdatedLiquidationTolerance,
104    UpdatedMarginRatios,
105    UpdatedMaxPendingOrders,
106    UpdatedMinOrderUsdValue,
107    UpdatedOpenInterestAndFeesAccrued,
108    UpdatedPremiumTwap,
109    UpdatedSpreadTwap,
110    UpdatedSpreadTwapParameters,
111    UpdatedStopOrderMistCost,
112    WithdrewCollateral<Otw>,
113    WithdrewCollateralSubAccount,
114    WithdrewFees,
115    WithdrewInsuranceFund,
116});