-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange_price_feed.js
91 lines (83 loc) · 2.25 KB
/
exchange_price_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
/*jslint node: true */
'use strict';
const async = require('async');
const request = require('request');
const eventBus = require('byteballcore/event_bus.js');
const network = require('byteballcore/network.js');
const symbols = ['USDT-BTC', 'BTC-GBYTE'];
const rates = {};
function updateBittrexRates(state, onDone) {
const apiUri = 'https://bittrex.com/api/v1.1/public/getmarketsummaries';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
let arrCoinInfos = JSON.parse(body).result;
let prices = {};
arrCoinInfos.forEach(coinInfo => {
if (!coinInfo.Last)
return;
if (symbols.includes(coinInfo.MarketName)) {
prices[coinInfo.MarketName] = coinInfo.Last;
console.log("new exchange rate: " + coinInfo.MarketName + "=" + coinInfo.Last);
}
});
if (Object.keys(prices).length == symbols.length) {
rates['GBYTE_USD'] = prices['BTC-GBYTE'] * prices['USDT-BTC'];
state.updated = true;
}
}
else {
console.log("Can't get currency rates from bittrex");
}
onDone();
});
}
function updateFreebeRates(state, onDone) {
const apiUri = 'http://freebe.byte-ball.com/last';
request(apiUri, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("freebe: ", body);
let price;
try{
price = parseFloat(JSON.parse(body).price_bytes);
console.log("GBB/GB = "+price);
}
catch(e){
console.log('bad response from freebe:', e);
return onDone();
}
if (rates['GBYTE_USD'] && price) {
rates['GBB_USD'] = rates['GBYTE_USD'] * price;
state.updated = true;
}
}
else {
console.log("Can't get currency rates from freebe");
}
onDone();
});
}
function updateRates(){
let state = {updated: false};
async.series([
function(cb){
updateBittrexRates(state, cb);
},
function(cb){
updateFreebeRates(state, cb);
}
], function(){
console.log(rates);
if (state.updated)
broadcastNewRates();
});
}
function broadcastNewRates(){
network.sendAllInboundJustsaying('exchange_rates', rates);
}
eventBus.on('client_logged_in', function(ws){
if (Object.keys(rates).length > 0)
network.sendJustsaying(ws, 'exchange_rates', rates);
});
updateRates();
setInterval(updateRates, 1000 * 60 * 5);
exports.rates = rates;