1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::graphql;
use graphql::updated_account_orders;
use graphql_client::GraphQLQuery;
use crate::types::{BuyOrSell, OrderStatus, OrderType, DateTimeRange};

/// Initiate subscription to get new orders for an account
#[derive(Clone, Debug)]
pub struct SubscribeAccountOrders {
    pub market: Option<String>,
    pub buy_or_sell: Option<BuyOrSell>,
    pub status: Option<Vec<OrderStatus>>,
    pub order_type: Option<Vec<OrderType>>,
    pub range: Option<DateTimeRange>
}

impl From<BuyOrSell> for updated_account_orders::OrderBuyOrSell {
    fn from(buy_or_sell: BuyOrSell) -> Self {
        match buy_or_sell {
            BuyOrSell::Buy => updated_account_orders::OrderBuyOrSell::BUY,
            BuyOrSell::Sell => updated_account_orders::OrderBuyOrSell::SELL,
        }
    }
}

impl From<OrderStatus> for updated_account_orders::OrderStatus {
    fn from(status: OrderStatus) -> Self {
        match status {
            OrderStatus::Canceled => updated_account_orders::OrderStatus::CANCELLED,
            OrderStatus::Filled => updated_account_orders::OrderStatus::FILLED,
            OrderStatus::Open => updated_account_orders::OrderStatus::OPEN,
            OrderStatus::Pending => updated_account_orders::OrderStatus::PENDING,
        }
    }
}

impl From<OrderType> for updated_account_orders::OrderType {
    fn from(type_: OrderType) -> Self {
        match type_ {
            OrderType::Limit => updated_account_orders::OrderType::LIMIT,
            OrderType::Market => updated_account_orders::OrderType::MARKET,
            OrderType::StopLimit => updated_account_orders::OrderType::STOP_LIMIT,
            OrderType::StopMarket => updated_account_orders::OrderType::STOP_MARKET,
        }
    }
}

impl SubscribeAccountOrders {
    pub fn make_query(&self) -> graphql_client::QueryBody<updated_account_orders::Variables> {
        graphql::UpdatedAccountOrders::build_query(updated_account_orders::Variables {
            payload: updated_account_orders::UpdatedAccountOrdersParams {
                market_name: self.market.clone(),
                buy_or_sell: self.buy_or_sell.map(|x| x.into()),
                status: self
                    .status
                    .as_ref()
                    .map(|x| x.iter().map(|x| Some(x.clone().into())).collect()),
                type_: self
                    .order_type
                    .as_ref()
                    .map(|x| x.iter().map(|x| Some(x.clone().into())).collect()),
                range_start: self.range.as_ref().map(|x| format!("{:?}", x.start)),
                range_stop: self.range.as_ref().map(|x| format!("{:?}", x.stop)),
            }
        })
    }
}