nautilus_dydx/common/consts.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 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 shared across the dYdX adapter components.
17
18use std::sync::LazyLock;
19
20use nautilus_model::identifiers::{ClientId, Venue};
21use nautilus_network::http::StatusCode;
22use ustr::Ustr;
23
24/// dYdX adapter name.
25pub const DYDX: &str = "DYDX";
26
27/// dYdX venue identifier.
28pub static DYDX_VENUE: LazyLock<Venue> = LazyLock::new(|| Venue::new(Ustr::from(DYDX)));
29
30/// dYdX client ID.
31pub static DYDX_CLIENT_ID: LazyLock<ClientId> = LazyLock::new(|| ClientId::new(Ustr::from(DYDX)));
32
33/// dYdX mainnet chain ID.
34pub const DYDX_CHAIN_ID: &str = "dydx-mainnet-1";
35
36/// dYdX testnet chain ID.
37pub const DYDX_TESTNET_CHAIN_ID: &str = "dydx-testnet-4";
38
39/// Cosmos SDK bech32 address prefix for dYdX.
40pub const DYDX_BECH32_PREFIX: &str = "dydx";
41
42/// Order router address for the NautilusTrader order attribution.
43/// Defined by dYdX governance proposal 381 (<https://mintscan.io/dydx/proposals/381>).
44pub const DYDX_NAUTILUS_ORDER_ROUTER_ADDRESS: &str = "dydx1pahjv32ex740hahnp5dc4hnmlchkeea6ndqat5";
45
46/// USDC gas denomination (native chain token).
47pub const USDC_GAS_DENOM: &str =
48 "ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5";
49
50/// USDC asset denomination for transfers.
51pub const USDC_DENOM: &str = "uusdc";
52
53/// HD wallet derivation path for dYdX accounts (Cosmos SLIP-0044).
54/// Format: m/44'/118'/0'/0/{account_index}
55pub const DYDX_DERIVATION_PATH_PREFIX: &str = "m/44'/118'/0'/0";
56
57/// Coin type for Cosmos ecosystem (SLIP-0044).
58pub const COSMOS_COIN_TYPE: u32 = 118;
59
60// Mainnet URLs
61/// dYdX v4 mainnet HTTP API base URL.
62pub const DYDX_HTTP_URL: &str = "https://indexer.dydx.trade";
63
64/// dYdX v4 mainnet WebSocket URL.
65pub const DYDX_WS_URL: &str = "wss://indexer.dydx.trade/v4/ws";
66
67/// dYdX v4 mainnet REST API URL (Cosmos LCD for chain queries).
68///
69/// Used for querying on-chain state like authenticators.
70pub const DYDX_REST_URL: &str = "https://dydx-ops-rest.kingnodes.com";
71
72/// dYdX v4 mainnet gRPC URLs (public validator nodes with fallbacks).
73///
74/// Multiple nodes are provided for redundancy. The client should attempt to connect
75/// to nodes in order, falling back to the next if connection fails. This is critical
76/// for DEX environments where individual nodes can fail or become unavailable.
77///
78/// Endpoints sourced from:
79/// - <https://docs.dydx.xyz/interaction/endpoints#node>
80///
81/// # Notes
82///
83/// URLs use domain:port format for tonic gRPC client (TLS is automatic on port 443).
84pub const DYDX_GRPC_URLS: &[&str] = &[
85 "https://dydx-ops-grpc.kingnodes.com:443",
86 "https://dydx-dao-grpc-1.polkachu.com:443",
87 "https://dydx-grpc.publicnode.com:443",
88];
89
90/// dYdX v4 mainnet gRPC URL (primary public node).
91///
92/// # Notes
93///
94/// For production use, consider using `DYDX_GRPC_URLS` array with fallback logic
95/// via `DydxGrpcClient::new_with_fallback()`.
96pub const DYDX_GRPC_URL: &str = DYDX_GRPC_URLS[0];
97
98// Testnet URLs
99/// dYdX v4 testnet HTTP API base URL.
100pub const DYDX_TESTNET_HTTP_URL: &str = "https://indexer.v4testnet.dydx.exchange";
101
102/// dYdX v4 testnet WebSocket URL.
103pub const DYDX_TESTNET_WS_URL: &str = "wss://indexer.v4testnet.dydx.exchange/v4/ws";
104
105/// dYdX v4 testnet REST API URL (Cosmos LCD for chain queries).
106///
107/// Used for querying on-chain state like authenticators.
108pub const DYDX_TESTNET_REST_URL: &str = "https://test-dydx-rest.kingnodes.com";
109
110/// dYdX v4 testnet gRPC URLs (public validator nodes with fallbacks).
111///
112/// Multiple nodes are provided for redundancy. The client should attempt to connect
113/// to nodes in order, falling back to the next if connection fails.
114///
115/// Endpoints sourced from:
116/// - <https://docs.dydx.xyz/interaction/endpoints#node>
117///
118/// # Notes
119///
120/// URLs use domain:port format for tonic gRPC client (TLS is automatic on port 443).
121pub const DYDX_TESTNET_GRPC_URLS: &[&str] = &[
122 "https://test-dydx-grpc.kingnodes.com:443",
123 "https://testnet-dydx.lavenderfive.com:443",
124];
125
126/// dYdX v4 testnet gRPC URL (primary public node).
127///
128/// # Notes
129///
130/// For production use, consider using `DYDX_TESTNET_GRPC_URLS` array with fallback logic
131/// via `DydxGrpcClient::new_with_fallback()`.
132pub const DYDX_TESTNET_GRPC_URL: &str = DYDX_TESTNET_GRPC_URLS[0];
133
134/// Determines if an HTTP status code should trigger a retry.
135///
136/// Retries on:
137/// - 429 (Too Many Requests)
138/// - 500-599 (Server Errors)
139///
140/// Does NOT retry on:
141/// - 400 (Bad Request) - indicates client error that won't be fixed by retrying
142/// - 401 (Unauthorized) - not applicable for dYdX Indexer (no auth required)
143/// - 403 (Forbidden) - typically compliance/screening issues
144/// - 404 (Not Found) - resource doesn't exist
145#[must_use]
146pub const fn should_retry_error_code(status: &StatusCode) -> bool {
147 matches!(status.as_u16(), 429 | 500..=599)
148}
149
150#[cfg(test)]
151mod tests {
152 use rstest::rstest;
153
154 use super::*;
155
156 #[rstest]
157 fn test_should_retry_429() {
158 assert!(should_retry_error_code(&StatusCode::TOO_MANY_REQUESTS));
159 }
160
161 #[rstest]
162 fn test_should_retry_server_errors() {
163 assert!(should_retry_error_code(&StatusCode::INTERNAL_SERVER_ERROR));
164 assert!(should_retry_error_code(&StatusCode::BAD_GATEWAY));
165 assert!(should_retry_error_code(&StatusCode::SERVICE_UNAVAILABLE));
166 assert!(should_retry_error_code(&StatusCode::GATEWAY_TIMEOUT));
167 }
168
169 #[rstest]
170 fn test_should_not_retry_client_errors() {
171 assert!(!should_retry_error_code(&StatusCode::BAD_REQUEST));
172 assert!(!should_retry_error_code(&StatusCode::UNAUTHORIZED));
173 assert!(!should_retry_error_code(&StatusCode::FORBIDDEN));
174 assert!(!should_retry_error_code(&StatusCode::NOT_FOUND));
175 }
176
177 #[rstest]
178 fn test_should_not_retry_success() {
179 assert!(!should_retry_error_code(&StatusCode::OK));
180 assert!(!should_retry_error_code(&StatusCode::CREATED));
181 }
182}