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