binance-common 0.2.3

Common data structures, error types, and utilities shared across Binance API crates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use serde::Serialize;

use crate::enums::futures::{
    OrderResponseType, OrderSide, OrderType, PositionSide, PriceMatch, StpModes, TimeInForce,
    WorkingType,
};

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NewOrderParams<'a> {
    pub symbol: &'a str,
    pub side: OrderSide,
    pub position_side: Option<PositionSide>,
    pub r#type: OrderType,
    pub time_in_force: Option<TimeInForce>,
    pub quantity: Option<f64>,
    pub reduce_only: Option<bool>,
    pub price: Option<f64>,
    pub new_client_order_id: Option<&'a str>,
    pub stop_price: Option<f64>,
    pub close_position: Option<&'a str>,
    pub activation_price: Option<f64>,
    pub callback_rate: Option<f64>,
    pub working_type: Option<WorkingType>,
    pub price_protect: Option<&'a str>,
    pub new_order_resp_type: Option<OrderResponseType>,
    pub price_match: Option<PriceMatch>,
    pub self_trade_prevention_mode: Option<StpModes>,
    pub good_till_date: Option<u64>,
    pub recv_window: Option<u16>,
}

impl<'a> NewOrderParams<'a> {
    pub fn new(symbol: &'a str, side: OrderSide, r#type: OrderType) -> Self {
        NewOrderParams {
            symbol,
            side,
            r#type,
            ..Default::default()
        }
    }

    pub fn symbol(mut self, symbol: &'a str) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn side(mut self, side: OrderSide) -> Self {
        self.side = side;
        self
    }

    pub fn position_side(mut self, position_side: PositionSide) -> Self {
        self.position_side = Some(position_side);
        self
    }

    pub fn r#type(mut self, r#type: OrderType) -> Self {
        self.r#type = r#type;
        self
    }

    pub fn time_in_force(mut self, time_in_force: TimeInForce) -> Self {
        self.time_in_force = Some(time_in_force);
        self
    }

    pub fn quantity(mut self, quantity: f64) -> Self {
        self.quantity = Some(quantity);
        self
    }

    pub fn reduce_only(mut self, reduce_only: bool) -> Self {
        self.reduce_only = Some(reduce_only);
        self
    }

    pub fn price(mut self, price: f64) -> Self {
        self.price = Some(price);
        self
    }

    pub fn new_client_order_id(mut self, new_client_order_id: &'a str) -> Self {
        self.new_client_order_id = Some(new_client_order_id);
        self
    }

    pub fn stop_price(mut self, stop_price: f64) -> Self {
        self.stop_price = Some(stop_price);
        self
    }

    pub fn close_position(mut self, close_position: &'a str) -> Self {
        self.close_position = Some(close_position);
        self
    }

    pub fn activation_price(mut self, activation_price: f64) -> Self {
        self.activation_price = Some(activation_price);
        self
    }

    pub fn callback_rate(mut self, callback_rate: f64) -> Self {
        self.callback_rate = Some(callback_rate);
        self
    }

    pub fn working_type(mut self, working_type: WorkingType) -> Self {
        self.working_type = Some(working_type);
        self
    }

    pub fn price_protect(mut self, price_protect: &'a str) -> Self {
        self.price_protect = Some(price_protect);
        self
    }

    pub fn new_order_resp_type(mut self, new_order_resp_type: OrderResponseType) -> Self {
        self.new_order_resp_type = Some(new_order_resp_type);
        self
    }

    pub fn price_match(mut self, price_match: PriceMatch) -> Self {
        self.price_match = Some(price_match);
        self
    }

    pub fn self_trade_prevention_mode(mut self, self_trade_prevention_mode: StpModes) -> Self {
        self.self_trade_prevention_mode = Some(self_trade_prevention_mode);
        self
    }

    pub fn good_till_date(mut self, good_till_date: u64) -> Self {
        self.good_till_date = Some(good_till_date);
        self
    }

    pub fn recv_window(mut self, recv_window: u16) -> Self {
        self.recv_window = Some(recv_window);
        self
    }

    pub fn limit(symbol: &'a str, side: OrderSide, price: f64, quantity: f64) -> Self {
        NewOrderParams {
            symbol,
            side,
            r#type: OrderType::Limit,
            price: Some(price),
            quantity: Some(quantity),
            time_in_force: Some(TimeInForce::Gtc),
            ..Default::default()
        }
    }

    pub fn market(symbol: &'a str, side: OrderSide, quantity: f64) -> Self {
        NewOrderParams {
            symbol,
            side,
            r#type: OrderType::Market,
            quantity: Some(quantity),
            ..Default::default()
        }
    }

    pub fn stop(
        symbol: &'a str,
        side: OrderSide,
        stop_price: f64,
        price: f64,
        quantity: f64,
    ) -> Self {
        Self {
            symbol,
            side,
            r#type: OrderType::Stop,
            price: Some(price),
            stop_price: Some(stop_price),
            quantity: Some(quantity),
            time_in_force: Some(TimeInForce::Gtc),
            ..Default::default()
        }
    }

    pub fn take_profit(
        symbol: &'a str,
        side: OrderSide,
        stop_price: f64,
        price: f64,
        quantity: f64,
    ) -> Self {
        Self {
            symbol,
            side,
            r#type: OrderType::TakeProfit,
            price: Some(price),
            stop_price: Some(stop_price),
            quantity: Some(quantity),
            time_in_force: Some(TimeInForce::Gtc),
            ..Default::default()
        }
    }

    pub fn stop_market(symbol: &'a str, side: OrderSide, stop_price: f64, quantity: f64) -> Self {
        Self {
            symbol,
            side,
            r#type: OrderType::StopMarket,
            stop_price: Some(stop_price),
            quantity: Some(quantity),
            ..Default::default()
        }
    }

    pub fn take_profit_market(
        symbol: &'a str,
        side: OrderSide,
        stop_price: f64,
        quantity: f64,
    ) -> Self {
        Self {
            symbol,
            side,
            r#type: OrderType::TakeProfitMarket,
            stop_price: Some(stop_price),
            quantity: Some(quantity),
            ..Default::default()
        }
    }

    pub fn trailing_stop_market(
        symbol: &'a str,
        side: OrderSide,
        callback_rate: f64,
        activation_price: Option<f64>,
    ) -> Self {
        Self {
            symbol,
            side,
            r#type: OrderType::TrailingStopMarket,
            callback_rate: Some(callback_rate),
            activation_price,
            ..Default::default()
        }
    }
}

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetLeverageParams<'a> {
    pub symbol: &'a str,
    pub leverage: u8,
    pub recv_window: Option<u16>,
}

