af_iperps/
errors.rs

1// Copyright (c) Aftermath Technologies, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4#![expect(non_upper_case_globals, reason = "Copied from Move")]
5
6macro_rules! move_aborts {
7    (module $_:ident::$module:ident {$(
8        $(#[$meta:meta])*
9        const $Error:ident: u64 = $num:literal;
10    )*}) => {
11        $(
12            $(#[$meta])*
13            pub const $Error: u64 = $num;
14        )*
15        #[derive(
16            Debug,
17            PartialEq,
18            Eq,
19            Hash,
20            num_enum::IntoPrimitive,
21            num_enum::TryFromPrimitive,
22            strum::Display,
23            strum::EnumIs,
24            strum::EnumMessage,
25            strum::IntoStaticStr,
26        )]
27        #[repr(u64)]
28        pub enum MoveAbort {$(
29            $(#[$meta])*
30            $Error = $num,
31        )*}
32    };
33}
34
35move_aborts! {
36module perpetuals::errors {
37    // ClearingHouse ---------------------------------------------------------------
38
39    /// Cannot deposit/withdraw zero coins to/from the account's collateral.
40    const DepositOrWithdrawAmountZero: u64 = 0;
41    /// Orderbook size or price are invalid values
42    const InvalidSizeOrPrice: u64 = 1;
43    /// Index price returned from oracle is 0 or invalid value
44    const BadIndexPrice: u64 = 2;
45    /// Order value in USD is too low
46    const OrderUsdValueTooLow: u64 = 4;
47    /// Passed a vector of invalid order ids to perform force cancellation
48    /// during liquidation
49    const InvalidForceCancelIds: u64 = 5;
50    /// Liquidate must be the first operation of the session, if performed.
51    const LiquidateNotFirstOperation: u64 = 6;
52    /// Passed a vector of invalid order ids to cancel
53    const InvalidCancelOrderIds: u64 = 7;
54    /// Ticket has already passed `expire_timestamp` and can only be cancelled
55    const StopOrderTicketExpired: u64 = 8;
56    /// Index price is not at correct value to satisfy stop order conditions
57    const StopOrderConditionsViolated: u64 = 9;
58    /// Index price is not at correct value to satisfy stop order conditions
59    const WrongOrderDetails: u64 = 10;
60    /// Invalid base price feed storage for the clearing house
61    const InvalidBasePriceFeedStorage: u64 = 11;
62    /// Same liquidator and liqee account ids
63    const SelfLiquidation: u64 = 12;
64    /// User trying to access the subaccount is not the one specified by parent
65    const InvalidSubAccountUser: u64 = 13;
66    /// The parent `Account` trying to delete the subaccount is not the correct one.
67    const WrongParentForSubAccount: u64 = 14;
68    /// Raised when trying to delete a subaccount still containing collateral.
69    const SubAccountContainsCollateral: u64 = 15;
70    /// Raised when trying to call a function with the wrong package's version
71    const WrongVersion: u64 = 16;
72    /// Raised when trying to have a session composed by only `start_session` and `end_session`
73    const EmptySession: u64 = 17;
74    /// Market already registered in the registry
75    const MarketAlreadyRegistered: u64 = 18;
76    /// Collateral is not registered in the registry
77    const CollateralIsNotRegistered: u64 = 19;
78    /// Market is not registered in the registry
79    const MarketIsNotRegistered: u64 = 20;
80    /// Invalid collateral price feed storage for the clearing house
81    const InvalidCollateralPriceFeedStorage: u64 = 21;
82    /// Fees accrued are negative
83    const NegativeFeesAccrued: u64 = 22;
84    /// Reduce only conditions are not respected for stop order execution
85    const NotReduceOnlyStopOrder: u64 = 23;
86    /// Stop order gas cost provided is not enough
87    const NotEnoughGasForStopOrder: u64 = 24;
88
89    // Market ---------------------------------------------------------------
90
91    /// While creating ordered map with invalid parameters,
92    /// or changing them improperly for an existent map.
93    const InvalidMarketParameters: u64 = 1000;
94    /// Tried to call `update_funding` before enough time has passed since the
95    /// last update.
96    const UpdatingFundingTooEarly: u64 = 1001;
97    /// Margin ratio update proposal already exists for market
98    const ProposalAlreadyExists: u64 = 1002;
99    /// Margin ratio update proposal cannot be commited too early
100    const PrematureProposal: u64 = 1003;
101    /// Margin ratio update proposal delay is outside the valid range
102    const InvalidProposalDelay: u64 = 1004;
103    /// Margin ratio update proposal does not exist for market
104    const ProposalDoesNotExist: u64 = 1005;
105    /// Exchange has no available fees to withdraw
106    const NoFeesAccrued: u64 = 1006;
107    /// Tried to withdraw more insurance funds than the allowed amount
108    const InsufficientInsuranceSurplus: u64 = 1007;
109    /// Cannot create a market for which a price feed does not exist
110    const NoPriceFeedForMarket: u64 = 1008;
111    /// Cannot delete a proposal that already matured. It can only be committed.
112    const ProposalAlreadyMatured: u64 = 1009;
113
114    // Position  ---------------------------------------------------------------
115
116    /// Tried placing a new pending order when the position already has the maximum
117    /// allowed number of pending orders.
118    const MaxPendingOrdersExceeded: u64 = 2000;
119    /// Used for checking both liqee and liqor positions during liquidation
120    const PositionBelowIMR: u64 = 2001;
121    /// When leaving liqee's position with a margin ratio above tolerance,
122    /// meaning that liqor has overbought position
123    const PositionAboveTolerance: u64 = 2002;
124    /// An operation brought an account below initial margin requirements.
125    const InitialMarginRequirementViolated: u64 = 2003;
126    /// Position is above MMR, so can't be liquidated.
127    const PositionAboveMMR: u64 = 2004;
128    /// Cannot realize bad debt via means other than calling 'liquidate'.
129    const PositionBadDebt: u64 = 2005;
130    /// Cannot withdraw more than the account's free collateral.
131    const InsufficientFreeCollateral: u64 = 2006;
132    /// Cannot have more than 1 position in a market.
133    const PositionAlreadyExists: u64 = 2007;
134    /// Cannot compute deallocate amount for a target MR < IMR.
135    const DeallocateTargetMrTooLow: u64 = 2008;
136
137    // Orderbook & OrderedMap -------------------------------------------------------
138
139    /// While creating ordered map with wrong parameters.
140    const InvalidMapParameters: u64 = 3000;
141    /// While searching for a key, but it doesn't exist.
142    const KeyNotExist: u64 = 3001;
143    /// While inserting already existing key.
144    const KeyAlreadyExists: u64 = 3002;
145    /// When attempting to destroy a non-empty map
146    const DestroyNotEmpty: u64 = 3003;
147    /// Invalid user tries to modify an order
148    const InvalidUserForOrder: u64 = 3004;
149    /// Orderbook flag requirements violated
150    const FlagRequirementsViolated: u64 = 3005;
151    /// Minimum size matched not reached
152    const NotEnoughLiquidity: u64 = 3006;
153    /// When trying to change a map configuration, but the map has
154    /// length less than 4
155    const MapTooSmall: u64 = 3007;
156    /// When taker matches its own order
157    const SelfTrading: u64 = 3008;
158}
159}
160
161#[cfg(test)]
162mod tests {
163    use super::MoveAbort;
164
165    #[test]
166    fn variant_to_code() {
167        assert_eq!(MoveAbort::MaxPendingOrdersExceeded as u64, 2000);
168        assert_eq!(MoveAbort::SelfTrading as u64, 3008);
169        assert_eq!(Ok(MoveAbort::MaxPendingOrdersExceeded), 2000_u64.try_into());
170        assert_eq!(Ok(MoveAbort::SelfTrading), 3008_u64.try_into());
171    }
172}