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 /// Stop order collateral to allocate provided is not enough
89 const NotEnoughCollateralToAllocateForStopOrder: u64 = 25;
90 /// Invalid account trying to perform an action on a StopOrderTicket
91 const InvalidAccountForStopOrder: u64 = 26;
92 /// Invalid executor trying to execute the StopOrderTicket
93 const InvalidExecutorForStopOrder: u64 = 27;
94 /// Raised when the market's max open interest is surpassed as a result of
95 /// the session's actions
96 const MaxOpenInterestSurpassed: u64 = 28;
97 /// Raised when a position's would get a base amount higher than the
98 /// allowed percentage of open interest
99 const MaxOpenInterestPositionPercentSurpassed: u64 = 29;
100
101 // Market ---------------------------------------------------------------
102
103 /// While creating ordered map with invalid parameters,
104 /// or changing them improperly for an existent map.
105 const InvalidMarketParameters: u64 = 1000;
106 /// Tried to call `update_funding` before enough time has passed since the
107 /// last update.
108 const UpdatingFundingTooEarly: u64 = 1001;
109 /// Margin ratio update proposal already exists for market
110 const ProposalAlreadyExists: u64 = 1002;
111 /// Margin ratio update proposal cannot be commited too early
112 const PrematureProposal: u64 = 1003;
113 /// Margin ratio update proposal delay is outside the valid range
114 const InvalidProposalDelay: u64 = 1004;
115 /// Margin ratio update proposal does not exist for market
116 const ProposalDoesNotExist: u64 = 1005;
117 /// Exchange has no available fees to withdraw
118 const NoFeesAccrued: u64 = 1006;
119 /// Tried to withdraw more insurance funds than the allowed amount
120 const InsufficientInsuranceSurplus: u64 = 1007;
121 /// Cannot create a market for which a price feed does not exist
122 const NoPriceFeedForMarket: u64 = 1008;
123 /// Cannot delete a proposal that already matured. It can only be committed.
124 const ProposalAlreadyMatured: u64 = 1009;
125
126 // Position ---------------------------------------------------------------
127
128 /// Tried placing a new pending order when the position already has the maximum
129 /// allowed number of pending orders.
130 const MaxPendingOrdersExceeded: u64 = 2000;
131 /// Used for checking both liqee and liqor positions during liquidation
132 const PositionBelowIMR: u64 = 2001;
133 /// When leaving liqee's position with a margin ratio above tolerance,
134 /// meaning that liqor has overbought position
135 const PositionAboveTolerance: u64 = 2002;
136 /// An operation brought an account below initial margin requirements.
137 const InitialMarginRequirementViolated: u64 = 2003;
138 /// Position is above MMR, so can't be liquidated.
139 const PositionAboveMMR: u64 = 2004;
140 /// Cannot realize bad debt via means other than calling 'liquidate'.
141 const PositionBadDebt: u64 = 2005;
142 /// Cannot withdraw more than the account's free collateral.
143 const InsufficientFreeCollateral: u64 = 2006;
144 /// Cannot have more than 1 position in a market.
145 const PositionAlreadyExists: u64 = 2007;
146 /// Cannot compute deallocate amount for a target MR < IMR.
147 const DeallocateTargetMrTooLow: u64 = 2008;
148
149 // Orderbook & OrderedMap -------------------------------------------------------
150
151 /// While creating ordered map with wrong parameters.
152 const InvalidMapParameters: u64 = 3000;
153 /// While searching for a key, but it doesn't exist.
154 const KeyNotExist: u64 = 3001;
155 /// While inserting already existing key.
156 const KeyAlreadyExists: u64 = 3002;
157 /// When attempting to destroy a non-empty map
158 const DestroyNotEmpty: u64 = 3003;
159 /// Invalid user tries to modify an order
160 const InvalidUserForOrder: u64 = 3004;
161 /// Orderbook flag requirements violated
162 const FlagRequirementsViolated: u64 = 3005;
163 /// Minimum size matched not reached
164 const NotEnoughLiquidity: u64 = 3006;
165 /// When trying to change a map configuration, but the map has
166 /// length less than 4
167 const MapTooSmall: u64 = 3007;
168 /// When taker matches its own order
169 const SelfTrading: u64 = 3008;
170}
171}
172
173#[cfg(test)]
174mod tests {
175 use super::MoveAbort;
176
177 #[test]
178 fn variant_to_code() {
179 assert_eq!(MoveAbort::MaxPendingOrdersExceeded as u64, 2000);
180 assert_eq!(MoveAbort::SelfTrading as u64, 3008);
181 assert_eq!(Ok(MoveAbort::MaxPendingOrdersExceeded), 2000_u64.try_into());
182 assert_eq!(Ok(MoveAbort::SelfTrading), 3008_u64.try_into());
183 }
184}