forked from rexthetech/pricefeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorcheck.js
178 lines (147 loc) · 5.77 KB
/
errorcheck.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
const fs = require("fs");
const request = require("request");
var config = JSON.parse(fs.readFileSync("config.json"));
function log(msg) {
console.log(new Date().toString() + ' - ' + msg);
}
function startProcess() {
log("Grabbing prices...");
let exchangePrices = [];
let aggregatorPrices = [];
// Exchanges
loadPriceBinance(function (price) {
exchangePrices.push(price);
}, 0);
loadPricePoloniex(function (price) {
exchangePrices.push(price);
}, 0);
loadPriceBittrex(function (price) {
exchangePrices.push(price);
}, 0);
// Aggregators
loadPriceCoingecko(function (price) {
aggregatorPrices.push(price);
}, 0);
loadPriceCryptocompare(function (price) {
aggregatorPrices.push(price);
}, 0);
// Wait for prices
setTimeout(function() {
// Show results
aggregatorPrice = aggregatorPrices.reduce((t, v) => t + v, 0) / aggregatorPrices.length;
log(" Aggregator-only ('correct') average: " + aggregatorPrice);
exchangePrice = exchangePrices.reduce((t, v) => t + v, 0) / exchangePrices.length;
exchangePriceError = percentageDiff(exchangePrice, aggregatorPrice).toFixed(3);
log(" Exchanges-only average: " + exchangePrice + " (" + exchangePriceError + "% error)");
combinedPrices = aggregatorPrices.concat(exchangePrices);
combinedPrice = combinedPrices.reduce((t, v) => t + v, 0) / combinedPrices.length;
combinedPriceError = percentageDiff(combinedPrice, aggregatorPrice).toFixed(3);;
log(" Default config average: " + combinedPrice + " (" + combinedPriceError + "% error)");
log("=============================================================================");
}, 2000);
}
function percentageDiff(v1, v2) {
return ((v1 - v2) / ((v1 + v2) / 2)) * 100;
}
function loadPriceCryptocompare(callback, retries) {
// Load STEEM price in BTC from Cryptocompare and convert that to USD using BTC price
request.get('https://min-api.cryptocompare.com/data/price?fsym=STEEM&tsyms=USDT', function (e, r, data) {
try {
const steem_price = parseFloat(JSON.parse(data).USDT);
log('Loaded STEEM Price from Cryptocompare: ' + steem_price);
if (callback) {
callback(steem_price);
}
} catch (err) {
log('Error loading STEEM price from Cryptocompare: ' + err);
if(retries <= config.price_feed_max_retry) {
setTimeout(function () {
loadPriceCryptocompare(callback, retries + 1);
}, config.retry_interval * 1000);
}
}
});
}
function loadPriceCoingecko(callback, retries) {
// Load STEEM price in BTC from Coingecko and convert that to USD using BTC price
request.get('https://api.coingecko.com/api/v3/simple/price?ids=steem&vs_currencies=usd', function (e, r, data) {
try {
const steem_price = parseFloat(JSON.parse(data).steem.usd);
log('Loaded STEEM Price from Coingecko: ' + steem_price);
if (callback) {
callback(steem_price);
}
} catch (err) {
log('Error loading STEEM price from Coingecko: ' + err);
if(retries <= config.price_feed_max_retry) {
setTimeout(function () {
loadPriceCoingecko(callback, retries + 1);
}, config.retry_interval * 1000);
}
}
});
}
function loadPriceBittrex(callback, retries) {
// Load STEEM price in BTC from bittrex and convert that to USD using BTC price
request.get('https://api.bittrex.com/v3/markets/BTC-USD/ticker', function (e, r, data) {
request.get('https://api.bittrex.com/v3/markets/STEEM-BTC/ticker', function (e, r, btc_data) {
try {
const steem_price = parseFloat(JSON.parse(data).lastTradeRate) * parseFloat(JSON.parse(btc_data).lastTradeRate);
log('Loaded STEEM Price from Bittrex: ' + steem_price);
if (callback) {
callback(steem_price);
}
} catch (err) {
log('Error loading STEEM price from Bittrex: ' + err);
if(retries <= config.price_feed_max_retry) {
setTimeout(function () {
loadPriceBittrex(callback, retries + 1);
}, config.retry_interval * 1000);
}
}
});
});
}
function loadPriceBinance(callback, retries) {
// Load STEEM price in BTC and convert that to USD using BTC price
request.get('https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT', function (e, r, data) {
request.get('https://api.binance.com/api/v3/ticker/price?symbol=STEEMBTC', function (e, r, btc_data) {
try {
const steem_price = parseFloat(JSON.parse(data).price) * parseFloat(JSON.parse(btc_data).price);
log('Loaded STEEM Price from Binance: ' + steem_price);
if (callback) {
callback(steem_price);
}
} catch (err) {
log('Error loading STEEM price from Binance: ' + err);
if (retries <= config.price_feed_max_retry) {
setTimeout(function () {
loadPriceBinance(callback, retries + 1);
}, config.retry_interval * 1000);
}
}
});
});
}
function loadPricePoloniex(callback, retries) {
// Load STEEM price in BTC and convert that to USD using BTC price
request.get('https://poloniex.com/public?command=returnTicker', function (e, r, data) {
try {
const json_data = JSON.parse(data);
const steem_price = parseFloat(json_data['USDT_BTC'].last) * parseFloat(json_data['BTC_STEEM'].last)
log('Loaded STEEM Price from Poloniex: ' + steem_price);
if (callback) {
callback(steem_price);
}
} catch (err) {
log('Error loading STEEM price from Poloniex: ' + err);
if (retries <= config.price_feed_max_retry) {
setTimeout(function () {
loadPriceBinance(loadPricePoloniex, retries + 1);
}, config.retry_interval * 1000);
}
}
});
}
setInterval(startProcess, 60000);
startProcess();