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
// coinbase-api-rust
// Copyright (C) 2018 Thomas HUET
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#![forbid(unsafe_code)]

//! Rust client library for [Coinbase](https://docs.pro.coinbase.com).
//!
//! # Example
//!
//!```
//!extern crate coinbase_api;
//!extern crate hyper;
//!
//!use coinbase_api::*;
//!use hyper::rt::Future;
//!
//!fn make_future() -> impl Future<Item=(), Error=()> {
//!  let client = MarketDataClient::new(SANDBOX).unwrap();
//!  client.products()
//!  .map(|products| {
//!    println!("Pairs available for trading:");
//!    for p in products {
//!      println!("{}", p.id);
//!    }
//!  })
//!  .map_err(|err| println!("Error: {:?}", err))
//!}
//!
//!fn main() {
//!  hyper::rt::run(make_future());
//!}
//!```

extern crate base64;
extern crate chrono;
extern crate hmac;
extern crate hyper;
extern crate hyper_tls;
extern crate serde;
extern crate sha2;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

type DateTime = chrono::DateTime<chrono::Utc>;

/// A decimal number with full precision.
#[derive(Serialize, Deserialize, Debug)]
pub struct Decimal(String);

macro_rules! impl_num_from {
  ($t:ty) => {
    impl From<$t> for Decimal {
      fn from(x : $t) -> Self { Decimal(x.to_string()) }
    }
  };
}

impl_num_from!(f32);
impl_num_from!(f64);
impl_num_from!(i8);
impl_num_from!(i16);
impl_num_from!(i32);
impl_num_from!(i64);
impl_num_from!(isize);
impl_num_from!(u8);
impl_num_from!(u16);
impl_num_from!(u32);
impl_num_from!(u64);
impl_num_from!(usize);

use std::str::FromStr;

impl Decimal {
  pub fn to_f32(&self) -> Option<f32> {
    match self {
      Decimal(s) => match f32::from_str(s) {
        Ok(f) => Some(f),
        Err(_) => None,
      },
    }
  }

  pub fn to_f64(&self) -> Option<f64> {
    match self {
      Decimal(s) => match f64::from_str(s) {
        Ok(f) => Some(f),
        Err(_) => None,
      },
    }
  }
}

/// Description of a currency pair.
#[derive(Deserialize, Debug)]
pub struct Product {
  pub id : String,
  pub base_currency : String,
  pub quote_currency : String,
  pub base_min_size : Decimal,
  pub base_max_size : Decimal,
  pub quote_increment : Decimal,
}

/// Aggregated book of orders.
#[derive(Deserialize, Debug)]
pub struct AggregatedBook {
  pub sequence : u64,
  /// List of (price, size, num-orders).
  pub bids : Vec<(Decimal, Decimal, u64)>,
  pub asks : Vec<(Decimal, Decimal, u64)>,
}

/// Non aggregated book of orders.
#[derive(Deserialize, Debug)]
pub struct FullBook {
  pub sequence : u64,
  /// List of (price, size, order_id).
  pub bids : Vec<(Decimal, Decimal, String)>,
  pub asks : Vec<(Decimal, Decimal, String)>,
}

pub mod book_level {
  use AggregatedBook;
  use FullBook;

  pub struct Best();
  pub struct Top50();
  pub struct Full();

  pub trait BookLevel<T> {
    fn to_str(&self) -> &str;
  }

  impl BookLevel<AggregatedBook> for Best {
    fn to_str(&self) -> &str { "level=1" }
  }

  impl BookLevel<AggregatedBook> for Top50 {
    fn to_str(&self) -> &str { "level=2" }
  }

  impl BookLevel<FullBook> for Full {
    fn to_str(&self) -> &str { "level=3" }
  }
}

