This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
/
binance-client.js
361 lines (319 loc) · 9.5 KB
/
binance-client.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
const { EventEmitter } = require("events");
const winston = require("winston");
const semaphore = require("semaphore");
const { wait } = require("../util");
const https = require("../https");
const Ticker = require("../ticker");
const Trade = require("../trade");
const Level2Point = require("../level2-point");
const Level2Update = require("../level2-update");
const Level2Snapshot = require("../level2-snapshot");
const SmartWss = require("../smart-wss");
const Watcher = require("../watcher");
class BinanceClient extends EventEmitter {
constructor({ useAggTrades = true, requestSnapshot = true, reconnectIntervalMs = 300000 } = {}) {
super();
this._name = "Binance";
this._tickerSubs = new Map();
this._tradeSubs = new Map();
this._level2SnapshotSubs = new Map();
this._level2UpdateSubs = new Map();
this._wss = undefined;
this._reconnectDebounce = undefined;
this.requestSnapshot = requestSnapshot;
this.useAggTrades = useAggTrades;
this.hasTickers = true;
this.hasTrades = true;
this.hasLevel2Snapshots = true;
this.hasLevel2Updates = true;
this.hasLevel3Snapshots = false;
this.hasLevel3Updates = false;
this._watcher = new Watcher(this, reconnectIntervalMs);
this._restSem = semaphore(1);
this.REST_REQUEST_DELAY_MS = 1000;
}
//////////////////////////////////////////////
subscribeTicker(market) {
this._subscribe(market, "subscribing to ticker", this._tickerSubs);
}
unsubscribeTicker(market) {
this._unsubscribe(market, "unsubscribing from ticker", this._tickerSubs);
}
subscribeTrades(market) {
this._subscribe(market, "subscribing to trades", this._tradeSubs);
}
unsubscribeTrades(market) {
this._unsubscribe(market, "unsubscribing to trades", this._tradeSubs);
}
subscribeLevel2Snapshots(market) {
this._subscribe(market, "subscribing to l2 snapshots", this._level2SnapshotSubs);
}
unsubscribeLevel2Snapshots(market) {
this._unsubscribe(market, "unsubscribing from l2 snapshots", this._level2SnapshotSubs);
}
subscribeLevel2Updates(market) {
this._subscribe(market, "subscribing to l2 upates", this._level2UpdateSubs);
}
unsubscribeLevel2Updates(market) {
this._unsubscribe(market, "unsubscribing from l2 updates", this._level2UpdateSubs);
}
reconnect() {
winston.info("reconnecting");
this._reconnect();
this.emit("reconnected");
}
close() {
this._close();
}
////////////////////////////////////////////
// PROTECTED
_subscribe(market, msg, map) {
let remote_id = market.id.toLowerCase();
if (!map.has(remote_id)) {
winston.info(msg, this._name, remote_id);
map.set(remote_id, market);
this._reconnect();
}
}
_unsubscribe(market, msg, map) {
let remote_id = market.id.toLowerCase();
if (map.has(remote_id)) {
winston.info(msg, this._name, remote_id);
map.delete(remote_id);
this._reconnect();
}
}
/**
* Reconnects the socket after a debounce period
* so that multiple calls don't cause connect/reconnect churn
*/
_reconnect() {
clearTimeout(this._reconnectDebounce);
this._reconnectDebounce = setTimeout(() => {
this._close();
this._connect();
}, 100);
}
/**
* Close the underlying connction, which provides a way to reset the things
*/
_close() {
if (this._wss) {
this._wss.close();
this._wss = undefined;
this.emit("closed");
}
this._watcher.stop();
}
/** Connect to the websocket stream by constructing a path from
* the subscribed markets.
*/
_connect() {
if (!this._wss) {
let streams = [].concat(
Array.from(this._tradeSubs.keys()).map(
p => p + (this.useAggTrades ? "@aggTrade" : "@trade")
),
Array.from(this._level2SnapshotSubs.keys()).map(p => p + "@depth20"),
Array.from(this._level2UpdateSubs.keys()).map(p => p + "@depth")
);
if (this._tickerSubs.size > 0) {
streams.push("!ticker@arr");
}
let wssPath = "wss://stream.binance.com:9443/stream?streams=" + streams.join("/");
this._wss = new SmartWss(wssPath);
this._wss.on("message", this._onMessage.bind(this));
this._wss.on("open", this._onConnected.bind(this));
this._wss.on("disconnected", this._onDisconnected.bind(this));
this._wss.connect();
}
}
////////////////////////////////////////////
// ABSTRACT
_onConnected() {
this._watcher.start();
this._requestLevel2Snapshots(); // now that we're connected...
this.emit("connected");
}
_onDisconnected() {
this._watcher.stop();
this.emit("disconnected");
}
_onMessage(raw) {
let msg = JSON.parse(raw);
// ticker
if (msg.stream === "!ticker@arr") {
for (let raw of msg.data) {
let remote_id = raw.s.toLowerCase();
let market = this._tickerSubs.get(remote_id);
if (!market) continue;
let ticker = this._constructTicker(raw, market);
this.emit("ticker", ticker, market);
}
}
// trades
if (msg.stream.toLowerCase().endsWith("trade")) {
let remote_id = msg.data.s.toLowerCase();
let market = this._tradeSubs.get(remote_id);
if (!market) return;
let trade = this.useAggTrades
? this._constructAggTrade(msg, market)
: this._constructRawTrade(msg, market);
this.emit("trade", trade, market);
return;
}
// l2snapshot
if (msg.stream.endsWith("depth20")) {
let remote_id = msg.stream.split("@")[0];
let market = this._level2SnapshotSubs.get(remote_id);
if (!market) return;
let snapshot = this._constructLevel2Snapshot(msg, market);
this.emit("l2snapshot", snapshot, market);
return;
}
// l2update
if (msg.stream.endsWith("depth")) {
let remote_id = msg.stream.split("@")[0];
let market = this._level2UpdateSubs.get(remote_id);
if (!market) return;
let update = this._constructLevel2Update(msg, market);
this.emit("l2update", update, market);
return;
}
}
_constructTicker(msg, market) {
let {
E: timestamp,
c: last,
v: volume,
q: quoteVolume,
h: high,
l: low,
p: change,
P: changePercent,
a: ask,
A: askVolume,
b: bid,
B: bidVolume,
} = msg;
let open = parseFloat(last) + parseFloat(change);
return new Ticker({
exchange: "Binance",
base: market.base,
quote: market.quote,
timestamp: timestamp * 1000,
last,
open: open.toFixed(8),
high,
low,
volume,
quoteVolume,
change,
changePercent,
bid,
bidVolume,
ask,
askVolume,
});
}
_constructAggTrade({ data }, market) {
let { a: trade_id, p: price, q: size, T: time, m: buyer } = data;
let unix = time;
let amount = size;
let side = buyer ? "buy" : "sell";
return new Trade({
exchange: "Binance",
base: market.base,
quote: market.quote,
tradeId: trade_id,
unix,
side,
price,
amount,
});
}
_constructRawTrade({ data }, market) {
let { t: trade_id, p: price, q: size, b: buyOrderId, a: sellOrderId, T: time, m: buyer } = data;
let unix = time;
let amount = size;
let side = buyer ? "buy" : "sell";
return new Trade({
exchange: "Binance",
base: market.base,
quote: market.quote,
tradeId: trade_id,
unix,
side,
price,
amount,
buyOrderId,
sellOrderId,
});
}
_constructLevel2Snapshot(msg, market) {
let sequenceId = msg.data.lastUpdateId;
let asks = msg.data.asks.map(p => new Level2Point(p[0], p[1]));
let bids = msg.data.bids.map(p => new Level2Point(p[0], p[1]));
return new Level2Snapshot({
exchange: "Binance",
base: market.base,
quote: market.quote,
sequenceId,
asks,
bids,
});
}
_constructLevel2Update(msg, market) {
let sequenceId = msg.data.U;
let lastSequenceId = msg.data.u;
let asks = msg.data.a.map(p => new Level2Point(p[0], p[1]));
let bids = msg.data.b.map(p => new Level2Point(p[0], p[1]));
return new Level2Update({
exchange: "Binance",
base: market.base,
quote: market.quote,
sequenceId,
lastSequenceId,
asks,
bids,
});
}
_requestLevel2Snapshots() {
if (this.requestSnapshot) {
for (let market of this._level2UpdateSubs.values()) {
this._requestLevel2Snapshot(market);
}
}
}
async _requestLevel2Snapshot(market) {
this._restSem.take(async () => {
let failed = false;
try {
winston.info(`requesting snapshot for ${market.id}`);
let remote_id = market.id;
let uri = `https://api.binance.com/api/v1/depth?limit=1000&symbol=${remote_id}`;
let raw = await https.get(uri);
let sequenceId = raw.lastUpdateId;
let asks = raw.asks.map(p => new Level2Point(p[0], p[1]));
let bids = raw.bids.map(p => new Level2Point(p[0], p[1]));
let snapshot = new Level2Snapshot({
exchange: "Binance",
base: market.base,
quote: market.quote,
sequenceId,
asks,
bids,
});
this.emit("l2snapshot", snapshot, market);
} catch (ex) {
winston.warn(`failed to fetch snapshot for ${market.id} - ${ex}`);
failed = true;
} finally {
await wait(this.REST_REQUEST_DELAY_MS);
this._restSem.leave();
if (failed) this._requestLevel2Snapshot(market);
}
});
}
}
module.exports = BinanceClient;