nash_protocol/protocol/orderbook/
response.rs

1use super::super::{DataResponse, ResponseOrError};
2use super::types::{OrderbookRequest, OrderbookResponse};
3use crate::errors::Result;
4use crate::graphql::get_orderbook;
5use crate::types::OrderbookOrder;
6use crate::protocol::state::State;
7use std::sync::Arc;
8use tokio::sync::RwLock;
9use bigdecimal::BigDecimal;
10use std::str::FromStr;
11
12
13impl OrderbookRequest {
14    pub async fn response_from_graphql(
15        &self,
16        response: ResponseOrError<get_orderbook::ResponseData>, _state: Arc<RwLock<State>>
17    ) -> Result<ResponseOrError<OrderbookResponse>> {
18        match response {
19            ResponseOrError::Response(data) => {
20                let response = data.data;
21                let book = response.get_order_book;
22                let mut asks = Vec::new();
23                let mut bids = Vec::new();
24                let update_id = book.update_id;
25                let last_update_id = book.last_update_id;
26                for ask in book.asks {
27                    asks.push(OrderbookOrder {
28                        price: ask.price.amount.to_string(),
29                        amount: BigDecimal::from_str(&ask.amount.amount)?,
30                    });
31                }
32                for bid in book.bids {
33                    bids.push(OrderbookOrder {
34                        price: bid.price.amount.to_string(),
35                        amount: BigDecimal::from_str(&bid.amount.amount)?,
36                    });
37                }
38                Ok(ResponseOrError::Response(DataResponse {
39                    data: OrderbookResponse {
40                        asks,
41                        bids,
42                        update_id,
43                        last_update_id
44                    },
45                }))
46            }
47            ResponseOrError::Error(error) => Ok(ResponseOrError::Error(error)),
48        }
49    }
50}