Skip to main content

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