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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
#![allow (non_snake_case,
          non_camel_case_types,
          non_upper_case_globals)]
#![warn (missing_docs)]


/*! An interface to the Kraken cryptocurrency exchange via its RESTful
    JSON-based API.

    It allows general exchange-wide data requests, and fully authenticated
    trading account-based requests.  The Kraken API is fully documented at
    <https://docs.kraken.com/rest>.

    Use this crate if you want to build custom automatons for algorithmic
    trading through this exchange, or if you want to provide an alternative
    interface to the one which the Kraken web site gives you.

    To use, call the [connect] method to obtain a handle object, maybe call
    [Kraken_API::set_opt] on the handle if you want to make a call to a Kraken
    end-point which accepts optional filter arguments, and then call the method
    on the handle which corresponds to the end-point you wish to invoke.  As
    this is a low-level library, the return will be a JSON string as sent by the
    exchange itself; you will need to be familiar with the Kraken API itself to
    interpret the returned data (and the intended use of the various
    end-points).

    Thus, an example program might appear as

    ```ignore
    use  DMBCS_KRAKEN_API  as  KKN;
    use  serde_json  as  JSN;

    fn  main  ()  ->  Result<(), String>   {

           let  mut  K  =  KKN::connect ("account key".to_string (),
                                         "secret".to_string ());

           K.set_opt (KKN::API_Option::START_TIME, "2022-01-01");

           let  json_result  =  K.account_balance () ?;

           let  json_result  =  JSN::from_str::<JSN::Value> (&json_result)
                                   .map_err (|E| E.to_string ()) ?;

           //  There should be code here to deal with the possibility that
           //  the exchange returned an error message to us.

           println! ("We currently have ${} in our account.",
                     json_result ["result"] ["ZUSD"].as_str ().ok_or ("0"));

           Ok (())
      }
    ```

    The 'account key' and the 'secret' should be obtained through the Kraken web
    service, and care must be taken to keep the secret secret (don't let it find
    its way into a public code repository!)

    Note that we made use of the `serde_json` crate to parse the response from
    the Kraken exchange, but this is absolutely not mandated by this library.

    ##  Limitations / To do

    * The user needs to be familiar with the Kraken documentation to be able to
      use this crate effectively.  While the crate does help with the formatting
      of calls and authentication and authorization information, it gives little
      help with interpreting the responses received from the exchange; as well
      as working knowledge of the Kraken protocols, an external JSON crate is
      almost certainly required to handle this.

    * We have currently implemented all of the *Market Data*, *User Data* and
      *User Trading* end-points.  The *User Funding* and *User Staking*
      end-points are not yet implemented, nor is the *Websockets
      Authentication* end-point.

    * Some specific strings which the exchange needs to see are not provided by
      the crate, and in particular the peculiarities of trading pairs like
      "ZUSDXBTC" have to be dealt with entirely by the user.  The exchange
      provides little consistency among these and coding for them is difficult
      and use-case specific.
*/



use  openssl  as  SSL;
use  std::collections::HashMap  as  Map;
use  std::sync::{Arc, Mutex};



/** Enumeration of available optional arguments which may be given to some of
    Kraken's API's end-points.  Note that the value given to the arguments will
    always be strings; the comments below indicate how the strings will be
    interpreted by the exchange.  */

#[derive(PartialEq,Eq,Hash)]
pub  enum  API_Option
{
    /** Information to be retrieved, one of "info", "leverage", "fees", or
        "margin".  */
    INFO = 0,

    /** Asset class; the only known valid value seems to be "currency". */
    ACLASS,

    /** An asset (e.g. "usd") about which to get information.  Can also be a
        comma-delimited list for some functions.  Can also be "all", which is
        the default when this optional argument is missing. */
    ASSET,

    /** Request to see trades (boolean as str); means slightly different things
        to different exchange end-points.  */
    TRADES,

    /** Restrict results to given user reference ID (i32 as str). */
    USERREF,

    /** Either a UNIX timestamp or transaction ID demarking the start of
        returned results. */
    START,

    /** Either a UNIX timestmap or transaction ID demarking the end of returned
        results. */
    END,

    /** Offset into full list of results, to effect pagination into the list. */
    OFS,

    /** One of "open", "close", or "both", to determine which time stamp to use
        for searching/filtering. */
    CLOSE_TIME,

    /** A boolean indicating whether to do profit and loss calculations. */
    DO_CALCS,

    /** A trading pair, such as "XETCXETH", or, for some functions, a
     * comma-separated list such as "XXBTCZUSD,XETHXXBT". */
    PAIR,

    /** A boolean value indicating whether or not to include fee info in the
        results. */
    FEE_INFO,

