2
2
//! See examples for more informations.
3
3
4
4
use hmac:: { Hmac , Mac } ;
5
- use sha2:: { Sha512 } ;
5
+ use sha2:: Sha512 ;
6
6
7
7
use hyper_native_tls:: NativeTlsClient ;
8
8
use hyper:: Client ;
@@ -41,6 +41,36 @@ header! {
41
41
( ContentHeader , "Content-Type" ) => [ String ]
42
42
}
43
43
44
+ #[ derive( Debug , Copy , Clone ) ]
45
+ pub enum PlaceOrderOption {
46
+ FillOrKill ,
47
+ ImmediateOrCancel ,
48
+ PostOnly ,
49
+ }
50
+ impl PlaceOrderOption {
51
+ fn repr ( & self ) -> & ' static str {
52
+ match self {
53
+ & PlaceOrderOption :: FillOrKill => "fillOrKill" ,
54
+ & PlaceOrderOption :: ImmediateOrCancel => "immediateOrCancel" ,
55
+ & PlaceOrderOption :: PostOnly => "postOnly" ,
56
+ }
57
+ }
58
+ }
59
+
60
+ #[ derive( Debug , Copy , Clone ) ]
61
+ pub enum MoveOrderOption {
62
+ ImmediateOrCancel ,
63
+ PostOnly ,
64
+ }
65
+ impl MoveOrderOption {
66
+ fn repr ( & self ) -> & ' static str {
67
+ match self {
68
+ & MoveOrderOption :: ImmediateOrCancel => "immediateOrCancel" ,
69
+ & MoveOrderOption :: PostOnly => "postOnly" ,
70
+ }
71
+ }
72
+ }
73
+
44
74
#[ derive( Debug ) ]
45
75
pub struct PoloniexApi {
46
76
last_request : i64 , // unix timestamp in ms, to avoid ban
@@ -50,7 +80,6 @@ pub struct PoloniexApi {
50
80
burst : bool ,
51
81
}
52
82
53
-
54
83
impl PoloniexApi {
55
84
/// Create a new PoloniexApi by providing an API key & API secret
56
85
pub fn new < C : Credentials > ( creds : C ) -> Result < PoloniexApi > {
@@ -66,12 +95,12 @@ impl PoloniexApi {
66
95
let connector = HttpsConnector :: new ( ssl) ;
67
96
68
97
Ok ( PoloniexApi {
69
- last_request : 0 ,
70
- api_key : creds. get ( "api_key" ) . unwrap_or_default ( ) ,
71
- api_secret : creds. get ( "api_secret" ) . unwrap_or_default ( ) ,
72
- http_client : Client :: with_connector ( connector) ,
73
- burst : false ,
74
- } )
98
+ last_request : 0 ,
99
+ api_key : creds. get ( "api_key" ) . unwrap_or_default ( ) ,
100
+ api_secret : creds. get ( "api_secret" ) . unwrap_or_default ( ) ,
101
+ http_client : Client :: with_connector ( connector) ,
102
+ burst : false ,
103
+ } )
75
104
}
76
105
77
106
/// The number of calls in a given period is limited. In order to avoid a ban we limit
@@ -84,7 +113,7 @@ impl PoloniexApi {
84
113
}
85
114
86
115
fn block_or_continue ( & self ) {
87
- if ! self . burst {
116
+ if !self . burst {
88
117
let threshold: u64 = 167 ; // 6 requests/sec = 1/6*1000
89
118
let offset: u64 = helpers:: get_unix_timestamp_ms ( ) as u64 - self . last_request as u64 ;
90
119
if offset < threshold {
@@ -94,14 +123,10 @@ impl PoloniexApi {
94
123
}
95
124
}
96
125
97
- fn public_query ( & mut self ,
98
- method : & str ,
99
- params : & HashMap < & str , & str > )
100
- -> Result < Map < String , Value > > {
126
+ fn public_query ( & mut self , method : & str , params : & HashMap < & str , & str > ) -> Result < Map < String , Value > > {
101
127
let mut params = params. clone ( ) ;
102
128
helpers:: strip_empties ( & mut params) ;
103
- let url = "https://poloniex.com/public?command=" . to_string ( ) + method + "&" +
104
- & helpers:: url_encode_hashmap ( & params) ;
129
+ let url = "https://poloniex.com/public?command=" . to_string ( ) + method + "&" + & helpers:: url_encode_hashmap ( & params) ;
105
130
106
131
self . block_or_continue ( ) ;
107
132
let mut response = match self . http_client . get ( & url) . send ( ) {
@@ -114,10 +139,7 @@ impl PoloniexApi {
114
139
utils:: deserialize_json ( & buffer)
115
140
}
116
141
117
- fn private_query ( & mut self ,
118
- method : & str ,
119
- params : & HashMap < & str , & str > )
120
- -> Result < Map < String , Value > > {
142
+ fn private_query ( & mut self , method : & str , params : & HashMap < & str , & str > ) -> Result < Map < String , Value > > {
121
143
let unix_timestamp = helpers:: get_unix_timestamp_us ( ) . to_string ( ) ;
122
144
let mut post_params = params. clone ( ) ;
123
145
post_params. insert ( "command" , method) ;
@@ -133,15 +155,18 @@ impl PoloniexApi {
133
155
let mut custom_header = header:: Headers :: new ( ) ;
134
156
custom_header. set ( KeyHeader ( self . api_key . to_owned ( ) ) ) ;
135
157
custom_header. set ( SignHeader ( sign) ) ;
136
- custom_header. set ( ContentHeader ( "application/x-www-form-urlencoded" . to_owned ( ) ) ) ;
158
+ custom_header. set ( ContentHeader (
159
+ "application/x-www-form-urlencoded" . to_owned ( ) ,
160
+ ) ) ;
137
161
138
162
self . block_or_continue ( ) ;
139
163
140
164
let mut response = match self . http_client
141
- . post ( "https://poloniex.com/tradingApi" )
142
- . body ( & post_data)
143
- . headers ( custom_header)
144
- . send ( ) {
165
+ . post ( "https://poloniex.com/tradingApi" )
166
+ . body ( & post_data)
167
+ . headers ( custom_header)
168
+ . send ( )
169
+ {
145
170
Ok ( response) => response,
146
171
Err ( err) => return Err ( ErrorKind :: ServiceUnavailable ( err. to_string ( ) ) . into ( ) ) ,
147
172
} ;
@@ -187,10 +212,7 @@ impl PoloniexApi {
187
212
/// {"asks":[[0.00007600,1164],[0.00007620,1300], ... ], "bids":[[0.00006901,200],
188
213
/// [0.00006900,408], ... ], "isFrozen": 0, "seq": 18849}
189
214
/// ```
190
- pub fn return_order_book ( & mut self ,
191
- currency_pair : & str ,
192
- depth : & str )
193
- -> Result < Map < String , Value > > {
215
+ pub fn return_order_book ( & mut self , currency_pair : & str , depth : & str ) -> Result < Map < String , Value > > {
194
216
let mut params = HashMap :: new ( ) ;
195
217
params. insert ( "currencyPair" , currency_pair) ;
196
218
params. insert ( "depth" , depth) ;
@@ -205,11 +227,7 @@ impl PoloniexApi {
205
227
/// {"date":"2014-02-10 01:19:37","type":"buy","rate":"0.00007600","amount":"655",
206
228
/// "total":"0.04978"}, ... ]
207
229
/// ```
208
- pub fn return_trade_history ( & mut self ,
209
- currency_pair : & str ,
210
- start : & str ,
211
- end : & str )
212
- -> Result < Map < String , Value > > {
230
+ pub fn return_trade_history ( & mut self , currency_pair : & str , start : & str , end : & str ) -> Result < Map < String , Value > > {
213
231
let mut params = HashMap :: new ( ) ;
214
232
params. insert ( "currencyPair" , currency_pair) ;
215
233
params. insert ( "start" , start) ;
@@ -223,12 +241,7 @@ impl PoloniexApi {
223
241
/// [{"date":1405699200,"high":0.0045388,"low":0.00403001,"open":0.00404545,"close":0.00427592,
224
242
/// "volume":44.11655644,"quoteVolume":10259.29079097,"weightedAverage":0.00430015}, ...]
225
243
/// ```
226
- pub fn return_chart_data ( & mut self ,
227
- currency_pair : & str ,
228
- start : & str ,
229
- end : & str ,
230
- period : & str )
231
- -> Result < Map < String , Value > > {
244
+ pub fn return_chart_data ( & mut self , currency_pair : & str , start : & str , end : & str , period : & str ) -> Result < Map < String , Value > > {
232
245
let mut params = HashMap :: new ( ) ;
233
246
params. insert ( "currencyPair" , currency_pair) ;
234
247
params. insert ( "start" , start) ;
@@ -334,10 +347,7 @@ impl PoloniexApi {
334
347
/// "status":"COMPLETE: 36e483efa6aff9fd53a235177579d98451c4eb237c210e66cd2b9a2d4a988f8e",
335
348
/// "ipAddress":"..."}]}
336
349
/// ```
337
- pub fn return_deposits_withdrawals ( & mut self ,
338
- start : & str ,
339
- end : & str )
340
- -> Result < Map < String , Value > > {
350
+ pub fn return_deposits_withdrawals ( & mut self , start : & str , end : & str ) -> Result < Map < String , Value > > {
341
351
let mut params = HashMap :: new ( ) ;
342
352
params. insert ( "start" , start) ;
343
353
params. insert ( "end" , end) ;
@@ -397,11 +407,7 @@ impl PoloniexApi {
397
407
/// "orderNumber": "12603319116", "type": "sell", "category": "marginTrade" }, ... ],
398
408
/// "BTC_LTC":[ ... ] ... }
399
409
/// ```
400
- pub fn return_private_trade_history ( & mut self ,
401
- currency_pair : & str ,
402
- start : & str ,
403
- end : & str )
404
- -> Result < Map < String , Value > > {
410
+ pub fn return_private_trade_history ( & mut self , currency_pair : & str , start : & str , end : & str ) -> Result < Map < String , Value > > {
405
411
let mut params = HashMap :: new ( ) ;
406
412
params. insert ( "currencyPair" , currency_pair) ;
407
413
params. insert ( "start" , start) ;
@@ -437,31 +443,29 @@ impl PoloniexApi {
437
443
/// "date":"2014-10-18 23:03:21", "rate":"0.00000173","total":"0.00058625","tradeID":"16164",
438
444
/// "type":"buy"}]}
439
445
/// ```
440
- pub fn buy ( & mut self ,
441
- currency_pair : & str ,
442
- rate : & str ,
443
- amount : & str )
444
- -> Result < Map < String , Value > > {
445
- // TODO: "fillOrKill", "immediateOrCancel", "postOnly"
446
+ pub fn buy < O > ( & mut self , currency_pair : & str , rate : & str , amount : & str , option : O ) -> Result < Map < String , Value > >
447
+ where
448
+ O : Into < Option < PlaceOrderOption > > ,
449
+ {
446
450
let mut params = HashMap :: new ( ) ;
447
451
params. insert ( "currencyPair" , currency_pair) ;
448
452
params. insert ( "rate" , rate) ;
449
453
params. insert ( "amount" , amount) ;
454
+ option. into ( ) . map ( |o| params. insert ( o. repr ( ) , "1" ) ) ;
450
455
self . private_query ( "buy" , & params)
451
456
}
452
457
453
458
/// Places a sell order in a given market. Parameters and output are the same as for the buy
454
459
/// method.
455
- pub fn sell ( & mut self ,
456
- currency_pair : & str ,
457
- rate : & str ,
458
- amount : & str )
459
- -> Result < Map < String , Value > > {
460
- // TODO: "fillOrKill", "immediateOrCancel", "postOnly"
460
+ pub fn sell < O > ( & mut self , currency_pair : & str , rate : & str , amount : & str , option : O ) -> Result < Map < String , Value > >
461
+ where
462
+ O : Into < Option < PlaceOrderOption > > ,
463
+ {
461
464
let mut params = HashMap :: new ( ) ;
462
465
params. insert ( "currencyPair" , currency_pair) ;
463
466
params. insert ( "rate" , rate) ;
464
467
params. insert ( "amount" , amount) ;
468
+ option. into ( ) . map ( |o| params. insert ( o. repr ( ) , "1" ) ) ;
465
469
self . private_query ( "sell" , & params)
466
470
}
467
471
@@ -486,11 +490,15 @@ impl PoloniexApi {
486
490
/// ```json
487
491
/// {"success":1,"orderNumber":"239574176","resultingTrades":{"BTC_BTS":[]}}
488
492
/// ```
489
- pub fn move_order ( & mut self , order_number : & str , rate : & str ) -> Result < Map < String , Value > > {
493
+ pub fn move_order < O > ( & mut self , order_number : & str , rate : & str , option : O ) -> Result < Map < String , Value > >
494
+ where
495
+ O : Into < Option < MoveOrderOption > > ,
496
+ {
490
497
// TODO: add optional parameters
491
498
let mut params = HashMap :: new ( ) ;
492
499
params. insert ( "orderNumber" , order_number) ;
493
500
params. insert ( "rate" , rate) ;
501
+ option. into ( ) . map ( |o| params. insert ( o. repr ( ) , "1" ) ) ;
494
502
self . private_query ( "moveOrder" , & params)
495
503
}
496
504
@@ -504,11 +512,7 @@ impl PoloniexApi {
504
512
/// ```json
505
513
/// {"response":"Withdrew 2398 NXT."}
506
514
/// ```
507
- pub fn withdraw ( & mut self ,
508
- currency : & str ,
509
- amount : & str ,
510
- address : & str )
511
- -> Result < Map < String , Value > > {
515
+ pub fn withdraw ( & mut self , currency : & str , amount : & str , address : & str ) -> Result < Map < String , Value > > {
512
516
let mut params = HashMap :: new ( ) ;
513
517
params. insert ( "currency" , currency) ;
514
518
params. insert ( "amount" , amount) ;
@@ -544,9 +548,7 @@ impl PoloniexApi {
544
548
/// "margin":{"BTC":"3.90015637", "DASH":"250.00238240","XMR":"497.12028113"},
545
549
/// "lending":{"DASH":"0.01174765","LTC":"11.99936230"}}
546
550
/// ```
547
- pub fn return_available_account_balances ( & mut self ,
548
- account : & str )
549
- -> Result < Map < String , Value > > {
551
+ pub fn return_available_account_balances ( & mut self , account : & str ) -> Result < Map < String , Value > > {
550
552
let mut params = HashMap :: new ( ) ;
551
553
params. insert ( "account" , account) ;
552
554
self . private_query ( "returnAvailableAccountBalances" , & params)
@@ -576,12 +578,7 @@ impl PoloniexApi {
576
578
/// ```json
577
579
/// {"success":1,"message":"Transferred 2 BTC from exchange to margin account."}
578
580
/// ```
579
- pub fn transfer_balance ( & mut self ,
580
- currency : & str ,
581
- amount : & str ,
582
- from_account : & str ,
583
- to_account : & str )
584
- -> Result < Map < String , Value > > {
581
+ pub fn transfer_balance ( & mut self , currency : & str , amount : & str , from_account : & str , to_account : & str ) -> Result < Map < String , Value > > {
585
582
let mut params = HashMap :: new ( ) ;
586
583
params. insert ( "currency" , currency) ;
587
584
params. insert ( "amount" , amount) ;
@@ -590,7 +587,6 @@ impl PoloniexApi {
590
587
self . private_query ( "transferBalance" , & params)
591
588
}
592
589
593
-
594
590
/// Returns a summary of your entire margin account. This is the same information you will
595
591
/// find in the Margin Account section of the Margin Trading page, under the Markets list.
596
592
///
@@ -617,12 +613,7 @@ impl PoloniexApi {
617
613
/// "resultingTrades":{"BTC_DASH":[{"amount":"1.00000000","date":"2015-05-10 22:47:05",
618
614
/// "rate":"0.01383692","total":"0.01383692","tradeID":"1213556","type":"buy"}]}}
619
615
/// ```
620
- pub fn margin_buy ( & mut self ,
621
- currency_pair : & str ,
622
- rate : & str ,
623
- amount : & str ,
624
- lending_rate : & str )
625
- -> Result < Map < String , Value > > {
616
+ pub fn margin_buy ( & mut self , currency_pair : & str , rate : & str , amount : & str , lending_rate : & str ) -> Result < Map < String , Value > > {
626
617
let mut params = HashMap :: new ( ) ;
627
618
params. insert ( "currencyPair" , currency_pair) ;
628
619
params. insert ( "rate" , rate) ;
@@ -643,12 +634,7 @@ impl PoloniexApi {
643
634
/// "resultingTrades":{"BTC_DASH":[{"amount":"1.00000000","date":"2015-05-10 22:47:05",
644
635
/// "rate":"0.01383692","total":"0.01383692","tradeID":"1213556","type":"sell"}]}}
645
636
/// ```
646
- pub fn margin_sell ( & mut self ,
647
- currency_pair : & str ,
648
- rate : & str ,
649
- amount : & str ,
650
- lending_rate : & str )
651
- -> Result < Map < String , Value > > {
637
+ pub fn margin_sell ( & mut self , currency_pair : & str , rate : & str , amount : & str , lending_rate : & str ) -> Result < Map < String , Value > > {
652
638
let mut params = HashMap :: new ( ) ;
653
639
params. insert ( "currencyPair" , currency_pair) ;
654
640
params. insert ( "rate" , rate) ;
@@ -703,13 +689,7 @@ impl PoloniexApi {
703
689
/// ```json
704
690
/// {"success":1,"message":"Loan order placed.","orderID":10590}
705
691
/// ```
706
- pub fn create_loan_offer ( & mut self ,
707
- currency : & str ,
708
- amount : & str ,
709
- duration : & str ,
710
- auto_renew : & str ,
711
- lending_rate : & str )
712
- -> Result < Map < String , Value > > {
692
+ pub fn create_loan_offer ( & mut self , currency : & str , amount : & str , duration : & str , auto_renew : & str , lending_rate : & str ) -> Result < Map < String , Value > > {
713
693
let mut params = HashMap :: new ( ) ;
714
694
params. insert ( "currency" , currency) ;
715
695
params. insert ( "amount" , amount) ;
@@ -774,11 +754,7 @@ impl PoloniexApi {
774
754
/// "duration": "0.47610000", "interest": "0.00001196", "fee": "-0.00000179",
775
755
/// "earned": "0.00001017", "open": "2016-09-28 06:47:26", "close": "2016-09-28 18:13:03" }]
776
756
/// ```
777
- pub fn return_lending_history ( & mut self ,
778
- start : & str ,
779
- end : & str ,
780
- limit : & str )
781
- -> Result < Map < String , Value > > {
757
+ pub fn return_lending_history ( & mut self , start : & str , end : & str , limit : & str ) -> Result < Map < String , Value > > {
782
758
let mut params = HashMap :: new ( ) ;
783
759
params. insert ( "start" , start) ;
784
760
params. insert ( "end" , end) ;
@@ -801,7 +777,6 @@ impl PoloniexApi {
801
777
}
802
778
}
803
779
804
-
805
780
#[ cfg( test) ]
806
781
mod poloniex_api_tests {
807
782
use super :: * ;
@@ -827,7 +802,6 @@ mod poloniex_api_tests {
827
802
assert ! ( difference >= 166 ) ;
828
803
assert ! ( difference < 1000 ) ;
829
804
830
-
831
805
api. set_burst ( true ) ;
832
806
let start = helpers:: get_unix_timestamp_ms ( ) ;
833
807
api. block_or_continue ( ) ;
@@ -837,7 +811,9 @@ mod poloniex_api_tests {
837
811
assert ! ( difference < 10 ) ;
838
812
839
813
counter = counter + 1 ;
840
- if counter >= 3 { break ; }
814
+ if counter >= 3 {
815
+ break ;
816
+ }
841
817
}
842
818
}
843
819
}
0 commit comments