nash_protocol/protocol/place_order/
response.rs

1use super::types::PlaceOrderResponse;
2use crate::graphql::place_limit_order;
3use crate::graphql::place_market_order;
4use crate::types::{BuyOrSell, OrderStatus, OrderType};
5use chrono::{DateTime, Utc};
6use std::str::FromStr;
7use crate::protocol::place_order::types::MarketName;
8
9impl From<place_limit_order::ResponseData> for PlaceOrderResponse {
10    fn from(response: place_limit_order::ResponseData) -> Self {
11        let response = response.place_limit_order;
12        Self {
13            status: response.status.into(),
14            order_id: response.id,
15            remaining_orders: response.orders_till_sign_state as u64,
16            placed_at: DateTime::<Utc>::from_str(&response.placed_at)
17                .expect("ME returned invalid placed_at DateTime for PlaceOrderResponse"),
18            order_type: response.type_.into(),
19            buy_or_sell: response.buy_or_sell.into(),
20            market: MarketName {
21                name: response.market.name.clone()
22            }
23        }
24    }
25}
26
27impl From<place_limit_order::OrderStatus> for OrderStatus {
28    fn from(status: place_limit_order::OrderStatus) -> Self {
29        match status {
30            place_limit_order::OrderStatus::PENDING => Self::Pending,
31            place_limit_order::OrderStatus::CANCELLED => Self::Canceled,
32            place_limit_order::OrderStatus::OPEN => Self::Open,
33            place_limit_order::OrderStatus::FILLED => Self::Filled,
34            // This should never happen. Seems to be generated by the the rust graphql library, not the schema
35            place_limit_order::OrderStatus::Other(_) => {
36                panic!("Order status set to something invalid")
37            }
38        }
39    }
40}
41
42impl From<place_limit_order::OrderBuyOrSell> for BuyOrSell {
43    fn from(response: place_limit_order::OrderBuyOrSell) -> Self {
44        match response {
45            place_limit_order::OrderBuyOrSell::BUY => Self::Buy,
46            place_limit_order::OrderBuyOrSell::SELL => Self::Sell,
47            _ => panic!("Unexpected value in BuyOrSell enum"),
48        }
49    }
50}
51
52impl From<place_limit_order::OrderType> for OrderType {
53    fn from(response: place_limit_order::OrderType) -> Self {
54        match response {
55            place_limit_order::OrderType::MARKET => Self::Market,
56            place_limit_order::OrderType::LIMIT => Self::Limit,
57            place_limit_order::OrderType::STOP_MARKET => Self::StopMarket,
58            place_limit_order::OrderType::STOP_LIMIT => Self::StopLimit,
59            _ => panic!("Unexpected value in OrderType enum"),
60        }
61    }
62}
63
64
65
66impl From<place_market_order::ResponseData> for PlaceOrderResponse {
67    fn from(response: place_market_order::ResponseData) -> Self {
68        let response = response.place_market_order;
69        Self {
70            status: response.status.into(),
71            order_id: response.id,
72            remaining_orders: response.orders_till_sign_state as u64,
73            placed_at: DateTime::<Utc>::from_str(&response.placed_at)
74                .expect("ME returned invalid placed_at DateTime for PlaceOrderResponse"),
75            order_type: OrderType::Market,
76            buy_or_sell: response.buy_or_sell.into(),
77            market: MarketName {
78                name: response.market.name.clone()
79            },
80        }
81    }
82}
83
84impl From<place_market_order::OrderStatus> for OrderStatus {
85    fn from(status: place_market_order::OrderStatus) -> Self {
86        match status {
87            place_market_order::OrderStatus::PENDING => Self::Pending,
88            place_market_order::OrderStatus::CANCELLED => Self::Canceled,
89            place_market_order::OrderStatus::OPEN => Self::Open,
90            place_market_order::OrderStatus::FILLED => Self::Filled,
91            // This should never happen. Seems to be generated by the the rust graphql library, not the schema
92            place_market_order::OrderStatus::Other(_) => {
93                panic!("Order status set to something invalid")
94            }
95        }
96    }
97}
98
99impl From<place_market_order::OrderBuyOrSell> for BuyOrSell {
100    fn from(response: place_market_order::OrderBuyOrSell) -> Self {
101        match response {
102            place_market_order::OrderBuyOrSell::BUY => Self::Buy,
103            place_market_order::OrderBuyOrSell::SELL => Self::Sell,
104            _ => panic!("Unexpected value in BuyOrSell enum"),
105        }
106    }
107}