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
use super::super::channel::BinanceChannel;
use super::BinanceLevel;
use crate::{
exchange::subscription::ExchangeSub,
subscription::book::{OrderBook, OrderBookSide},
Identifier,
};
use barter_integration::model::{Side, SubscriptionId};
use chrono::Utc;
use serde::{Deserialize, Serialize};
/// [`Binance`](super::super::Binance) OrderBook Level2 snapshot HTTP message.
///
/// Used as the starting [`OrderBook`] before OrderBook Level2 delta WebSocket updates are
/// applied.
///
/// ### Payload Examples
/// See docs: <https://binance-docs.github.io/apidocs/spot/en/#order-book>
/// #### BinanceSpot OrderBookL2Snapshot
/// ```json
/// {
/// "lastUpdateId": 1027024,
/// "bids": [
/// ["4.00000000", "431.00000000"]
/// ],
/// "asks": [
/// ["4.00000200", "12.00000000"]
/// ]
/// }
/// ```
///
/// #### BinanceFuturesUsd OrderBookL2Snapshot
/// See docs: <https://binance-docs.github.io/apidocs/futures/en/#order-book>
/// ```json
/// {
/// "lastUpdateId": 1027024,
/// "E": 1589436922972,
/// "T": 1589436922959,
/// "bids": [
/// ["4.00000000", "431.00000000"]
/// ],
/// "asks": [
/// ["4.00000200", "12.00000000"]
/// ]
/// }
/// ```
#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
pub struct BinanceOrderBookL2Snapshot {
#[serde(rename = "lastUpdateId")]
pub last_update_id: u64,
pub bids: Vec<BinanceLevel>,
pub asks: Vec<BinanceLevel>,
}
impl From<BinanceOrderBookL2Snapshot> for OrderBook {
fn from(snapshot: BinanceOrderBookL2Snapshot) -> Self {
Self {
last_update_time: Utc::now(),
bids: OrderBookSide::new(Side::Buy, snapshot.bids),
asks: OrderBookSide::new(Side::Sell, snapshot.asks),
}
}
}
/// Deserialize a
/// [`BinanceSpotOrderBookL2Delta`](super::super::spot::l2::BinanceSpotOrderBookL2Delta) or
/// [`BinanceFuturesOrderBookL2Delta`](super::super::futures::l2::BinanceFuturesOrderBookL2Delta)
/// "s" field (eg/ "BTCUSDT") as the associated [`SubscriptionId`]
///
/// eg/ "@depth@100ms|BTCUSDT"
pub fn de_ob_l2_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_L2, market)).id())
}
#[cfg(test)]
mod tests {
use super::*;
mod de {
use super::*;
#[test]
fn test_binance_order_book_l2_snapshot() {
struct TestCase {
input: &'static str,
expected: BinanceOrderBookL2Snapshot,
}
let tests = vec![
TestCase {
// TC0: valid Spot BinanceOrderBookL2Snapshot
input: r#"
{
"lastUpdateId": 1027024,
"bids": [
[
"4.00000000",
"431.00000000"
]
],
"asks": [
[
"4.00000200",
"12.00000000"
]
]
}
"#,
expected: BinanceOrderBookL2Snapshot {
last_update_id: 1027024,
bids: vec![BinanceLevel {
price: 4.0,
amount: 431.0,
}],
asks: vec![BinanceLevel {
price: 4.00000200,
amount: 12.0,
}],
},
},
TestCase {
// TC1: valid FuturePerpetual BinanceOrderBookL2Snapshot
input: r#"
{
"lastUpdateId": 1027024,
"E": 1589436922972,
"T": 1589436922959,
"bids": [
[
"4.00000000",
"431.00000000"
]
],
"asks": [
[
"4.00000200",
"12.00000000"
]
]
}
"#,
expected: BinanceOrderBookL2Snapshot {
last_update_id: 1027024,
bids: vec![BinanceLevel {
price: 4.0,
amount: 431.0,
}],
asks: vec![BinanceLevel {
price: 4.00000200,
amount: 12.0,
}],
},
},
];
for (index, test) in tests.into_iter().enumerate() {
assert_eq!(
serde_json::from_str::<BinanceOrderBookL2Snapshot>(test.input).unwrap(),
test.expected,
"TC{} failed",
index
);
}
}
}
}