af_iperps/
event_instance.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use af_move_type::otw::Otw;
use af_move_type::{FromRawStructError, MoveInstance};
use af_sui_types::StructTag;
use derive_more::{Display, From, IsVariant, TryInto};

#[derive(thiserror::Error, Debug)]
pub enum FromRawEventError {
    #[error(transparent)]
    FromRawStruct(#[from] FromRawStructError),
    #[error("Not a Perpetuals event name: {0}")]
    Name(String),
}

/// Creates an `$Enum` enum with each `$variant` containing a [`MoveInstance<T>`] where `T` is a
/// type in [`events`](crate::events).
macro_rules! event_instance {
    ($Enum:ident {
        $($variant:ident$(<$($T:ident),+>)?),+ $(,)?
    }) => {
        /// A Perpetuals event instance of any kind.
        // WARN: do not add serde to the below. Since the enum has to remain sorted, adding a
        // variant may change the 'index' of the others, and some serialization formats (e.g., BCS)
        // use the variants' indices; so backwards compatibility could be broken.
        #[remain::sorted]
        #[derive(Clone, Debug, Display, From, IsVariant, TryInto)]
        #[non_exhaustive]
        pub enum $Enum {
            $(
                $variant(MoveInstance<crate::events::$variant$(<$($T),+>)?>)
            ),+
        }

        impl $Enum {
            pub fn new(type_: StructTag, bcs: impl AsRef<[u8]>) -> Result<Self, FromRawEventError> {
                let name = type_.name.to_string();
                let name_str = name.as_str();
                Ok(match name_str {
                    $(
                        stringify!($variant) => Self::$variant(MoveInstance::from_raw_struct(
                            type_, bcs.as_ref()
                        )?),
                    )+
                    name => return Err(FromRawEventError::Name(name.to_owned())),
                })
            }

            pub fn struct_tag(&self) -> StructTag {
                match self {
                    $(
                        Self::$variant(inner) => inner.type_.clone().into(),
                    )+
                }
            }
        }
    };
}

event_instance!(EventInstance {
    AcceptedPositionFeesProposal,
    AllocatedCollateral,
    AllocatedCollateralSubAccount,
    CanceledOrder,
    CanceledOrders,
    CreatedAccount<Otw>,
    CreatedClearingHouse,
    CreatedMarginRatiosProposal,
    CreatedOrderbook,
    CreatedPosition,
    CreatedPositionFeesProposal,
    CreatedStopOrderTicket,
    CreatedSubAccount,
    DeallocatedCollateral,
    DeallocatedCollateralSubAccount,
    DeletedMarginRatiosProposal,
    DeletedPositionFeesProposal,
    DeletedStopOrderTicket,
    DeletedSubAccount,
    DepositedCollateral<Otw>,
    DepositedCollateralSubAccount,
    DonatedToInsuranceFund,
    FilledMakerOrder,
    FilledTakerOrder,
    LiquidatedPosition,
    OrderbookPostReceipt,
    PostedOrder,
    RegisteredCollateralInfo<Otw>,
    RegisteredMarketInfo<Otw>,
    RejectedPositionFeesProposal,
    RemovedRegisteredMarketInfo<Otw>,
    ResettedPositionFees,
    SetSubAccountUser,
    SettledFunding,
    UpdatedBaseOracleTolerance,
    UpdatedClearingHouseVersion,
    UpdatedCollateralOracleTolerance,
    UpdatedCumFundings,
    UpdatedFees,
    UpdatedFunding,
    UpdatedFundingParameters,
    UpdatedLiquidationTolerance,
    UpdatedMarginRatios,
    UpdatedMaxPendingOrders,
    UpdatedMinOrderUsdValue,
    UpdatedOpenInterestAndFeesAccrued,
    UpdatedPremiumTwap,
    UpdatedSpreadTwap,
    UpdatedSpreadTwapParameters,
    WithdrewCollateral<Otw>,
    WithdrewCollateralSubAccount,
    WithdrewFees,
    WithdrewInsuranceFund,
});