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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crypto_market_type::MarketType;
use crypto_msg_type::MessageType;
use serde::{Deserialize, Serialize};
use crate::order::Order;
macro_rules! add_common_fields {
(
$(#[$outer:meta])*
struct $name:ident {
$(
$(#[$inner:meta])*
$field:ident: $ty:ty
),* $(,)*
}
) => {
$(#[$outer])*
pub struct $name {
pub exchange: String,
pub market_type: MarketType,
pub symbol: String,
pub pair: String,
pub msg_type: MessageType,
pub timestamp: i64,
pub json: String,
$(
$(#[$inner])*
pub $field: $ty
),*
}
};
}
add_common_fields!(
#[derive(Serialize, Deserialize)]
struct Msg {}
);
#[derive(Copy, Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum TradeSide {
Buy,
Sell,
}
#[derive(Serialize, Deserialize)]
pub struct TradeMsg {
pub exchange: String,
pub market_type: MarketType,
pub symbol: String,
pub pair: String,
pub msg_type: MessageType,
pub timestamp: i64,
pub price: f64,
pub quantity_base: f64,
pub quantity_quote: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub quantity_contract: Option<f64>,
pub side: TradeSide,
pub trade_id: String,
pub json: String,
}
#[derive(Serialize, Deserialize)]
pub struct OrderBookMsg {
pub exchange: String,
pub market_type: MarketType,
pub symbol: String,
pub pair: String,
pub msg_type: MessageType,
pub timestamp: i64,
pub seq_id: Option<u64>,
pub prev_seq_id: Option<u64>,
pub asks: Vec<Order>,
pub bids: Vec<Order>,
pub snapshot: bool,
pub json: String,
}
#[derive(Serialize, Deserialize)]
pub struct FundingRateMsg {
pub exchange: String,
pub market_type: MarketType,
pub symbol: String,
pub pair: String,
pub msg_type: MessageType,
pub timestamp: i64,
pub funding_rate: f64,
pub funding_time: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub estimated_rate: Option<f64>,
pub json: String,
}
add_common_fields!(
#[derive(Serialize, Deserialize)]
struct TickerMsg {
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
quote_volume: f64,
last_quantity: Option<f64>,
best_bid_price: Option<f64>,
best_bid_quantity: Option<f64>,
best_ask_price: Option<f64>,
best_ask_quantity: Option<f64>,
open_interest: Option<f64>,
open_interest_quote: Option<f64>,
}
);
add_common_fields!(
#[derive(Serialize, Deserialize)]
struct BboMsg {
bid_price: f64,
bid_quantity: f64,
ask_price: f64,
ask_quantity: f64,
}
);
add_common_fields!(
#[derive(Serialize, Deserialize)]
struct KlineMsg {
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
period: String,
quote_volume: Option<f64>,
}
);