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

use std::str::FromStr;
use bigdecimal::BigDecimal;

pub struct OrderRequest<'a> {
    pub symbol: &'a str,
    pub instruction: &'a str,
    pub buy_or_sell: &'static str,
    pub price: BigDecimal,
    pub amount: BigDecimal,
}

pub struct OrderQuery {
    pub symbol: String,
    pub states: String,
    pub before: Option<u16>,
    pub after: Option<u16>,
    pub limit: Option<u16>,
}

#[derive(Deserialize, Debug, Eq, PartialEq)]
pub struct OrderInfo {
    pub id: String,
    pub symbol: String,
    #[serde(rename = "type")]
    pub instruction: String,
    pub side: String,
    pub price: String,
    pub amount: String,
    pub state: String,
    pub executed_value: String,
    pub fill_fees: String,
    pub filled_amount: String,
    pub created_at: u64,
    pub source: String,
}

#[derive(Deserialize, Debug, Eq, PartialEq)]
pub struct MatchResult {
    pub price: String,
    pub fill_fees: String,
    pub filled_amount: String,
    pub side: Option<String>,
    #[serde(rename = "type")]
    pub instruction: String,
    pub created_at: u64,
}

#[derive(Deserialize, Debug, PartialEq)]
pub struct Orderbook {
    pub bids: Vec<f64>,
    pub asks: Vec<f64>,
    pub ts: i64,
    pub seq: i64,
    #[serde(rename = "type")]
    pub order_type: String,
}

impl<'a> OrderRequest<'a> {
    pub fn sell_limit(symbol: &'a str, amount: BigDecimal, price: BigDecimal) -> OrderRequest<'a> {
        OrderRequest {
            symbol: symbol,
            instruction: "limit",
            price: price,
            amount: amount,
            buy_or_sell: "sell",
        }
    }

    pub fn sell_market(symbol: &'a str, amount: BigDecimal) -> OrderRequest<'a> {
        OrderRequest {
            symbol: symbol,
            instruction: "market",
            price: BigDecimal::from_str("0.0").unwrap(),
            amount: amount,
            buy_or_sell: "sell",
        }
    }

    pub fn buy_limit(symbol: &'a str, amount: BigDecimal, price: BigDecimal) -> OrderRequest<'a> {
        OrderRequest {
            symbol: symbol,
            instruction: "limit",
            price: price,
            amount: amount,
            buy_or_sell: "buy",
        }
    }

    pub fn buy_market(symbol: &'a str, amount: BigDecimal) -> OrderRequest<'a> {
        OrderRequest {
            symbol: symbol,
            instruction: "market",
            price: BigDecimal::from_str("0.0").unwrap(),
            amount: amount,
            buy_or_sell: "buy",
        }
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn test_order() {
        // let buy_limit = super::OrderRequest::buy_limit(
        //     "usdtbtc",
        //     BigDecimal::from_str("100.0").unwrap(),
        //     BigDecimal::from_str("1.00000").unwrap(),
        // );
        // let sell_limit = super::OrderRequest::sell_limit(
        //     "usdtbtc",
        //     BigDecimal::from_str("101.00").unwrap(),
        //     BigDecimal::from_str("0.99999").unwrap(),
        // );
        // let buy_market =
        //     super::OrderRequest::buy_market("usdtbtc", BigDecimal::from_str("1.2000").unwrap());
        // let sell_market =
        //     super::OrderRequest::sell_market("usdtbtc", BigDecimal::from_str("1.09999").unwrap());
    }
}