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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#[cfg(feature = "netidx")]
use crate::{folio::FolioMessage, symbology::MarketId, utils::messaging::MaybeRequest};
use crate::{orderflow::*, Dir, DirPair};
#[cfg(feature = "netidx")]
use arcstr::ArcStr;
use chrono::{DateTime, Utc};
#[cfg(feature = "netidx")]
use derive::FromValue;
#[cfg(feature = "netidx")]
use netidx_derive::Pack;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use uuid::Uuid;

/// Protocol for ExternalCpty component to talk to the external cpty;
/// Ser/de is optimized for JSON communication.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
#[serde(tag = "type")]
pub enum ExternalCptyProtocol {
    GetSymbology,
    Symbology(ExternalSymbology),
    GetOpenOrders,
    OpenOrders {
        open_orders: Arc<Vec<OrderId>>,
    },
    GetBalances {
        id: Uuid,
    },
    Balances {
        id: Uuid,
        #[serde(skip_serializing_if = "Option::is_none")]
        result: Option<ExternalBalances>,
        #[serde(skip_serializing_if = "Option::is_none")]
        error: Option<String>,
    },
    Order(ExternalOrder),
    Cancel(Cancel),
    Reject(ExternalReject),
    Ack(Ack),
    Fill(ExternalFill),
    Out(Out),
    GetBookSnapshot {
        id: Uuid,
        market: String,
    },
    BookSnapshot {
        id: Uuid,
        #[serde(skip_serializing_if = "Option::is_none")]
        result: Option<ExternalBookSnapshot>,
        #[serde(skip_serializing_if = "Option::is_none")]
        error: Option<String>,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct ExternalSymbology {
    pub markets: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct ExternalBalances {
    pub balances: Vec<(String, Decimal)>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct ExternalOrder {
    pub id: OrderId,
    pub market: String,
    pub dir: Dir,
    pub quantity: Decimal,
    pub order_type: OrderType,
    pub time_in_force: TimeInForce,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct ExternalReject {
    pub order_id: OrderId,
    pub reason: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct ExternalFill {
    pub kind: FillKind,
    pub fill_id: FillId,
    pub order_id: Option<OrderId>,
    pub market: String,
    pub quantity: Decimal,
    pub price: Decimal,
    pub dir: Dir,
    #[serde(default)]
    pub is_maker: Option<bool>,
    pub trade_time: DateTime<Utc>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "netidx", derive(Pack, FromValue))]
pub struct ExternalBookSnapshot {
    pub book: DirPair<Arc<Vec<(Decimal, Decimal)>>>, // (price, size)
    pub timestamp: DateTime<Utc>,
}

/// Internal core message type for the ExternalCpty component.
#[cfg(feature = "netidx")]
#[derive(Debug, Clone, Pack, FromValue, Serialize, Deserialize)]
pub enum ExternalCptyMessage {
    Orderflow(OrderflowMessage),
    Folio(FolioMessage),
    External(ExternalCptyProtocol),
    Initialize,
    GetBookSnapshot(Uuid, MarketId),
    BookSnapshot(Uuid, Result<ExternalBookSnapshot, ArcStr>),
}

#[cfg(feature = "netidx")]
impl MaybeRequest for ExternalCptyMessage {
    fn request_id(&self) -> Option<Uuid> {
        match self {
            ExternalCptyMessage::GetBookSnapshot(id, _) => Some(*id),
            _ => None,
        }
    }

    fn response_id(&self) -> Option<Uuid> {
        match self {
            ExternalCptyMessage::BookSnapshot(id, _) => Some(*id),
            _ => None,
        }
    }
}

#[cfg(feature = "netidx")]
impl TryInto<OrderflowMessage> for &ExternalCptyMessage {
    type Error = ();

    fn try_into(self) -> Result<OrderflowMessage, ()> {
        match self {
            ExternalCptyMessage::Orderflow(o) => Ok(o.clone()),
            _ => Err(()),
        }
    }
}

#[cfg(feature = "netidx")]
impl TryInto<ExternalCptyMessage> for &OrderflowMessage {
    type Error = ();

    fn try_into(self) -> Result<ExternalCptyMessage, ()> {
        Ok(ExternalCptyMessage::Orderflow(self.clone()))
    }
}

#[cfg(feature = "netidx")]
impl TryInto<FolioMessage> for &ExternalCptyMessage {
    type Error = ();

    fn try_into(self) -> Result<FolioMessage, ()> {
        match self {
            ExternalCptyMessage::Folio(f) => Ok(f.clone()),
            _ => Err(()),
        }
    }
}

#[cfg(feature = "netidx")]
impl TryInto<ExternalCptyMessage> for &FolioMessage {
    type Error = ();

    fn try_into(self) -> Result<ExternalCptyMessage, ()> {
        Ok(ExternalCptyMessage::Folio(self.clone()))
    }
}