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 account used the wrong account cap
68 const InvalidAccountCap: u64 = 13;
69 /// Raised when passing an integrator taker fee that is greater than
70 /// the `max_integrator_taker_fee` set by the user in its account
71 const InvalidIntegratorTakerFee: u64 = 14;
72 /// Raised when trying to call a function with the wrong package's version
73 const WrongVersion: u64 = 16;
74 /// Raised when trying to have a session composed by only `start_session` and `end_session`
75 const EmptySession: u64 = 17;
76 /// Market already registered in the registry
77 const MarketAlreadyRegistered: u64 = 18;
78 /// Collateral is not registered in the registry
79 const CollateralIsNotRegistered: u64 = 19;
80 /// Market is not registered in the registry
81 const MarketIsNotRegistered: u64 = 20;
82 /// Invalid collateral price feed storage for the clearing house
83 const InvalidCollateralPriceFeedStorage: u64 = 21;
84 /// Fees accrued are negative
85 const NegativeFeesAccrued: u64 = 22;
86 /// Passed a timestamp older than current Clock's one
87 const InvalidExpirationTimestamp: u64 = 23;
88 /// Stop order gas cost provided is not enough
89 const NotEnoughGasForStopOrder: u64 = 24;
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 /// Raised processing a session that requires a collateral allocation,
101 /// but not enough collateral is available in the account
102 const NotEnoughCollateralToAllocateForSession: u64 = 30;
103 /// Raised processing a session that requires a collateral allocation
104 /// and a wrong account is being used to fund it
105 const WrongAccountIdForAllocation: u64 = 31;
106 /// Raised when trying to create an integrator vault for an address
107 /// that already has one
108 const IntegratorVaultAlreadyExists: u64 = 32;
109 /// Raised when trying to access an integrator vault that does not exist
110 const IntegratorVaultDoesNotExist: u64 = 33;
111 /// Raised when trying to place an order passing a `size` that is not a multiple
112 /// of market's lot size
113 const SizeNotMultipleOfLotSize: u64 = 34;
114 /// Raised when trying to place an order passing a `price` that is not a multiple
115 /// of market's tick size
116 const PriceNotMultipleOfTickSize: u64 = 35;
117
118 // Market ---------------------------------------------------------------
119
120 /// While creating ordered map with invalid parameters,
121 /// or changing them improperly for an existent map.
122 const InvalidMarketParameters: u64 = 1000;
123 /// Tried to call `update_funding` before enough time has passed since the
124 /// last update.
125 const UpdatingFundingTooEarly: u64 = 1001;
126 /// Margin ratio update proposal already exists for market
127 const ProposalAlreadyExists: u64 = 1002;
128 /// Margin ratio update proposal cannot be commited too early
129 const PrematureProposal: u64 = 1003;
130 /// Margin ratio update proposal delay is outside the valid range
131 const InvalidProposalDelay: u64 = 1004;
132 /// Margin ratio update proposal does not exist for market
133 const ProposalDoesNotExist: u64 = 1005;
134 /// Exchange has no available fees to withdraw
135 const NoFeesAccrued: u64 = 1006;
136 /// Tried to withdraw more insurance funds than the allowed amount
137 const InsufficientInsuranceSurplus: u64 = 1007;
138 /// Cannot create a market for which a price feed does not exist
139 const NoPriceFeedForMarket: u64 = 1008;
140 /// Cannot delete a proposal that already matured. It can only be committed.
141 const ProposalAlreadyMatured: u64 = 1009;
142 /// Raised when an operation is performed while the market is paused
143 const MarketIsPaused: u64 = 1010;
144 /// Raised when trying to call `close_position_at_settlement_prices` while
145 /// the market is not paused
146 const MarketIsNotPaused: u64 = 1011;
147 /// Raised when trying to call `close_position_at_settlement_prices` while
148 /// before `close_market` has been called by admin
149 const MarketIsNotClosed: u64 = 1012;
150 /// Raised when Admin tries to resume a closed market
151 const MarketIsClosed: u64 = 1013;
152
153 // Position ---------------------------------------------------------------
154
155 /// Tried placing a new pending order when the position already has the maximum
156 /// allowed number of pending orders.
157 const MaxPendingOrdersExceeded: u64 = 2000;
158 /// Used for checking both liqee and liqor positions during liquidation
159 const PositionBelowIMR: u64 = 2001;
160 /// When leaving liqee's position with a margin ratio above tolerance,
161 /// meaning that liqor has overbought position
162 const PositionAboveTolerance: u64 = 2002;
163 /// An operation brought an account below initial margin requirements.
164 const InitialMarginRequirementViolated: u64 = 2003;
165 /// Position is above MMR, so can't be liquidated.
166 const PositionAboveMMR: u64 = 2004;
167 /// Cannot realize bad debt via means other than calling 'liquidate'.
168 const PositionBadDebt: u64 = 2005;
169 /// Cannot withdraw more than the account's free collateral.
170 const InsufficientFreeCollateral: u64 = 2006;
171 /// Cannot have more than 1 position in a market.
172 const PositionAlreadyExists: u64 = 2007;
173 /// Cannot compute deallocate amount for a target MR < IMR.
174 const DeallocateTargetMrTooLow: u64 = 2008;
175 /// Raised when trying to set a position's IMR lower than market's IMR or higher than 1
176 const InvalidPositionIMR: u64 = 2009;
177 /// Invalid stop order type
178 const InvalidStopOrderType: u64 = 2010;
179 /// Invalid position' status for placing a SLTP order
180 const InvalidPositionForSLTP: u64 = 2011;
181
182 // Orderbook & OrderedMap -------------------------------------------------------
183
184 /// While creating ordered map with wrong parameters.
185 const InvalidMapParameters: u64 = 3000;
186 /// While searching for a key, but it doesn't exist.
187 const KeyNotExist: u64 = 3001;
188 /// While inserting already existing key.
189 const KeyAlreadyExists: u64 = 3002;
190 /// When attempting to destroy a non-empty map
191 const DestroyNotEmpty: u64 = 3003;
192 /// Invalid user tries to modify an order
193 const InvalidUserForOrder: u64 = 3004;
194 /// Orderbook flag requirements violated
195 const FlagRequirementsViolated: u64 = 3005;
196 /// Minimum size matched not reached
197 const NotEnoughLiquidity: u64 = 3006;
198 /// When trying to change a map configuration, but the map has
199 /// length less than 4
200 const MapTooSmall: u64 = 3007;
201 /// When taker matches its own order
202 const SelfTrading: u64 = 3008;
203}
204}
205
206#[cfg(test)]
207mod tests {
208 use super::MoveAbort;
209
210 #[test]
211 fn variant_to_code() {
212 assert_eq!(MoveAbort::MaxPendingOrdersExceeded as u64, 2000);
213 assert_eq!(MoveAbort::SelfTrading as u64, 3008);
214 assert_eq!(Ok(MoveAbort::MaxPendingOrdersExceeded), 2000_u64.try_into());
215 assert_eq!(Ok(MoveAbort::SelfTrading), 3008_u64.try_into());
216 }
217}