From 6c1dabbce86599bf041f1f1a45579177f03d8553 Mon Sep 17 00:00:00 2001 From: edgardo Date: Thu, 12 Nov 2020 16:24:31 -0300 Subject: [PATCH] Changed btc provider default to sochain --- app.js | 2 +- lib/collection.js | 5 +++++ lib/history.js | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 3664f883..57e77ab3 100644 --- a/app.js +++ b/app.js @@ -413,7 +413,7 @@ const usePrimaryBtcHashrateProvider = require('./lib/utils/config').usePrimaryBt const btcHashrateUpdater = setInterval(() => { if(usePrimaryBtcHashrateProvider) { - Nodes.updateBtcHashrate('http://35.202.2.222:5000/blockchain/hashrate/'); + Nodes.updateBtcHashrate('https://sochain.com/api/v2/get_info/BTC'); } else { Nodes.updateBtcHashrateFromBackUp('https://api.blockchain.info/stats'); } diff --git a/lib/collection.js b/lib/collection.js index e153c60e..5e78e932 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -343,4 +343,9 @@ Collection.prototype.updateBtcHashrateFromBackUp = function(source) this._blockchain.updateBtcHashrateFromBackUpExternalSource(source); } +Collection.prototype.updateBtcHashrateFromAlternativeBackUp = function(source) +{ + this._blockchain.updateBtcHashrateFromAlternativeBackUpExternalSource(source); +} + module.exports = Collection; diff --git a/lib/history.js b/lib/history.js index d420052d..14fd9aee 100644 --- a/lib/history.js +++ b/lib/history.js @@ -692,7 +692,7 @@ function tryParseJSON (jsonString, customErrorMessage){ History.prototype.updateBtcHashrateFromExternalSource = function(source) { - http.get(source, (resp) => { + https.get(source, (resp) => { let data = ''; resp.on('data', (chunk) => { @@ -701,14 +701,12 @@ History.prototype.updateBtcHashrateFromExternalSource = function(source) resp.on('end', () => { const parsedData = tryParseJSON(data, "Failed to update BTC hashrate. Unable to parse JSON response"); + if (!parsedData || !parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){ return; } - const oldBtcHashrate = this._btcHashrate; - const btcHashrateExa = parsedData.data.hashrate; - this._btcHashrate = btcHashrateExa * 1000000000000000000; - + this._btcHashrate = parsedData.data.hashrate // trigger client update if(oldBtcHashrate !== this._btcHashrate) { this.getCharts(); @@ -734,7 +732,7 @@ History.prototype.updateBtcHashrateFromBackUpExternalSource = function(source) if (!parsedData || !parsedData.hasOwnProperty("hash_rate")){ return; } - + const oldBtcHashrate = this._btcHashrate; const btcHashrateGiga = parsedData.hash_rate; this._btcHashrate = btcHashrateGiga * 1000000000; @@ -750,4 +748,34 @@ History.prototype.updateBtcHashrateFromBackUpExternalSource = function(source) }); } +History.prototype.updateBtcHashrateFromAlternativeExternalSource = function(source) +{ + http.get(source, (resp) => { + let data = ''; + + resp.on('data', (chunk) => { + data += chunk; + }); + + resp.on('end', () => { + const parsedData = tryParseJSON(data, "Failed to update BTC hashrate. Unable to parse JSON response"); + if (!parsedData || !parsedData.hasOwnProperty("data") || !parsedData.data.hasOwnProperty("hashrate")){ + return; + } + + const oldBtcHashrate = this._btcHashrate; + const btcHashrateExa = parsedData.data.hashrate; + this._btcHashrate = btcHashrateExa * 1000000000000000000; + + // trigger client update + if(oldBtcHashrate !== this._btcHashrate) { + this.getCharts(); + } + }); + + }).on("error", (err) => { + console.error("Error while retrieving BTC hash rate: " + err.message); + }); +} + module.exports = History;