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
use super::super::super::{DataResponse, ResponseOrError};
use super::request::SubscribeOrderbook;
use crate::errors::Result;
use crate::graphql::updated_orderbook;
use crate::types::OrderbookOrder;

/// Order book updates pushed over a subscription consist of a list of bid orders and
/// a list of ask orders.
#[derive(Clone, Debug)]
pub struct SubscribeOrderbookResponse {
    pub bids: Vec<OrderbookOrder>,
    pub asks: Vec<OrderbookOrder>,
}

// FIXME: if possible, remove duplication with orderbook query
impl SubscribeOrderbook {
    pub fn response_from_graphql(
        &self,
        response: ResponseOrError<updated_orderbook::ResponseData>,
    ) -> Result<ResponseOrError<SubscribeOrderbookResponse>> {
        match response {
            ResponseOrError::Response(data) => {
                let response = data.data;
                let book = response.updated_order_book;
                let mut asks = Vec::new();
                let mut bids = Vec::new();
                for ask in book.asks {
                    asks.push(OrderbookOrder {
                        price: ask.price.amount.to_string(),
                        amount: self.market.asset_a.with_amount(&ask.amount.amount)?,
                    });
                }
                for bid in book.bids {
                    bids.push(OrderbookOrder {
                        price: bid.price.amount.to_string(),
                        amount: self.market.asset_a.with_amount(&bid.amount.amount)?,
                    });
                }
                Ok(ResponseOrError::Response(DataResponse {
                    data: SubscribeOrderbookResponse {
                        asks: asks,
                        bids: bids,
                    },
                }))
            }
            ResponseOrError::Error(error) => Ok(ResponseOrError::Error(error)),
        }
    }
}