    /** A comma-delimited list of order flags: "post", "fcib", "fciq", "nompp".
        See the [upstream
        documentation](https://docs.kraken.com/rest/#operation/addOrder) for the
        [Kraken_API::add_order] end-point for details about the precise meanings
        of these terms. **/
    OFLAGS,

    /** UXIX timestamp of the start of a report.  */
    START_TIME,

    /** UNIX timestamp of the end of a report.  */
    END_TIME,

    /** One of "CSV" or "TSV".  */
    FORMAT,

    /** Comma-delimited list of fields to include in a report; see the
        documentation for the
        [AddExport](https://docs.kraken.com/rest/#operation/addExport) end-point
        for valid entries.  */
    FIELDS,

    /** Expiration time, either "+<N>" for a number of seconds from now, or just
        "N" for a UNIX timestamp. */
    EXPIRE_TIME,

    /** Boolean indicating that the order should be validated but not actually
        submitted. */
    VALIDATE,

    /** Date-time stamp (RFC3339) after which the new order request should be
        rejected. */
    DEADLINE,

    /** One of "market", "limit", "stop-loss", "take-profit", "stop-loss-limit",
        "take-profit-limit", or "settle-position". */
    ORDER_TYPE,

    /** One of "all", "any position", "closed position", "closing position", "no
        position", to describe the type of trades. */
    TYPE,

    /** One of "limit", "stop-loss", "take-profit", "stop-loss-limit", or
        "take-profit-limit".  */
    CLOSE_TYPE,

    /** Conditional close order [API_Option::PRICE]. */
    CLOSE_PRICE_1,

    /** Conditional close order [API_Option::PRICE_2]. */
    CLOSE_PRICE_2,

    /** Limit price for limit orders, trigger price for all other types. */
    PRICE,

    /** Limit price for "stop-loss-limit" and "take-profit-limit" orders.  */
    PRICE_2,

    /**  One of "index" or "last" to indicate which price signal triggers an
         order. */
    TRIGGER,

    /**  Amount of leverage desired. */
    LEVERAGE,

    /**  One of "GTC" ('good 'til cancelled'), "IOC" ('immediate or cancel'),
         or "GTD" ('good 'til date'). */
    TIME_IN_FORCE,

    /** Order quantity in terms of the base asset. */
    VOLUME,

    /** Time frame interval in minutes (i32 as str). */
    INTERVAL,

    /** Time interval in seconds (isize as str). */
    TIMEOUT,

    /** Return data points since the given UNIX timestamp (i32 as str). */
    SINCE,

    /** The maximum number of data to return.  */
    COUNT,

    /** Varies by function, but lists one or more transaction IDs, or sometimes
        user reference IDs. */
    TXID,

    /** For the get_open_positions function, carries a market or symbol pair
        over which to consolidate the open margin positions. */
    CONSOLIDATION,

    /** Comma-delimited list of ledger IDs. */
    ID,

    /** Use pending replace, before complete replace (bool as str).  */
    CANCEL_RESPONSE,

    #[doc(hidden)]
    REPORT,
    
    #[doc(hidden)]
    DESCRIPTION,
    
    #[doc(hidden)]
    __CEILING
}

use  API_Option  as  Opt;



/**  When submitting a trade instruction, are we buying or selling?  */
pub  enum  Instruction  {  /** We are buying. */
                           BUY,

                           /** We are selling.  */
                           SELL  }

impl  Instruction  {  /** Get the exact string Kraken needs to express this
                          option. */
                      pub  fn  as_kraken_string (&self)  ->  &'static str
                      {   match self
                          {   Instruction::BUY  =>  "buy",
                              Instruction::SELL =>  "sell" } } }



/**  When submitting a trade instruction, what order type do we want to make? */
pub  enum  Order_Type
{
    /** A market order: to be executed as soon as possible at whatever the
        market price happens to be.  */
    MARKET = 0,
    
    /** A limit order: to be executed when the price is below the limit when
        buying, or above the limit when selling. */
    LIMIT,

    /** A stop-loss order: to be executed when the price is above the order when
        buying, or below the order when selling. */
    STOP_LOSS,

    /** Execute the order when the price is below the order when buying, above
        the order when selling. */
    TAKE_PROFIT,

    /**  */
    STOP_LOSS_PROFIT,

    /**  */
    STOP_LOSS_PROFIT_LIMIT,

    /**  */
    STOP_LOSS_LIMIT,

    /**  */
    TAKE_PROFIT_LIMIT,

    /**  */
    TRAILING_STOP,

    /**  */
    TRAILING_STOP_LIMIT,

    /**  */
    STOP_LOSS_AND_LIMIT,

    /**  */
    SETTLE_POSITION
}

