Skip to main content

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::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 REST API URL (Cosmos LCD for chain queries).
61///
62/// Used for querying on-chain state like authenticators.
63pub const DYDX_REST_URL: &str = "https://dydx-ops-rest.kingnodes.com";
64
65/// dYdX v4 mainnet gRPC URLs (public validator nodes with fallbacks).
66///
67/// Multiple nodes are provided for redundancy. The client should attempt to connect
68/// to nodes in order, falling back to the next if connection fails. This is critical
69/// for DEX environments where individual nodes can fail or become unavailable.
70///
71/// Endpoints sourced from:
72/// - <https://docs.dydx.xyz/interaction/endpoints#node>
73///
74/// # Notes
75///
76/// URLs use domain:port format for tonic gRPC client (TLS is automatic on port 443).
77pub const DYDX_GRPC_URLS: &[&str] = &[
78    "https://dydx-ops-grpc.kingnodes.com:443",
79    "https://dydx-dao-grpc-1.polkachu.com:443",
80    "https://dydx-grpc.publicnode.com:443",
81];
82
83/// dYdX v4 mainnet gRPC URL (primary public node).
84///
85/// # Notes
86///
87/// For production use, consider using `DYDX_GRPC_URLS` array with fallback logic
88/// via `DydxGrpcClient::new_with_fallback()`.
89pub const DYDX_GRPC_URL: &str = DYDX_GRPC_URLS[0];
90
91// Testnet URLs
92/// dYdX v4 testnet HTTP API base URL.
93pub const DYDX_TESTNET_HTTP_URL: &str = "https://indexer.v4testnet.dydx.exchange";
94
95/// dYdX v4 testnet WebSocket URL.
96pub const DYDX_TESTNET_WS_URL: &str = "wss://indexer.v4testnet.dydx.exchange/v4/ws";
97
98/// dYdX v4 testnet REST API URL (Cosmos LCD for chain queries).
99///
100/// Used for querying on-chain state like authenticators.
101pub const DYDX_TESTNET_REST_URL: &str = "https://test-dydx-rest.kingnodes.com";
102
103/// dYdX v4 testnet gRPC URLs (public validator nodes with fallbacks).
104///
105/// Multiple nodes are provided for redundancy. The client should attempt to connect
106/// to nodes in order, falling back to the next if connection fails.
107///
108/// Endpoints sourced from:
109/// - <https://docs.dydx.xyz/interaction/endpoints#node>
110///
111/// # Notes
112///
113/// URLs use domain:port format for tonic gRPC client (TLS is automatic on port 443).
114pub const DYDX_TESTNET_GRPC_URLS: &[&str] = &[
115    "https://test-dydx-grpc.kingnodes.com:443",
116    "https://testnet-dydx.lavenderfive.com:443",
117];
118
119/// dYdX v4 testnet gRPC URL (primary public node).
120///
121/// # Notes
122///
123/// For production use, consider using `DYDX_TESTNET_GRPC_URLS` array with fallback logic
124/// via `DydxGrpcClient::new_with_fallback()`.
125pub const DYDX_TESTNET_GRPC_URL: &str = DYDX_TESTNET_GRPC_URLS[0];
126
127/// Determines if an HTTP status code should trigger a retry.
128///
129/// Retries on:
130/// - 429 (Too Many Requests)
131/// - 500-599 (Server Errors)
132///
133/// Does NOT retry on:
134/// - 400 (Bad Request) - indicates client error that won't be fixed by retrying
135/// - 401 (Unauthorized) - not applicable for dYdX Indexer (no auth required)
136/// - 403 (Forbidden) - typically compliance/screening issues
137/// - 404 (Not Found) - resource doesn't exist
138#[must_use]
139pub const fn should_retry_error_code(status: &StatusCode) -> bool {
140    matches!(status.as_u16(), 429 | 500..=599)
141}
142
143#[cfg(test)]
144mod tests {
145    use rstest::rstest;
146
147    use super::*;
148
149    #[rstest]
150    fn test_should_retry_429() {
151        assert!(should_retry_error_code(&StatusCode::TOO_MANY_REQUESTS));
152    }
153
154    #[rstest]
155    fn test_should_retry_server_errors() {
156        assert!(should_retry_error_code(&StatusCode::INTERNAL_SERVER_ERROR));
157        assert!(should_retry_error_code(&StatusCode::BAD_GATEWAY));
158        assert!(should_retry_error_code(&StatusCode::SERVICE_UNAVAILABLE));
159        assert!(should_retry_error_code(&StatusCode::GATEWAY_TIMEOUT));
160    }
161
162    #[rstest]
163    fn test_should_not_retry_client_errors() {
164        assert!(!should_retry_error_code(&StatusCode::BAD_REQUEST));
165        assert!(!should_retry_error_code(&StatusCode::UNAUTHORIZED));
166        assert!(!should_retry_error_code(&StatusCode::FORBIDDEN));
167        assert!(!should_retry_error_code(&StatusCode::NOT_FOUND));
168    }
169
170    #[rstest]
171    fn test_should_not_retry_success() {
172        assert!(!should_retry_error_code(&StatusCode::OK));
173        assert!(!should_retry_error_code(&StatusCode::CREATED));
174    }
175}