Skip to main content

nautilus_binance/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 resolution helpers for Binance API endpoints.
17
18use super::{
19    consts::{
20        BINANCE_FUTURES_COIN_HTTP_URL, BINANCE_FUTURES_COIN_TESTNET_HTTP_URL,
21        BINANCE_FUTURES_COIN_TESTNET_WS_URL, BINANCE_FUTURES_COIN_WS_URL,
22        BINANCE_FUTURES_DEMO_HTTP_URL, BINANCE_FUTURES_USD_HTTP_URL,
23        BINANCE_FUTURES_USD_TESTNET_HTTP_URL, BINANCE_FUTURES_USD_TESTNET_WS_URL,
24        BINANCE_FUTURES_USD_WS_URL, BINANCE_OPTIONS_HTTP_URL, BINANCE_OPTIONS_WS_URL,
25        BINANCE_SPOT_DEMO_HTTP_URL, BINANCE_SPOT_DEMO_WS_URL, BINANCE_SPOT_HTTP_URL,
26        BINANCE_SPOT_TESTNET_HTTP_URL, BINANCE_SPOT_TESTNET_WS_URL, BINANCE_SPOT_WS_URL,
27    },
28    enums::{BinanceEnvironment, BinanceProductType},
29};
30
31/// Returns the HTTP base URL for the given product type and environment.
32#[must_use]
33pub fn get_http_base_url(
34    product_type: BinanceProductType,
35    environment: BinanceEnvironment,
36) -> &'static str {
37    match (product_type, environment) {
38        // Mainnet
39        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Mainnet) => {
40            BINANCE_SPOT_HTTP_URL
41        }
42        (BinanceProductType::UsdM, BinanceEnvironment::Mainnet) => BINANCE_FUTURES_USD_HTTP_URL,
43        (BinanceProductType::CoinM, BinanceEnvironment::Mainnet) => BINANCE_FUTURES_COIN_HTTP_URL,
44        (BinanceProductType::Options, BinanceEnvironment::Mainnet) => BINANCE_OPTIONS_HTTP_URL,
45
46        // Testnet
47        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Testnet) => {
48            BINANCE_SPOT_TESTNET_HTTP_URL
49        }
50        (BinanceProductType::UsdM, BinanceEnvironment::Testnet) => {
51            BINANCE_FUTURES_USD_TESTNET_HTTP_URL
52        }
53        (BinanceProductType::CoinM, BinanceEnvironment::Testnet) => {
54            BINANCE_FUTURES_COIN_TESTNET_HTTP_URL
55        }
56        (BinanceProductType::Options, BinanceEnvironment::Testnet) => BINANCE_OPTIONS_HTTP_URL,
57
58        // Demo (futures demo uses same URLs as futures testnet)
59        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Demo) => {
60            BINANCE_SPOT_DEMO_HTTP_URL
61        }
62        (BinanceProductType::UsdM | BinanceProductType::CoinM, BinanceEnvironment::Demo) => {
63            BINANCE_FUTURES_DEMO_HTTP_URL
64        }
65        (BinanceProductType::Options, BinanceEnvironment::Demo) => BINANCE_OPTIONS_HTTP_URL,
66    }
67}
68
69/// Returns the WebSocket base URL for the given product type and environment.
70#[must_use]
71pub fn get_ws_base_url(
72    product_type: BinanceProductType,
73    environment: BinanceEnvironment,
74) -> &'static str {
75    match (product_type, environment) {
76        // Mainnet
77        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Mainnet) => {
78            BINANCE_SPOT_WS_URL
79        }
80        (BinanceProductType::UsdM, BinanceEnvironment::Mainnet) => BINANCE_FUTURES_USD_WS_URL,
81        (BinanceProductType::CoinM, BinanceEnvironment::Mainnet) => BINANCE_FUTURES_COIN_WS_URL,
82        (BinanceProductType::Options, BinanceEnvironment::Mainnet) => BINANCE_OPTIONS_WS_URL,
83
84        // Testnet
85        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Testnet) => {
86            BINANCE_SPOT_TESTNET_WS_URL
87        }
88        (BinanceProductType::UsdM, BinanceEnvironment::Testnet) => {
89            BINANCE_FUTURES_USD_TESTNET_WS_URL
90        }
91        (BinanceProductType::CoinM, BinanceEnvironment::Testnet) => {
92            BINANCE_FUTURES_COIN_TESTNET_WS_URL
93        }
94        (BinanceProductType::Options, BinanceEnvironment::Testnet) => BINANCE_OPTIONS_WS_URL,
95
96        // Demo (futures demo uses same WS URLs as futures testnet)
97        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Demo) => {
98            BINANCE_SPOT_DEMO_WS_URL
99        }
100        (BinanceProductType::UsdM, BinanceEnvironment::Demo) => BINANCE_FUTURES_USD_TESTNET_WS_URL,
101        (BinanceProductType::CoinM, BinanceEnvironment::Demo) => {
102            BINANCE_FUTURES_COIN_TESTNET_WS_URL
103        }
104        (BinanceProductType::Options, BinanceEnvironment::Demo) => BINANCE_OPTIONS_WS_URL,
105    }
106}
107
108#[cfg(test)]
109mod tests {
110    use rstest::rstest;
111
112    use super::*;
113
114    #[rstest]
115    fn test_http_url_spot_mainnet() {
116        let url = get_http_base_url(BinanceProductType::Spot, BinanceEnvironment::Mainnet);
117        assert_eq!(url, "https://api.binance.com");
118    }
119
120    #[rstest]
121    fn test_http_url_spot_testnet() {
122        let url = get_http_base_url(BinanceProductType::Spot, BinanceEnvironment::Testnet);
123        assert_eq!(url, "https://testnet.binance.vision");
124    }
125
126    #[rstest]
127    fn test_http_url_spot_demo() {
128        let url = get_http_base_url(BinanceProductType::Spot, BinanceEnvironment::Demo);
129        assert_eq!(url, "https://demo-api.binance.com");
130    }
131
132    #[rstest]
133    fn test_http_url_usdm_mainnet() {
134        let url = get_http_base_url(BinanceProductType::UsdM, BinanceEnvironment::Mainnet);
135        assert_eq!(url, "https://fapi.binance.com");
136    }
137
138    #[rstest]
139    fn test_http_url_coinm_mainnet() {
140        let url = get_http_base_url(BinanceProductType::CoinM, BinanceEnvironment::Mainnet);
141        assert_eq!(url, "https://dapi.binance.com");
142    }
143
144    #[rstest]
145    fn test_ws_url_spot_mainnet() {
146        let url = get_ws_base_url(BinanceProductType::Spot, BinanceEnvironment::Mainnet);
147        assert_eq!(url, "wss://stream.binance.com:9443/ws");
148    }
149
150    #[rstest]
151    fn test_ws_url_spot_demo() {
152        let url = get_ws_base_url(BinanceProductType::Spot, BinanceEnvironment::Demo);
153        assert_eq!(url, "wss://demo-stream.binance.com/ws");
154    }
155
156    #[rstest]
157    fn test_ws_url_usdm_mainnet() {
158        let url = get_ws_base_url(BinanceProductType::UsdM, BinanceEnvironment::Mainnet);
159        assert_eq!(url, "wss://fstream.binance.com/ws");
160    }
161}