impl  Order_Type
{   /** Present the order type precisely as the Kraken API specifies. */
    pub fn as_kraken_string (&self)  ->  &'static str
    {   match self
        {   Order_Type::MARKET                 =>  "market",
            Order_Type::LIMIT                  =>  "limit",
            Order_Type::STOP_LOSS              =>  "stop-loss",
            Order_Type::TAKE_PROFIT            =>  "take-profit",
            Order_Type::STOP_LOSS_PROFIT       =>  "stop-loss-profit",
            Order_Type::STOP_LOSS_PROFIT_LIMIT =>  "stop-loss-profit-limit",
            Order_Type::STOP_LOSS_LIMIT        =>  "stop-loss-limit",
            Order_Type::TAKE_PROFIT_LIMIT      =>  "take-profit-limit",
            Order_Type::TRAILING_STOP          =>  "trailing-stop",
            Order_Type::TRAILING_STOP_LIMIT    =>  "trailing-stop-limit",
            Order_Type::STOP_LOSS_AND_LIMIT    =>  "stop-loss-and-limit",
            Order_Type::SETTLE_POSITION        =>  "settle-position" } } }




/** When exporting bulk data, we must specify the nature of the reporting
    format. */
pub  enum  Report_Type  {  /** Trades. */ TRADES,  /** Ledgers. */ LEDGERS  }

impl  Report_Type  {  fn  as_kraken_string (&self) -> &'static str
                      { match self { Report_Type::TRADES => "trades",
                                     Report_Type::LEDGERS => "ledgers" } } }



/** A handle on the connection to the Kraken exchange.

    This can be used multiple times, so should only be instantiated once,
    preferably using the [connect] function; due to the nature of the exchange
    and the REST API it does not make sense to make multiple requests in
    parallel, and so there should be no need for more than one of these objects
    to be instantiated.

    This object has a method implemented on it for every end-point in the Kraken
    API, which will make the call to the exchange as appropriate and marshall
    the returned data through the method's return object, described below.

    ## Optional arguments

    Many of the Kraken functions (end-points) can take optional extra arguments.
    These are set into the [Kraken_API] object prior to making a function call,
    and then those arguments which have been set and are pertinent to that
    function will be passed along.  The documentation for the individual
    functions below indicates the options pertinent to that function.

    The set of optional arguments persist between function calls: this may work
    in your favour or against you depending on your work flow.  In any case, use
    the methods [Kraken_API::set_opt], [Kraken_API::clear_opt] and
    [Kraken_API::clear_all_options] to manipulate the current option set.

    ## Errors

    Errors which occur at our end, such as failure to contact the Kraken
    exchange, or to perform our own processing, are signalled by returns of
    `Result::Err(String)` in which the string is a human-readable explanation of
    the problem.

    Errors which occur at the Kraken exchange, such as failure to authenticate
    the user, are signalled by an 'error' entry in the JSON string returned as a
    `Result::Ok(String)`.

    A successful return of data from the exchange will be seen with a 'result'
    section in the JSON string returned as `Result::Ok(String)`.  */

#[derive(Default)]
pub  struct  Kraken_API  {  key:        String,
                            secret:     String,
                            query_url:  String,
                            options:    Map<Opt, String>  }



const url_base: &str  =  "https://api.kraken.com/0";



/** Obtain a handle on a connection to the Kraken exchange.

    This function must be called before any other, and is the only way to get a
    Kraken_API handle on which all the access methods are usable; the
    [Kraken_API::default] method can be used if only public access methods of
    the Kraken exchange are to be used.

    The ‘key’ and ‘secret’ must have been obtained through the Kraken web site,
    and must be supplied here precisely as given.  However, note that this
    method cannot fail: no checks are performed at this point on the
    plausibility or actual validity of the credentials supplied.  */

pub  fn  connect  (key:  String,  secret:  String)  ->  Kraken_API
          {   Kraken_API { key,  secret,  ..Default::default ()  }   }



impl  Kraken_API
{
/********************  OPTIONAL ARGUMENT PROCESSING  **************************/


/** Give a value to the optional argument, which may be passed to the Kraken
    end-point if the end-point accepts this option. */

    pub  fn  set_opt<T: std::fmt::Display>
                                   (&mut  self,  opt:  API_Option,  value:  T)
          {   self.options.insert (opt, value.to_string ());   }



/** Clear an option; this will not be sent to any end-points which would accept
    such an optional argument. */

    pub  fn  clear_opt  (&mut  self,  opt: API_Option)
          {   self.options.remove (&opt);  }



/** The options set in a [Kraken_API] object are persistent across back-end
    calls; it may be prudent to call this method to make sure the set of options
    is in a well-known state, before making settings for the next back-end
    call.  */

    pub  fn  clear_all_options  (&mut  self)   {   self.options.clear ();   }




/***********************  USER DATA ENQUIRIES  ******************************/


/** Retrieve all cash balances.

    [Here](https://docs.kraken.com/rest/#operation/getAccountBalance) is the
    Kraken documentation.  */

