-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoloniex.js
More file actions
139 lines (127 loc) · 5.82 KB
/
poloniex.js
File metadata and controls
139 lines (127 loc) · 5.82 KB
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
const https = require('https');
const autobahn = require('autobahn');
const wsuri = 'wss://api.poloniex.com';
const connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
const tickers = {};
function httpsGetTickers() {
var pubApiUrl = 'https://poloniex.com/public?command=returnTicker';
https.get(pubApiUrl, function(res) {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request failed:' + `Status Code ${statusCode}`);
} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content type:' + `Content Type ${contentType}`);
}
if (error) {
console.error(error.message);
// Consume response data to free up memory
res.resume();
} else {
res.setEncoding('utf8');
let rawText = '';
res.on('data', function(chunk) { rawText += chunk; });
res.on('end', function() {
try {
const data = JSON.parse(rawText);
//console.log(data);
for (var pair in data) {
//console.log(pair);
if (pair === 'USDT_BTC' || pair === 'USDT_ETH' ||
pair === 'USDT_ZEC' || pair === 'USDT_BCH' ||
pair === 'BTC_ETH' || pair === 'BTC_ZEC' ||
pair === 'ETH_ZEC') {
if (tickers[pair] == undefined) {
tickers[pair] = {};
console.log('Ticker initialized:' + pair);
}
tickers[pair].pair = pair;
tickers[pair].last = data[pair].last;
tickers[pair].ask = data[pair].lowestAsk;
tickers[pair].bid = data[pair].highestBid;
tickers[pair].change = data[pair].percentChange;
tickers[pair].volume = data[pair].quoteVolume;
tickers[pair].forzen = data[pair].isForzen;
tickers[pair].highest = data[pair].high24hr;
tickers[pair].lowest = data[pair].low24hr;
tickers[pair].updatetime = new Date();
console.log('Ticker updated:' + pair);
}
}
} catch (e) {
console.error(e.message);
}
});
}
}).on('error', function(e) {
console.error(`https.get error: ${e.message}`);
});
}
function routineUpdateFunction() {
httpsGetTickers();
routineUpdatetime = new Date();
}
function initModule() {
}
var routineUpdatetime = null;
var routineInterval = null;
function startRoutineInterval() {
console.log('Start trace poloniex price');
// Update current tickers data
httpsGetTickers();
routineUpdatetime = new Date();
if (routineInterval == undefined || routineInterval == null) {
// Use public API get tickers data for each 5 minutes
routineInterval = setInterval(routineUpdateFunction, 5 * 60 * 1000);
} else {
console.log('Routine interval has been exist, should be stopped before start again');
}
}
function stopRoutineInterval() {
if (routineInterval == undefined || routineInterval == null) {
console.log('Routine interval is not exists, should be started to stop');
} else {
// Clear interval timer
clearInterval(routineInterval);
routineInterval = null;
}
}
module.exports = {
init: initModule,
start: startRoutineInterval,
stop: stopRoutineInterval,
getTicker: (pair) => {
// poloniex pair format: USDT_BTC
pair = pair.toLowerCase();
var ticker = tickers[pair];
return ticker;
},
getCurrencyInfo: (currency, withTimestamp = true) => {
currency = currency.toUpperCase();
var response;
// for-in loop will looping within key(in this case, key is pair)
for (pair in tickers) {
if (pair.indexOf('_' + currency) === -1) {
continue;
}
var price = currency.toUpperCase() + '->' + pair.replace('_' + currency, '').toUpperCase() + ': ' + (Math.round(tickers[pair].last * 10000) / 10000);
if (response == undefined) {
if (withTimestamp == true) {
response = tickers[pair].updatetime.toISOString() + '\n' + price;
} else {
response = price;
}
} else {
response += '\n' + price;
}
}
// if tickers does not have given currency, return empty
if (response == undefined)
response = '';
return response;
}
};