nautilus_dydx/common/
urls.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//! URL helpers for dYdX services.
17
18use super::consts::{
19    DYDX_GRPC_URLS, DYDX_HTTP_URL, DYDX_TESTNET_GRPC_URLS, DYDX_TESTNET_HTTP_URL,
20    DYDX_TESTNET_WS_URL, DYDX_WS_URL,
21};
22
23/// Gets the HTTP base URL for the specified network.
24#[must_use]
25pub const fn http_base_url(is_testnet: bool) -> &'static str {
26    if is_testnet {
27        DYDX_TESTNET_HTTP_URL
28    } else {
29        DYDX_HTTP_URL
30    }
31}
32
33/// Gets the WebSocket URL for the specified network.
34#[must_use]
35pub const fn ws_url(is_testnet: bool) -> &'static str {
36    if is_testnet {
37        DYDX_TESTNET_WS_URL
38    } else {
39        DYDX_WS_URL
40    }
41}
42
43/// Gets the gRPC URLs with fallback support for the specified network.
44///
45/// Returns an array of gRPC endpoints that should be tried in order.
46/// This is important for DEX environments where individual validator nodes
47/// can become unavailable or fail.
48#[must_use]
49pub const fn grpc_urls(is_testnet: bool) -> &'static [&'static str] {
50    if is_testnet {
51        DYDX_TESTNET_GRPC_URLS
52    } else {
53        DYDX_GRPC_URLS
54    }
55}
56
57/// Gets the primary gRPC URL for the specified network.
58///
59/// # Notes
60///
61/// For production use, consider using `grpc_urls()` to get all available
62/// endpoints and implement fallback logic via `DydxGrpcClient::new_with_fallback()`.
63#[must_use]
64pub const fn grpc_url(is_testnet: bool) -> &'static str {
65    grpc_urls(is_testnet)[0]
66}