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
use crate::{
event::{MarketEvent, MarketIter},
exchange::{binance::channel::BinanceChannel, subscription::ExchangeSub, ExchangeId},
subscription::book::{Level, OrderBookL1},
Identifier,
};
use barter_integration::model::{Exchange, Instrument, SubscriptionId};
use chrono::Utc;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
pub struct BinanceOrderBookL1 {
#[serde(alias = "s", deserialize_with = "de_ob_l1_subscription_id")]
pub subscription_id: SubscriptionId,
#[serde(alias = "b", deserialize_with = "barter_integration::de::de_str")]
pub best_bid_price: f64,
#[serde(alias = "B", deserialize_with = "barter_integration::de::de_str")]
pub best_bid_amount: f64,
#[serde(alias = "a", deserialize_with = "barter_integration::de::de_str")]
pub best_ask_price: f64,
#[serde(alias = "A", deserialize_with = "barter_integration::de::de_str")]
pub best_ask_amount: f64,
}
impl Identifier<Option<SubscriptionId>> for BinanceOrderBookL1 {
fn id(&self) -> Option<SubscriptionId> {
Some(self.subscription_id.clone())
}
}
impl From<(ExchangeId, Instrument, BinanceOrderBookL1)> for MarketIter<OrderBookL1> {
fn from((exchange_id, instrument, book): (ExchangeId, Instrument, BinanceOrderBookL1)) -> Self {
let time_now = Utc::now();
Self(vec![Ok(MarketEvent {
exchange_time: time_now,
received_time: time_now,
exchange: Exchange::from(exchange_id),
instrument,
kind: OrderBookL1 {
last_update_time: time_now,
best_bid: Level::new(book.best_bid_price, book.best_bid_amount),
best_ask: Level::new(book.best_ask_price, book.best_ask_amount),
},
})])
}
}
pub fn de_ob_l1_subscription_id<'de, D>(deserializer: D) -> Result<SubscriptionId, D::Error>
where
D: serde::de::Deserializer<'de>,
{
<&str as Deserialize>::deserialize(deserializer)
.map(|market| ExchangeSub::from((BinanceChannel::ORDER_BOOK_L1, market)).id())
}
#[cfg(test)]
mod tests {
use super::*;
mod de {
use super::*;
#[test]
fn test_binance_order_book_l1() {
struct TestCase {
input: &'static str,
expected: BinanceOrderBookL1,
}
let tests = vec![
TestCase {
input: r#"
{
"u":22606535573,
"s":"ETHUSDT",
"b":"1215.27000000",
"B":"32.49110000",
"a":"1215.28000000",
"A":"13.93900000"
}
"#,
expected: BinanceOrderBookL1 {
subscription_id: SubscriptionId::from("@bookTicker|ETHUSDT"),
best_bid_price: 1215.27000000,
best_bid_amount: 32.49110000,
best_ask_price: 1215.28000000,
best_ask_amount: 13.93900000,
},
},
TestCase {
input: r#"
{
"e":"bookTicker",
"u":2286618712950,
"s":"BTCUSDT",
"b":"16858.90",
"B":"13.692",
"a":"16859.00",
"A":"30.219",
"T":1671621244670,
"E":1671621244673
}"#,
expected: BinanceOrderBookL1 {
subscription_id: SubscriptionId::from("@bookTicker|BTCUSDT"),
best_bid_price: 16858.90,
best_bid_amount: 13.692,
best_ask_price: 16859.00,
best_ask_amount: 30.219,
},
},
];
for (index, test) in tests.into_iter().enumerate() {
assert_eq!(
serde_json::from_str::<BinanceOrderBookL1>(test.input).unwrap(),
test.expected,
"TC{} failed",
index
);
}
}
}
}