-
Notifications
You must be signed in to change notification settings - Fork 15
/
log-account-events.ts
111 lines (95 loc) · 3.11 KB
/
log-account-events.ts
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
import {
isWsFormattedFuturesUserDataEvent,
isWsFormattedSpotUserDataEvent,
isWsFormattedUserDataEvent,
WebsocketClient,
WsUserDataEvents,
} from 'binance';
const key = process.env.APIKEY || 'APIKEY';
const secret = process.env.APISECRET || 'APISECRET';
/**
* Prepare websocket client and connect the event handlers you're interested in
*/
const wsClient = new WebsocketClient({
api_key: key,
api_secret: secret,
// some events have numbers stored as strings or unreadable properties (e.g E instead of `eventTime`)
// the beautifier processes these into more intuitive types
beautify: true,
});
// ALL RAW websocket events will be printed here:
wsClient.on('message', (data) => {
// console.log('raw message received ', JSON.stringify(data, null, 2));
});
// OR listen to only formatted messages (requires beautify:true)
wsClient.on('formattedMessage', (data) => {
// The wsKey can be parsed to determine the type of message (which websocket connection it came from)
// if (!Array.isArray(data) && data.wsKey.includes('userData')) {
// return onUserDataEvent(data);
// }
// or use a type guard if available
if (isWsFormattedUserDataEvent(data)) {
return onUserDataEvent(data);
}
// any other unhandled formatted events for topics you've subscribed to:
console.log(new Date(), 'formattedMsg: ', JSON.stringify(data, null, 2));
});
// OR use a dedicated event that only provides user data updates (if you notice any are missing here, check the on('message') handler)
// wsClient.on('formattedUserDataMessage', data => {
// return onUserDataEvent(data);
// });
/**
* Call the subscribe functions for the user data categories you're interested in (e.g. spot and usd future events)
*/
wsClient.subscribeSpotUserDataStream();
// wsClient.subscribeMarginUserDataStream();
// wsClient.subscribeIsolatedMarginUserDataStream('BTCUSDT');
wsClient.subscribeUsdFuturesUserDataStream();
/**
* Define a function to handle this sub-set of events
*/
function onUserDataEvent(data: WsUserDataEvents) {
// the wsMarket property denotes which API category it came from (e.g. spot vs futures)
// if (data.wsMarket.includes('spot')) {
// or use a type guard, (if one exists already - or make a pull request to add them to the connector)
if (isWsFormattedSpotUserDataEvent(data)) {
console.log(
new Date(),
'spot user data event: ',
JSON.stringify(data, null, 2),
);
return;
}
if (data.wsMarket.includes('margin')) {
console.log(
new Date(),
'margin user data event: ',
JSON.stringify(data, null, 2),
);
return;
}
if (data.wsMarket.includes('isolatedMargin')) {
console.log(
new Date(),
'isolatedMargin user data event: ',
JSON.stringify(data, null, 2),
);
return;
}
if (data.wsMarket.includes('usdmTestnet')) {
console.log(
new Date(),
'usdmTestnet user data event: ',
JSON.stringify(data, null, 2),
);
return;
}
if (isWsFormattedFuturesUserDataEvent(data)) {
console.log(
new Date(),
'usdm user data event: ',
JSON.stringify(data, null, 2),
);
return;
}
}