forked from rexthetech/pricefeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.js
268 lines (224 loc) · 7.73 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
});
// if feed_steem_active_key is not set in config.json
// then look for it in environment variables
function get_active_key() {
const key = config.feed_steem_active_key;
if (key) return key;
return process.env.feed_steem_active_key;
}
// if feed_steem_account is not set in config.json
// then look for it in environment variables
function get_account_name() {
const key = config.feed_steem_account;
if (key) return key;
return process.env.feed_steem_account;
}
// if coinmarketcap_api_key is not set in config.json
// then look for it in environment variables
function get_coinmarketcap_api_key() {
const key = config.coinmarketcap_api_key;
if (key) return key;
return process.env.coinmarketcap_api_key;
}
if (!get_account_name()) {
console.log("feed_steem_account not set in config.json or environment");
process.exit(1);
}
if (!get_active_key()) {
console.log("feed_steem_active_key not set in config.json or environment");
process.exit(1);
}
if (!config.exchanges || config.exchanges.length == 0) {
console.log("no exchanges are specified.");
process.exit(1);
}
function log(msg) {
console.log(new Date().toString() + ' - ' + msg);
}
function startProcess() {
let prices = [];
if (config.exchanges.indexOf('cloudflare') >= 0) {
loadPriceCloudflare(function (price) {
prices.push(price);
}, 0);
}
if (config.exchanges.indexOf('coingecko') >= 0) {
loadPriceCoingecko(function (price) {
prices.push(price);
}, 0);
}
if (config.exchanges.indexOf('cryptocompare') >= 0) {
loadPriceCryptocompare(function (price) {
prices.push(price);
}, 0);
}
if (config.exchanges.indexOf('coinmarketcap') >= 0) {
loadPriceCoinMarketCap(function (price) {
prices.push(price);
}, 0);
}
if (config.exchanges.indexOf('binance') >= 0) {
loadPriceBinance(function (price) {
prices.push(price);
}, 0);
}
// Publish the average of all markets that were loaded
setTimeout(function() {
if (prices.length == 0) {
log("no prices found.");
return;
}
const price = prices.reduce((t, v) => t + v, 0) / prices.length;
console.log(prices);
log("Price = " + price);
publishFeed(price, 0);
}, config.feed_publish_interval * 1000);
}
function publishFeed(price, retries) {
const peg_multi = config.peg_multi ? config.peg_multi : 1;
const 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(get_active_key(), get_account_name(), exchange_rate, function (err, result) {
if (result && !err) {
log('Broadcast successful!');
} else {
log('Error broadcasting feed_publish transaction: ' + err);
if (retries % config.feed_publish_fail_retry == 0) {
failover();
}
setTimeout(function () {
publishFeed(price, retries + 1);
}, config.retry_interval * 1000);
}
});
}
function loadPriceCoinMarketCap(callback, retries) {
// Load STEEM price in USD directly from CoinMarketCap
api_key = get_coinmarketcap_api_key();
if (!api_key) {
console.log("coinmarketcap_api_key not set in config.json or environment");
process.exit(1);
}
request.get('https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=STEEM&CMC_PRO_API_KEY=' + api_key, function (e, r, data) {
try {
const steem_price = parseFloat(JSON.parse(data).data.STEEM[0].quote.USD.price);
log('Loaded STEEM Price from CoinMarketCap: ' + steem_price);
if (callback) {
callback(steem_price);
}
} catch (err) {
log('Error loading STEEM price from CoinMarketCap: ' + err);
if(retries <= config.price_feed_max_retry) {
setTimeout(function () {
loadPriceCoinMarketCap(callback, retries + 1);
}, config.retry_interval * 1000);
}
}
});
}
function loadPriceCryptocompare(callback, retries) {
// Load STEEM price in USD directly from CryptoCompare
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 USD directly from CoinGecko
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 loadPriceCloudflare(callback, retries) {
// Load STEEM price
request.get('https://price.justyy.workers.dev/query/?s=STEEM+USDT', function (e, r, data) {
try {
const json_data = JSON.parse(data);
const arr = json_data.result[0].split(' ')
const steem_price = parseFloat(arr[3]);
log('Loaded STEEM Price from Cloudflare: ' + steem_price);
if (callback) {
callback(steem_price);
}
} catch (err) {
log('Error loading STEEM price from Cloudflare: ' + err);
if (retries <= config.price_feed_max_retry) {
setTimeout(function () {
loadPriceCloudflare(loadPriceCloudflare, retries + 1);
}, config.retry_interval * 1000);
}
}
});
}
function loadPriceBinance(callback, retries) {
// Load STEEM price in USDT directly from Binance
request.get('https://api.binance.com/api/v3/avgPrice?symbol=STEEMUSDT', function (e, r, data) {
try {
const steem_price = parseFloat(JSON.parse(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 failover() {
if (config.rpc_nodes && config.rpc_nodes.length > 1) {
let cur_node_index = config.rpc_nodes.indexOf(steem.api.options.url) + 1;
if (cur_node_index == config.rpc_nodes.length) {
cur_node_index = 0;
}
const rpc_node = config.rpc_nodes[cur_node_index];
steem.api.setOptions({ transport: 'http', uri: rpc_node, url: rpc_node });
log('***********************************************');
log('Failing over to: ' + rpc_node);
log('***********************************************');
}
}
setInterval(startProcess, config.interval * 60 * 1000);
startProcess();