/// Information about the last trade (tick), best bid/ask and 24h volume.
#[derive(Deserialize, Debug)]
pub struct Ticker {
  pub trade_id : u64,
  pub price : Decimal,
  pub size : Decimal,
  pub bid : Decimal,
  pub ask : Decimal,
  pub volume : Decimal,
  pub time : DateTime,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Side {
  Buy,
  Sell,
}

/// Description of a trade.
#[derive(Deserialize, Debug)]
pub struct Trade {
  pub time : DateTime,
  pub trade_id : u64,
  pub price : Decimal,
  pub size : Decimal,
  /// Maker order side (buy indicates a down-tick and sell an up-tick).
  pub side : Side,
}

/// `(time, low, high, open, close, volume)`
/// - `time`: Bucket start time.
/// - `low`: Lowest price during the bucket interval.
/// - `high`: Highest price during the bucket interval.
/// - `open`: Opening price (first trade) in the bucket interval.
/// - `close`: Closing price (last trade) in the bucket interval.
/// - `volume`: Volume of trading activity during the bucket interval.
#[derive(Deserialize, Debug)]
pub struct Candle(u64, f64, f64, f64, f64, f64);

/// Trading stats for a product.
/// `volume` is in base currency units. `open`, `high`, `low` are in quote currency units.
#[derive(Deserialize, Debug)]
pub struct Stats {
  pub open : Decimal,
  pub high : Decimal,
  pub low : Decimal,
  pub volume : Decimal,
}

/// Currency description.
#[derive(Deserialize, Debug)]
pub struct Currency {
  pub id : String,
  pub name : String,
  pub min_size : Decimal,
}

/// Time of the API server.
#[derive(Deserialize, Debug)]
pub struct ServerTime {
  pub iso : DateTime,
  pub epoch : f64,
}

/// URL for the sandbox API.
pub const SANDBOX : &str = "https://api-public.sandbox.pro.coinbase.com";

/// URL for the live API.
/// Be sure to test your code on the sandbox API before trying the live one.
pub const LIVE : &str = "https://api.pro.coinbase.com";

use hyper::{
  rt::{Future, Stream},
  Client, Request,
};
use hyper_tls::HttpsConnector;

/// Errors that can happen during a request to the API.
#[derive(Debug)]
pub enum Error {
  HttpError(hyper::error::Error),
  JsonError(
    serde_json::Error,
    Result<String, std::string::FromUtf8Error>,
  ),
}

/// HTTP client for the unauthenticated market data API.
pub struct MarketDataClient {
  client : Client<HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
  base : &'static str,
}

impl MarketDataClient {
  /// Creates a new client.
  /// The `base` argument should be `SANDBOX` or `LIVE`.
  pub fn new(base : &'static str) -> Result<Self, hyper_tls::Error> {
    let mut https = HttpsConnector::new(4)?;
    https.https_only(true);
    Ok(MarketDataClient {
      client : Client::builder().build::<_, hyper::Body>(https),
      base,
    })
  }

  fn get<T>(&self, uri : &str) -> impl Future<Item = T, Error = Error>
  where
    T : serde::de::DeserializeOwned,
  {
    let req = Request::builder()
      .uri(uri)
      .header(hyper::header::USER_AGENT, "coinbase-api-rust")
      .body(hyper::Body::empty())
      .unwrap();
    self
      .client
      .request(req)
      .and_then(|res| res.into_body().concat2())
      .map_err(Error::HttpError)
      .and_then(|body| {
        serde_json::from_slice(body.as_ref())
          .map_err(|err| Error::JsonError(err, String::from_utf8(body.as_ref().to_vec())))
      })
  }

  /// Retrieves a list of available currency pairs for trading.
  pub fn products(&self) -> impl Future<Item = Vec<Product>, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/products");
    self.get(&uri)
  }

  /// Retrieves a list of open orders for a product.
  /// The amount of detail shown depends on the level argument:
  /// - `book_level::Best()` shows only the best bid and ask.
  /// - `book_level::Top50()` shows the top 50 aggregated bids and asks.
  /// - `book_level::Full()` shows the full non aggregated order book.
  pub fn book<T>(
    &self,
    product_id : &str,
    level : &book_level::BookLevel<T>,
  ) -> impl Future<Item = T, Error = Error>
  where
    T : serde::de::DeserializeOwned,
  {
    let mut uri = self.base.to_string();
    uri.push_str("/products/");
    uri.push_str(product_id);
    uri.push_str("/book?");
    uri.push_str(level.to_str());
    self.get(&uri)
  }

