Skip to content

Commit

Permalink
Push results to frontend via socket.io
Browse files Browse the repository at this point in the history
manu354 committed Jul 9, 2017
1 parent 131ce40 commit 6a5688f
Showing 4 changed files with 99 additions and 43 deletions.
37 changes: 37 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>

<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();

socket.on('news', function (data) {
console.log(data);1
$('#messages').empty();
for(var i = data.length-1; i >= 0 ; i--) {
$('#messages').append($('<li>').text(data[i]));
}
});
});
</script>


</body>
</html>
89 changes: 54 additions & 35 deletions main.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,25 @@

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'}));

const port = 3000;

app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
socket.emit('news', results);
});

http.listen(port, function () {
console.log('listening on *:3000');
});


require('./settings.js')(); //Includes settings file.

// coin_prices is an object with data on price differences between markets. = {BTC : {market1 : 2000, market2: 4000, p : 2}, } (P for percentage difference)
@@ -20,11 +39,12 @@ function getMarketData(options, coin_prices, callback) { //GET JSON DATA
let data = JSON.parse(body);

if (options.marketName) {

let newCoinPrices;

newCoinPrices = options.last(data, coin_prices, options.toBTCURL);


numberOfRequests++;
if(numberOfRequests >=1) computePrices(coin_prices);
resolve(newCoinPrices);

}
@@ -44,58 +64,57 @@ function getMarketData(options, coin_prices, callback) { //GET JSON DATA
}

function computePrices(data) {
for(let coin in data) {

if(Object.keys(data[coin]).length > 1) {
let arr= [];
for( let market in data[coin]) {
// console.log(coin, market, data[coin][market]);
arr.push([market, data[coin][market]]);
}
arr.sort(function(a, b) {
return a[1] - b[1];
});
if(numberOfRequests >= 2) {
results = [];
for (let coin in data) {

if (Object.keys(data[coin]).length > 1) {
let arr = [];
for (let market in data[coin]) {
arr.push([market, data[coin][market]]);
}
arr.sort(function (a, b) {
return a[1] - b[1];
});

let min = arr[0][1], max = arr[arr.length-1][1];
// console.log(min, max);
let min = arr[0][1], max = arr[arr.length - 1][1];

if(min == 0) min = arr[1][1];
if(max == 0) max = arr[arr.length-2][1];
if (min == 0) min = arr[1][1];
if (max == 0) max = arr[arr.length - 2][1];

if(min != 0 && max != 0) {
let marketPair = arr[arr.length-1][0] + "/" + arr[0][0];
if (min != 0 && max != 0) {
let marketPair = arr[arr.length - 1][0] + "/" + arr[0][0];

results.push([coin, max/min, marketPair]);
results.push([coin, max / min, marketPair]);
}
}
}
results.sort(function (a, b) {
return a[1] - b[1];
});

io.emit('news', results);
console.log(results);
}
results.sort(function(a, b) {
return a[1] - b[1];
});
console.log(results);
}






(async function main() {
// console.log("heloo");
let arrayOfRequests = [];

for(let i = 0; i <= markets.length; i++) {
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))
// .then(results => console.log(coin_prices))

.catch(e => console.log(e));
})()

.then(v => {
// console.log(v);
});
setTimeout(main, 10000);
})();

// .then(v => {
// // console.log(v);
// });
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
{
"name": "arbittrex",
"version": "1.0.0",
"version": "1.0.3",
"description": "An arbitrage bot",
"main": "main.js",
"dependencies": {
"bluebird": "^3.5.0",
"request" : "^2.81.0"
"express": "^4.15.3",
"helmet": "^3.6.1",
"request": "^2.81.0",
"socket.io": "^2.0.3"
},
"devDependencies": {},
"scripts": {
"test": "node main",
"start" : "node main"
"start": "node main"
},
"repository": {
"type": "git",
"url": "git+https://github.com/manu354/arbitrage.git"
},
"keywords": [
"Bittrex",
"bittrex",
"arbitrage"
],
"author": "Manu Masson",
5 changes: 1 addition & 4 deletions settings.js
Original file line number Diff line number Diff line change
@@ -23,8 +23,7 @@
//
// }


markets = [
let markets = [
{
marketName: 'bittrex',
URL: 'https://bittrex.com/api/v1.1/public/getmarketsummaries',
@@ -84,7 +83,6 @@ markets = [

last: function (data, coin_prices, toBTCURL) { //Where to find the last price of coin in JSON data
return new Promise(function (res, rej) {
console.log("HHELLEEO!!");
let priceOfBTC = data.btc.last;
console.log(priceOfBTC);
try {
@@ -94,7 +92,6 @@ markets = [
if (!coin_prices[coinName]) coin_prices[coinName] = {};

coin_prices[coinName]["jubi"] = data[key].last / priceOfBTC;
console.log(data[key].last, coinName);
}
res(coin_prices);
}

0 comments on commit 6a5688f

Please sign in to comment.