binance_api/spot/endpoints.rs
1//! All endpoints for spot-specific HTTP requests.
2//!
3//! <https://binance-docs.github.io/apidocs/spot/en/#general-info>
4use std::fmt;
5
6use crate::endpoints::{Endpoint, HttpVerb, SecurityType};
7
8#[derive(Debug, Copy, Clone)]
9/// Set of possible HTTP methods.
10/// Every variant corresponds to a single API call (combination of path and HTTP verb).
11pub enum Api {
12 // Market Data <https://binance-docs.github.io/apidocs/spot/en/#market-data-endpoints>
13 /// Test connectivity to the Rest API.
14 TestConnectivity,
15 /// Test connectivity to the Rest API and get the current server time.
16 CheckServerTime,
17 /// Current exchange trading rules and symbol information.
18 ExchangeInformation,
19
20 /// Get the order book.
21 OrderBook,
22
23 /// Get recent trades list.
24 RecentTrades,
25 /// Get older market trades.
26 HistoricalTrades,
27 /// Get compressed, aggregate trades. Trades that fill at the time, from the same order,
28 /// with the same price will have the quantity aggregated.
29 AggregatedTrades,
30
31 /// Kline/candlestick bars for a symbol.
32 /// Klines are uniquely identified by their open time.
33 KLines,
34 /// The request is similar to [KLines][Self::KLines] having the same parameters and response.
35 /// Returns modified kline data, optimized for presentation of candlestick charts.
36 UiKLines,
37
38 /// Current average price for a symbol.
39 AveragePrice,
40 /// 24 hour rolling window price change statistics for a **single** symbol.
41 Ticker24hr,
42 /// 24 hour rolling window price change statistics for **multiple** symbols.
43 /// **Careful** when accessing this with no symbol.
44 Ticker24hrMultiple,
45 /// Latest price for a **single** symbol.
46 PriceTicker,
47 /// Latest price for **multiple** symbols.
48 PriceTickerMultiple,
49 /// Best price/qty on the order book for a **single** symbol.
50 OrderBookTicker,
51 /// Best price/qty on the order book for **multiple** symbols.
52 OrderBookTickerMultiple,
53 /// Rolling window price change statistics.
54 ///
55 /// **Note**: This endpoint is different from the [24 hour ticker][Self::Ticker24hr] endpoint.
56 /// The window used to compute statistics will be no more than 59999ms from the requested windowSize.
57 /// openTime always starts on a minute, while the closeTime is the current time of the request.
58 /// As such, the effective window will be up to 59999ms wider than windowSize.
59 ///
60 /// E.g. If the closeTime is 1641287867099 (`January 04, 2022 09:17:47:099 UTC`),
61 /// and the windowSize is `1d` then the openTime will be: 1641201420000 (`January 3, 2022, 09:17:00 UTC`)
62 RollingWindowTicker,
63 /// Rolling window price change statistics for **multiple** symbols.
64 RollingWindowTickerMultiple,
65
66 // Spot Account/Trade <https://binance-docs.github.io/apidocs/spot/en/#spot-account-trade>
67 /// Test new order creation and signature/recvWindow long.
68 /// Creates and validates a new order but does not send it into the matching engine.
69 TestNewOrder,
70 /// Send in a new order.
71 ///
72 /// See detailed documentation at
73 /// <https://binance-docs.github.io/apidocs/spot/en/#new-order-trade>
74 NewOrder,
75 /// Check an order's status.
76 GetOrder,
77 /// Cancel an active order.
78 CancelOrder,
79 /// Cancels an existing order and places a new order on the same symbol.
80 ///
81 /// `Filters` and `Order Count` are evaluated before
82 /// the processing of the cancellation and order placement occurs.
83 ///
84 /// A new order that was not attempted (i.e. when `newOrderResult: NOT_ATTEMPTED`),
85 /// will still increase the order count by 1.
86 ///
87 /// Similar to [NewOrder][Self::NewOrder], additional mandatory parameters are determined by type.
88 /// Response format varies depending on whether the processing of the message
89 /// [succeeded, partially succeeded, or failed](https://binance-docs.github.io/apidocs/spot/en/#2021-order-cancel-replace-partially-failed).
90 CancelReplaceOrder,
91
92 /// Get all open orders on a **single** symbol.
93 GetOpenOrders,
94 /// Get all open orders for **multiple** symbols. **Careful** when accessing this as it has too much weight.
95 GetOpenOrdersAllSymbols,
96 /// Cancels all active orders on a symbol.
97 CancelOpenOrders,
98 /// Get all account orders; active, canceled, or filled.
99 GetAllOrders,
100
101 /// Send in a new OCO (One-Cancels-the-Other) order.
102 NewOco,
103 /// Retrieves a specific OCO based on provided optional parameters.
104 GetOco,
105 /// Cancel an entire Order List.
106 CancelOco,
107 /// Retrieves all OCO based on provided optional parameters.
108 GetAllOco,
109 /// Query Open OCO.
110 GetOpenOco,
111
112 /// Get current account information.
113 AccountInformation,
114 /// Get trades for a specific account and symbol.
115 AccountTrades,
116 /// Displays the user's current order count usage for all intervals.
117 OrderCountUsage,
118
119 // User Data Streams <https://binance-docs.github.io/apidocs/spot/en/#user-data-streams>
120 /// Start a new user data stream. The stream will close after 60 minutes
121 /// unless a keepalive is sent. If the account has an active `listenKey`,
122 /// that `listenKey` will be returned and its validity will be extended for 60 minutes.
123 StartUserDataStream,
124 /// Keepalive a user data stream to prevent a time out.
125 /// User data streams will close after 60 minutes.
126 /// It's recommended to send a ping about every 30 minutes.
127 KeepAliveUserDataStream,
128 /// Close out a user data stream.
129 CloseUserDataStream,
130}
131
132impl fmt::Display for Api {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 let path = match self {
135 Self::TestConnectivity => "ping",
136 Self::CheckServerTime => "time",
137 Self::ExchangeInformation => "exchangeInfo",
138
139 Self::OrderBook => "depth",
140
141 Self::RecentTrades => "trades",
142 Self::HistoricalTrades => "historicalTrades",
143 Self::AggregatedTrades => "aggTrades",
144
145 Self::KLines => "klines",
146 Self::UiKLines => "uiKlines",
147
148 Self::AveragePrice => "avgPrice",
149 Self::Ticker24hr | Self::Ticker24hrMultiple => "ticker/24hr",
150 Self::PriceTicker | Self::PriceTickerMultiple => "ticker/price",
151 Self::OrderBookTicker | Self::OrderBookTickerMultiple => "ticker/bookTicker",
152 Self::RollingWindowTicker | Self::RollingWindowTickerMultiple => "ticker",
153
154 Self::TestNewOrder => "order/test",
155 Self::NewOrder | Self::GetOrder | Self::CancelOrder => "order",
156 Self::CancelReplaceOrder => "order/cancelReplace",
157
158 Self::GetOpenOrders | Self::GetOpenOrdersAllSymbols | Self::CancelOpenOrders => {
159 "openOrders"
160 }
161 Self::GetAllOrders => "allOrders",
162
163 Self::NewOco => "order/oco",
164 Self::GetOco | Self::CancelOco => "orderList",
165 Self::GetAllOco => "allOrderList",
166 Self::GetOpenOco => "openOrderList",
167
168 Self::AccountInformation => "account",
169 Self::AccountTrades => "myTrades",
170 Self::OrderCountUsage => "rateLimit/order",
171
172 Self::StartUserDataStream
173 | Self::KeepAliveUserDataStream
174 | Self::CloseUserDataStream => "userDataStream",
175 };
176
177 f.write_str(path)
178 }
179}
180
181impl Endpoint for Api {
182 fn as_endpoint(&self) -> String {
183 format!("/api/v3/{}", self)
184 }
185
186 fn http_verb(&self) -> HttpVerb {
187 match self {
188 Self::TestConnectivity
189 | Self::CheckServerTime
190 | Self::ExchangeInformation
191 | Self::OrderBook
192 | Self::RecentTrades
193 | Self::HistoricalTrades
194 | Self::AggregatedTrades
195 | Self::KLines
196 | Self::UiKLines
197 | Self::AveragePrice
198 | Self::Ticker24hr
199 | Self::Ticker24hrMultiple
200 | Self::PriceTicker
201 | Self::PriceTickerMultiple
202 | Self::OrderBookTicker
203 | Self::OrderBookTickerMultiple
204 | Self::RollingWindowTicker
205 | Self::RollingWindowTickerMultiple => HttpVerb::GET,
206
207 Self::TestNewOrder | Self::NewOrder => HttpVerb::POST,
208 Self::GetOrder => HttpVerb::GET,
209 Self::CancelOrder => HttpVerb::DELETE,
210 Self::CancelReplaceOrder => HttpVerb::POST,
211
212 Self::GetOpenOrders | Self::GetOpenOrdersAllSymbols => HttpVerb::GET,
213 Self::CancelOpenOrders => HttpVerb::DELETE,
214 Self::GetAllOrders => HttpVerb::GET,
215
216 Self::NewOco => HttpVerb::POST,
217 Self::GetOco => HttpVerb::GET,
218 Self::CancelOco => HttpVerb::DELETE,
219 Self::GetAllOco | Self::GetOpenOco => HttpVerb::GET,
220
221 Self::AccountInformation | Self::AccountTrades => HttpVerb::GET,
222 Self::OrderCountUsage => HttpVerb::GET,
223
224 Self::StartUserDataStream => HttpVerb::POST,
225 Self::KeepAliveUserDataStream => HttpVerb::PUT,
226 Self::CloseUserDataStream => HttpVerb::DELETE,
227 }
228 }
229
230 fn ip_weight(&self) -> Option<u16> {
231 match self {
232 Self::TestConnectivity => Some(1),
233 Self::CheckServerTime => Some(1),
234 Self::ExchangeInformation => Some(10),
235
236 // | Limit | Weight |
237 // |-----------|-------:|
238 // | 1-100 | 1
239 // | 101-500 | 5
240 // | 501-1000 | 10
241 // | 1001-5000 | 50
242 Self::OrderBook => None,
243
244 Self::RecentTrades => Some(1),
245 Self::HistoricalTrades => Some(5),
246 Self::AggregatedTrades => Some(1),
247
248 Self::KLines => Some(1),
249 Self::UiKLines => Some(1),
250
251 Self::AveragePrice => Some(1),
252 Self::Ticker24hr => Some(1),
253 // | # of Symbols Provided | Weight |
254 // |----------------------------|-------:|
255 // | 1-20 | 1
256 // | 21-100 | 20
257 // | >100 or all (no 'symbols') | 40
258 Self::Ticker24hrMultiple => None,
259 Self::PriceTicker => Some(1),
260 Self::PriceTickerMultiple => Some(2),
261 Self::OrderBookTicker => Some(1),
262 Self::OrderBookTickerMultiple => Some(2),
263 Self::RollingWindowTicker => Some(2),
264 // | # of Symbols Provided | Weight |
265 // |----------------------------|-------:|
266 // | 1-50 | 2*N
267 // | 51-100 | 100
268 Self::RollingWindowTickerMultiple => None,
269
270 Self::TestNewOrder => Some(1),
271 Self::NewOrder => Some(1),
272 Self::GetOrder => Some(2),
273 Self::CancelOrder => Some(1),
274 Self::CancelReplaceOrder => Some(1),
275
276 Self::GetOpenOrders => Some(3),
277 Self::GetOpenOrdersAllSymbols => Some(40),
278 Self::CancelOpenOrders => Some(1),
279 Self::GetAllOrders => Some(10),
280
281 Self::NewOco => Some(1),
282 Self::GetOco => Some(2),
283 Self::CancelOco => Some(1),
284 Self::GetAllOco => Some(10),
285 Self::GetOpenOco => Some(3),
286
287 Self::AccountInformation => Some(10),
288 Self::AccountTrades => Some(10),
289 Self::OrderCountUsage => Some(20),
290
291 Self::StartUserDataStream
292 | Self::KeepAliveUserDataStream
293 | Self::CloseUserDataStream => Some(1),
294 }
295 }
296
297 fn order_weight(&self) -> Option<u16> {
298 match self {
299 Self::NewOrder => Some(1),
300 Self::NewOco => Some(2),
301 _ => None,
302 }
303 }
304
305 fn security_type(&self) -> SecurityType {
306 match self {
307 Self::TestConnectivity
308 | Self::CheckServerTime
309 | Self::ExchangeInformation
310 | Self::OrderBook
311 | Self::RecentTrades => SecurityType::None,
312
313 Self::HistoricalTrades => SecurityType::MarketData,
314
315 Self::AggregatedTrades
316 | Self::KLines
317 | Self::UiKLines
318 | Self::AveragePrice
319 | Self::Ticker24hr
320 | Self::Ticker24hrMultiple
321 | Self::PriceTicker
322 | Self::PriceTickerMultiple
323 | Self::OrderBookTicker
324 | Self::OrderBookTickerMultiple
325 | Self::RollingWindowTicker
326 | Self::RollingWindowTickerMultiple => SecurityType::None,
327
328 Self::TestNewOrder | Self::NewOrder => SecurityType::Trade,
329 Self::GetOrder => SecurityType::UserData,
330 Self::CancelOrder => SecurityType::Trade,
331 Self::CancelReplaceOrder => SecurityType::Trade,
332
333 Self::GetOpenOrders | Self::GetOpenOrdersAllSymbols => SecurityType::UserData,
334 Self::CancelOpenOrders => SecurityType::Trade,
335 Self::GetAllOrders => SecurityType::UserData,
336
337 Self::NewOco => SecurityType::Trade,
338 Self::GetOco => SecurityType::UserData,
339 Self::CancelOco => SecurityType::Trade,
340 Self::GetAllOco | Self::GetOpenOco => SecurityType::UserData,
341
342 Self::AccountInformation | Self::AccountTrades => SecurityType::UserData,
343 Self::OrderCountUsage => SecurityType::Trade,
344
345 Self::StartUserDataStream
346 | Self::KeepAliveUserDataStream
347 | Self::CloseUserDataStream => SecurityType::UserStream,
348 }
349 }
350}