-
Notifications
You must be signed in to change notification settings - Fork 3
/
bibox-fetcher.js
246 lines (207 loc) · 8.27 KB
/
bibox-fetcher.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
import WebSocket from 'ws';
import fetch from 'node-fetch';
import zlib from 'zlib';
import getenv from 'getenv';
import * as commonFunctions from './CommonFunctions/CommonFunctions.js';
// define REST URL
const restUrl = "https://api.bibox.com/v3/mdata/pairList";
const response = await fetch(restUrl);
//extract JSON from the http response
const myJson = await response.json();
var currencies = [];
var trades_count_5min = {};
var orders_count_5min = {};
// extract symbols from JSON returned information
for(let i = 0; i < myJson['result'].length; ++i){
currencies.push(myJson['result'][i]['pair']);
}
// print metadata about pairs
async function Metadata(){
myJson['result'].forEach((item, index)=>{
trades_count_5min[item['pair'].toUpperCase()] = 0;
orders_count_5min[item['pair'].toUpperCase()] = 0;
let pair_data = '@MD ' + item['pair'].toUpperCase() + ' spot ' + item['pair'].split('_')[0].toUpperCase() + ' ' + item['pair'].split('_')[1].toUpperCase() + ' '
+ item['decimal'] + ' 1 1 0 0';
console.log(pair_data);
})
console.log('@MDEND')
}
Metadata();
// func to print trades
async function getTrades(message){
trades_count_5min[message['d'][0].toUpperCase()] += 1;
var trade_output = '! ' + commonFunctions.getUnixTime() + ' ' +
message['d'][0].toUpperCase() + ' ' +
(message['d'][3] === '2' ? 'S ' : 'B ') + message['d'][1] + ' ' + message['d'][2];
console.log(trade_output);
}
// func to print orderbooks and deltas
async function getSnaphots(message){
orders_count_5min[message['d']['pair'].toUpperCase()] += message['d']['bids'].length + message['d']['asks'].length;
// check if bids array is not Null
if(message['d']['bids']){
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + message['d']['pair'].toUpperCase() + ' B '
var pq = '';
for(let i = 0; i < message['d']['bids'].length; i++){
pq += message['d']['bids'][i][1] + '@' + message['d']['bids'][i][0] + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq + ' R');
}
// check if asks array is not Null
if(message['d']['asks']){
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + message['d']['pair'].toUpperCase() + ' S '
var pq = '';
for(let i = 0; i < message['d']['asks'].length; i++){
pq += message['d']['asks'][i][1] + '@' + message['d']['asks'][i][0] + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq + ' R');
}
}
// func to print orderbooks and deltas
async function getDeltas(message){
// check if bids array is not Null
if(message['d']['add']){
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + message['d']['pair'].toUpperCase() + ' B '
var pq = '';
if(message['d']['add']['bids']){
orders_count_5min[message['d']['pair'].toUpperCase()] += message['d']['add']['bids'].length;
for(let i = 0; i < message['d']['add']['bids'].length; i++){
pq += message['d']['add']['bids'][i][1] + '@' + message['d']['add']['bids'][i][0] + '|';
}
pq = pq.slice(0, -1);
}
if(message['d']['del']){
if(message['d']['del']['bids']){
orders_count_5min[message['d']['pair'].toUpperCase()] += message['d']['del']['bids'].length;
for(let i = 0; i < message['d']['del']['bids'].length; i++){
pq += '0@' + message['d']['del']['bids'][i][0] + '|';
}
pq = pq.slice(0, -1);
}
}
if(pq !== ''){
console.log(order_answer + pq);
}
}
// check if asks array is not Null
if(message['d']['add']){
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + message['d']['pair'].toUpperCase() + ' S '
var pq = '';
if(message['d']['add']['asks']){
orders_count_5min[message['d']['pair'].toUpperCase()] += message['d']['add']['asks'].length;
for(let i = 0; i < message['d']['add']['asks'].length; i++){
pq += message['d']['add']['asks'][i][1] + '@' + message['d']['add']['asks'][i][0] + '|';
}
pq = pq.slice(0, -1);
}
if(message['d']['del']){
if(message['d']['del']['asks']){
orders_count_5min[message['d']['pair'].toUpperCase()] += message['d']['del']['asks'].length;
for(let i = 0; i < message['d']['del']['asks'].length; i++){
pq += '0@' + message['d']['del']['asks'][i][0] + '|';
}
pq = pq.slice(0, -1);
}
}
if(pq !== ''){
console.log(order_answer + pq);
}
}
}
async function sendStats(){
commonFunctions.stats(trades_count_5min, orders_count_5min);
setTimeout(sendStats, parseFloat(5 - ((Date.now() / 60000) % 5)) * 60000);
}
const biboxws = 'wss://npush.bibox360.com';
let wsClass = function () {
};
wsClass.prototype._decodeMsg = function (data) {
let data1 = data.slice(1, data.length);
zlib.unzip(data1, (err, buffer) => {
if (err) {
console.log(err);
} else {
try {
let res = JSON.parse(buffer.toString());
if(res['topic'].slice(-5, res['topic'].length) === 'depth' && res['t'] === 0){
getSnaphots(res);
}else if(res['topic'].slice(-5, res['topic'].length) === 'deals' && res['t'] === 0){
// skip trade history
}else if(res['topic'].slice(-5, res['topic'].length) === 'depth' && res['t'] === 1){
getDeltas(res);
}
} catch (e) {
console.log(e);
}
}
});
};
wsClass.prototype._initWs = async function () {
let that = this;
let ws = new WebSocket(biboxws);
that.ws = ws;
ws.on('open', function open() {
currencies.forEach((item)=>{
{
ws.send(JSON.stringify({
event: 'addChannel',
sub: `${item}_deals`,
}));
if(getenv.string("SKIP_ORDERBOOKS", '') === '' || getenv.string("SKIP_ORDERBOOKS") === null){
ws.send(JSON.stringify({
sub: `${item}_depth`,
}));
}
}
});
});
ws.on('close', err => {
console.log('close, ', err);
setTimeout(async function() {
CreateInstance();
}, 500);
});
ws.on('error', err => {
console.log('error', err);
});
ws.on('ping', err => {
console.log('ping ', err.toString('utf8'));
});
ws.on('pong', err => {
console.log('pong ', err.toString('utf8'));
});
ws.on('message', data => {
if (data[0] == '1') {
that._decodeMsg(data);
// skip trades history
} else if (data[0] == '0') {
let dataJSON = JSON.parse(data.slice(1));
// console.log(dataJSON);
if(dataJSON['topic'].slice(-5, dataJSON['topic'].length) === 'deals' && dataJSON['t'] === 0){
// skip trade history
}else if(dataJSON['topic'].slice(-5, dataJSON['topic'].length) === 'deals' && dataJSON['t'] === 1){
getTrades(dataJSON);
}
// else if(dataJSON['topic'].slice(-5) === 'depth' && dataJSON['t'] === false){
// getSnaphots(dataJSON);
// }else if(dataJSON['topic'].slice(-5) === 'depth' && dataJSON['t'] === true){
// getDeltas(dataJSON);
// }
else{
console.log(dataJSON);
}
} else {
//console.log(that._decodeMsg(data));
}
});
};
function CreateInstance(){
let instance = new wsClass();
instance._initWs().catch(err => {
console.log(err);
});
}
setTimeout(sendStats, parseFloat(5 - ((Date.now() / 60000) % 5)) * 60000);
CreateInstance();