  pub  fn  account_balance  (&mut self)  ->  Result<String, String>
    {  api_function (self, "Balance", &[], query_private)  }



/** Get a summary of standing with an asset.

    [Here](https://docs.kraken.com/rest/#operation/getTradeBalance) is the
    Kraken documentation.

    This function understands the [API_Option::ASSET] optional argument.  */

  pub  fn  trade_balance  (&mut self)  ->  Result<String, String>
    {  api_function (self, "TradeBalance", &[Opt::ASSET], query_private)  }



/** Get detailed information about currently open orders.

    The Kraken documentation is
    [here](https://docs.kraken.com/rest/#operation/getOpenOrders).

    The end-point is responsive to the [API_Option::TRADES] and
    [API_Option::USERREF] optional arguments.  */

  pub  fn  open_orders  (&mut self)  ->  Result<String, String>
    {  api_function
            (self, "OpenOrders", &[Opt::TRADES, Opt::USERREF], query_private)  }



/** Get a detailed list of closed orders, paged to up to 50 at a time.

    [Here](https://docs.kraken.com/rest/#operation/getClosedOrders) is the
    Kraken documentation.

    This function accepts [API_Option::TRADES], [API_Option::USERREF],
    [API_Option::START], [API_Option::END], [API_Option::OFS], and
    [API_Option::CLOSE_TIME] optional arguments.  */

  pub  fn  closed_orders  (&mut self)  ->  Result<String, String>
    {  api_function (self,
                    "ClosedOrders",
                    &[Opt::TRADES,  Opt::USERREF,  Opt::START,
                      Opt::END,     Opt::OFS,      Opt::CLOSE_TIME],
                    query_private)   }



/** Get a list of order details.

    This function is variously known in the [Kraken
    documentation](https://docs.kraken.com/rest/#operation/getOrdersInfo) as
    'Query Orders Info', 'GetOrdersInfo' and 'QueryOrders'.

    We see that 'txid' can be a comma-separated list of transaction identifiers,
    and that options [API_Option::TRADES] and [API_Option::USERREF] can
    optionally be set in the 'self' [Kraken_API] object prior to this call.  */

  pub  fn  query_orders  (&mut self, txid:  String)  ->  Result<String, String>
    {
      self.options.insert (Opt::TXID, txid);
      api_function (self,
                    "QueryOrders",
                    &[Opt::TXID, Opt::TRADES, Opt::USERREF],
                    query_private)
    }



/** Get a detailed list of past trades, paged to up to 50 at a time.

    The upstream documentation is
    [here](https://docs.kraken.com/rest/#operation/getTradeHistory).

    The function accepts [API_Option::TYPE], [API_Option::TRADES],
    [API_Option::START], [API_Option::END], and [API_Option::OFS] optional
    arguments.  */

  pub  fn  trades_history  (&mut self)  ->  Result<String, String>
    {  api_function  (self,
                      "TradesHistory",
                      &[Opt::TYPE, Opt::TRADES, Opt::START, Opt::END, Opt::OFS],
                      query_private)  }



/** Get information about specific trades.

    This function is known in the
    [Kraken documentation](https://docs.kraken.com/rest/#operation/getTradesInfo)
    as 'getTradesInfo', 'QueryTrades' and 'Query Trades Info'.

    We see that 'txid' can be a comma-separated list of transaction IDs, and the
    function accepts the [API_Option::TRADES] option, a string holding either
    "true" or "false".  */

  pub  fn  trades_info  (&mut self, txid:  String)  ->  Result<String, String>
    {
      self.options.insert (Opt::TXID, txid);
      api_function
               (self, "QueryTrades", &[Opt::TXID, Opt::TRADES], query_private)
    }



/** This function is for getting information about open *margin* positions.

    The end-point
    [documentation](https://docs.kraken.com/rest/#operation/getOpenPositions)
    is at Kraken, where it is known as 'getOpenPositions'.

    The method is sensitive to the optional arguments [API_Option::TXID],
    [API_Option::DO_CALCS] and [API_Option::CONSOLIDATION].  */

  pub  fn  open_margin_positions  (&mut self)  ->  Result<String, String>
    {  api_function  (self,
                      "OpenPositions",
                      &[Opt::TXID, Opt::DO_CALCS, Opt::CONSOLIDATION],
                      query_private)   }



/** Retrieve information about ledger entries.

    This end-point is known variously as "GetLedgersInfo", "getLedgers" and
    "Ledgers" in the
    [Kraken documentation](https://docs.kraken.com/rest/#operation/getLedgers).

    The function is sensitive to the [API_Option::ACLASS], [API_Option::ASSET],
    [API_Option::TYPE], [API_Option::START], [API_Option::END] and
    [API_Option::OFS] optional arguments.  */

