forked from rDrayBen/Algohouse-fetchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitex-fetcher.js
172 lines (145 loc) · 5.51 KB
/
vitex-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
import WebSocket from 'ws';
import fetch from 'node-fetch';
// define the websocket and REST URLs
const wsUrl = 'wss://api.vitex.net/v2/ws';
const restUrl = "https://api.vitex.net/api/v2/markets";
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['data'].length; ++i){
if(myJson['data'][i]['s'] !== 0){
currencies.push(myJson['data'][i]['symbol']);
}
}
// print metadata about pairs
async function Metadata(){
myJson['data'].forEach((item)=>{
let pair_data = '@MD ' + item['tradeTokenSymbol'].split('-')[0] + '-' + item['quoteTokenSymbol'].split('-')[0] + ' spot ' +
item['tradeTokenSymbol'].split('-')[0] + ' ' + item['quoteTokenSymbol'].split('-')[0] + ' ' + item['pricePrecision'] + ' 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;
}
async function getTrades(message){
let pair_name = message['topic'].replace('market.', '').replace('.trade', '');
pair_name = pair_name.split('_')[0].split('-')[0] + '-' + pair_name.split('_')[1].split('-')[0];
message['data'].forEach((trade)=>{
var trade_output = '! ' + getUnixTime() + ' ' + pair_name + ' ' +
(trade['side'] === 0 ? 'B' : 'S') + ' ' + parseFloat(trade['p']).noExponents() + ' ' + parseFloat(trade['q']).noExponents();
console.log(trade_output);
})
}
async function getOrders(message){
let pair_name = message['topic'].replace('market.', '').replace('.depth', '');
pair_name = pair_name.split('_')[0].split('-')[0] + '-' + pair_name.split('_')[1].split('-')[0];
// check if bids array is not Null
if(message['data']['bids'].length > 0){
var order_answer = '$ ' + getUnixTime() + ' ' + pair_name + ' B '
var pq = '';
for(let i = 0; i < message['data']['bids'].length; i++){
pq += parseFloat(message['data']['bids'][i][1]).noExponents() + '@' + parseFloat(message['data']['bids'][i][0]).noExponents() + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq + ' R');
}
// check if asks array is not Null
if(message['data']['asks'].length > 0){
var order_answer = '$ ' + getUnixTime() + ' ' + pair_name + ' S '
var pq = '';
for(let i = 0; i < message['data']['asks'].length; i++){
pq += parseFloat(message['data']['asks'][i][1]).noExponents() + '@' + parseFloat(message['data']['asks'][i][0]).noExponents() + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq + ' R');
}
}
async function Connect(){
// create a new websocket instance
var ws = new WebSocket(wsUrl);
ws.onopen = function(e) {
// create ping function to keep connection alive
setInterval(function() {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(
{
"command":"ping"
}
));
console.log('Ping request sent');
}
}, 10000);
// subscribe to trades and orders for all instruments
currencies.forEach((pair)=>{
ws.send(JSON.stringify(
{
"command": "sub",
"params": [`market.${pair}.trade`]
}
));
ws.send(JSON.stringify(
{
"command": "sub",
"params": [`market.${pair}.depth`]
}
));
});
};
// func to handle input messages
ws.onmessage = function(event) {
try{
// parse input data to JSON format
let dataJSON = JSON.parse(event.data);
// console.log(dataJSON);
if (dataJSON.event === 'push' && dataJSON['topic'].includes('trade')) {
getTrades(dataJSON);
}
else if(dataJSON.event === 'push' && dataJSON['topic'].includes('depth')){
getOrders(dataJSON);
}
else {
console.log(dataJSON);
}
}catch(e){
// skip confirmation messages cause they can`t be parsed into JSON format without an error
}
};
// func to handle closing connection
ws.onclose = function(event) {
if (event.wasClean) {
console.log(`Connection closed with code ${event.code} and reason ${event.reason}`);
} else {
console.log('Connection lost');
setTimeout(async function() {
Connect();
}, 500);
}
};
// func to handle errors
ws.onerror = function(error) {
console.log(`Error ${error} occurred`);
};
}
Metadata();
Connect();