impl<'a> SetLeverageParams<'a> {
    pub fn new(symbol: &'a str, leverage: u8) -> Self {
        SetLeverageParams {
            symbol,
            leverage,
            recv_window: None,
        }
    }

    pub fn symbol(mut self, symbol: &'a str) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn leverage(mut self, leverage: u8) -> Self {
        self.leverage = leverage;
        self
    }

    pub fn recv_window(mut self, recv_window: u16) -> Self {
        self.recv_window = Some(recv_window);
        self
    }
}

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CancelAllOrdersParams<'a> {
    pub symbol: &'a str,
    pub recv_window: Option<u16>,
}

impl<'a> CancelAllOrdersParams<'a> {
    pub fn new(symbol: &'a str) -> Self {
        CancelAllOrdersParams {
            symbol,
            ..Default::default()
        }
    }

    pub fn symbol(mut self, symbol: &'a str) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn recv_window(mut self, recv_window: u16) -> Self {
        self.recv_window = Some(recv_window);
        self
    }
}

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PositionRiskV3Params<'a> {
    pub symbol: &'a str,
    pub recv_window: Option<u16>,
}

impl<'a> PositionRiskV3Params<'a> {
    pub fn new(symbol: &'a str) -> Self {
        PositionRiskV3Params {
            symbol,
            ..Default::default()
        }
    }

    pub fn symbol(mut self, symbol: &'a str) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn recv_window(mut self, recv_window: u16) -> Self {
        self.recv_window = Some(recv_window);
        self
    }
}

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CancelOrderParams<'a> {
    pub symbol: &'a str,
    pub order_id: Option<u64>,
    pub orig_client_order_id: Option<&'a str>,
    pub recv_window: Option<u16>,
}

impl<'a> CancelOrderParams<'a> {
    pub fn new(symbol: &'a str) -> Self {
        CancelOrderParams {
            symbol,
            ..Default::default()
        }
    }

    pub fn symbol(mut self, symbol: &'a str) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn order_id(mut self, order_id: u64) -> Self {
        self.order_id = Some(order_id);
        self
    }

    pub fn orig_client_order_id(mut self, orig_client_order_id: &'a str) -> Self {
        self.orig_client_order_id = Some(orig_client_order_id);
        self
    }

    pub fn recv_window(mut self, recv_window: u16) -> Self {
        self.recv_window = Some(recv_window);
        self
    }
}

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetOrderParams<'a> {
    pub symbol: &'a str,
    pub order_id: Option<u64>,
    pub orig_client_order_id: Option<&'a str>,
    pub recv_window: Option<u16>,
}

impl<'a> GetOrderParams<'a> {
    pub fn new(symbol: &'a str) -> Self {
        GetOrderParams {
            symbol,
            ..Default::default()
        }
    }

    pub fn symbol(mut self, symbol: &'a str) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn order_id(mut self, order_id: u64) -> Self {
        self.order_id = Some(order_id);
        self
    }

    pub fn orig_client_order_id(mut self, orig_client_order_id: &'a str) -> Self {
        self.orig_client_order_id = Some(orig_client_order_id);
        self
    }

    pub fn recv_window(mut self, recv_window: u16) -> Self {
        self.recv_window = Some(recv_window);
        self
    }
}

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetOpenOrderParams<'a> {
    pub symbol: &'a str,
    pub order_id: Option<u64>,
    pub orig_client_order_id: Option<&'a str>,
    pub recv_window: Option<u16>,
}

impl<'a> GetOpenOrderParams<'a> {
    pub fn new(symbol: &'a str) -> Self {
        GetOpenOrderParams {
            symbol,
            ..Default::default()
        }
    }

    pub fn symbol(mut self, symbol: &'a str) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn order_id(mut self, order_id: u64) -> Self {
        self.order_id = Some(order_id);
        self
    }

    pub fn orig_client_order_id(mut self, orig_client_order_id: &'a str) -> Self {
        self.orig_client_order_id = Some(orig_client_order_id);
        self
    }

    pub fn recv_window(mut self, recv_window: u16) -> Self {
        self.recv_window = Some(recv_window);
        self
    }
}