nautilus_binance/common/
consts.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//! Binance venue constants and API endpoints.
17
18use std::sync::LazyLock;
19
20use nautilus_model::identifiers::Venue;
21
22/// The Binance venue identifier string.
23pub const BINANCE: &str = "BINANCE";
24
25/// Static venue instance for Binance.
26pub static BINANCE_VENUE: LazyLock<Venue> = LazyLock::new(|| Venue::new(BINANCE));
27
28// ------------------------------------------------------------------------------------------------
29// HTTP Base URLs - Mainnet
30// ------------------------------------------------------------------------------------------------
31
32/// Binance Spot API base URL (mainnet).
33pub const BINANCE_SPOT_HTTP_URL: &str = "https://api.binance.com";
34
35/// Binance USD-M Futures API base URL (mainnet).
36pub const BINANCE_FUTURES_USD_HTTP_URL: &str = "https://fapi.binance.com";
37
38/// Binance COIN-M Futures API base URL (mainnet).
39pub const BINANCE_FUTURES_COIN_HTTP_URL: &str = "https://dapi.binance.com";
40
41/// Binance European Options API base URL (mainnet).
42pub const BINANCE_OPTIONS_HTTP_URL: &str = "https://eapi.binance.com";
43
44// ------------------------------------------------------------------------------------------------
45// HTTP Base URLs - Testnet
46// ------------------------------------------------------------------------------------------------
47
48/// Binance Spot API base URL (testnet).
49pub const BINANCE_SPOT_TESTNET_HTTP_URL: &str = "https://testnet.binance.vision";
50
51/// Binance USD-M Futures API base URL (testnet).
52pub const BINANCE_FUTURES_USD_TESTNET_HTTP_URL: &str = "https://testnet.binancefuture.com";
53
54/// Binance COIN-M Futures API base URL (testnet).
55pub const BINANCE_FUTURES_COIN_TESTNET_HTTP_URL: &str = "https://testnet.binancefuture.com";
56
57// Note: Binance Options testnet is not publicly available
58
59// ------------------------------------------------------------------------------------------------
60// WebSocket URLs - Mainnet
61// ------------------------------------------------------------------------------------------------
62
63/// Binance Spot WebSocket base URL (mainnet).
64pub const BINANCE_SPOT_WS_URL: &str = "wss://stream.binance.com:9443/ws";
65
66/// Binance USD-M Futures WebSocket base URL (mainnet).
67pub const BINANCE_FUTURES_USD_WS_URL: &str = "wss://fstream.binance.com/ws";
68
69/// Binance COIN-M Futures WebSocket base URL (mainnet).
70pub const BINANCE_FUTURES_COIN_WS_URL: &str = "wss://dstream.binance.com/ws";
71
72/// Binance European Options WebSocket base URL (mainnet).
73pub const BINANCE_OPTIONS_WS_URL: &str = "wss://nbstream.binance.com/eoptions";
74
75// ------------------------------------------------------------------------------------------------
76// WebSocket URLs - Testnet
77// ------------------------------------------------------------------------------------------------
78
79/// Binance Spot WebSocket base URL (testnet).
80pub const BINANCE_SPOT_TESTNET_WS_URL: &str = "wss://testnet.binance.vision/ws";
81
82/// Binance USD-M Futures WebSocket base URL (testnet).
83pub const BINANCE_FUTURES_USD_TESTNET_WS_URL: &str = "wss://stream.binancefuture.com/ws";
84
85/// Binance COIN-M Futures WebSocket base URL (testnet).
86pub const BINANCE_FUTURES_COIN_TESTNET_WS_URL: &str = "wss://dstream.binancefuture.com/ws";
87
88// ------------------------------------------------------------------------------------------------
89// API Paths
90// ------------------------------------------------------------------------------------------------
91
92/// Binance Spot API version path.
93pub const BINANCE_SPOT_API_PATH: &str = "/api/v3";
94
95/// Binance USD-M Futures API version path.
96pub const BINANCE_FAPI_PATH: &str = "/fapi/v1";
97
98/// Binance COIN-M Futures API version path.
99pub const BINANCE_DAPI_PATH: &str = "/dapi/v1";
100
101/// Binance European Options API version path.
102pub const BINANCE_EAPI_PATH: &str = "/eapi/v1";
103
104// ------------------------------------------------------------------------------------------------
105// Rate Limiting
106// ------------------------------------------------------------------------------------------------
107
108/// Describes a static rate limit quota for a product type.
109#[derive(Clone, Copy, Debug)]
110pub struct BinanceRateLimitQuota {
111    /// Rate limit type identifier (REQUEST_WEIGHT or ORDERS).
112    pub rate_limit_type: &'static str,
113    /// Time interval unit (SECOND, MINUTE, DAY).
114    pub interval: &'static str,
115    /// Number of intervals.
116    pub interval_num: u32,
117    /// Maximum allowed requests for the interval.
118    pub limit: u32,
119}
120
121/// Spot & margin REST limits (default IP weights).
122///
123/// References:
124/// - <https://developers.binance.com/docs/binance-spot-api-docs/limits>
125pub const BINANCE_SPOT_RATE_LIMITS: &[BinanceRateLimitQuota] = &[
126    BinanceRateLimitQuota {
127        rate_limit_type: "REQUEST_WEIGHT",
128        interval: "MINUTE",
129        interval_num: 1,
130        limit: 1_200,
131    },
132    BinanceRateLimitQuota {
133        rate_limit_type: "ORDERS",
134        interval: "SECOND",
135        interval_num: 1,
136        limit: 10,
137    },
138    BinanceRateLimitQuota {
139        rate_limit_type: "ORDERS",
140        interval: "DAY",
141        interval_num: 1,
142        limit: 100_000,
143    },
144];
145
146/// USD-M Futures REST limits (default IP weights).
147///
148/// References:
149/// - <https://developers.binance.com/docs/derivatives/usds-margined-futures/general-info#limits>
150pub const BINANCE_FAPI_RATE_LIMITS: &[BinanceRateLimitQuota] = &[
151    BinanceRateLimitQuota {
152        rate_limit_type: "REQUEST_WEIGHT",
153        interval: "MINUTE",
154        interval_num: 1,
155        limit: 2_400,
156    },
157    BinanceRateLimitQuota {
158        rate_limit_type: "ORDERS",
159        interval: "SECOND",
160        interval_num: 1,
161        limit: 50,
162    },
163    BinanceRateLimitQuota {
164        rate_limit_type: "ORDERS",
165        interval: "MINUTE",
166        interval_num: 1,
167        limit: 1_200,
168    },
169];
170
171/// COIN-M Futures REST limits (default IP weights).
172///
173/// References:
174/// - <https://developers.binance.com/docs/derivatives/coin-margined-futures/general-info#limits>
175pub const BINANCE_DAPI_RATE_LIMITS: &[BinanceRateLimitQuota] = &[
176    BinanceRateLimitQuota {
177        rate_limit_type: "REQUEST_WEIGHT",
178        interval: "MINUTE",
179        interval_num: 1,
180        limit: 1_200,
181    },
182    BinanceRateLimitQuota {
183        rate_limit_type: "ORDERS",
184        interval: "SECOND",
185        interval_num: 1,
186        limit: 20,
187    },
188    BinanceRateLimitQuota {
189        rate_limit_type: "ORDERS",
190        interval: "MINUTE",
191        interval_num: 1,
192        limit: 1_200,
193    },
194];
195
196/// Options REST limits (default IP weights).
197///
198/// References:
199/// - <https://developers.binance.com/docs/derivatives/european-options/general-info#limits>
200pub const BINANCE_EAPI_RATE_LIMITS: &[BinanceRateLimitQuota] = &[
201    BinanceRateLimitQuota {
202        rate_limit_type: "REQUEST_WEIGHT",
203        interval: "MINUTE",
204        interval_num: 1,
205        limit: 3_000,
206    },
207    BinanceRateLimitQuota {
208        rate_limit_type: "ORDERS",
209        interval: "SECOND",
210        interval_num: 1,
211        limit: 5,
212    },
213    BinanceRateLimitQuota {
214        rate_limit_type: "ORDERS",
215        interval: "MINUTE",
216        interval_num: 1,
217        limit: 200,
218    },
219];