  pub  fn  ledgers_info  (&mut self)  ->  Result<String, String>
    {  api_function (self,
                      "Ledgers",
                      &[Opt::ACLASS, Opt::ASSET, Opt::TYPE,
                        Opt::START,  Opt::END,   Opt::OFS],
                      query_private)   }



/** Retrieve information about specific ledger entries.

    [Here](https://docs.kraken.com/rest/#operation/getLedgersInfo)
    is the Kraken documentation.

    This is sensitive to the [API_Option::TRADES] and [API_Option::ID] optional
    arguments. */

  pub  fn  query_ledgers  (&mut self)  ->  Result<String, String>
    {  api_function
             (self, "QueryLedgers", &[Opt::ID, Opt::TRADES], query_private)  }


  
/** Get trade volue.

    Documented at
    [Kraken](https://docs.kraken.com/rest/#operation/getTradeVolume).

    The pair argument is the trading pair, e.g., "XETCXETH", for which data are
    required.

    The function also accepts the [API_Option::FEE_INFO] optional argument, to
    indicate that fee information should be included in the returned result
    set.  */

  /* !!!!  The handling of the pair argument is funny here, and we are doing it
           wrong.  */

  pub  fn  trade_volume  (&mut self, pair: &str)  ->  Result<String, String>
    {
       self.set_opt (Opt::PAIR, pair);
       api_function
            (self, "TradeVolume", &[Opt::PAIR, Opt::FEE_INFO], query_private)
    }



/** Request export of trades or ledgers.

    The upstream documentation is
    [here](https://docs.kraken.com/rest/#operation/addExport).

    Also uses the [API_Option::FORMAT], [API_Option::FIELDS],
    [API_Option::START_TIME] and [API_Option::END_TIME] optional arguments.  */

  pub  fn  request_export_report
                      (&mut self,  report_type: Report_Type,  description: &str)
               ->  Result<String, String>
    {
        self.set_opt (Opt::REPORT, report_type.as_kraken_string ());
        self.set_opt (Opt::DESCRIPTION,  description);
        api_function (self,
                      "AddExport",
                      &[Opt::REPORT, Opt::FORMAT,     Opt::DESCRIPTION,
                        Opt::FIELDS, Opt::START_TIME, Opt::END_TIME],
                      query_private)
    }



/** Get status of requested data exports.

    The Kraken documentation is
    [here](https://docs.kraken.com/rest/#operation/exportStatus).   */

  pub  fn  get_export_report_status  (&mut self,  report_type: Report_Type)
                ->  Result<String, String>
    {
        self.set_opt  (Opt::REPORT, report_type.as_kraken_string ());
        api_function  (self, "ExportStatus", &[Opt::REPORT], query_private)
    }



/** Retrieve a processed data export.

    The end-point documentation is
    [here](https://docs.kraken.com/rest/#operation/retrieveExport).  */

  pub  fn  retrieve_data_export  (&mut self,  id: &str)
                   ->  Result<String, String>
    {
        self.set_opt (Opt::ID,  id);
        api_function (self, "RetrieveExport", &[Opt::ID],  query_private)
    }
    


/** Delete an exported data report.

    [Here](https://docs.kraken.com/rest/#operation/removeExport) is the upstream
    documentation.

    NOTE that *type* MUST be one of the strings "delete" or "cancel", or a panic
    may occur.   */

    /* !!!!!  We must do better than this with the type argument. */

  pub  fn  delete_export_report  (&mut self,  id: &str,  type_: &str)
                    ->  Result<String, String>
    {
      assert! (type_ == "delete"  ||  type_ == "cancel");

      self.set_opt  (Opt::ID,  id);
      self.set_opt  (Opt::TYPE,  type_);
      api_function  (self, "RemoveExport", &[Opt::ID, Opt::TYPE], query_private)
    }




/**********************   USER TRADING   **************************************/



/** Place a new order onto the exchange's order book.

    The upstream documentation is
    [here](https://docs.kraken.com/rest/#operation/addOrder).
    
    The following optional arguments are considered by this end-point:
    [API_Option::USERREF], [API_Option::PRICE], [API_Option::PRICE_2],
    [API_Option::TRIGGER], [API_Option::LEVERAGE], [API_Option::OFLAGS],
    [API_Option::TIME_IN_FORCE], [API_Option::START_TIME],
    [API_Option::EXPIRE_TIME], [API_Option::CLOSE_TYPE],
    [API_Option::CLOSE_PRICE_1], [API_Option::CLOSE_PRICE_2],
    [API_Option::DEADLINE], and [API_Option::VALIDATE].  */