  /// Retrieves information about the last trade (tick), best bid/ask and 24h volume.
  pub fn ticker(&self, product_id : &str) -> impl Future<Item = Ticker, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/products/");
    uri.push_str(product_id);
    uri.push_str("/ticker");
    self.get(&uri)
  }

  /// Lists the latest trades for a product.
  pub fn trades(&self, product_id : &str) -> impl Future<Item = Vec<Trade>, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/products/");
    uri.push_str(product_id);
    uri.push_str("/trades");
    self.get(&uri)
  }

  /// Retrieves historic rates for a product.
  /// `granularity` must be one of { one minute, five minutes, fifteen minutes, one hour, six hours, one day }.
  /// The maximum number of data points for a single request is 300 candles.
  /// If your selection of start/end time and granularity will result in more than 300 data points, your request will be rejected.
  pub fn candles(
    &self,
    product_id : &str,
    start : &DateTime,
    end : &DateTime,
    granularity : &chrono::Duration,
  ) -> impl Future<Item = Vec<Candle>, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/products/");
    uri.push_str(product_id);
    uri.push_str("/candles?start=");
    uri.push_str(&start.to_rfc3339_opts(chrono::SecondsFormat::Millis, true));
    uri.push_str("&end=");
    uri.push_str(&end.to_rfc3339_opts(chrono::SecondsFormat::Millis, true));
    uri.push_str("&granularity=");
    uri.push_str(&granularity.num_seconds().to_string());
    self.get(&uri)
  }

  /// Retrieves the latest 300 data points.
  pub fn latest_candles(
    &self,
    product_id : &str,
    granularity : &chrono::Duration,
  ) -> impl Future<Item = Vec<Candle>, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/products/");
    uri.push_str(product_id);
    uri.push_str("/candles?granularity=");
    uri.push_str(&granularity.num_seconds().to_string());
    self.get(&uri)
  }

  /// Retrieves 24 hr stats for the product.
  pub fn stats(&self, product_id : &str) -> impl Future<Item = Stats, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/products/");
    uri.push_str(product_id);
    uri.push_str("/stats");
    self.get(&uri)
  }

  /// Lists known currencies.
  pub fn currencies(&self) -> impl Future<Item = Vec<Currency>, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/currencies");
    self.get(&uri)
  }

  /// Gets the API server time.
  pub fn time(&self) -> impl Future<Item = ServerTime, Error = Error> {
    let mut uri = self.base.to_string();
    uri.push_str("/time");
    self.get(&uri)
  }
}

/// Description of a trading account.
#[derive(Deserialize, Debug)]
pub struct Account {
  pub id : String,
  pub currency : String,
  pub balance : Decimal,
  pub available : Decimal,
  pub hold : Decimal,
  pub profile_id : String,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ActivityType {
  Transfer,
  Match,
  Fee,
  Rebate,
}

#[derive(Deserialize, Debug)]
pub struct ActivityDetails {
  pub order_id : Option<String>,
  pub trade_id : Option<String>,
  pub product_id : Option<String>,
  pub transfer_id : Option<String>,
  pub transfer_type : Option<String>,
}

#[derive(Deserialize, Debug)]
pub struct Activity {
  pub id : u64,
  pub created_at : DateTime,
  pub amount : Decimal,
  pub balance : Decimal,
  #[serde(rename = "type")]
  pub activity_type : ActivityType,
  pub details : ActivityDetails,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum HoldType {
  Order,
  Transfer,
}

#[derive(Deserialize, Debug)]
pub struct Hold {
  pub id : String,
  pub created_at : DateTime,
  pub updated_at : Option<DateTime>,
  pub amount : Decimal,
  #[serde(rename = "type")]
  pub hold_type : HoldType,
  #[serde(rename = "ref")]
  pub hold_ref : String,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum OrderType {
  Limit,
  Market,
}

#[derive(Deserialize, Debug)]
pub struct Order {
  pub id : String,
  pub price : Option<Decimal>,
  pub size : Option<Decimal>,
  pub product_id : String,
  pub side : Side,
  pub stp : Option<String>,
  pub funds : Option<Decimal>,
  pub specified_funds : Option<Decimal>,
  #[serde(rename = "type")]
  pub order_type : OrderType,
  pub time_in_force : Option<String>,
  pub post_only : bool,
  pub created_at : DateTime,
  pub done_at : Option<DateTime>,
  pub done_reason : Option<String>,
  pub fill_fees : Decimal,
  pub filled_size : Decimal,
  pub executed_value : Decimal,
  pub status : String,
  pub settled : bool,
}

#[derive(Deserialize, Debug)]
pub struct Fill {
  pub trade_id : u64,
  pub product_id : String,
  pub price : Decimal,
  pub size : Decimal,
  pub order_id : String,
  pub created_at : DateTime,
  pub liquidity : String,
  pub fee : Decimal,
  pub settled : bool,
  pub side : Side,
}

#[derive(Deserialize, Debug)]
pub struct TrailingVolume {
  pub product_id : String,
  pub exchange_volume : Decimal,
  pub volume : Decimal,
  pub recorded_at : DateTime,
}

fn now() -> u64 {
  std::time::SystemTime::now()
    .duration_since(std::time::UNIX_EPOCH)
    .unwrap()
    .as_secs()
}

/// HTTP client for the authenticated private API.
pub struct PrivateClient {
  client : Client<HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
  base : &'static str,
  key : String,
  secret : String,
  passphrase : String,
}

use hmac::{Hmac, Mac};
use sha2::Sha256;

type HmacSha256 = Hmac<Sha256>;

impl PrivateClient {
  /// Creates a new client.
  /// The `base` argument should be `SANDBOX` or `LIVE`.
  pub fn new(
    base : &'static str,
    key : String,
    secret : String,
    passphrase : String,
  ) -> Result<Self, hyper_tls::Error> {
    let mut https = HttpsConnector::new(4)?;
    https.https_only(true);
    Ok(PrivateClient {
      client : Client::builder().build::<_, hyper::Body>(https),
      base,
      key,
      secret,
      passphrase,
    })
  }

