Skip to main content

nautilus_dydx/common/
urls.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//! URL helpers for dYdX services.
17
18use super::{
19    consts::{
20        DYDX_GRPC_URLS, DYDX_HTTP_URL, DYDX_REST_URL, DYDX_TESTNET_GRPC_URLS,
21        DYDX_TESTNET_HTTP_URL, DYDX_TESTNET_REST_URL, DYDX_TESTNET_WS_URL, DYDX_WS_URL,
22    },
23    enums::DydxNetwork,
24};
25
26/// Gets the HTTP base URL for the specified network.
27#[must_use]
28pub fn http_base_url(network: DydxNetwork) -> &'static str {
29    match network {
30        DydxNetwork::Testnet => DYDX_TESTNET_HTTP_URL,
31        DydxNetwork::Mainnet => DYDX_HTTP_URL,
32    }
33}
34
35/// Gets the WebSocket URL for the specified network.
36#[must_use]
37pub fn ws_url(network: DydxNetwork) -> &'static str {
38    match network {
39        DydxNetwork::Testnet => DYDX_TESTNET_WS_URL,
40        DydxNetwork::Mainnet => DYDX_WS_URL,
41    }
42}
43
44/// Gets the gRPC URLs with fallback support for the specified network.
45///
46/// Returns an array of gRPC endpoints that should be tried in order.
47/// This is important for DEX environments where individual validator nodes
48/// can become unavailable or fail.
49#[must_use]
50pub fn grpc_urls(network: DydxNetwork) -> &'static [&'static str] {
51    match network {
52        DydxNetwork::Testnet => DYDX_TESTNET_GRPC_URLS,
53        DydxNetwork::Mainnet => 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 fn grpc_url(network: DydxNetwork) -> &'static str {
65    grpc_urls(network)[0]
66}
67
68/// Gets the REST API URL (Cosmos LCD) for the specified network.
69///
70/// Used for querying on-chain state like authenticators.
71#[must_use]
72pub fn rest_url(network: DydxNetwork) -> &'static str {
73    match network {
74        DydxNetwork::Testnet => DYDX_TESTNET_REST_URL,
75        DydxNetwork::Mainnet => DYDX_REST_URL,
76    }
77}