forked from MattyIce/pricefeed
-
Notifications
You must be signed in to change notification settings - Fork 1
/
feed.js
191 lines (154 loc) · 6.17 KB
/
feed.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
179
180
181
182
183
184
185
186
187
188
189
190
191
const fs = require("fs");
const steem = require('steem');
const request = require("request");
var config = JSON.parse(fs.readFileSync("config.json"));
// Connect to the specified RPC node
var rpc_node = config.rpc_nodes ? config.rpc_nodes[0] : (config.rpc_node ? config.rpc_node : 'https://api.steemit.com');
steem.api.setOptions({ transport: 'http', uri: rpc_node, url: rpc_node });
setInterval(startProcess, config.interval * 60 * 1000);
startProcess();
function startProcess() {
if(config.exchanges && config.exchanges.length > 0) {
var prices = [];
if(config.exchanges.indexOf('huobi') >= 0) {
loadPriceHuobi(function (price) {
prices.push(price);
}, 0);
}
if(config.exchanges.indexOf('bittrex') >= 0) {
loadPriceBittrex(function (price) {
prices.push(price);
}, 0);
}
if(config.exchanges.indexOf('binance') >= 0) {
loadPriceBinance(function (price) {
prices.push(price);
}, 0);
}
if(config.exchanges.indexOf('poloniex') >= 0) {
loadPricePoloniex(function (price) {
prices.push(price);
}, 0);
}
// Publish the average of all markets that were loaded
setTimeout(function() { publishFeed(prices.reduce((t, v) => t + v, 0) / prices.length, 0); }, 30 * 1000);
} else {
// Load price from coingecko if no exchanges are specified.
loadPricecoingecko(function (price) {
publishFeed(price, 0);
}, 0);
}
}
function publishFeed(price, retries) {
var peg_multi = config.peg_multi ? config.peg_multi : 1;
var exchange_rate = { base: price.toFixed(3) + ' SBD', quote: (1 / peg_multi).toFixed(3) + ' STEEM' };
log('Broadcasting feed_publish transaction: ' + JSON.stringify(exchange_rate));
steem.broadcast.feedPublish(config.active_key, config.account, exchange_rate, function (err, result) {
if (result && !err) {
log('Broadcast successful!');
} else {
log('Error broadcasting feed_publish transaction: ' + err);
if (retries == 5)
failover();
if (retries < 2)
setTimeout(function () { publishFeed(price, retries + 1); }, 10 * 1000);
}
});
}
function loadPricecoingecko(callback, retries) {
log('Loading STEEM price...');
// Load the price feed data
request.get('https://api.coingecko.com/api/v3/simple/price?ids=STEEM&vs_currencies=USD', function (e, r, data) {
try {
var 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 < 2)
setTimeout(function () { loadPrice(callback, retries + 1); }, 10 * 1000);
}
});
}
function loadPriceBittrex(callback, retries) {
// Load STEEM price in BTC from bittrex and convert that to USD using BTC price in coinmarketcap
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 {
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 < 2)
setTimeout(function () { loadPriceBittrex(callback, retries + 1); }, 10 * 1000);
}
});
});
}
function loadPriceBinance(callback, retries) {
// Load STEEM price in BTC from Binance and convert that to USD using BTC price in Binance
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 {
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 < 2)
setTimeout(function () { loadPriceBinance(callback, retries + 1); }, 10 * 1000);
}
});
});
}
function loadPricePoloniex(callback, retries) {
// Load STEEM price in Poloniex
request.get('https://poloniex.com/public?command=returnTicker', function (e, r, data) {
try {
var json_data = JSON.parse(data);
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 < 2)
setTimeout(function () { loadPricePoloniex(callback, retries + 1); }, 10 * 1000);
}
});
}
function loadPriceHuobi(callback, retries) {
// Load STEEM price in Huobi
request.get('https://api.huobi.pro/market/history/trade?symbol=steemusdt&size=1', function (e, r, data) {
try {
var json_data = JSON.parse(data);
steem_price = parseFloat(json_data.data[0].data[0].price)
log('Loaded STEEM Price from Huobi: ' + steem_price);
if (callback)
callback(steem_price);
} catch (err) {
log('Error loading STEEM price from Huobi: ' + err);
if(retries < 2)
setTimeout(function () { loadPriceHuobi(callback, retries + 1); }, 10 * 1000);
}
});
}
function failover() {
if (config.rpc_nodes && config.rpc_nodes.length > 1) {
var cur_node_index = config.rpc_nodes.indexOf(steem.api.options.url) + 1;
if (cur_node_index == config.rpc_nodes.length)
cur_node_index = 0;
var rpc_node = config.rpc_nodes[cur_node_index];
steem.api.setOptions({ transport: 'http', uri: rpc_node, url: rpc_node });
utils.log('');
utils.log('***********************************************');
utils.log('Failing over to: ' + rpc_node);
utils.log('***********************************************');
utils.log('');
}
}
function log(msg) { console.log(new Date().toString() + ' - ' + msg); }