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