lighter_rs/
errors.rs

1//! Error types for the Lighter Protocol SDK
2
3use thiserror::Error;
4
5/// Result type alias using LighterError
6pub type Result<T> = std::result::Result<T, LighterError>;
7
8/// Main error type for the Lighter SDK
9#[derive(Error, Debug)]
10pub enum LighterError {
11    // Account and API Key Errors
12    #[error(
13        "Account index {0} is too low, minimum is {}",
14        crate::constants::MIN_ACCOUNT_INDEX
15    )]
16    AccountIndexTooLow(i64),
17
18    #[error(
19        "Account index {0} is too high, maximum is {}",
20        crate::constants::MAX_ACCOUNT_INDEX
21    )]
22    AccountIndexTooHigh(i64),
23
24    #[error(
25        "API key index {0} is too low, minimum is {}",
26        crate::constants::MIN_API_KEY_INDEX
27    )]
28    ApiKeyIndexTooLow(u8),
29
30    #[error(
31        "API key index {0} is too high, maximum is {}",
32        crate::constants::MAX_API_KEY_INDEX
33    )]
34    ApiKeyIndexTooHigh(u8),
35
36    // Market Errors
37    #[error(
38        "Market index {0} is too low, minimum is {}",
39        crate::constants::MIN_MARKET_INDEX
40    )]
41    MarketIndexTooLow(u8),
42
43    #[error(
44        "Market index {0} is too high, maximum is {}",
45        crate::constants::MAX_MARKET_INDEX
46    )]
47    MarketIndexTooHigh(u8),
48
49    #[error("Market index mismatch")]
50    MarketIndexMismatch,
51
52    // Order Errors
53    #[error(
54        "Client order index {0} is too low, minimum is {}",
55        crate::constants::MIN_CLIENT_ORDER_INDEX
56    )]
57    ClientOrderIndexTooLow(i64),
58
59    #[error(
60        "Client order index {0} is too high, maximum is {}",
61        crate::constants::MAX_CLIENT_ORDER_INDEX
62    )]
63    ClientOrderIndexTooHigh(i64),
64
65    #[error("Client order index should be nil")]
66    ClientOrderIndexNotNil,
67
68    #[error(
69        "Order index {0} is too low, minimum is {}",
70        crate::constants::MIN_ORDER_INDEX
71    )]
72    OrderIndexTooLow(i64),
73
74    #[error(
75        "Order index {0} is too high, maximum is {}",
76        crate::constants::MAX_ORDER_INDEX
77    )]
78    OrderIndexTooHigh(i64),
79
80    #[error(
81        "Base amount {0} is too low, minimum is {}",
82        crate::constants::MIN_ORDER_BASE_AMOUNT
83    )]
84    BaseAmountTooLow(i64),
85
86    #[error(
87        "Base amount {0} is too high, maximum is {}",
88        crate::constants::MAX_ORDER_BASE_AMOUNT
89    )]
90    BaseAmountTooHigh(i64),
91
92    #[error("Base amounts are not equal")]
93    BaseAmountsNotEqual,
94
95    #[error("Base amount should be nil")]
96    BaseAmountNotNil,
97
98    #[error(
99        "Order price {0} is too low, minimum is {}",
100        crate::constants::MIN_ORDER_PRICE
101    )]
102    PriceTooLow(u32),
103
104    #[error(
105        "Order price {0} is too high, maximum is {}",
106        crate::constants::MAX_ORDER_PRICE
107    )]
108    PriceTooHigh(u32),
109
110    #[error("IsAsk should be 0 or 1")]
111    IsAskInvalid,
112
113    #[error("Order type is invalid")]
114    OrderTypeInvalid,
115
116    #[error("Order time-in-force is invalid")]
117    OrderTimeInForceInvalid,
118
119    #[error("Order reduce-only flag is invalid")]
120    OrderReduceOnlyInvalid,
121
122    #[error("Order trigger price is invalid")]
123    OrderTriggerPriceInvalid,
124
125    #[error("Order expiry is invalid")]
126    OrderExpiryInvalid,
127
128    #[error("Grouping type is invalid")]
129    GroupingTypeInvalid,
130
131    #[error("Order group size is invalid")]
132    OrderGroupSizeInvalid,
133
134    // Pool Errors
135    #[error(
136        "Public pool index {0} is too low, minimum is {}",
137        crate::constants::MIN_ACCOUNT_INDEX
138    )]
139    PublicPoolIndexTooLow(i64),
140
141    #[error(
142        "Public pool index {0} is too high, maximum is {}",
143        crate::constants::MAX_ACCOUNT_INDEX
144    )]
145    PublicPoolIndexTooHigh(i64),
146
147    #[error(
148        "Pool operator fee is invalid, should be 0 to {}",
149        crate::constants::FEE_TICK
150    )]
151    InvalidPoolOperatorFee,
152
153    #[error("Pool status is invalid, should be 0 or 1")]
154    InvalidPoolStatus,
155
156    #[error(
157        "Pool initial total shares {0} is too low, minimum is {}",
158        crate::constants::MIN_INITIAL_TOTAL_SHARES
159    )]
160    PoolInitialTotalSharesTooLow(i64),
161
162    #[error(
163        "Pool initial total shares {0} is too high, maximum is {}",
164        crate::constants::MAX_INITIAL_TOTAL_SHARES
165    )]
166    PoolInitialTotalSharesTooHigh(i64),
167
168    #[error("Pool min operator share rate is too low, should be greater than 0")]
169    PoolMinOperatorShareRateTooLow,
170
171    #[error(
172        "Pool min operator share rate is too high, maximum is {}",
173        crate::constants::SHARE_TICK
174    )]
175    PoolMinOperatorShareRateTooHigh,
176
177    #[error(
178        "Pool mint share amount {0} is too low, minimum is {}",
179        crate::constants::MIN_POOL_SHARES_TO_MINT_OR_BURN
180    )]
181    PoolMintShareAmountTooLow(i64),
182
183    #[error(
184        "Pool mint share amount {0} is too high, maximum is {}",
185        crate::constants::MAX_POOL_SHARES_TO_MINT_OR_BURN
186    )]
187    PoolMintShareAmountTooHigh(i64),
188
189    #[error(
190        "Pool burn share amount {0} is too low, minimum is {}",
191        crate::constants::MIN_POOL_SHARES_TO_MINT_OR_BURN
192    )]
193    PoolBurnShareAmountTooLow(i64),
194
195    #[error(
196        "Pool burn share amount {0} is too high, maximum is {}",
197        crate::constants::MAX_POOL_SHARES_TO_MINT_OR_BURN
198    )]
199    PoolBurnShareAmountTooHigh(i64),
200
201    // Transfer and Withdrawal Errors
202    #[error(
203        "Withdrawal amount {0} is too low, minimum is {}",
204        crate::constants::MIN_WITHDRAWAL_AMOUNT
205    )]
206    WithdrawalAmountTooLow(u64),
207
208    #[error(
209        "Withdrawal amount {0} is too high, maximum is {}",
210        crate::constants::MAX_WITHDRAWAL_AMOUNT
211    )]
212    WithdrawalAmountTooHigh(u64),
213
214    #[error(
215        "Transfer amount {0} is too low, minimum is {}",
216        crate::constants::MIN_TRANSFER_AMOUNT
217    )]
218    TransferAmountTooLow(i64),
219
220    #[error(
221        "Transfer amount {0} is too high, maximum is {}",
222        crate::constants::MAX_TRANSFER_AMOUNT
223    )]
224    TransferAmountTooHigh(i64),
225
226    #[error("Transfer fee is negative")]
227    TransferFeeNegative,
228
229    #[error(
230        "Transfer fee is too high, maximum is {}",
231        crate::constants::MAX_TRANSFER_AMOUNT
232    )]
233    TransferFeeTooHigh,
234
235    #[error(
236        "To account index {0} is too low, minimum is {}",
237        crate::constants::MIN_ACCOUNT_INDEX
238    )]
239    ToAccountIndexTooLow(i64),
240
241    #[error(
242        "To account index {0} is too high, maximum is {}",
243        crate::constants::MAX_ACCOUNT_INDEX
244    )]
245    ToAccountIndexTooHigh(i64),
246
247    #[error(
248        "From account index {0} is too low, minimum is {}",
249        crate::constants::MIN_ACCOUNT_INDEX
250    )]
251    FromAccountIndexTooLow(i64),
252
253    #[error(
254        "From account index {0} is too high, maximum is {}",
255        crate::constants::MAX_ACCOUNT_INDEX
256    )]
257    FromAccountIndexTooHigh(i64),
258
259    // Margin Errors
260    #[error("Initial margin fraction is too low, minimum is 0")]
261    InitialMarginFractionTooLow,
262
263    #[error(
264        "Initial margin fraction {0} is too high, maximum is {}",
265        crate::constants::MARGIN_FRACTION_TICK
266    )]
267    InitialMarginFractionTooHigh(u16),
268
269    #[error("Margin mode is invalid")]
270    InvalidMarginMode,
271
272    #[error("Margin movement direction is invalid")]
273    InvalidUpdateMarginDirection,
274
275    // General Errors
276    #[error("Nonce {0} is too low, minimum is {}", crate::constants::MIN_NONCE)]
277    NonceTooLow(i64),
278
279    #[error("ExpiredAt is invalid")]
280    ExpiredAtInvalid,
281
282    #[error("Public key is invalid")]
283    PubKeyInvalid,
284
285    #[error("Transaction signature is invalid")]
286    InvalidSignature,
287
288    #[error("Cancel all time-in-force is invalid")]
289    InvalidCancelAllTimeInForce,
290
291    #[error("Cancel all time is not in valid range")]
292    CancelAllTimeIsNotInRange,
293
294    #[error("Cancel all time should be nil")]
295    CancelAllTimeIsNotNil,
296
297    #[error("Cancel mode is invalid")]
298    CancelModeInvalid,
299
300    // Cryptographic Errors
301    #[error("Invalid private key length: expected {expected}, got {actual}")]
302    InvalidPrivateKeyLength { expected: usize, actual: usize },
303
304    #[error("Invalid public key length: expected {expected}, got {actual}")]
305    InvalidPublicKeyLength { expected: usize, actual: usize },
306
307    #[error("Failed to parse hex: {0}")]
308    HexParseError(#[from] hex::FromHexError),
309
310    #[error("Cryptographic operation failed: {0}")]
311    CryptoError(String),
312
313    // HTTP and Network Errors
314    #[error("HTTP request failed: {0}")]
315    HttpError(#[from] reqwest::Error),
316
317    #[error("API error: {0}")]
318    ApiError(String),
319
320    #[error("Invalid response from server: {0}")]
321    InvalidResponse(String),
322
323    #[error("Network timeout")]
324    Timeout,
325
326    // JSON Errors
327    #[error("JSON serialization/deserialization error: {0}")]
328    JsonError(#[from] serde_json::Error),
329
330    // Generic Errors
331    #[error("Missing required field: {0}")]
332    MissingField(String),
333
334    #[error("Invalid configuration: {0}")]
335    InvalidConfiguration(String),
336
337    #[error("Validation error: {0}")]
338    ValidationError(String),
339
340    #[error("{0}")]
341    Other(String),
342}
343
344impl From<String> for LighterError {
345    fn from(s: String) -> Self {
346        LighterError::Other(s)
347    }
348}
349
350impl From<&str> for LighterError {
351    fn from(s: &str) -> Self {
352        LighterError::Other(s.to_string())
353    }
354}