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    /// Size to place is 0. Raised also when there is no open position and
42    /// an order with `reduce_only` is passed.
43    const SizeOrPositionZero: u64 = 1;
44    /// Index price returned from oracle is 0 or invalid value
45    const BadIndexPrice: u64 = 2;
46    /// Price is either 0 or greater than 0x8000_0000_0000_0000
47    const InvalidPrice: u64 = 3;
48    /// Order value in USD is too low
49    const OrderUsdValueTooLow: u64 = 4;
50    /// Passed a vector of invalid order ids to perform force cancellation
51    /// during liquidation
52    const InvalidForceCancelIds: u64 = 5;
53    /// Liquidate must be the first operation of the session, if performed.
54    const LiquidateNotFirstOperation: u64 = 6;
55    /// Passed a vector of invalid order ids to cancel
56    const InvalidCancelOrderIds: u64 = 7;
57    /// Ticket has already passed `expire_timestamp` and can only be cancelled
58    const StopOrderTicketExpired: u64 = 8;
59    /// Index price is not at correct value to satisfy stop order conditions
60    const StopOrderConditionsViolated: u64 = 9;
61    /// Index price is not at correct value to satisfy stop order conditions
62    const WrongOrderDetails: u64 = 10;
63    /// Invalid base price feed storage for the clearing house
64    const InvalidBasePriceFeedStorage: u64 = 11;
65    /// Same liquidator and liqee account ids
66    const SelfLiquidation: u64 = 12;
67    /// User trying to access the subaccount is not the one specified by parent
68    const InvalidSubAccountUser: u64 = 13;
69    /// The parent `Account` trying to delete the subaccount is not the correct one.
70    const WrongParentForSubAccount: u64 = 14;
71    /// Raised when trying to call a function with the wrong package's version
72    const WrongVersion: u64 = 16;
73    /// Raised when trying to have a session composed by only `start_session` and `end_session`
74    const EmptySession: u64 = 17;
75    /// Market already registered in the registry
76    const MarketAlreadyRegistered: u64 = 18;
77    /// Collateral is not registered in the registry
78    const CollateralIsNotRegistered: u64 = 19;
79    /// Market is not registered in the registry
80    const MarketIsNotRegistered: u64 = 20;
81    /// Invalid collateral price feed storage for the clearing house
82    const InvalidCollateralPriceFeedStorage: u64 = 21;
83    /// Fees accrued are negative
84    const NegativeFeesAccrued: u64 = 22;
85    /// Passed a timestamp older than current Clock's one
86    const InvalidExpirationTimestamp: u64 = 23;
87    /// Stop order gas cost provided is not enough
88    const NotEnoughGasForStopOrder: u64 = 24;
89    /// Invalid account trying to perform an action on a StopOrderTicket
90    const InvalidAccountForStopOrder: u64 = 26;
91    /// Invalid executor trying to execute the StopOrderTicket
92    const InvalidExecutorForStopOrder: u64 = 27;
93    /// Raised when the market's max open interest is surpassed as a result of
94    /// the session's actions
95    const MaxOpenInterestSurpassed: u64 = 28;
96    /// Raised when a position's would get a base amount higher than the
97    /// allowed percentage of open interest
98    const MaxOpenInterestPositionPercentSurpassed: u64 = 29;
99    /// Raised processing a session that requires a collateral allocation,
100    /// but not enough collateral is available in the account or subaccount
101    const NotEnoughCollateralToAllocateForSession: u64 = 30;
102    /// Raised processing a session that requires a collateral allocation
103    /// and a wrong account or subaccount is being used to fund it
104    const WrongAccountIdForAllocation: u64 = 31;
105
106    // Market ---------------------------------------------------------------
107
108    /// While creating ordered map with invalid parameters,
109    /// or changing them improperly for an existent map.
110    const InvalidMarketParameters: u64 = 1000;
111    /// Tried to call `update_funding` before enough time has passed since the
112    /// last update.
113    const UpdatingFundingTooEarly: u64 = 1001;
114    /// Margin ratio update proposal already exists for market
115    const ProposalAlreadyExists: u64 = 1002;
116    /// Margin ratio update proposal cannot be commited too early
117    const PrematureProposal: u64 = 1003;
118    /// Margin ratio update proposal delay is outside the valid range
119    const InvalidProposalDelay: u64 = 1004;
120    /// Margin ratio update proposal does not exist for market
121    const ProposalDoesNotExist: u64 = 1005;
122    /// Exchange has no available fees to withdraw
123    const NoFeesAccrued: u64 = 1006;
124    /// Tried to withdraw more insurance funds than the allowed amount
125    const InsufficientInsuranceSurplus: u64 = 1007;
126    /// Cannot create a market for which a price feed does not exist
127    const NoPriceFeedForMarket: u64 = 1008;
128    /// Cannot delete a proposal that already matured. It can only be committed.
129    const ProposalAlreadyMatured: u64 = 1009;
130
131    // Position  ---------------------------------------------------------------
132
133    /// Tried placing a new pending order when the position already has the maximum
134    /// allowed number of pending orders.
135    const MaxPendingOrdersExceeded: u64 = 2000;
136    /// Used for checking both liqee and liqor positions during liquidation
137    const PositionBelowIMR: u64 = 2001;
138    /// When leaving liqee's position with a margin ratio above tolerance,
139    /// meaning that liqor has overbought position
140    const PositionAboveTolerance: u64 = 2002;
141    /// An operation brought an account below initial margin requirements.
142    const InitialMarginRequirementViolated: u64 = 2003;
143    /// Position is above MMR, so can't be liquidated.
144    const PositionAboveMMR: u64 = 2004;
145    /// Cannot realize bad debt via means other than calling 'liquidate'.
146    const PositionBadDebt: u64 = 2005;
147    /// Cannot withdraw more than the account's free collateral.
148    const InsufficientFreeCollateral: u64 = 2006;
149    /// Cannot have more than 1 position in a market.
150    const PositionAlreadyExists: u64 = 2007;
151    /// Cannot compute deallocate amount for a target MR < IMR.
152    const DeallocateTargetMrTooLow: u64 = 2008;
153    /// Raised when trying to set a position's IMR lower than market's IMR or higher than 1
154    const InvalidPositionIMR: u64 = 2009;
155    /// Invalid stop order type
156    const InvalidStopOrderType: u64 = 2010;
157    /// Invalid position' status for placing a SLTP order
158    const InvalidPositionForSLTP: u64 = 2011;
159
160    // Orderbook & OrderedMap -------------------------------------------------------
161
162    /// While creating ordered map with wrong parameters.
163    const InvalidMapParameters: u64 = 3000;
164    /// While searching for a key, but it doesn't exist.
165    const KeyNotExist: u64 = 3001;
166    /// While inserting already existing key.
167    const KeyAlreadyExists: u64 = 3002;
168    /// When attempting to destroy a non-empty map
169    const DestroyNotEmpty: u64 = 3003;
170    /// Invalid user tries to modify an order
171    const InvalidUserForOrder: u64 = 3004;
172    /// Orderbook flag requirements violated
173    const FlagRequirementsViolated: u64 = 3005;
174    /// Minimum size matched not reached
175    const NotEnoughLiquidity: u64 = 3006;
176    /// When trying to change a map configuration, but the map has
177    /// length less than 4
178    const MapTooSmall: u64 = 3007;
179    /// When taker matches its own order
180    const SelfTrading: u64 = 3008;
181}
182}
183
184#[cfg(test)]
185mod tests {
186    use super::MoveAbort;
187
188    #[test]
189    fn variant_to_code() {
190        assert_eq!(MoveAbort::MaxPendingOrdersExceeded as u64, 2000);
191        assert_eq!(MoveAbort::SelfTrading as u64, 3008);
192        assert_eq!(Ok(MoveAbort::MaxPendingOrdersExceeded), 2000_u64.try_into());
193        assert_eq!(Ok(MoveAbort::SelfTrading), 3008_u64.try_into());
194    }
195}