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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
extern crate reqwest;
extern crate serde;
extern crate serde_json;
extern crate chrono;
extern crate hmac;
extern crate sha2;
extern crate base64;

#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate hyper;

pub mod kraken {
    use reqwest;
    use std::io;
    use std::fmt;
    use serde_json;
    use std::error::{self, Error as StdError};
    use std::string::String;
    use std::collections::HashMap;
    use hyper::header::Headers;
    use base64;
    use sha2::Sha256;
    use sha2::Sha512;
    use hmac::{Hmac, Mac};
    use std::time::{SystemTime, UNIX_EPOCH};
    use sha2::Digest;
    use std::str;
    header! { (ApiKey, "API-Key") => [String] }
    header! { (ApiSign, "API-Sign") => [String] }

    pub static API_BASE: &'static str = "https://api.kraken.com";
    pub static API_VERSION: u8 = 0;

    pub enum ApiType {
        Public,
        Private,
    }

    #[derive(Debug)]
    pub enum Error {
        /// The API returned an error.
        Api(u32, String),
        /// An error occured when decoding JSON from the API.
        Json(serde_json::error::Error),
        /// An error occured with the network.
        Io(io::Error),
        /// An HTTP error occured with the webserver.
        Http(reqwest::Error),
    }

    #[derive(Serialize, Deserialize, Debug)]
    pub struct ResultTime {
        pub rfc1123: String,
        pub unixtime: i64,
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultAssets {
        pub aclass: String,
        pub altname: String,
        pub decimals: i64,
        pub display_decimals: i64,
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultAssetPairs {
        pub aclass_base: String,
        pub altname: String,
        pub base: String,
        pub aclass_quote: String,
        pub quote: String,
        pub lot: String,
        pub pair_decimals: i64,
        pub lot_decimals: i64,
        pub lot_multiplier: i64,
        pub leverage_buy: Vec<i64>,
        pub leverage_sell: Vec<i64>,
        pub fees: Vec<Vec<f32>>,
        pub fees_maker: Option<Vec<Vec<f32>>>,
        pub fee_volume_currency: String,
        pub margin_call: i64,
        pub margin_stop: i64,
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultTicker {
        pub a: Vec<String>,
        pub b: Vec<String>,
        pub c: Vec<String>,
        pub v: Vec<String>,
        pub p: Vec<String>,
        pub t: Vec<i64>,
        pub l: Vec<String>,
        pub h: Vec<String>,
        pub o: String,
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultSpreadTimeAskBid(i64, String, String);

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultSpread {
        pub data: Vec<ResultSpreadTimeAskBid>,
        pub last: i64,
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultTradesEntry(String, String, f64, String, String, String);

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultTrades {
        pub data: Vec<ResultTradesEntry>,
        pub last: String,
    }

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultDepthEntry(String, String, i64);

    #[derive(Serialize, Deserialize, Debug, PartialEq)]
    pub struct ResultDepth {
        pub asks: Vec<ResultDepthEntry>,
        pub bids: Vec<ResultDepthEntry>,
    }

    pub struct SignResult {
        pub hash: String,
        pub map: HashMap<String, String>,
    }

    pub type ResultBalance = HashMap<String, String>;

    impl From<serde_json::error::Error> for Error {
        fn from(err: serde_json::error::Error) -> Self {
            Error::Json(err)
        }
    }

    impl From<reqwest::Error> for Error {
        fn from(err: reqwest::Error) -> Self {
            Error::Http(err)
        }
    }

    impl error::Error for Error {
        fn description(&self) -> &str {
            match *self {
                Error::Api(_, ref msg) => msg,
                Error::Json(ref err) => err.description(),
                Error::Io(ref err) => err.description(),
                Error::Http(ref err) => err.description(),
            }
        }

        fn cause(&self) -> Option<&error::Error> {
            match *self {
                Error::Api(_, _) => None,
                Error::Json(ref err) => Some(err),
                Error::Io(ref err) => Some(err),
                Error::Http(ref err) => Some(err),
            }
        }
    }

    impl fmt::Display for Error {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            fmt.write_str(self.description())
        }
    }

    pub struct Kraken {
        secret: String,
        key: String,
        client: reqwest::Client,
    }

    pub type KrakenResult = Result<serde_json::Value, Error>;
    pub type SimpleMap<T> = HashMap<String, T>;

    pub fn new(secret: String, key: String) -> Kraken {
        Kraken {
            secret,
            key,
            client: reqwest::Client::new().unwrap(),
        }
    }

    impl Kraken {
        pub fn get_config(&self) -> String {
            format!("{}, {}", self.secret, self.key)
        }

        pub fn time(&self) -> Result<ResultTime, Error> {
            let url = get_api_url(&String::from("Time"), API_VERSION, ApiType::Public);
            let build = self.client.get(url);
            let res = process_request(build)?;
            let result_data: ResultTime = serde_json::from_value(res)?;
            Ok(result_data)
        }

        pub fn assets(&self) -> Result<SimpleMap<ResultAssets>, Error> {
            let url = get_api_url(&String::from("Assets"), API_VERSION, ApiType::Public);
            let build = self.client.get(url);
            let res = process_request(build);
            if res.is_ok() {
                // remap hashmap
                let result_data: serde_json::Value = serde_json::from_value(res.unwrap())?;
                let obj = result_data.as_object().unwrap();
                let mut obj_new: SimpleMap<ResultAssets> = HashMap::new();
                obj.keys().for_each(|s: &String| {
                    let v = obj.get(s).unwrap().to_owned();
                    let k = s.to_owned();
                    let v_p = serde_json::from_value(v).unwrap();
                    obj_new.insert(k, v_p);
                });
                Ok(obj_new)
            } else {
                Err(res.unwrap_err())
            }
        }

        pub fn asset_pairs(&self) -> Result<SimpleMap<ResultAssetPairs>, Error> {
            let url = get_api_url(&String::from("AssetPairs"), API_VERSION, ApiType::Public);
            let build = self.client.get(url);
            let res = process_request(build)?;
            // remap hashmap
            let result_data: serde_json::Value = serde_json::from_value(res)?;
            let obj = result_data.as_object().unwrap();
            let mut obj_new: SimpleMap<ResultAssetPairs> = HashMap::new();
            obj.keys().for_each(|s: &String| {
                let v = obj.get(s).unwrap().to_owned();
                let k = s.to_owned();
                let v_p = serde_json::from_value(v).unwrap();
                obj_new.insert(k, v_p);
            });
            Ok(obj_new)
        }

        pub fn ticker(&self, pairs: Vec<String>) -> Result<SimpleMap<ResultTicker>, Error> {
            let mut url = get_api_url(&String::from("Ticker"), API_VERSION, ApiType::Public);
            url.query_pairs_mut().append_pair("pair", &pairs.join(","));
            let build = self.client.get(url);
            let res = process_request(build)?;
            // remap hashmap
            let result_data: serde_json::Value = serde_json::from_value(res)?;
            let obj = result_data.as_object().unwrap();
            let obj_new: SimpleMap<ResultTicker> = HashMap::new();
            let export_obj = obj.iter().fold(obj_new, |mut acc, (k, v)| -> SimpleMap<ResultTicker> {
                let v_p: Result<ResultTicker, serde_json::Error> = serde_json::from_value(v.clone());
                acc.insert(k.to_string(), v_p.unwrap()); // TODO: fix unsafe unwrap
                acc
            });
            Ok(export_obj)
        }

        pub fn spread(&self, pair: String) -> Result<ResultSpread, Error> {
            let mut url = get_api_url(&String::from("Spread"), API_VERSION, ApiType::Public);
            url.query_pairs_mut().append_pair("pair", &pair);
            let build = self.client.get(url);
            let res = process_request(build)?;
            let result_data: serde_json::Value = serde_json::from_value(res)?;
            let default = serde_json::Value::from("{}");
            let data: Vec<ResultSpreadTimeAskBid> = serde_json::from_value(result_data.get(pair).unwrap_or(&default).to_owned())?;
            let last: i64 = serde_json::from_value(result_data.get("last").unwrap_or(&default).to_owned())?;
            Ok(ResultSpread { data, last })
        }

        pub fn trades(&self, pair: String, since: Option<String>) -> Result<ResultTrades, Error> {
            let mut url = get_api_url(&String::from("Trades"), API_VERSION, ApiType::Public);
            url.query_pairs_mut().append_pair("pair", &pair);
            if since.is_some() {
                url.query_pairs_mut().append_pair("since", &since.unwrap());
            }
            let build = self.client.get(url);
            let res = process_request(build)?;
            let result_data: serde_json::Value = serde_json::from_value(res)?;
            let default = serde_json::Value::from("{}");
            let field_data = result_data.get(pair).unwrap_or(&default); // safe unwrap
            let data: Vec<ResultTradesEntry> = serde_json::from_value(field_data.to_owned())?;
            let last: String = serde_json::from_value(result_data.get("last").unwrap_or(&default).to_owned())?;
            Ok(ResultTrades { data, last })
        }
        pub fn depth(&self, pair: String, count: Option<String>) -> Result<ResultDepth, Error> {
            let mut url = get_api_url(&String::from("Depth"), API_VERSION, ApiType::Public);
            url.query_pairs_mut().append_pair("pair", &pair);
            if count.is_some() {
                url.query_pairs_mut().append_pair("count", &count.unwrap());
            }
            let build = self.client.get(url);
            let res: serde_json::Value = process_request(build)?;
            let result_data: serde_json::Value = serde_json::from_value(res)?;
            let default = serde_json::Value::from("{}");
            let asks_field = result_data.get(&pair).unwrap_or(&default).get("asks").unwrap_or(&default); // safe unwrap
            let bids_field = result_data.get(&pair).unwrap_or(&default).get("bids").unwrap_or(&default); // safe unwrap
            let asks: Vec<ResultDepthEntry> = serde_json::from_value(asks_field.to_owned())?;
            let bids: Vec<ResultDepthEntry> = serde_json::from_value(bids_field.to_owned())?;
            Ok(ResultDepth { bids, asks })
        }

        pub fn balance(&self) -> Result<ResultBalance, Error> {
            let params: HashMap<String, String> = HashMap::new();
            let res = self.private_request(String::from("Balance"), params)?;
            let output: ResultBalance = serde_json::from_value(res)?;
            Ok(output)
        }

        pub fn private_request(&self, path: String, map: HashMap<String, String>) -> Result<serde_json::Value, Error> {
            let url = get_api_url(&path, API_VERSION, ApiType::Private);
            let mut headers = Headers::new();
            let secret = base_64(self.secret.clone());
            let res = create_sign(url.path().to_string().into_bytes().clone(), secret, &map.clone());
            headers.set(ApiKey(self.key.to_owned()));
            headers.set(ApiSign(res.hash));
            let build = self.client.post(url).headers(headers).form(&res.map);
            let res = process_request(build)?;
            Ok(res)
        }
    }

    pub fn process_request(builder: reqwest::RequestBuilder) -> KrakenResult {
        let mut result = builder.send()?;

        let json_result = match serde_json::from_reader(&mut result)? {
            serde_json::Value::Object(obj) => obj,
            _ => return Err(Error::Api(0, "Invalid response".to_string())),
        };

        if !json_result.contains_key("result") {
            let error = format!("{:#?}", json_result);
            return Err(Error::Api(500, error.to_string()));
        }
        Ok(json_result.get("result").unwrap().to_owned())
    }

    pub fn get_api_url_string(api: &str, version: u8, atype: ApiType) -> String {
        let type_string = match atype {
            ApiType::Private => String::from("private"),
            ApiType::Public => String::from("public"),
        };
        format!("{}/{}/{}/{}", API_BASE, version, type_string, api)
    }

    pub fn get_api_url(api: &str, version: u8, atype: ApiType) -> reqwest::Url {
        reqwest::Url::parse(&get_api_url_string(api, version, atype)).unwrap()
    }

    pub fn base_64(input: String) -> Vec<u8> {
        let bytes = base64::decode(&input).unwrap();
        bytes
    }

    pub fn sha256(input: String) -> Vec<u8> {
        let mut hasher = Sha256::new();
        hasher.input(input.as_bytes());
        hasher.result().to_vec()
    }

    pub fn sha512(input: Vec<u8>, secret: Vec<u8>) -> Vec<u8> {
        let mut mac = Hmac::<Sha512>::new(&secret).unwrap();
        mac.input(&input);
        mac.result().code().to_vec()
    }

    pub fn create_sign(path: Vec<u8>, secret: Vec<u8>, hash_map: &HashMap<String, String>) -> SignResult {
        let timestamp = get_time().to_string();
        let mut should_insert = false;
        let mut hash_map_clone = hash_map.clone();
        let time: String = match hash_map_clone.get("nonce") {
            Some(val) => val.to_string(),
            None => {
                should_insert = true;
                timestamp.clone()
            }
        };
        if should_insert {
            hash_map_clone.insert("nonce".to_string(), timestamp);
        }
        let mut query = String::from("");
        query.push_str(&time.to_string());
        query.push_str(&map_to_formdata(&hash_map_clone));
        let sha256_res = sha256(query);
        let mut to_hash = vec![];
        to_hash.append(&mut path.to_owned());
        to_hash.append(&mut sha256_res.clone());
        let sha512_res = sha512(to_hash, secret);
        SignResult { hash: base64::encode(&sha512_res), map: hash_map_clone.clone() }
    }

    pub fn map_to_formdata(map: &HashMap<String, String>) -> String {
        let mut res: String = map.iter().map(|(k, v)| -> String {
            format!("{}={}", k, v)
        }).fold(String::from(""), |mut acc, val| {
            acc.push_str(&val);
            acc.push_str(&"&".to_string());
            acc
        });
        if res.len() > 0 {
            let index = res.len() - 1;
            res.remove(index);
        }
        res
    }

    pub fn get_time() -> u64 {
        let start = SystemTime::now();
        let since_the_epoch = start.duration_since(UNIX_EPOCH).unwrap();
        let since_the_epoch_ms = since_the_epoch.as_secs() * 1000000000;
        since_the_epoch_ms
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::DateTime;
    use std::collections::HashMap;
    use std::env;

    fn get_test_instance() -> kraken::Kraken {
        kraken::new(
            env::var("KRAKEN_SECRET").unwrap_or("".to_string()),
            env::var("KRAKEN_KEY").unwrap_or("".to_string()),
        )
    }

    #[test]
    fn stores_config() {
        let instance = kraken::new("secret".to_string(), "key".to_string());
        assert_eq!(String::from("secret, key"), instance.get_config());
    }

    #[test]
    fn builds_api_url() {
        let res = kraken::get_api_url_string("Message", 10, kraken::ApiType::Public);
        assert_eq!(String::from("https://api.kraken.com/10/public/Message"), res)
    }

    #[test]
    fn can_request_time() {
        let res = get_test_instance().time().unwrap();
        let target = 1536050055;
        assert_eq!(res.unixtime > target, true);
        assert_eq!(DateTime::parse_from_str(&res.rfc1123, "%a, %d %b %y %H:%M:%S %z").unwrap().timestamp() > target, true);
    }

    #[test]
    fn can_request_assets() {
        let res = get_test_instance().assets().unwrap();
        assert_eq!(res["XDAO"], kraken::ResultAssets {
            aclass: String::from("currency"),
            altname: String::from("DAO"),
            display_decimals: 3,
            decimals: 10,
        });
    }

    #[test]
    fn can_request_asset_pairs() {
        let res = get_test_instance().asset_pairs().unwrap();
        assert_eq!(res["XREPXETH"], kraken::ResultAssetPairs {
            aclass_base: String::from("currency"),
            altname: String::from("REPETH"),
            base: String::from("XREP"),
            aclass_quote: String::from("currency"),
            quote: String::from("XETH"),
            lot: String::from("unit"),
            pair_decimals: 5,
            lot_decimals: 8,
            lot_multiplier: 1,
            leverage_buy: vec![2],
            leverage_sell: vec![2],
            fees: vec![
                vec![
                    0.00,
                    0.26
                ],
                vec![
                    50000.0,
                    0.24
                ],
                vec![
                    100000.0,
                    0.22
                ],
                vec![
                    250000.0,
                    0.2
                ],
                vec![
                    500000.0,
                    0.18
                ],
                vec![
                    1000000.0,
                    0.16
                ],
                vec![
                    2500000.0,
                    0.14
                ],
                vec![
                    5000000.0,
                    0.12
                ],
                vec![
                    10000000.0,
                    0.1
                ]
            ],
            fees_maker: Some(vec![
                vec![
                    0.00,
                    0.16
                ],
                vec![
                    50000.0,
                    0.14
                ],
                vec![
                    100000.0,
                    0.12
                ],
                vec![
                    250000.0,
                    0.1
                ],
                vec![
                    500000.0,
                    0.08
                ],
                vec![
                    1000000.0,
                    0.06
                ],
                vec![
                    2500000.0,
                    0.04
                ],
                vec![
                    5000000.0,
                    0.02
                ],
                vec![
                    10000000.0,
                    0.0
                ]]),
            fee_volume_currency: String::from("ZUSD"),
            margin_call: 80,
            margin_stop: 40,
        })
    }

    #[test]
    fn can_request_ticker() {
        let res = get_test_instance().ticker(vec![String::from("BCHEUR"), String::from("BCHUSD")]).unwrap();
        assert_eq!(res.len(), 2);
        assert_eq!(res["BCHUSD"].h.len(), 2);
        assert_eq!(res["BCHUSD"].l.len(), 2);
        assert_eq!(res["BCHUSD"].t.len(), 2);
        assert_eq!(res["BCHUSD"].p.len(), 2);
        assert_eq!(res["BCHUSD"].c.len(), 2);
        assert_eq!(res["BCHUSD"].b.len(), 3);
        assert_eq!(res["BCHUSD"].a.len(), 3);
    }

    #[test]
    fn can_request_spread() {
        let res = get_test_instance().spread(String::from("BCHEUR")).unwrap();
        assert_eq!(res.last > 1536218176, true);
        assert_eq!(res.data.len(), 200);
    }

    #[test]
    fn can_request_trades() {
        let instance = get_test_instance();
        let res = instance.trades(String::from("BCHEUR"), None).unwrap();
        assert_eq!(res.data.len() > 10, true);
        let res1 = instance.trades(String::from("BCHEUR"), Some(String::from("2501609926936199446"))).unwrap();
        assert_eq!(res1.data.len() == 0, true);
    }

    #[test]
    fn can_request_depth() {
        let instance = get_test_instance();
        let res = instance.depth(String::from("BCHEUR"), None).unwrap();
        assert_eq!(res.asks.len() > 1, true);
        assert_eq!(res.bids.len() > 1, true);
        let res1 = instance.depth(String::from("BCHEUR"), Some(String::from("1"))).unwrap();
        assert_eq!(res1.asks.len() == 1, true);
        assert_eq!(res1.bids.len() == 1, true);
    }

    #[test]
    fn can_request_calc_crypto() {
        let base_64 = kraken::base_64(String::from("aaa"));
        let sha_256 = kraken::sha256(String::from("bbb"));
        let sha_512 = kraken::sha512(String::from("ccc").into_bytes(), base_64.clone());

        assert_eq!(base_64, vec![105, 166]);
        assert_eq!(sha_256, vec![62, 116, 75, 157, 195, 147, 137, 186, 240, 197, 160, 102, 5, 137, 184, 64, 47, 61, 187, 73, 184, 155, 62, 117, 242, 201, 53, 88, 82, 163, 198, 119]);
        assert_eq!(sha_512, vec![128, 176, 49, 74, 23, 137, 186, 64, 12, 84, 246, 44, 29, 157, 230, 76, 110, 195, 235, 85, 179, 93, 13, 71, 51, 110, 214, 77, 13, 121, 248, 168, 227, 214, 91, 92, 207, 226, 140, 0, 185, 33, 145, 193, 128, 84, 86, 184, 41, 234, 245, 3, 85, 240, 179, 196, 38, 43, 51, 62, 240, 34, 235, 28]);

        let path = String::from("/private/Balance").into_bytes();
        let mut map: HashMap<String, String> = HashMap::new();
        map.insert("nonce".to_string(), "1234".to_string());
        let res = kraken::create_sign(path, String::from("secret").into_bytes(), &map);
        assert_eq!(res.hash, "teHrl2ZpQpbA1fA4mLcL1at2cBr/qLwn72bEdNgYLCap4+V38rwhBYyYJlubku4kWrZu5CSgxcghyJuapcnD3w==".to_string());
    }

    #[test]
    fn can_gen_params() {
        let mut map: HashMap<String, String> = HashMap::new();
        map.insert("a".to_string(), "a_res".to_string());
        map.insert("b".to_string(), "b_res".to_string());
        let res = kraken::map_to_formdata(&map);
        assert_eq!((res == "b=b_res&a=a_res".to_string() || res == "a=a_res&b=b_res".to_string()), true);
        map.remove("b");
        let res1 = kraken::map_to_formdata(&map);
        assert_eq!(res1, "a=a_res".to_string());
        map.remove("a");
        let res2 = kraken::map_to_formdata(&map);
        assert_eq!(res2, "".to_string());
    }

    #[test]
    fn can_request_balance() {
        let instance = get_test_instance();
        let res = instance.balance().unwrap();
        let equal: HashMap<String, String> = HashMap::new();
        assert_eq!(res, equal);
    }
}

// todo
// 1. clean up
// 2. remove my keys from tests
// 3. release
// 4. improve errors