Skip to main content

nash_protocol/protocol/get_ticker/
response.rs

1use super::super::{DataResponse, ResponseOrError};
2use super::types::{TickerRequest, TickerResponse};
3use crate::errors::Result;
4use crate::graphql::get_ticker;
5use crate::protocol::state::State;
6use tokio::sync::RwLock;
7use std::sync::Arc;
8use bigdecimal::BigDecimal;
9use std::str::FromStr;
10
11impl TickerRequest {
12    pub async fn response_from_graphql(
13        &self,
14        response: ResponseOrError<get_ticker::ResponseData>,
15        _state: Arc<RwLock<State>>,
16    ) -> Result<ResponseOrError<TickerResponse>> {
17        // These unwraps are safe. ME_FIXME
18        match response {
19            ResponseOrError::Response(data) => {
20                let response = data.data;
21                let ticker = response.get_ticker;
22
23                let high_price_24h = match &ticker.high_price24h {
24                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
25                    None => None,
26                };
27                let low_price_24h = match &ticker.low_price24h {
28                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
29                    None => None,
30                };
31                let last_price = match &ticker.last_price {
32                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
33                    None => None,
34                };
35                let price_change_24h = match &ticker.price_change24h {
36                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
37                    None => None,
38                };
39                let best_ask_amount = match &ticker.best_ask_size {
40                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
41                    None => None,
42                };
43                let best_ask_price = match &ticker.best_ask_price {
44                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
45                    None => None,
46                };
47                let best_bid_amount = match &ticker.best_bid_size {
48                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
49                    None => None,
50                };
51                let best_bid_price = match &ticker.best_bid_price {
52                    Some(price) => Some(BigDecimal::from_str(&price.amount)?),
53                    None => None,
54                };
55
56                let converted_ticker = TickerResponse {
57                    id: ticker.id.clone(),
58                    market_name: ticker.market_name.clone(),
59                    a_volume_24h: BigDecimal::from_str(&ticker.a_volume24h.amount)?,
60                    b_volume_24h: BigDecimal::from_str(&ticker.b_volume24h.amount)?,
61                    high_price_24h,
62                    low_price_24h,
63                    last_price,
64                    price_change_24h,
65                    best_ask_amount,
66                    best_ask_price,
67                    best_bid_amount,
68                    best_bid_price,
69                };
70
71                Ok(ResponseOrError::Response(DataResponse {
72                    data: converted_ticker,
73                }))
74            }
75            ResponseOrError::Error(error) => Ok(ResponseOrError::Error(error)),
76        }
77    }
78}