nautilus_bybit/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//! Helpers for resolving Bybit REST and WebSocket base URLs at runtime.
17
18use super::enums::{BybitEnvironment, BybitProductType};
19
20const STREAM_MAINNET: &str = "stream";
21const STREAM_TESTNET: &str = "stream-testnet";
22const STREAM_DEMO: &str = "stream-demo";
23
24/// Returns the base HTTP endpoint for the given environment.
25#[must_use]
26pub const fn bybit_http_base_url(environment: BybitEnvironment) -> &'static str {
27    match environment {
28        BybitEnvironment::Mainnet => "https://api.bybit.com",
29        BybitEnvironment::Demo => "https://api-demo.bybit.com",
30        BybitEnvironment::Testnet => "https://api-testnet.bybit.com",
31    }
32}
33
34fn ws_public_subdomain(environment: BybitEnvironment) -> &'static str {
35    match environment {
36        BybitEnvironment::Mainnet => STREAM_MAINNET,
37        BybitEnvironment::Demo => STREAM_DEMO,
38        BybitEnvironment::Testnet => STREAM_TESTNET,
39    }
40}
41
42/// Builds the public WebSocket endpoint for the given product/environment pair.
43#[must_use]
44pub fn bybit_ws_public_url(
45    product_type: BybitProductType,
46    environment: BybitEnvironment,
47) -> String {
48    let subdomain = ws_public_subdomain(environment);
49    format!(
50        "wss://{subdomain}.bybit.com/v5/public/{}",
51        product_type.as_str()
52    )
53}
54
55/// Returns the private WebSocket endpoint for the given environment.
56///
57/// # Notes
58///
59/// Bybit only exposes dedicated demo endpoints for public market data streams.
60/// Private and trade channels share the main/testnet hosts.
61#[must_use]
62pub const fn bybit_ws_private_url(environment: BybitEnvironment) -> &'static str {
63    match environment {
64        BybitEnvironment::Testnet => "wss://stream-testnet.bybit.com/v5/private",
65        BybitEnvironment::Mainnet | BybitEnvironment::Demo => "wss://stream.bybit.com/v5/private",
66    }
67}
68
69/// Returns the trade WebSocket endpoint for order operations.
70#[must_use]
71pub const fn bybit_ws_trade_url(environment: BybitEnvironment) -> &'static str {
72    match environment {
73        BybitEnvironment::Testnet => "wss://stream-testnet.bybit.com/v5/trade",
74        BybitEnvironment::Mainnet | BybitEnvironment::Demo => "wss://stream.bybit.com/v5/trade",
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use rstest::rstest;
81
82    use super::*;
83
84    #[rstest]
85    fn resolves_public_urls() {
86        assert_eq!(
87            bybit_ws_public_url(BybitProductType::Linear, BybitEnvironment::Mainnet),
88            "wss://stream.bybit.com/v5/public/linear"
89        );
90        assert_eq!(
91            bybit_ws_public_url(BybitProductType::Option, BybitEnvironment::Demo),
92            "wss://stream-demo.bybit.com/v5/public/option"
93        );
94        assert_eq!(
95            bybit_ws_public_url(BybitProductType::Inverse, BybitEnvironment::Testnet),
96            "wss://stream-testnet.bybit.com/v5/public/inverse"
97        );
98    }
99
100    #[rstest]
101    fn resolves_private_urls() {
102        assert_eq!(
103            bybit_ws_private_url(BybitEnvironment::Mainnet),
104            "wss://stream.bybit.com/v5/private"
105        );
106        assert_eq!(
107            bybit_ws_private_url(BybitEnvironment::Demo),
108            "wss://stream.bybit.com/v5/private"
109        );
110        assert_eq!(
111            bybit_ws_private_url(BybitEnvironment::Testnet),
112            "wss://stream-testnet.bybit.com/v5/private"
113        );
114    }
115
116    #[rstest]
117    fn resolves_trade_urls() {
118        assert_eq!(
119            bybit_ws_trade_url(BybitEnvironment::Mainnet),
120            "wss://stream.bybit.com/v5/trade"
121        );
122        assert_eq!(
123            bybit_ws_trade_url(BybitEnvironment::Demo),
124            "wss://stream.bybit.com/v5/trade"
125        );
126        assert_eq!(
127            bybit_ws_trade_url(BybitEnvironment::Testnet),
128            "wss://stream-testnet.bybit.com/v5/trade"
129        );
130    }
131
132    #[rstest]
133    fn resolves_http_urls() {
134        assert_eq!(
135            bybit_http_base_url(BybitEnvironment::Mainnet),
136            "https://api.bybit.com"
137        );
138        assert_eq!(
139            bybit_http_base_url(BybitEnvironment::Demo),
140            "https://api-demo.bybit.com"
141        );
142        assert_eq!(
143            bybit_http_base_url(BybitEnvironment::Testnet),
144            "https://api-testnet.bybit.com"
145        );
146    }
147}