nash_protocol/protocol/list_trades/
types.rs

1use std::sync::Arc;
2use async_trait::async_trait;
3use tokio::sync::RwLock;
4use crate::errors::Result;
5use crate::graphql::list_trades;
6use crate::types::Trade;
7use super::super::list_markets::ListMarketsRequest;
8use super::super::hooks::{ProtocolHook, NashProtocolRequest};
9use super::super::{
10    NashProtocol, ResponseOrError, serializable_to_json, State, try_response_with_state_from_json,
11};
12
13/// Get trades associated with market, filtering on several optional fields.
14#[derive(Clone, Debug)]
15pub struct ListTradesRequest {
16    pub market: String,
17    /// max trades to return
18    pub limit: Option<i64>,
19    /// page before if using pagination
20    pub before: Option<String>,
21}
22
23/// List of trades as well as an optional link to the next page of data.
24#[derive(Debug)]
25pub struct ListTradesResponse {
26    pub trades: Vec<Trade>,
27    pub next_page: Option<String>,
28}
29
30#[async_trait]
31impl NashProtocol for ListTradesRequest {
32    type Response = ListTradesResponse;
33
34    async fn graphql(&self, _state: Arc<RwLock<State>>) -> Result<serde_json::Value> {
35        let query = self.make_query();
36        serializable_to_json(&query)
37    }
38
39    async fn response_from_json(
40        &self,
41        response: serde_json::Value,
42        state: Arc<RwLock<State>>
43    ) -> Result<ResponseOrError<Self::Response>> {
44        try_response_with_state_from_json::<ListTradesResponse, list_trades::ResponseData>(response, state).await
45    }
46
47    async fn run_before(&self, state: Arc<RwLock<State>>) -> Result<Option<Vec<ProtocolHook>>> {
48        let state = state.read().await;
49        let mut hooks = Vec::new();
50        if let None = state.markets {
51            hooks.push(ProtocolHook::Protocol(NashProtocolRequest::ListMarkets(
52                ListMarketsRequest,
53            )))
54        }
55        Ok(Some(hooks))
56    }
57}