nash_protocol/protocol/orderbook/
types.rs

1use crate::errors::Result;
2use crate::types::OrderbookOrder;
3use super::super::list_markets::ListMarketsRequest;
4use super::super::hooks::{ProtocolHook, NashProtocolRequest};
5use async_trait::async_trait;
6use tokio::sync::RwLock;
7use std::sync::Arc;
8
9use super::super::{
10    json_to_type_or_error, serializable_to_json, NashProtocol, ResponseOrError, State,
11};
12
13/// Get order book data for the provided market
14#[derive(Clone, Debug)]
15pub struct OrderbookRequest {
16    pub market: String,
17}
18
19/// An order book is a list of bid and ask orders
20#[derive(Debug)]
21pub struct OrderbookResponse {
22    pub last_update_id: i64,
23    pub update_id: i64,
24    pub asks: Vec<OrderbookOrder>,
25    pub bids: Vec<OrderbookOrder>,
26}
27
28#[async_trait]
29impl NashProtocol for OrderbookRequest {
30    type Response = OrderbookResponse;
31
32    async fn graphql(&self, _state: Arc<RwLock<State>>) -> Result<serde_json::Value> {
33        let query = self.make_query();
34        serializable_to_json(&query)
35    }
36
37    async fn response_from_json(
38        &self,
39        response: serde_json::Value,
40        state: Arc<RwLock<State>>
41    ) -> Result<ResponseOrError<Self::Response>> {
42        let as_graphql = json_to_type_or_error(response)?;
43        self.response_from_graphql(as_graphql, state).await
44    }
45
46    async fn run_before(&self, state: Arc<RwLock<State>>) -> Result<Option<Vec<ProtocolHook>>> {
47        let state = state.read().await;
48        let mut hooks = Vec::new();
49        if let None = state.markets {
50            hooks.push(ProtocolHook::Protocol(NashProtocolRequest::ListMarkets(
51                ListMarketsRequest,
52            )))
53        }
54        Ok(Some(hooks))
55    }
56}