nautilus_deribit/common/consts.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Core constants for the Deribit adapter.
17
18use std::sync::LazyLock;
19
20use ahash::AHashSet;
21use nautilus_model::identifiers::Venue;
22use ustr::Ustr;
23
24/// Venue identifier string.
25pub const DERIBIT: &str = "DERIBIT";
26
27/// Static venue instance.
28pub static DERIBIT_VENUE: LazyLock<Venue> = LazyLock::new(|| Venue::new(Ustr::from(DERIBIT)));
29
30// Production URLs
31pub const DERIBIT_HTTP_URL: &str = "https://www.deribit.com";
32pub const DERIBIT_WS_URL: &str = "wss://www.deribit.com/ws/api/v2";
33
34// Testnet URLs
35pub const DERIBIT_TESTNET_HTTP_URL: &str = "https://test.deribit.com";
36pub const DERIBIT_TESTNET_WS_URL: &str = "wss://test.deribit.com/ws/api/v2";
37
38// API paths
39pub const DERIBIT_API_VERSION: &str = "v2";
40pub const DERIBIT_API_PATH: &str = "/api/v2";
41
42// JSON-RPC constants
43pub const JSONRPC_VERSION: &str = "2.0";
44
45/// Deribit error codes that should trigger retries.
46///
47/// Only retry on temporary network/system issues that are likely to resolve.
48/// Based on Deribit API documentation error codes.
49///
50/// # Error Code Categories
51///
52/// **Retriable (temporary issues):**
53/// - `10028`: "too_many_requests" - Rate limit exceeded
54/// - `10040`: "retry" - Explicitly says request should be retried
55/// - `10041`: "settlement_in_progress" - Settlement calculation in progress (few seconds)
56/// - `10047`: "matching_engine_queue_full" - Matching engine queue full
57/// - `10066`: "too_many_concurrent_requests" - Too many concurrent public requests
58/// - `11051`: "system_maintenance" - System under maintenance
59/// - `11094`: "internal_server_error" - Unhandled server error
60/// - `13028`: "temporarily_unavailable" - Service not responding or too slow
61/// - `13888`: "timed_out" - Server timeout processing request
62///
63/// **Non-retriable (permanent errors):**
64/// - `10000`: "authorization_required" - Auth issue, invalid signature
65/// - `10004`: "order_not_found" - Order can't be found
66/// - `10009`: "not_enough_funds" - Insufficient funds
67/// - `10020`: "invalid_or_unsupported_instrument" - Invalid instrument name
68/// - `10029`: "not_owner_of_order" - Attempt to operate with not own order
69/// - `11029`: "invalid_arguments" - Invalid input detected
70/// - `11050`: "bad_request" - Request not parsed properly
71/// - `13004`: "invalid_credentials" - Invalid API credentials
72/// - `13009`: "unauthorized" - Wrong/expired token or bad signature
73/// - `13020`: "not_found" - Instrument not found
74/// - `13021`: "forbidden" - Not enough permissions
75///
76/// # References
77///
78/// <https://docs.deribit.com/#rpc-error-codes>
79pub static DERIBIT_RETRY_ERROR_CODES: LazyLock<AHashSet<i64>> = LazyLock::new(|| {
80 let mut codes = AHashSet::new();
81
82 // Rate limiting (temporary - will resolve after backoff)
83 codes.insert(10028); // too_many_requests
84 codes.insert(10066); // too_many_concurrent_requests
85
86 // Explicit retry instruction
87 codes.insert(10040); // retry - API explicitly says to retry
88
89 // System issues (temporary - maintenance, settlement, or overload)
90 codes.insert(10041); // settlement_in_progress - daily settlement (few seconds)
91 codes.insert(10047); // matching_engine_queue_full
92 codes.insert(11051); // system_maintenance
93 codes.insert(11094); // internal_server_error
94 codes.insert(13028); // temporarily_unavailable
95
96 // Timeout (temporary - may succeed on retry)
97 codes.insert(13888); // timed_out
98
99 codes
100});
101
102/// Determines if a Deribit error code should trigger a retry.
103///
104/// # Arguments
105///
106/// * `error_code` - The Deribit error code from the JSON-RPC error response
107///
108/// # Returns
109///
110/// `true` if the error is temporary and should be retried, `false` otherwise
111pub fn should_retry_error_code(error_code: i64) -> bool {
112 DERIBIT_RETRY_ERROR_CODES.contains(&error_code)
113}