nash_protocol/protocol/list_account_orders/
response.rs

1use super::types::ListAccountOrdersResponse;
2use crate::errors::{ProtocolError, Result};
3use crate::graphql::list_account_orders;
4use crate::types::{
5    AccountTradeSide, BuyOrSell, Order, OrderCancellationPolicy, OrderCancellationReason,
6    OrderStatus, OrderType, Trade,
7};
8use chrono::{DateTime, Utc};
9use bigdecimal::BigDecimal;
10use std::str::FromStr;
11use crate::protocol::traits::TryFromState;
12use crate::protocol::state::State;
13use std::sync::Arc;
14use tokio::sync::RwLock;
15use async_trait::async_trait;
16
17#[async_trait]
18impl TryFromState<list_account_orders::ResponseData> for ListAccountOrdersResponse {
19    async fn from(response: list_account_orders::ResponseData, _state: Arc<RwLock<State>>) -> Result<ListAccountOrdersResponse> {
20        // FIXME (if possible): there is significant duplication of code between here and the response handles for list_account_trades
21        // and get_account_order. unfortunately, given the way the graphql_client library works, it is not obvious how to make this
22        // response processing generic
23        let mut orders = Vec::new();
24        for order_data in response.list_account_orders.orders {
25            let market = order_data.market.name.clone();
26            let amount_placed = BigDecimal::from_str(&order_data.amount.amount)?;
27            let amount_remaining = BigDecimal::from_str(&order_data.amount_remaining.amount)?;
28            let amount_executed = BigDecimal::from_str(&order_data.amount_executed.amount)?;
29            let limit_price = match &order_data.limit_price {
30                Some(price) => Some(BigDecimal::from_str(&price.amount)?),
31                None => None,
32            };
33            let stop_price = match &order_data.stop_price {
34                Some(price) => Some(BigDecimal::from_str(&price.amount)?),
35                None => None,
36            };
37            let placed_at = DateTime::<Utc>::from_str(&order_data.placed_at)
38                .map_err(|_| ProtocolError("Could not convert value to DateTime"))?;
39            let mut trades = Vec::new();
40            for trade_data in order_data.trades.as_ref().unwrap() {
41                let trade_data = trade_data.as_ref().unwrap();
42                let market = trade_data.market.name.clone();
43                let taker_fee = BigDecimal::from_str(&trade_data.taker_fee.amount)?;
44                let maker_fee = BigDecimal::from_str(&trade_data.maker_fee.amount)?;
45                let amount = BigDecimal::from_str(&trade_data.amount.amount)?;
46                let maker_recieved = BigDecimal::from_str(&trade_data.maker_received.amount)?;
47                let taker_recieved = BigDecimal::from_str(&trade_data.taker_received.amount)?;
48                let limit_price = BigDecimal::from_str(&trade_data.limit_price.amount)?;
49                trades.push(Trade {
50                    market,
51                    amount,
52                    taker_fee,
53                    maker_fee,
54                    maker_recieved,
55                    taker_recieved,
56                    taker_order_id: trade_data.taker_order_id.clone(),
57                    maker_order_id: trade_data.maker_order_id.clone(),
58                    account_side: (&trade_data.account_side).into(),
59                    id: trade_data.id.clone(),
60                    executed_at: DateTime::<Utc>::from_str(&trade_data.executed_at)
61                        .map_err(|_| ProtocolError("Could not convert value to DateTime"))?,
62                    limit_price,
63                    direction: (&trade_data.direction).into(),
64                })
65            }
66            orders.push(Order {
67                id: order_data.id.clone(),
68                client_order_id: order_data.client_order_id.clone(),
69                market,
70                amount_placed,
71                amount_remaining,
72                amount_executed,
73                limit_price,
74                stop_price,
75                placed_at,
76                trades,
77                cancellation_policy: (&order_data).into(),
78                cancellation_reason: order_data.cancellation_reason.as_ref().map(|x| x.into()),
79                buy_or_sell: order_data.buy_or_sell.into(),
80                order_type: order_data.type_.into(),
81                status: order_data.status.into(),
82            })
83        }
84        Ok(ListAccountOrdersResponse {
85            orders: orders,
86            next_page: response.list_account_orders.next.clone(),
87        })
88    }
89}
90
91impl From<list_account_orders::OrderBuyOrSell> for BuyOrSell {
92    fn from(response: list_account_orders::OrderBuyOrSell) -> Self {
93        match response {
94            list_account_orders::OrderBuyOrSell::BUY => Self::Buy,
95            list_account_orders::OrderBuyOrSell::SELL => Self::Sell,
96            _ => panic!("Unexpected value in BuyOrSell enum"),
97        }
98    }
99}
100
101impl From<list_account_orders::OrderType> for OrderType {
102    fn from(response: list_account_orders::OrderType) -> Self {
103        match response {
104            list_account_orders::OrderType::MARKET => Self::Market,
105            list_account_orders::OrderType::LIMIT => Self::Limit,
106            list_account_orders::OrderType::STOP_MARKET => Self::StopMarket,
107            list_account_orders::OrderType::STOP_LIMIT => Self::StopLimit,
108            _ => panic!("Unexpected value in OrderType enum"),
109        }
110    }
111}
112
113impl From<list_account_orders::OrderStatus> for OrderStatus {
114    fn from(response: list_account_orders::OrderStatus) -> Self {
115        match response {
116            list_account_orders::OrderStatus::PENDING => OrderStatus::Pending,
117            list_account_orders::OrderStatus::OPEN => OrderStatus::Open,
118            list_account_orders::OrderStatus::CANCELLED => OrderStatus::Canceled,
119            list_account_orders::OrderStatus::FILLED => OrderStatus::Filled,
120            _ => panic!("Unexpected value in OrderStatus enum"),
121        }
122    }
123}
124
125impl From<&list_account_orders::OrderCancellationReason> for OrderCancellationReason {
126    fn from(cancellation_reason: &list_account_orders::OrderCancellationReason) -> Self {
127        match cancellation_reason {
128            list_account_orders::OrderCancellationReason::ADMIN_CANCELED => {
129                OrderCancellationReason::AdminCancelled
130            }
131            list_account_orders::OrderCancellationReason::EXPIRATION => {
132                OrderCancellationReason::Expiration
133            }
134            list_account_orders::OrderCancellationReason::USER => OrderCancellationReason::User,
135            list_account_orders::OrderCancellationReason::NO_FILL => {
136                OrderCancellationReason::NoFill
137            }
138            list_account_orders::OrderCancellationReason::INVALID_FOR_ORDERBOOK_STATE => {
139                OrderCancellationReason::InvalidForOrderbookState
140            }
141            _ => panic!("Unsupported OrderCancellationReason"),
142        }
143    }
144}
145
146impl From<&list_account_orders::ListAccountOrdersListAccountOrdersOrders>
147    for Option<OrderCancellationPolicy>
148{
149    fn from(order: &list_account_orders::ListAccountOrdersListAccountOrdersOrders) -> Self {
150        if let Some(cancellation_policy) = &order.cancellation_policy {
151            Some(match cancellation_policy {
152                list_account_orders::OrderCancellationPolicy::FILL_OR_KILL => {
153                    OrderCancellationPolicy::FillOrKill
154                }
155                list_account_orders::OrderCancellationPolicy::GOOD_TIL_CANCELLED => {
156                    OrderCancellationPolicy::GoodTilCancelled
157                }
158                list_account_orders::OrderCancellationPolicy::GOOD_TIL_TIME => {
159                    // This unwrap is safe
160                    let cancel_at =
161                        DateTime::<Utc>::from_str(&order.cancel_at.as_ref().unwrap()).unwrap();
162                    OrderCancellationPolicy::GoodTilTime(cancel_at)
163                }
164                list_account_orders::OrderCancellationPolicy::IMMEDIATE_OR_CANCEL => {
165                    OrderCancellationPolicy::ImmediateOrCancel
166                }
167                _ => panic!("Unsupported OrderCancellationPolicy"),
168            })
169        } else {
170            None
171        }
172    }
173}
174
175impl From<&list_account_orders::AccountTradeSide> for AccountTradeSide {
176    fn from(trade_side: &list_account_orders::AccountTradeSide) -> Self {
177        match trade_side {
178            list_account_orders::AccountTradeSide::MAKER => AccountTradeSide::Maker,
179            list_account_orders::AccountTradeSide::TAKER => AccountTradeSide::Taker,
180            list_account_orders::AccountTradeSide::NONE => AccountTradeSide::None,
181            _ => panic!("Unsupported value in AccountTradeSide"),
182        }
183    }
184}
185
186impl From<&list_account_orders::Direction> for BuyOrSell {
187    fn from(response: &list_account_orders::Direction) -> Self {
188        match response {
189            list_account_orders::Direction::BUY => Self::Buy,
190            list_account_orders::Direction::SELL => Self::Sell,
191            _ => panic!("Unexpected value in BuyOrSell enum"),
192        }
193    }
194}