  pub  fn  add_order<V: std::fmt::Display>  (&mut self,
                                             order_type: Order_Type,
                                             direction: Instruction,
                                             volume:  V,
                                             pair:  &str)
               ->  Result<String, String>
    {
        self.set_opt (Opt::ORDER_TYPE, order_type.as_kraken_string ());
        self.set_opt (Opt::TYPE, direction.as_kraken_string ());
        self.set_opt (Opt::VOLUME, volume);
        self.set_opt (Opt::PAIR, pair);
        api_function  (self,
                       "AddOrder",
                       &[Opt::ORDER_TYPE,       Opt::TYPE,      Opt::VOLUME,
                         Opt::PAIR,             Opt::USERREF,   Opt::PRICE,
                         Opt::PRICE_2,          Opt::TRIGGER,   Opt::LEVERAGE,
                         Opt::OFLAGS,           Opt::TIME_IN_FORCE,
                         Opt::START_TIME,       Opt::EXPIRE_TIME,
                         Opt::CLOSE_TYPE,       Opt::CLOSE_PRICE_1,
                         Opt::CLOSE_PRICE_2,    Opt::DEADLINE,  Opt::VALIDATE],
                       query_private)
    }
                         


/** Edit an order on the exchange's order book.

    The upstream documentation is
    [here](https://docs.kraken.com/rest/#operation/editOrder).
    
    The following optional arguments are considered by this end-point:
    [API_Option::USERREF], [API_Option::PRICE], [API_Option::PRICE_2],
    [API_Option::OFLAGS], [API_Option::VOLUME], [API_Option::DEADLINE],
    [API_Option::CANCEL_RESPONSE], and [API_Option::VALIDATE].  */

  pub  fn  edit_order<V: std::fmt::Display>  (&mut self,
                                              tx_id: &str,
                                              pair:  &str)
               ->  Result<String, String>
    {
        self.set_opt (Opt::TXID, tx_id);
        self.set_opt (Opt::PAIR, pair);
        api_function  (self,
                       "AddOrder",
                       &[Opt::ORDER_TYPE,  Opt::VOLUME,
                         Opt::PAIR,        Opt::USERREF,   Opt::PRICE,
                         Opt::PRICE_2,     Opt::OFLAGS,
                         Opt::DEADLINE,    Opt::VALIDATE,
                         Opt::TXID,        Opt::CANCEL_RESPONSE],
                       query_private)
    }



/** Cancel an open order.

    The documentation for this end-point is at
    [Kraken](https://docs.kraken.com/rest/#operation/cancelOrder).  Note that
    'txid' can actually be a 'userref', in which case all open orders for that
    user are cancelled.  */
    
  pub  fn  cancel_order  (&mut self, txid:  &str)  ->  Result<String, String>
    {
      self.set_opt (Opt::TXID, txid);
      api_function (self, "CancelOrder", &[Opt::TXID], query_private)
    }



/** Cancel all orders open on this account.

    The documentation for this end-point is at
    [Kraken](https://docs.kraken.com/rest/#operation/cancelAllOrders).  */
    
  pub  fn  cancel_all_orders  (&mut self)  ->  Result<String, String>
    {
      api_function (self, "CancelAll", &[], query_private)
    }



/** Dead man's switch will cancel all orders after a time if not reset.

    The upstream documentation is
    [here](https://docs.kraken.com/rest/#operation/cancelAllOrdersAfter).  */

  pub  fn  cancel_all_orders_after_x  (&mut self,  timeout: isize)
               ->  Result<String, String>
    {
      self.set_opt (Opt::TIMEOUT,  timeout);
      api_function
              (self, "CancelAllOrdersAfter", &[Opt::TIMEOUT], query_private)
    }



/**********************   MARKET DATA   **************************************/

/** Get the server's time.
    Documented upstream
    [here](https://docs.kraken.com/rest/#tag/Market-Data). */

  pub  fn  server_time  (&mut self) ->  Result<String, String>
    {  api_function (self, "Time", &[], query_public)  }



      /* !!!!  We want to put a fully testable example of this function's use in
       *       here. */
/** Get the current exchange system status.

    Documented upstream
    [here](https://docs.kraken.com/rest/#operation/getSystemStatus).  */

  pub  fn  system_status  (&mut self) ->  Result<String, String>
    {  api_function (self, "SystemStatus", &[], query_public)  }




/** Get information about the assets that are available at this time at this
    exchange.

    The upstream documentation is
    [here](https://docs.kraken.com/rest/#operation/getAssetInfo).

    The function is responsive to the [API_Option::ACLASS] and
    [API_Option::ASSET] optional arguments.  */