  fn sign(&self, timestamp : u64, method : &hyper::Method, path : &str, body : &str) -> String {
    let mut message = timestamp.to_string();
    message.push_str(method.as_str());
    message.push_str(path);
    message.push_str(body);
    let mut mac = HmacSha256::new_varkey(&base64::decode(&self.secret).unwrap()).unwrap();
    mac.input(message.as_bytes());
    base64::encode(&mac.result().code())
  }

  fn get<T>(&self, query : &str) -> impl Future<Item = T, Error = Error>
  where
    T : serde::de::DeserializeOwned,
  {
    let mut uri = self.base.to_string();
    uri.push_str(query);
    let timestamp = now();
    let req = Request::builder()
      .uri(uri)
      .header(hyper::header::USER_AGENT, "coinbase-api-rust")
      .header("cb-access-key", self.key.as_str())
      .header("cb-access-passphrase", self.passphrase.as_str())
      .header("cb-access-timestamp", timestamp)
      .header(
        "cb-access-sign",
        self
          .sign(timestamp, &hyper::Method::GET, query, "")
          .as_str(),
      ).body(hyper::Body::empty())
      .unwrap();
    self
      .client
      .request(req)
      .and_then(|res| res.into_body().concat2())
      .map_err(Error::HttpError)
      .and_then(|body| {
        serde_json::from_slice(body.as_ref())
          .map_err(|err| Error::JsonError(err, String::from_utf8(body.as_ref().to_vec())))
      })
  }

  pub fn accounts(&self) -> impl Future<Item = Vec<Account>, Error = Error> {
    self.get("/accounts")
  }

  pub fn account(&self, id : &str) -> impl Future<Item = Account, Error = Error> {
    let mut query = "/accounts/".to_string();
    query.push_str(id);
    self.get(&query)
  }

  pub fn ledger(&self, id : &str) -> impl Future<Item = Vec<Activity>, Error = Error> {
    let mut query = "/accounts/".to_string();
    query.push_str(id);
    query.push_str("/ledger");
    self.get(&query)
  }

  pub fn holds(&self, id : &str) -> impl Future<Item = Vec<Hold>, Error = Error> {
    let mut query = "/accounts/".to_string();
    query.push_str(id);
    query.push_str("/holds");
    self.get(&query)
  }

  pub fn orders(&self) -> impl Future<Item = Vec<Order>, Error = Error> {
    self.get("/orders?status=all")
  }

  pub fn orders_for_product(&self, id : &str) -> impl Future<Item = Vec<Order>, Error = Error> {
    let mut query = "/orders?status=all&product_id=".to_string();
    query.push_str(id);
    self.get(&query)
  }

  pub fn order(&self, id : &str) -> impl Future<Item = Order, Error = Error> {
    let mut query = "/orders/".to_string();
    query.push_str(id);
    self.get(&query)
  }

  pub fn fills(&self) -> impl Future<Item = Vec<Fill>, Error = Error> { self.get("/fills") }

  pub fn fills_for_product(&self, id : &str) -> impl Future<Item = Vec<Fill>, Error = Error> {
    let mut query = "/fills?product_id=".to_string();
    query.push_str(id);
    self.get(&query)
  }

  pub fn fills_for_order(&self, id : &str) -> impl Future<Item = Vec<Fill>, Error = Error> {
    let mut query = "/fills?order_id=".to_string();
    query.push_str(id);
    self.get(&query)
  }

  pub fn trailing_volume(&self) -> impl Future<Item = Vec<TrailingVolume>, Error = Error> {
    self.get("/users/self/trailing-volume")
  }
}