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
use failure::Error;
use futures::Future;

use client::Binance;
use error::{BinanceError, Result};
use model::{AccountInformation, Balance, Order, OrderCanceled, TradeHistory, Transaction};
use transport::Dummy;

static ORDER_TYPE_LIMIT: &'static str = "LIMIT";
static ORDER_TYPE_MARKET: &'static str = "MARKET";
static ORDER_SIDE_BUY: &'static str = "BUY";
static ORDER_SIDE_SELL: &'static str = "SELL";
static TIME_IN_FORCE_GTC: &'static str = "GTC";

static API_V3_ORDER: &'static str = "/api/v3/order";

struct OrderRequest {
    pub symbol: String,
    pub qty: f64,
    pub price: f64,
    pub order_side: String,
    pub order_type: String,
    pub time_in_force: String,
}

impl Binance {
    // Account Information
    pub fn get_account(&self) -> Result<impl Future<Item = AccountInformation, Error = Error>> {
        let account_info = self.transport.signed_get::<_, Dummy, _, _>("/api/v3/account", None)?;
        Ok(account_info)
    }

    // Balance for ONE Asset
    pub fn get_balance(&self, asset: &str) -> Result<impl Future<Item = Balance, Error = Error>> {
        let asset = asset.to_string();
        let search = move |account: AccountInformation| -> Result<Balance> {
            let balance = account.balances.into_iter().find(|balance| balance.asset == asset);
            Ok(balance.ok_or(BinanceError::AssetsNotFound)?)
        };

        let balance = self.get_account()?.and_then(search);
        Ok(balance)
    }

    // Current open orders for ONE symbol
    pub fn get_open_orders(&self, symbol: &str) -> Result<impl Future<Item = Vec<Order>, Error = Error>> {
        let orders = self.transport.signed_get("/api/v3/openOrders", Some(vec![("symbol", symbol.to_string())]))?;
        Ok(orders)
    }

    // All current open orders
    pub fn get_all_open_orders(&self) -> Result<impl Future<Item = Vec<Order>, Error = Error>> {
        let orders = self.transport.signed_get::<_, Dummy, _, _>("/api/v3/openOrders", None)?;
        Ok(orders)
    }

    // Check an order's status
    pub fn order_status(&self, symbol: &str, order_id: u64) -> Result<impl Future<Item = Order, Error = Error>> {
        let params = vec![("symbol", symbol.to_string()), ("orderId", order_id.to_string())];
        let order = self.transport.signed_get(API_V3_ORDER, Some(params))?;
        Ok(order)
    }

    // Place a LIMIT order - BUY
    pub fn limit_buy(&self, symbol: &str, qty: f64, price: f64) -> Result<impl Future<Item = Transaction, Error = Error>> {
        let buy: OrderRequest = OrderRequest {
            symbol: symbol.into(),
            qty: qty.into(),
            price: price,
            order_side: ORDER_SIDE_BUY.to_string(),
            order_type: ORDER_TYPE_LIMIT.to_string(),
            time_in_force: TIME_IN_FORCE_GTC.to_string(),
        };
        let params = self.build_order(buy);

        let transaction = self.transport.signed_post(API_V3_ORDER, Some(params))?;

        Ok(transaction)
    }

    // Place a LIMIT order - SELL
    pub fn limit_sell(&self, symbol: &str, qty: f64, price: f64) -> Result<impl Future<Item = Transaction, Error = Error>> {
        let sell: OrderRequest = OrderRequest {
            symbol: symbol.into(),
            qty: qty.into(),
            price: price,
            order_side: ORDER_SIDE_SELL.to_string(),
            order_type: ORDER_TYPE_LIMIT.to_string(),
            time_in_force: TIME_IN_FORCE_GTC.to_string(),
        };
        let params = self.build_order(sell);
        let transaction = self.transport.signed_post(API_V3_ORDER, Some(params))?;

        Ok(transaction)
    }

    // Place a MARKET order - BUY
    pub fn market_buy(&self, symbol: &str, qty: f64) -> Result<impl Future<Item = Transaction, Error = Error>> {
        let buy: OrderRequest = OrderRequest {
            symbol: symbol.into(),
            qty: qty.into(),
            price: 0.0,
            order_side: ORDER_SIDE_BUY.to_string(),
            order_type: ORDER_TYPE_MARKET.to_string(),
            time_in_force: TIME_IN_FORCE_GTC.to_string(),
        };
        let params = self.build_order(buy);
        let transaction = self.transport.signed_post(API_V3_ORDER, Some(params))?;

        Ok(transaction)
    }

    // Place a MARKET order - SELL
    pub fn market_sell(&self, symbol: &str, qty: f64) -> Result<impl Future<Item = Transaction, Error = Error>> {
        let sell: OrderRequest = OrderRequest {
            symbol: symbol.into(),
            qty: qty.into(),
            price: 0.0,
            order_side: ORDER_SIDE_SELL.to_string(),
            order_type: ORDER_TYPE_MARKET.to_string(),
            time_in_force: TIME_IN_FORCE_GTC.to_string(),
        };
        let params = self.build_order(sell);
        let transaction = self.transport.signed_post(API_V3_ORDER, Some(params))?;
        Ok(transaction)
    }

    // Check an order's status
    pub fn cancel_order(&self, symbol: &str, order_id: u64) -> Result<impl Future<Item = OrderCanceled, Error = Error>> {
        let params = vec![("symbol", symbol.to_string()), ("orderId", order_id.to_string())];
        let order_canceled = self.transport.signed_delete(API_V3_ORDER, Some(params))?;
        Ok(order_canceled)
    }

    // Trade history
    pub fn trade_history(&self, symbol: &str) -> Result<impl Future<Item = TradeHistory, Error = Error>> {
        let params = vec![(("symbol", symbol.to_string()))];
        let trade_history = self.transport.signed_get("/api/v3/myTrades", Some(params))?;

        Ok(trade_history)
    }

    fn build_order(&self, order: OrderRequest) -> Vec<(&'static str, String)> {
        let mut params = vec![
            ("symbol", order.symbol),
            ("side", order.order_side),
            ("type", order.order_type),
            ("quantity", order.qty.to_string()),
        ];

        if order.price != 0.0 {
            params.push(("price", order.price.to_string()));
            params.push(("timeInForce", order.time_in_force));
        }

        params
    }
}