forked from manu354/cryptocurrency-arbitrage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
119 lines (87 loc) · 3.64 KB
/
main.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
/**
* Created by Manu Masson on 6/27/2017.
*
*/
'use strict';
const request = require('request'), Promise = require("bluebird"); //request for pulling JSON from api. Bluebird for Promises.
const app = require('express')(), helmet = require('helmet'), http = require('http').Server(app), io = require('socket.io')(http); // For websocket server functionality
app.use(helmet.hidePoweredBy({setTo: 'PHP/5.4.0'}));
// app.use(cors({credentials: false}));
const port = process.env.PORT || 3000;
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
http.listen(port, function () {
console.log('listening on', port);
});
require('./settings.js')(); //Includes settings file.
let coinNames = [];
io.on('connection', function (socket) {
socket.emit('coinsAndMarkets', [marketNames, coinNames]);
socket.emit('results', results);
});
// coin_prices is an object with data on price differences between markets. = {BTC : {market1 : 2000, market2: 4000, p : 2}, } (P for percentage difference)
// results is a 2D array with coin name and percentage difference, sorted from low to high.
let coin_prices = {}, numberOfRequests = 0, results = []; // GLOBAL variables to get pushed to browser.
function getMarketData(options, coin_prices, callback) { //GET JSON DATA
return new Promise(function (resolve, reject) {
request(options.URL, function (error, response, body) {
try {
let data = JSON.parse(body);
console.log("Success", options.marketName);
if (options.marketName) {
let newCoinPrices;
newCoinPrices = options.last(data, coin_prices, options.toBTCURL);
numberOfRequests++;
if (numberOfRequests >= 1) computePrices(coin_prices);
resolve(newCoinPrices);
}
else {
resolve(data);
}
} catch (error) {
console.log("Error getting JSON response from", options.URL, error); //Throws error
reject(error);
}
});
});
}
function computePrices(data) {
if (numberOfRequests >= 2) {
results = [];
for (let coin in data) {
if (Object.keys(data[coin]).length > 1){
if(coinNames.includes(coin) == false) coinNames.push(coin);
let arr = [];
for (let market in data[coin]) {
arr.push([data[coin][market], market]);
}
arr.sort(function (a, b) {
return a[0] - b[0];
});
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
results.push([coin, arr[i][0] / arr[j][0], arr[i][0], arr[j][0], arr[i][1], arr[j][1] ], [coin, arr[j][0] / arr[i][0], arr[j][0], arr[i][0], arr[j][1], arr[i][1]]);
}
}
}
}
results.sort(function (a, b) {
return a[1] - b[1];
});
io.emit('news', results);
}
}
(async function main() {
let arrayOfRequests = [];
for (let i = 0; i < markets.length; i++) {
arrayOfRequests.push(getMarketData(markets[i], coin_prices));
}
await Promise.all(arrayOfRequests.map(p => p.catch(e => e)))
.then(results => computePrices(coin_prices))
.catch(e => console.log(e));
setTimeout(main, 10000);
})();
// .then(v => {
// // console.log(v);
// });