  pub  fn  asset_info  (&mut self) ->  Result<String, String>
    {  api_function
         (self, "Assets", &[Opt::ACLASS, Opt::ASSET], query_public) }



/** Get tradable asset pairs.

    Documented at
    [Kraken](https://docs.kraken.com/rest/#operation/getTradableAssetPairs).

    The optional arguments [API_Option::INFO] and [API_Option::PAIR] will be
    used if set.  */

  pub  fn  asset_pairs  (&mut self) ->  Result<String, String>
    {  api_function (self, "AssetPairs", &[Opt::INFO, Opt::PAIR], query_public)}



/** Get ticker information.

    The upstream documentation is
    [here](https://docs.kraken.com/rest/#operation/getTickerInformation).  */

  pub  fn  ticker_info  (&mut self,  pair: String)  ->  Result<String, String>
    {  
      self.set_opt (Opt::PAIR, pair);
      api_function (self, "Ticker", &[Opt::PAIR], query_public)
    }



/** Get OLHC (open, low, high, close) data.

    The end-point is documented upstream
    [here](https://docs.kraken.com/rest/#operation/getOHLCData).

    The method respects the optional arguments [API_Option::INTERVAL] and
    [API_Option::SINCE].  */

  pub  fn  ohlc_data  (&mut self,  pair: String)  ->  Result<String, String>
    {
      self.set_opt (Opt::PAIR, pair);
      api_function
           (self, "OHLC", &[Opt::PAIR, Opt::INTERVAL, Opt::SINCE], query_public)
    }



/** Get live order book data.

    Upstream documentation is at
    [Kraken](https://docs.kraken.com/rest/#operation/getOrderBook).

    This end-point uses the optional argument [API_Option::COUNT] to limit the
    depth of data into the order book.  */

  pub  fn  order_book  (&mut self,  pair: String)  ->  Result<String, String>
    {
      self.set_opt (Opt::PAIR, pair);
      api_function (self, "Depth", &[Opt::PAIR, Opt::COUNT], query_public)
    }



/** Get recent trades made at the exchange.

    Documented
    [upstream](https://docs.kraken.com/rest/#operation/getRecentTrades).

    Allows the optional argument [API_Option::SINCE].  */

  pub  fn  recent_trades  (&mut self,  pair: String)  ->  Result<String, String>
    {
      self.set_opt (Opt::PAIR, pair);
      api_function (self, "Trades", &[Opt::PAIR, Opt::SINCE], query_public)
    }



/** Get recent spreads.

    Documented upstream
    [here](https://docs.kraken.com/rest/#operation/getRecentSpreads).

    Respects the optional argument [API_Option::SINCE].  */

  pub  fn  spread_data  (&mut self,  pair: String)  ->  Result<String, String>
    {
      self.set_opt (Opt::PAIR, pair);
      api_function (self, "Spread", &[Opt::PAIR, Opt::SINCE], query_public)
    }
}



fn  api_function  (K: &mut Kraken_API,
                   end_point: &str,
                   options: &[Opt],
                   do_query: fn(&Kraken_API)->Result<String,String>)
        ->  Result<String, String>
                {
                     K.query_url  =  end_point.to_string ();
                     query_add_options  (K,  options,  '?');
                     do_query (K)
                }



fn  query_public  (K:  &Kraken_API)  ->  Result<String, String>
{
    let  mut  C  =  curl::easy::Easy::new ();

    C.url (&(url_base.to_string () + "/public/" + &K.query_url)).unwrap ();

    let  query_result  =  Arc::new (Mutex::new (String::new ()));

    let  qr  =  query_result.clone ();
    C.write_function
            (move |data|
              {  *qr.lock ().unwrap () += std::str::from_utf8 (data).unwrap ();
                  Ok (data.len ())  })
        .map_err (|e| e.to_string ()) ?;

    C.perform ().map_err (|e| e.to_string ()) ?;

    let  x  =  Ok (query_result.lock ().unwrap ().to_string ());
    x
}



