Skip to main content

nash_protocol/protocol/subscriptions/updated_ticker/
response.rs

1use super::super::super::{DataResponse, ResponseOrError};
2use super::request::SubscribeTicker;
3use crate::errors::Result;
4use crate::graphql::updated_ticker;
5use crate::protocol::state::State;
6use bigdecimal::BigDecimal;
7use tokio::sync::RwLock;
8use std::str::FromStr;
9use std::sync::Arc;
10
11/// Order book updates pushed over a subscription consist of a list of bid orders and
12/// a list of ask orders.
13#[derive(Clone, Debug)]
14pub struct SubscribeTickerResponse {
15    pub id: String,
16    pub a_volume_24h: BigDecimal,
17    pub b_volume_24h: BigDecimal,
18    pub best_ask_price: Option<BigDecimal>,
19    pub best_bid_price: Option<BigDecimal>,
20    pub best_ask_amount: Option<BigDecimal>,
21    pub best_bid_amount: Option<BigDecimal>,
22    pub high_price_24h: Option<BigDecimal>,
23    pub last_price: Option<BigDecimal>,
24    pub low_price_24h: Option<BigDecimal>,
25    pub price_change_24h: Option<BigDecimal>,
26    pub market_name: String,
27}
28
29// FIXME: if possible, remove duplication with orderbook query
30impl SubscribeTicker {
31    pub async fn response_from_graphql(
32        &self,
33        response: ResponseOrError<updated_ticker::ResponseData>,
34        _state: Arc<RwLock<State>>,
35    ) -> Result<ResponseOrError<SubscribeTickerResponse>> {
36        match response {
37            ResponseOrError::Response(data) => {
38                let response = data.data;
39                let ticker = response.updated_ticker;
40
41                let high_price_24h = match &ticker.high_price24h {
42                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
43                    None => None,
44                };
45                let low_price_24h = match &ticker.low_price24h {
46                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
47                    None => None,
48                };
49                let last_price = match &ticker.last_price {
50                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
51                    None => None,
52                };
53                let price_change_24h = match &ticker.price_change24h {
54                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
55                    None => None,
56                };
57                let best_ask_amount = match &ticker.best_ask_size {
58                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
59                    None => None,
60                };
61                let best_ask_price = match &ticker.best_ask_price {
62                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
63                    None => None,
64                };
65                let best_bid_amount = match &ticker.best_bid_size {
66                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
67                    None => None,
68                };
69                let best_bid_price = match &ticker.best_bid_price {
70                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
71                    None => None,
72                };
73
74                let converted_ticker = SubscribeTickerResponse {
75                    id: ticker.id.clone(),
76                    market_name: ticker.market_name.clone(),
77                    a_volume_24h: BigDecimal::from_str(&ticker.a_volume24h.amount)?,
78                    b_volume_24h: BigDecimal::from_str(&ticker.b_volume24h.amount)?,
79                    high_price_24h,
80                    low_price_24h,
81                    last_price,
82                    price_change_24h,
83                    best_ask_amount,
84                    best_ask_price,
85                    best_bid_amount,
86                    best_bid_price,
87                };
88
89                Ok(ResponseOrError::Response(DataResponse {
90                    data: converted_ticker,
91                }))
92            }
93            ResponseOrError::Error(error) => Ok(ResponseOrError::Error(error)),
94        }
95    }
96}