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
//! Binance exchange private API client.

pub mod parse;

use std::collections::BTreeMap;
use std::convert::TryFrom;

use async_trait::async_trait;
use chrono::offset::Utc;
use hex::encode as hex_encode;
use hyper::{body::Buf, Body, Method, Request};
use ring::hmac;

use cxmr_api::Account;
use cxmr_api_clients_errors::Error;
use cxmr_balances::price;
use cxmr_currency::CurrencyPair;
use cxmr_exchanges::{AccountInfo, Exchange, ExchangeOrder, MarketOrder, OrderExecution};
use cxmr_http_client::{send_request, BufExt, HttpsClient};

use self::parse::{order_side_to_str, order_time_to_str, BncAccount, BncOrder, BncUserDataStream};
use crate::public::{parse::order_type_to_str, BINANCE_HOST};

/// Binance API request parameters map.
pub type RequestParams = BTreeMap<String, String>;

/// Binance API client struct.
pub struct PrivateClient {
    account: Account,
    client: HttpsClient,
    signer: hmac::Key,
}

#[async_trait]
impl cxmr_api::PrivateClient for PrivateClient {
    type Error = Error;

    fn exchange(&self) -> &'static Exchange {
        &Exchange::Binance
    }

    fn account(&self) -> &Account {
        &self.account
    }

    async fn account_info(&self) -> Result<AccountInfo, Self::Error> {
        let req = self.build_signed_request(Method::GET, "/api/v3/account", None)?;
        let body = send_request(&self.client, req).await?;
        let account: BncAccount = ::serde_json::from_reader(body.reader())?;
        let mut account = AccountInfo::try_from(account)?;
        account.account = self.account().name.clone();
        Ok(account)
    }

    async fn open_orders(&self, pair: CurrencyPair) -> Result<Vec<MarketOrder>, Self::Error> {
        let mut params = RequestParams::new();
        params.insert("symbol".to_string(), pair.join_no_sep());
        params.insert("recvWindow".into(), "60000".to_string());
        let req = self.build_signed_request(Method::GET, "/api/v3/openOrders", Some(params))?;
        let body = send_request(&self.client, req).await?;
        let orders: Vec<BncOrder> = ::serde_json::from_reader(body.reader())?;
        orders
            .into_iter()
            .map(move |order| order.market_order(pair.clone()))
            .collect::<Result<Vec<MarketOrder>, _>>()
    }

    async fn create_order(&self, order: &ExchangeOrder) -> Result<OrderExecution, Self::Error> {
        let mut params = RequestParams::new();
        params.insert("symbol".to_string(), order.pair.join_no_sep());
        params.insert(
            "side".to_string(),
            order_side_to_str(&order.side).to_string(),
        );
        params.insert(
            "type".to_string(),
            order_type_to_str(&order.kind).to_string(),
        );
        params.insert(
            "timeInForce".to_string(),
            order_time_to_str(&order.time).to_string(),
        );
        if let Some(rate) = order.rate {
            params.insert("rate".to_string(), price(rate).to_string());
        }
        params.insert("amount".to_string(), price(order.amount).to_string());
        let req = self.build_signed_request(Method::POST, "/api/v1/order", Some(params))?;
        let body = send_request(&self.client, req).await?;
        let account: BncOrder = ::serde_json::from_reader(body.reader())?;
        let account = OrderExecution::try_from(account)?;
        Ok(account)
    }

    async fn user_data_stream(&self, key: Option<String>) -> Result<String, Self::Error> {
        match key {
            None => self.user_data_stream_new().await,
            Some(key) => self.user_data_stream_renew(key).await,
        }
    }
}

impl PrivateClient {
    pub fn new(account: Account, client: HttpsClient) -> Self {
        let signer = hmac::Key::new(hmac::HMAC_SHA256, account.credentials.secret.as_bytes());
        PrivateClient {
            account: account,
            client: client,
            signer: signer,
        }
    }

    async fn user_data_stream_new(&self) -> Result<String, Error> {
        let req = self.build_request(Method::POST, "/api/v1/userDataStream", None)?;
        let body = send_request(&self.client, req).await?;
        let data: BncUserDataStream = ::serde_json::from_reader(body.reader())?;
        Ok(data.into())
    }

    async fn user_data_stream_renew(&self, key: String) -> Result<String, Error> {
        let mut params = RequestParams::new();
        params.insert("listenKey".to_string(), key.clone());
        let req = self.build_request(Method::PUT, "/api/v1/userDataStream", Some(params))?;
        let body = send_request(&self.client, req).await?;
        Ok(String::from_utf8_lossy(body.bytes()).to_owned().to_string())
    }

    fn build_request(
        &self,
        m: Method,
        endpoint: &str,
        params: Option<RequestParams>,
    ) -> Result<Request<Body>, cxmr_http_client::Error> {
        let uri = match params {
            Some(params) => {
                let request = join_params(params);
                format!("https://{}{}?{}", BINANCE_HOST, endpoint, request)
            }
            None => format!("https://{}{}", BINANCE_HOST, endpoint),
        };
        Request::builder()
            .uri(uri)
            .method(m)
            .header("X-MBX-APIKEY", self.account.credentials.key.clone())
            .body(Body::empty())
            .map_err(|e| e.into())
    }

    fn build_signed_request(
        &self,
        m: Method,
        endpoint: &str,
        params: Option<RequestParams>,
    ) -> Result<Request<Body>, cxmr_http_client::Error> {
        let uri = self.sign_path(endpoint, params);
        Request::builder()
            .uri(uri)
            .method(m)
            .header("X-MBX-APIKEY", self.account.credentials.key.clone())
            .body(Body::empty())
            .map_err(|e| e.into())
    }

    fn sign_path(&self, endpoint: &str, params: Option<RequestParams>) -> String {
        let mut params = params.unwrap_or_else(|| RequestParams::new());
        params.insert(
            "timestamp".into(),
            Utc::now().timestamp_millis().to_string(),
        );
        let request = join_params(params);
        let signature = hex_encode(hmac::sign(&self.signer, request.as_bytes()).as_ref());
        format!(
            "https://{}{}?{}&signature={}",
            BINANCE_HOST, endpoint, request, signature
        )
    }
}

impl Clone for PrivateClient {
    fn clone(&self) -> Self {
        PrivateClient::new(self.account.clone(), self.client.clone())
    }
}

fn join_params(params: RequestParams) -> String {
    let mut request = String::new();
    for (key, value) in &params {
        let param = format!("{}={}&", key, value);
        request.push_str(param.as_ref());
    }
    request.pop(); // remove last '&'
    request
}

#[cfg(test)]
mod test {
    use super::*;

    use cxmr_currency::CurrencyPair;
    use cxmr_http_client::build_https_client;

    use tokio::runtime::Runtime;

    #[test]
    fn invalid_credentials_error() {
        let http_client = build_https_client().unwrap();
        let account = Account::new(
            "invalid".to_string(),
            "test-secret".to_string(),
            Exchange::Binance,
        );
        let client = PrivateClient::new(account, http_client);
        let mut runtime = Runtime::new().unwrap();
        let req = cxmr_api::PrivateClient::open_orders(
            &client,
            CurrencyPair::split_reversed("TEST_PAIR").unwrap(),
        );
        let resp = runtime.block_on(req);
        match resp {
            Ok(_) => panic!("unexpected response"),
            Err(Error::Http(cxmr_http_client::Error::Response(
                cxmr_http_client::ErrorResponse { status, .. },
            ))) => assert_eq!(status, 401),
            Err(e) => panic!("unexpecter error: {:?}", e),
        }
    }
}