fn  query_private  (K:  &Kraken_API)  ->  Result<String, String>
{
    if  K.secret.len () != 88
        {   Err ("private key must be 88 characters long".to_string ()) ?   }

    let  nonce   =  std::time::SystemTime::now ()
                             .duration_since (std::time::UNIX_EPOCH) .unwrap ()
                             .as_micros ()
                             .to_string ();

    let  (query_url, post_data)  =  { let  mut  S  =  K.query_url.split ('?');
                                      (S.next ().unwrap ().to_string (),
                                       S.next ().unwrap_or ("").to_string ()) };

    let  post_data  =  &format! ("{}{}nonce={}",
                                 post_data,
                                 if post_data.is_empty () {""} else {"&"},
                                 nonce);

    let  mut  C  =  curl::easy::Easy::new ();

    C.url (&format! ("{}/private/{}", url_base, query_url)).unwrap ();

    C.post (true).unwrap ();
    C.post_fields_copy (post_data.as_bytes ()).unwrap ();

    C.http_headers
        ( {
             let  mut  L  =  curl::easy::List::new ();

             L.append (&format!("API-Key: {}", K.key)).unwrap ();

             let  key  =  SSL::pkey::PKey::hmac
                             (&SSL::base64::decode_block (&K.secret).unwrap ())
                           .unwrap ();

             let  mut  signer  =  SSL::sign::Signer::new
                                     (SSL::hash::MessageDigest::sha512 (), &key)
                                   .unwrap ();

             signer.update ("/0/private/".as_bytes ()).unwrap ();
             signer.update (query_url.as_bytes ()).unwrap ();
             signer.update (&SSL::hash::hash
                                         (SSL::hash::MessageDigest::sha256 (),
                                          (nonce + post_data).as_bytes ())
                               .unwrap ())
                   .unwrap ();

             L.append (&format!("API-Sign: {}",
                                &SSL::base64::encode_block
                                    (&signer.sign_to_vec ().unwrap ())))
              .unwrap ();

             L
        } ) .unwrap ();
    
    let  query_result  =  Arc::new  (Mutex::new (String::new ()));

    let  qr  =  query_result.clone ();
    C.write_function
            (move |data|
              {  *qr.lock ().unwrap () += std::str::from_utf8 (data).unwrap ();
                  Ok (data.len ())  })
     .unwrap ();

    C.perform ().map_err (|e| e.to_string ()) ?;

    let  x  =  Ok (query_result.lock ().unwrap ().to_string ());
    x
}



fn  kraken_argument  (O:  &Opt)  ->  &'static  str
{
    match  O  {   Opt::INFO             =>  "info",
                  Opt::ACLASS           =>  "aclass",
                  Opt::ASSET            =>  "asset",
                  Opt::TRADES           =>  "trades",
                  Opt::USERREF          =>  "userref",
                  Opt::START            =>  "start",
                  Opt::END              =>  "end",
                  Opt::OFS              =>  "ofs",
                  Opt::CLOSE_TIME       =>  "closetime",
                  Opt::DO_CALCS         =>  "docalcs",
                  Opt::PAIR             =>  "pair",
                  Opt::FEE_INFO         =>  "fee-info",
                  Opt::OFLAGS           =>  "oflags",
                  Opt::START_TIME       =>  "starttm",
                  Opt::END_TIME         =>  "endtm",
                  Opt::EXPIRE_TIME      =>  "expiretm",
                  Opt::FORMAT           =>  "format",
                  Opt::FIELDS           =>  "fields",
                  Opt::VALIDATE         =>  "validate",
                  Opt::DEADLINE         =>  "deadline",
                  Opt::ORDER_TYPE       =>  "ordertype",
                  Opt::LEVERAGE         =>  "leverage",
                  Opt::TIME_IN_FORCE    =>  "timeinforce",
                  Opt::VOLUME           =>  "volume",
                  Opt::TYPE             =>  "type",
                  Opt::CLOSE_TYPE       =>  "close[ordertype]",
                  Opt::CLOSE_PRICE_1    =>  "close[price]",
                  Opt::CLOSE_PRICE_2    =>  "close[price2]",
                  Opt::PRICE            =>  "price",
                  Opt::PRICE_2          =>  "price2",
                  Opt::TRIGGER          =>  "trigger",
                  Opt::INTERVAL         =>  "interval",
                  Opt::TIMEOUT          =>  "timeout",
                  Opt::SINCE            =>  "since",
                  Opt::COUNT            =>  "count",
                  Opt::TXID             =>  "txid",
                  Opt::CONSOLIDATION    =>  "consolidation",
                  Opt::ID               =>  "id",
                  Opt::CANCEL_RESPONSE  =>  "cancel_response",
                  Opt::DESCRIPTION      =>  "description",
                  Opt::REPORT           =>  "report",
                  Opt::__CEILING        =>  ""    }
}



fn  query_add_options  (K: &mut Kraken_API,
                        permitted_options: &[Opt],
                        mut joiner: char)
{   for  o  in  permitted_options
    {   if let Some(K_O) = K.options.get (o)
        {   K.query_url += &(std::mem::replace (&mut joiner, '&').to_string ()
                             + kraken_argument (o) + "=" + K_O);   }  }  }



#[cfg(test)]
mod  test
  {  #[test]  fn  server_time ()  ->  Result <(), String>
     {
         use  serde_json  as JSN;

         let  v = JSN::from_str::<JSN::Value> (&super::Kraken_API::default ()
                                     .server_time () ?)
                              .map_err (|E| E.to_string ()) ?;

         let  res  =  v ["result"] ["rfc1123"].as_str ().ok_or ("") ?;

         assert! (res.len () > 0);

         Ok (())
     }  }