forked from bohdandr/Algohouse-fetchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiconomy-fetcher.js
258 lines (220 loc) · 7.67 KB
/
biconomy-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
247
248
249
250
251
252
253
254
255
256
257
258
import WebSocket from 'ws';
import fetch from 'node-fetch';
// define the websocket and REST URLs
const wsUrl = 'wss://www.biconomy.com/ws';
const restUrl = "https://www.biconomy.com/api/v1/exchangeInfo";
const response = await fetch(restUrl);
//extract JSON from the http response
const myJson = await response.json();
var currencies = [];
// extract symbols from JSON returned information
for(let i = 0; i < myJson.length; ++i){
if(myJson[i]['status'] === 'trading'){
currencies.push(myJson[i]['symbol']);
}
}
// print metadata about pairs
async function Metadata(){
myJson.forEach((item, index)=>{
if(item['status'] === 'trading'){
let pair_data = '@MD ' + item['symbol'] + ' spot ' + item['baseAsset'] + ' ' + item['quoteAsset'] + ' '
+ item['quoteAssetPrecision'] + ' 1 1 0 0';
console.log(pair_data);
}
})
console.log('@MDEND')
}
//function to get current time in unix format
function getUnixTime(){
return Math.floor(Date.now());
}
Number.prototype.noExponents = function() {
var data = String(this).split(/[eE]/);
if (data.length == 1) return data[0];
var z = '',
sign = this < 0 ? '-' : '',
str = data[0].replace('.', ''),
mag = Number(data[1]) + 1;
if (mag < 0) {
z = sign + '0.';
while (mag++) z += '0';
return z + str.replace(/^\-/, '');
}
mag -= str.length;
while (mag--) z += '0';
return str + z;
}
// func to print trades
async function getTrades(message){
message['params'][1].forEach((item)=>{
var trade_output = '! ' + getUnixTime() + ' ' +
message['params'][0] + ' ' +
item['type'][0].toUpperCase() + ' ' + parseFloat(item['price']).noExponents() + ' ' + parseFloat(item['amount']).noExponents();
console.log(trade_output);
});
}
// func to print orderbooks and deltas
async function getOrders(message, update){
// check if bids array is not Null
if(message['params'][1]['bids']){
var order_answer = '$ ' + getUnixTime() + ' ' + message['params'][2] + ' B '
var pq = '';
for(let i = 0; i < message['params'][1]['bids'].length; i++){
pq += (parseFloat(message['params'][1]['bids'][i][1])).noExponents() + '@' + (parseFloat(message['params'][1]['bids'][i][0])).noExponents() + '|';
}
pq = pq.slice(0, -1);
// check if the input data is full order book or just update
if (update){
console.log(order_answer + pq)
}
else{
console.log(order_answer + pq + ' R')
}
}
// check if asks array is not Null
if(message['params'][1]['asks']){
var order_answer = '$ ' + getUnixTime() + ' ' + message['params'][2] + ' S '
var pq = '';
for(let i = 0; i < message['params'][1]['asks'].length; i++){
pq += (parseFloat(message['params'][1]['asks'][i][1])).noExponents() + '@' + (parseFloat(message['params'][1]['asks'][i][0])).noExponents() + '|';
}
pq = pq.slice(0, -1);
// check if the input data is full order book or just update
if (update){
console.log(order_answer + pq)
}
else{
console.log(order_answer + pq + ' R')
}
}
}
function Connect1(){
var ws1 = new WebSocket(wsUrl);
// call this func when first opening connection
ws1.onopen = function(e) {
// create ping function to keep connection alive
setInterval(function() {
if (ws1.readyState === WebSocket.OPEN) {
ws1.send(JSON.stringify(
{
"method":"server.ping",
"params":[],
"id":5160
}
));
console.log('Ping request sent');
}
}, 60000);
// sub for trades
ws1.send(JSON.stringify(
{
"method":"deals.subscribe",
"params":currencies,
"id":20
}
))
};
// func to handle input messages
ws1.onmessage = function(event) {
try{
var dataJSON = JSON.parse(event.data);
// console.log(dataJSON);
if (dataJSON['method'] === 'deals.update' && dataJSON['params'][1].length < 5){
getTrades(dataJSON);
}else if (dataJSON['method'] === 'deals.update' && dataJSON['params'][1].length > 5){
// skip trades history
}else{
console.log(dataJSON);
}
}catch(e){
// possible errors while parsing data to json format
}
};
// func to handle closing connection
ws1.onclose = function(event) {
if (event.wasClean) {
console.log(`Connection 1 closed with code ${event.code} and reason ${event.reason}`);
} else {
console.log('Connection 1 lost');
setTimeout(function() {
Connect1();
}, 500);
}
};
// func to handle errors
ws1.onerror = function(error) {
console.log(error);
};
}
async function Connect2(index){
// create a new websocket instance
var ws2 = new WebSocket(wsUrl);
ws2.onopen = function(e) {
// create ping function to keep connection alive
setInterval(function() {
if (ws2.readyState === WebSocket.OPEN) {
ws2.send(JSON.stringify(
{
"method":"server.ping",
"params":[],
"id":5160
}
));
console.log('Ping request sent');
}
}, 60000);
// sub for orders
ws2.send(JSON.stringify(
{
"method":"depth.subscribe",
"params":[currencies[index],100,"0.00000001"],
"id":2000+index
}
))
}
// func to handle input messages
ws2.onmessage = function(event) {
var dataJSON;
try{
dataJSON = JSON.parse(event.data);
if (dataJSON['method'] === 'depth.update' && dataJSON['params'][0] === true){
getOrders(dataJSON, false);
}else if (dataJSON['method'] === 'depth.update' && dataJSON['params'][0] === false){
getOrders(dataJSON, true);
}else{
console.log(dataJSON);
}
}catch(e) {
// console.log(e);
// error may occurr cause some part of incoming data can`t be properly parsed in json format due to inapropriate symbols
// error only occurrs in messages that confirming subs
// error caused here is exchanges fault
}
};
// func to handle closing connection
ws2.onclose = function(event) {
if (event.wasClean) {
console.log(`Connection 2 closed with code ${event.code} and reason ${event.reason}`);
} else {
console.log('Connection 2 lost');
setTimeout(async function() {
Connect2(index);
}, 500);
}
};
// func to handle errors
ws2.onerror = function(error) {
console.log(error);
};
}
// call metadata to execute
Metadata();
Connect1();
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var wsArr = [];
for(let i = 0; i < currencies.length; i++){
wsArr.push(Connect2(i));
await sleep(500);
}