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    AddedIntegratorConfig<Otw>,
61    AllocatedCollateral,
62    CanceledOrder,
63    ClosedMarket,
64    ClosedPositionAtSettlementPrices,
65    CreatedAccount<Otw>,
66    CreatedClearingHouse,
67    CreatedIntegratorVault,
68    CreatedMarginRatiosProposal,
69    CreatedOrderbook,
70    CreatedPosition,
71    CreatedPositionFeesProposal,
72    CreatedStopOrderTicket<Otw>,
73    DeallocatedCollateral,
74    DeletedMarginRatiosProposal,
75    DeletedPositionFeesProposal,
76    DeletedStopOrderTicket<Otw>,
77    DepositedCollateral<Otw>,
78    DonatedToInsuranceFund,
79    EditedStopOrderTicketDetails<Otw>,
80    EditedStopOrderTicketExecutors<Otw>,
81    ExecutedStopOrderTicket<Otw>,
82    FilledMakerOrder,
83    FilledMakerOrders,
84    FilledTakerOrder,
85    LiquidatedPosition,
86    PaidIntegratorFees<Otw>,
87    PausedMarket,
88    PerformedADL,
89    PerformedLiquidation,
90    PostedOrder,
91    RegisteredCollateralInfo<Otw>,
92    RegisteredMarketInfo<Otw>,
93    RejectedPositionFeesProposal,
94    RemovedIntegratorConfig<Otw>,
95    RemovedRegisteredMarketInfo<Otw>,
96    ResettedPositionFees,
97    ResumedMarket,
98    SetPositionInitialMarginRatio,
99    SettledFunding,
100    SocializedBadDebt,
101    UpdatedBasePfsID,
102    UpdatedBasePfsSourceID,
103    UpdatedBasePfsTolerance,
104    UpdatedClearingHouseVersion,
105    UpdatedCollateralHaircut,
106    UpdatedCollateralPfsID,
107    UpdatedCollateralPfsSourceID,
108    UpdatedCollateralPfsTolerance,
109    UpdatedFees,
110    UpdatedFunding,
111    UpdatedFundingParameters,
112    UpdatedGasPriceTwap,
113    UpdatedGasPriceTwapParameters,
114    UpdatedMarginRatios,
115    UpdatedMarketLotAndTick,
116    UpdatedMaxBadDebt,
117    UpdatedMaxOpenInterest,
118    UpdatedMaxOpenInterestPositionParams,
119    UpdatedMaxPendingOrders,
120    UpdatedMaxSocializeLossesMrDecrease,
121    UpdatedMinOrderUsdValue,
122    UpdatedOpenInterestAndFeesAccrued,
123    UpdatedPremiumTwap,
124    UpdatedSpreadTwap,
125    UpdatedSpreadTwapParameters,
126    UpdatedStopOrderMistCost,
127    WithdrewCollateral<Otw>,
128    WithdrewFees,
129    WithdrewFromIntegratorVault,
130    WithdrewInsuranceFund,
131});