-
Notifications
You must be signed in to change notification settings - Fork 3
/
coincheck-fetcher.js
270 lines (228 loc) · 8.72 KB
/
coincheck-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
259
260
261
262
263
264
265
266
267
268
269
270
import WebSocket from 'ws';
import fetch from 'node-fetch';
import getenv from 'getenv';
import * as commonFunctions from './CommonFunctions/CommonFunctions.js';
// define the websocket and REST URLs
const wsUrl = 'wss://ws-api.coincheck.com/';
const restUrl = "https://coincheck.com/api/rate/all";
const orderbookUrlBase = "https://coincheck.com/api/order_books?pair=";
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 = {};
var temp = [];
temp = Object.keys(myJson['jpy']);
// extract symbols from JSON returned information
for(let i = 0; i < temp.length; ++i){
currencies.push(temp[i] + '_jpy');
}
temp = Object.keys(myJson['btc']);
// extract symbols from JSON returned information
for(let i = 0; i < temp.length; ++i){
currencies.push(temp[i] + '_btc');
}
// print metadata about pairs
async function Metadata(){
currencies.forEach((item)=>{
trades_count_5min[item.toUpperCase()] = 0;
orders_count_5min[item.toUpperCase()] = 0;
let pair_data = '@MD ' + item.toUpperCase() + ' spot ' + item.split('_')[0].toUpperCase() + ' ' + item.split('_')[1].toUpperCase()
+ ' -1 1 1 0 0';
console.log(pair_data);
})
console.log('@MDEND')
}
async function getTrades(message){
message.forEach((trade)=>{
trades_count_5min[trade[2].toUpperCase()] += 1;
var trade_output = '! ' + commonFunctions.getUnixTime() + ' ' + trade[2].toUpperCase() + ' ' +
trade[5][0].toUpperCase() + ' ' + parseFloat(trade[3]).noExponents() + ' ' + parseFloat(trade[4]).noExponents();
console.log(trade_output);
});
}
// trade ex.
/*[
"1690463569",
"248164679",
"btc_jpy",
"4154377.0",
"0.0264",
"sell",
"5706675440",
"5706675437"
]*/
async function getOrders(message){
// check if asks array is not Null
if(message[1]['asks'].length > 0){
orders_count_5min[message[0].toUpperCase()] += message[1]['asks'].length;
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + message[0].toUpperCase() + ' S '
var pq = '';
for(let i = 0; i < message[1]['asks'].length; i++){
pq += parseFloat(message[1]['asks'][i][1]).noExponents() + '@' + parseFloat(message[1]['asks'][i][0]).noExponents() + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq);
}
// check if bids array is not Null
if(message[1]['bids'].length > 0){
orders_count_5min[message[0].toUpperCase()] += message[1]['bids'].length;
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + message[0].toUpperCase() + ' B '
var pq = '';
for(let i = 0; i < message[1]['bids'].length; i++){
pq += parseFloat(message[1]['bids'][i][1]).noExponents() + '@' + parseFloat(message[1]['bids'][i][0]).noExponents() + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq);
}
}
async function getOrderbook(pair){
const response = await fetch(orderbookUrlBase + pair);
//extract JSON from the http response
const responseJSON = await response.json();
try{
if(responseJSON['asks'].length > 0){
orders_count_5min[pair.toUpperCase()] += responseJSON['asks'].length;
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + pair.toUpperCase() + ' S '
var pq = '';
for(let i = 0; i < responseJSON['asks'].length; i++){
pq += parseFloat(responseJSON['asks'][i][1]).noExponents() + '@' + parseFloat(responseJSON['asks'][i][0]).noExponents() + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq + ' R');
}
if(responseJSON['bids'].length > 0){
orders_count_5min[pair.toUpperCase()] += responseJSON['bids'].length;
var order_answer = '$ ' + commonFunctions.getUnixTime() + ' ' + pair.toUpperCase() + ' B '
var pq = '';
for(let i = 0; i < responseJSON['bids'].length; i++){
pq += parseFloat(responseJSON['bids'][i][1]).noExponents() + '@' + parseFloat(responseJSON['bids'][i][0]).noExponents() + '|';
}
pq = pq.slice(0, -1);
console.log(order_answer + pq + ' R');
}
}catch(e){
}
}
async function sendStats(){
commonFunctions.stats(trades_count_5min, orders_count_5min);
setTimeout(sendStats, parseFloat(5 - ((Date.now() / 60000) % 5)) * 60000);
}
async function ConnectTrades(){
// create a new websocket instance
var ws = new WebSocket(wsUrl);
ws.onopen = function(e) {
// create ping function to keep connection alive
ws.ping();
// subscribe to trades and orders for given instrument
currencies.forEach((pair)=>{
ws.send(JSON.stringify(
{
"type": "subscribe",
"channel": `${pair}-trades`
}
));
});
};
// func to handle input messages
ws.onmessage = function(event) {
try{
// parse input data to JSON format
let dataJSON = JSON.parse(event.data);
if (dataJSON.length > 0) {
getTrades(dataJSON);
}
else {
console.log(dataJSON);
}
}catch(e){
// skip confirmation messages cause they can`t be parsed into JSON format without an error
(async () => {
await commonFunctions.sleep(1000); // commonFunctions.sleep for 1000 milliseconds (1 second)
console.log(event.data);
})();
}
};
// 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() {
ConnectTrades();
}, 500);
}
};
// func to handle errors
ws.onerror = function(error) {
console.log(`Error ${error} occurred`);
(async () => {
await commonFunctions.sleep(1000); // commonFunctions.sleep for 1000 milliseconds (1 second)
})();
};
}
async function ConnectDeltas(){
// create a new websocket instance
var ws = new WebSocket(wsUrl);
ws.onopen = function(e) {
// create ping function to keep connection alive
ws.ping();
// subscribe to trades and orders for given instrument
currencies.forEach((pair)=>{
ws.send(JSON.stringify(
{
"type": "subscribe",
"channel": `${pair}-orderbook`
}
));
});
};
// func to handle input messages
ws.onmessage = function(event) {
try{
// parse input data to JSON format
let dataJSON = JSON.parse(event.data);
if (dataJSON[1]['bids'] && dataJSON[1]['asks']) {
getOrders(dataJSON);
}
else {
console.log(dataJSON);
}
}catch(e){
// skip confirmation messages cause they can`t be parsed into JSON format without an error
(async () => {
await commonFunctions.sleep(1000); // commonFunctions.sleep for 1000 milliseconds (1 second)
console.log(event.data);
})();
}
};
// 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() {
ConnectDeltas();
}, 500);
}
};
// func to handle errors
ws.onerror = function(error) {
console.log(`Error ${error} occurred`);
(async () => {
await commonFunctions.sleep(1000); // commonFunctions.sleep for 1000 milliseconds (1 second)
})();
};
}
Metadata();
setTimeout(sendStats, parseFloat(5 - ((Date.now() / 60000) % 5)) * 60000);
ConnectTrades();
if(getenv.string("SKIP_ORDERBOOKS", '') === '' || getenv.string("SKIP_ORDERBOOKS") === null){
for(let pair of currencies){
getOrderbook(pair);
}
ConnectDeltas();
}