-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.js
113 lines (97 loc) · 2.83 KB
/
server1.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
const express = require('express');
const app = express();
//const bodyParser = require('body-parser');
const Gdax = require('../node_modules/gdax');
const publicClient = new Gdax.PublicClient();
const orderBookFuncs = require('./orderBook');
//app.use(bodyParser.urlencoded({extended: true}));
//app.use(bodyParser.json());
app.use(express.static('public'));
//app.set('view engine', 'ejs');
//Start Server
app.listen(8080, function() {
console.log('listening on IDE 8080'); //https://ide50-tony-rr.cs50.io:8080/
});
//Serves Root HTML file
app.get('/', function(req, res){
res.sendFile('/home/ubuntu/workspace/gdax'+'/index1.html');
});
//Get's Historical Data
var hp;
publicClient.getProductHistoricRates(
'ETH-USD',
{ granularity: 300 },
(error, response, data) => {
hp = data;
});
//Updates the Historical Prices every 5m
setInterval(function(){
publicClient.getProductHistoricRates(
'ETH-USD',
{ granularity: 300 },
(error, response, data) => {
hp = data;
});
}, 300000);
//Route that Sends Historical Data to Browser
app.get('/chart', (req, res) => {
if (hp) {
res.send(hp);
}
});
//Variables to store called API data on Server where we send to browser
let bidETH;
let askETH;
let midETH;
let bidTotal;
let askTotal;
let orderBookArr;
//Makes call to GDAX API and stored latest Price and OrderBook Details to Server every 2s
setInterval(function(){
publicClient.getProductOrderBook('ETH-USD',{ level: 3 },(error, response, book) => {
if (book) {
bidETH = parseFloat(book['bids'][0][0]);
askETH = parseFloat(book['asks'][0][0]);
midETH = ((bidETH + askETH)/2).toFixed(2);
bidTotal = orderBookFuncs.sum(book['bids'], -1.50);
askTotal = orderBookFuncs.sum(book['asks'], 1.50);
orderBookArr = [book['bids'], book['asks']];
}
});
}, 2000);
var stats24hr = {
eth: {},
btc: {},
ltc: {}
};
//Makes call to GDAX API and stored latest Price/Vol Details to Server every 2s
setInterval(function(){
publicClient.getProductTicker('BTC-USD' , (err, res) => {
stats24hr.btc = res.body;
});
publicClient.getProductTicker('ETH-USD' , (err, res) => {
stats24hr.eth = res.body;
});
publicClient.getProductTicker('LTC-USD' , (err, res) => {
stats24hr.ltc = res.body;
});
}, 2000);
//Route that Sends current mid (price) to browser
app.get('/price', (req, res) => {
if (midETH) {
res.send({
eth: midETH.toString(),
allCoins: stats24hr
});
}
});
//Route that Sends object of Total Bids & Asks at specified levels and also DataPoints to Chart Orderbook
app.get('/bidAsk', (req, res) => {
if (bidTotal) {
res.send({
bidTotal: bidTotal.toFixed(2),
askTotal: askTotal.toFixed(2),
chartData: orderBookFuncs.orderChart(orderBookArr)
});
}
});