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 reqwest::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.
65pub const DYDX_GRPC_URLS: &[&str] = &[
66    "https://dydx-grpc.publicnode.com:443",
67    "https://dydx-ops-grpc.kingnodes.com:443",
68    "https://dydx-mainnet-grpc.autostake.com:443",
69];
70
71/// dYdX v4 mainnet gRPC URL (primary public node).
72///
73/// # Notes
74///
75/// For production use, consider using `DYDX_GRPC_URLS` array with fallback logic
76/// via `DydxGrpcClient::new_with_fallback()`.
77pub const DYDX_GRPC_URL: &str = DYDX_GRPC_URLS[0];
78
79// Testnet URLs
80/// dYdX v4 testnet HTTP API base URL.
81pub const DYDX_TESTNET_HTTP_URL: &str = "https://indexer.v4testnet.dydx.exchange";
82
83/// dYdX v4 testnet WebSocket URL.
84pub const DYDX_TESTNET_WS_URL: &str = "wss://indexer.v4testnet.dydx.exchange/v4/ws";
85
86/// dYdX v4 testnet gRPC URLs (public validator nodes with fallbacks).
87///
88/// Multiple nodes are provided for redundancy. The client should attempt to connect
89/// to nodes in order, falling back to the next if connection fails.
90pub const DYDX_TESTNET_GRPC_URLS: &[&str] = &[
91    "https://dydx-testnet-grpc.publicnode.com:443",
92    "https://test-dydx-grpc.kingnodes.com:443",
93];
94
95/// dYdX v4 testnet gRPC URL (primary public node).
96///
97/// # Notes
98///
99/// For production use, consider using `DYDX_TESTNET_GRPC_URLS` array with fallback logic
100/// via `DydxGrpcClient::new_with_fallback()`.
101pub const DYDX_TESTNET_GRPC_URL: &str = DYDX_TESTNET_GRPC_URLS[0];
102
103/// Determines if an HTTP status code should trigger a retry.
104///
105/// Retries on:
106/// - 429 (Too Many Requests)
107/// - 500-599 (Server Errors)
108///
109/// Does NOT retry on:
110/// - 400 (Bad Request) - indicates client error that won't be fixed by retrying
111/// - 401 (Unauthorized) - not applicable for dYdX Indexer (no auth required)
112/// - 403 (Forbidden) - typically compliance/screening issues
113/// - 404 (Not Found) - resource doesn't exist
114#[must_use]
115pub const fn should_retry_error_code(status: &StatusCode) -> bool {
116    matches!(status.as_u16(), 429 | 500..=599)
117}
118
119////////////////////////////////////////////////////////////////////////////////
120// Tests
121////////////////////////////////////////////////////////////////////////////////
122
123#[cfg(test)]
124mod tests {
125    use rstest::rstest;
126
127    use super::*;
128
129    #[rstest]
130    fn test_should_retry_429() {
131        assert!(should_retry_error_code(&StatusCode::TOO_MANY_REQUESTS));
132    }
133
134    #[rstest]
135    fn test_should_retry_server_errors() {
136        assert!(should_retry_error_code(&StatusCode::INTERNAL_SERVER_ERROR));
137        assert!(should_retry_error_code(&StatusCode::BAD_GATEWAY));
138        assert!(should_retry_error_code(&StatusCode::SERVICE_UNAVAILABLE));
139        assert!(should_retry_error_code(&StatusCode::GATEWAY_TIMEOUT));
140    }
141
142    #[rstest]
143    fn test_should_not_retry_client_errors() {
144        assert!(!should_retry_error_code(&StatusCode::BAD_REQUEST));
145        assert!(!should_retry_error_code(&StatusCode::UNAUTHORIZED));
146        assert!(!should_retry_error_code(&StatusCode::FORBIDDEN));
147        assert!(!should_retry_error_code(&StatusCode::NOT_FOUND));
148    }
149
150    #[rstest]
151    fn test_should_not_retry_success() {
152        assert!(!should_retry_error_code(&StatusCode::OK));
153        assert!(!should_retry_error_code(&StatusCode::CREATED));
154    }
155}