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
pub mod errors;
mod wss;
mod rest;
use openssl::pkey::{PKey, Private};
use chashmap::CHashMap;
use std::collections::HashMap;
use std::sync::Arc;
use futures::prelude::*;
use serde_derive::{Serialize, Deserialize};
use log::debug;
use crate::api::{
self,
Params,
ApiClient,
GenerateOrderId,
Notification,
NotificationFlags,
Order,
OrderAck,
Cancel,
CancelAck,
Balances
};
use crate::api::symbol::{Symbol, WithSymbol};
use crate::api::timestamp::{Timestamped, IntoTimestamped};
#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub struct KeyPair {
api_key: String,
secret_key: String,
pass_phrase: String,
}
impl KeyPair {
pub fn new(api_key: String, secret_key: String, pass_phrase: String) -> Self {
KeyPair {
api_key,
secret_key,
pass_phrase,
}
}
}
#[derive(Clone)]
struct Keys {
api_key: String,
secret_key: Arc<PKey<Private>>,
pass_phrase: String,
}
pub struct Client {
params: Params,
keys: Option<Keys>,
order_ids: Arc<CHashMap<String, String>>,
symbols: HashMap<String, Symbol>,
http_client: hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
}
impl Client {
pub fn new(params: Params, key_pair: Option<KeyPair>) -> Result<Self, failure::Error> {
let keys = match key_pair {
Some(pair) => {
let secret_key = PKey::hmac(&base64::decode(&pair.secret_key)?)?;
Some(Keys {
api_key: pair.api_key,
secret_key: Arc::new(secret_key),
pass_phrase: pair.pass_phrase,
})
},
None => None,
};
let http_client = hyper::Client::builder().build::<_, hyper::Body>(
hyper_tls::HttpsConnector::new(2)?
);
let mut client = Client {
params,
keys,
order_ids: Arc::new(CHashMap::new()),
symbols: HashMap::new(),
http_client,
};
use tokio::runtime::current_thread;
debug!("requesting symbols");
client.symbols = current_thread::Runtime::new()?
.block_on(client.get_symbols())?;
debug!("received symbols");
Ok(client)
}
}
impl ApiClient for Client {
type Stream = futures::sync::mpsc::UnboundedReceiver<Notification>;
fn find_symbol(&self, symbol: &str) -> Option<Symbol> {
self.symbols.get(&symbol.to_lowercase()).cloned()
}
fn stream_with_flags(&self, symbol: Symbol, flags: NotificationFlags) -> Self::Stream {
self.new_stream(symbol, flags)
}
fn order(&self, order: WithSymbol<&Order>)
-> Box<dyn Future<Item = Timestamped<OrderAck>, Error = api::errors::OrderError> + Send + 'static>
{
Box::new(self.order_impl(order))
}
fn cancel(&self, cancel: WithSymbol<&Cancel>)
-> Box<dyn Future<Item = Timestamped<CancelAck>, Error = api::errors::CancelError> + Send + 'static>
{
self.cancel_impl(cancel)
}
fn ping(&self)
-> Box<dyn Future<Item = Timestamped<()>, Error = api::errors::Error> + Send + 'static>
{
Box::new(Ok(().timestamped()).into_future())
}
fn balances(&self)
-> Box<dyn Future<Item = Balances, Error = api::errors::Error> + Send + 'static>
{
Box::new(self.balances_impl())
}
}
impl GenerateOrderId for Client {
fn new_order_id(_: &str) -> String {
use uuid::Uuid;
Uuid::new_v4().to_string()
}
}