Skip to content

Commit

Permalink
added translation foundation
Browse files Browse the repository at this point in the history
added hashrate chart
  • Loading branch information
npq7721 committed Dec 20, 2019
1 parent b36b64f commit eda5bec
Show file tree
Hide file tree
Showing 16 changed files with 381 additions and 96 deletions.
18 changes: 10 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ var addressApi = require("./app/api/addressApi.js");
var electrumAddressApi = require("./app/api/electrumAddressApi.js");
var coreApi = require("./app/api/coreApi.js");
var auth = require('./app/auth.js');

var package_json = require('./package.json');
var Restful = require('./routes/restfulRouter.js');
var translations = require("./translations.js");
global.appVersion = package_json.version;

var crawlerBotUserAgentStrings = [ "Googlebot", "Bingbot", "Slurp", "DuckDuckBot", "Baiduspider", "YandexBot", "Sogou", "Exabot", "facebot", "ia_archiver" ];
Expand All @@ -59,8 +59,10 @@ if(process.env.BTCEXP_HTTPS) {
var helmet = require("helmet");
app.use(helmet());
}


// app.configure(() => {
// app.use(translations.init);
// });
app.use(translations.init);
// view engine setup
app.set('views', path.join(__dirname, 'views'));

Expand Down Expand Up @@ -268,7 +270,7 @@ app.continueStartup = function() {
if (config.electrumXServers && config.electrumXServers.length > 0) {
electrumAddressApi.connectToServers().then(function() {
global.electrumAddressApi = electrumAddressApi;

}).catch(function(err) {
utils.logError("31207ugf4e0fed", err, {electrumXServers:config.electrumXServers});
});
Expand Down Expand Up @@ -324,7 +326,7 @@ app.use(function(req, res, next) {

res.locals.config = global.config;
res.locals.coinConfig = global.coinConfig;

res.locals.host = req.session.host;
res.locals.port = req.session.port;

Expand Down Expand Up @@ -356,7 +358,7 @@ app.use(function(req, res, next) {
} else {
req.session.uiTheme = "dark";
}
}
}
// homepage banner
if (!req.session.hideHomepageBanner) {
var cookieValue = req.cookies['user-setting-hideHomepageBanner'];
Expand All @@ -380,10 +382,10 @@ app.use(function(req, res, next) {

if (req.session.userMessage) {
res.locals.userMessage = req.session.userMessage;

if (req.session.userMessageType) {
res.locals.userMessageType = req.session.userMessageType;

} else {
res.locals.userMessageType = "warning";
}
Expand Down
23 changes: 17 additions & 6 deletions app/api/coreApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var miscCache = new Cache(process.env.MAX_MISC_CACHE ? process.env.MAX_MISC_CACH
var blockCache = new Cache(process.env.MAX_BLOCK_CACHE ? process.env.MAX_BLOCK_CACHE : 50);
var txCache = new Cache(process.env.MAX_TX_CACHE ? process.env.MAX_TX_CACHE : 200);
var assetsCache = new Cache(process.env.MAX_ASSET_CACHE ? process.env.MAX_ASSET_CACHE : 100);
var chartingCache = new Cache(process.env.MAX_CHART_CACHE ? process.env.MAX_CHART_CACHE : 1200);

function getGenesisBlockHash() {
return coins[config.coin].genesisBlockHash;
Expand Down Expand Up @@ -72,8 +73,14 @@ function getUptimeSeconds() {
return miscCache.tryCache("getUptimeSeconds", 1000, rpcApi.getUptimeSeconds);
}

function getNetworkHash(height) {
return chartingCache.tryCache("getNetworkHash-" + height, 86400000, () => {
return rpcApi.getNetworkHash(height);
});
}

function getTotalAssetAddresses(assetName) {
function getTotalAssetAddresses(req) {
var assetName = req.query.assetname;
return assetsCache.tryCache("getTotalAssetAddresses-"+assetName, 300000, () => {
return rpcApi.getTotalAssetAddresses(assetName);
});
Expand All @@ -85,7 +92,8 @@ function getAssetAddresses(assetName, start, limit) {
});
}

function getTotalAddressAssetBalances(address) {
function getTotalAddressAssetBalances(req) {
var address = req.query.address;
return assetsCache.tryCache("getTotalAddressAssetBalances-"+address, 300000, () => {
return rpcApi.getTotalAddressAssetBalances(address);
});
Expand Down Expand Up @@ -542,7 +550,8 @@ function getBlockByHeight(blockHeight) {
});
}

function getBlock(blockHeight) {
function getBlock(req) {
var blockHeight = req.query.height;
return blockCache.tryCache("getBlock-" + blockHeight, 3600000, function() {
return rpcApi.getBlock(blockHeight);
});
Expand Down Expand Up @@ -592,7 +601,8 @@ function getBlocksByHash(blockHashes) {
});
}

function getRawTransaction(txid) {
function getRawTransaction(req) {
var txid = req.query.txid;
var rpcApiFunction = function() {
return rpcApi.getRawTransaction(txid);
};
Expand Down Expand Up @@ -654,7 +664,7 @@ function getRawTransactions(txids) {
return new Promise(function(resolve, reject) {
var promises = [];
for (var i = 0; i < txids.length; i++) {
promises.push(getRawTransaction(txids[i]));
promises.push(getRawTransaction({query : {txid : txids[i]}}));
}

Promise.all(promises).then(function(results) {
Expand Down Expand Up @@ -953,5 +963,6 @@ module.exports = {
getTotalAddressAssetBalances : getTotalAddressAssetBalances,
getAddressAssetBalances : getAddressAssetBalances,
getTotalAssetCount : getTotalAssetCount,
queryAssets : queryAssets
queryAssets : queryAssets,
getNetworkHash : getNetworkHash
};
10 changes: 8 additions & 2 deletions app/api/rpcApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,16 @@ function queryAssets(searchTerm, start, limit) {
return getRpcDataWithParams({method:"listassets", parameters:[searchTerm, true, limit, start]});
}

function getNetworkHash(height) {
return getRpcDataWithParams({method:"getnetworkhashps", parameters:[120, height]});
}

function getMempoolTxids(verbose = false) {
return getRpcDataWithParams({method:"getrawmempool", parameters:[verbose]});
}

function broadcast(rawtxhex) {
function broadcast(req) {
var rawtxhex = req.query.tx;
return getRpcDataWithParams({method:"sendrawtransaction", parameters:[rawtxhex]});
}

Expand Down Expand Up @@ -549,5 +554,6 @@ module.exports = {
getTotalAddressAssetBalances : getTotalAddressAssetBalances,
getAddressAssetBalances : getAddressAssetBalances,
getTotalAssetCount : getTotalAssetCount,
queryAssets : queryAssets
queryAssets : queryAssets,
getNetworkHash : getNetworkHash
};
45 changes: 44 additions & 1 deletion app/coins/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class CoinBase {
this.ticker = ticker;
this.priceid = priceid;
this.priceApiUrl = `https://api.coingecko.com/api/v3/coins/${priceid}?localization=false`;
this.unlimittedIps = process.env.UNLIMIT_IPS ? process.env.UNLIMIT_IPS.split(",") : [];
var currencyUnits = [
{
type:"native",
Expand Down Expand Up @@ -145,7 +146,30 @@ class CoinBase {
limit : {
windowMs: 15 * 60 * 1000, // 15 minutes
max: 3000, // limit each IP to 100 requests per windowMs
message: "Too calls from this IP with 15 mins, please try again after 15 mins"
message: "Too calls from this IP with 15 mins, please try again after 15 mins",
keyGenerator : (req, res) => {
var ip = req.headers['x-forwarded-for'];
if(!ip) {
ip = req.ip;
} else {
console.log("x-forwarded-for ip", ip);
}
return ip;
},
skip : (req, res) => {
var ip = req.headers['x-forwarded-for'];
if(!ip) {
ip = req.ip;
}
var skip = this.unlimittedIps.includes(ip);
if(skip) {
console.log("ip=%s get unlimitted api requests", ip)
} else {
console.log(this.unlimittedIps);
console.log("ip=%s get limitted api requests", ip)
}
return skip;
}
},
api_map : [
{
Expand Down Expand Up @@ -181,6 +205,25 @@ class CoinBase {
description : "Get current block height",
"return" : "block height as number"
},
{
name : "getnetworkhashes",
uri : "getnetworkhashes",
api_source : "getNetworkHashes",
params : [
{
name : "total",
type : "number",
description : "total number of blocks to get hashrates starting from certain block or latest block by default"
},
{
name : "from",
type : "number",
description : "Optional. default to be current height. Starting blocks to get hashrates data"
}
],
description : "Get hashrates for total of blocks starting from block height",
"return" : "A map of height and its hashrate"
},
{
name : "getblock",
uri : "getblock",
Expand Down
3 changes: 3 additions & 0 deletions examples/rtm/btc-rpc-explorer.env
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ BTCEXP_OLD_SPACE_MAX_SIZE=8096
# Show tools list in a sub-nav at top of screen
# Default: true
BTCEXP_UI_SHOW_TOOLS_SUBHEADER=false

#API Request limitter exceptions
UNLIMIT_IPS=localhost,127.0.0.1
7 changes: 7 additions & 0 deletions locales/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Tx Stats" : "Tx Stats",
"Hashrates Chart" : "Hashrates Chart",
"Hash Rate" : "Hash Rate Last 100 Blocks",
"Hashes" : "Hashes",
"Block" : "Block"
}
1 change: 1 addition & 0 deletions locales/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions locales/vn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Tx Stats": "Tx Stats",
"Hashrates Chart": "Hashrates Chart",
"Hash Rate": "Hash Rate",
"Block": "Block",
"Hashes": "Hashes"
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rpc-explorer",
"version": "1.2.1",
"version": "1.2.2",
"description": "Explorer for Bitcoin and RPC-compatible blockchains",
"private": false,
"bin": "bin/cli.js",
Expand Down Expand Up @@ -32,12 +32,13 @@
"csurf": "^1.10.0",
"debug": "~4.1.1",
"decimal.js": "10.1.1",
"helmet": "^3.21.2",
"dotenv": "^8.1.0",
"electrum-client": "github:chaintools/node-electrum-client#43a999036f9c5",
"express": "^4.17.1",
"express-rate-limit": "^5.0.0",
"express-session": "1.16.1",
"helmet": "^3.21.2",
"i18n": "^0.8.4",
"jstransformer-markdown-it": "^2.1.0",
"lru-cache": "5.1.1",
"marked": "0.7.0",
Expand Down
105 changes: 105 additions & 0 deletions public/js/chartUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
function chartLineGraph(id, graphTitle, xTitle, yTitle, dataUrl) {
$.ajax({url: dataUrl, success: function(dataJson) {
if((typeof dataJson) === "string") {
dataJson = JSON.parse(dataJson);
}
var labels = Object.keys(dataJson);
var values = Object.values(dataJson);
var ctx = $(`#${id}`)[0].getContext('2d');;
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
borderColor: '#36a2eb',
borderWidth: 1,
backgroundColor: '#84CBFA',
data: values
},]
},
//HERE COMES THE AXIS Y LABEL
options : {
responsive: true,
title: {
display: true,
text: graphTitle
},
legend: {
display: false
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: yTitle
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: xTitle
}
}]
}
}
});
/*var graph = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
datasets: [{
borderColor: '#36a2eb',
borderWidth: 1,
backgroundColor: '#84CBFA',
data: [1,2,4,5,6,7,8]
}]
},
options: {
responsive: true,
animation:{
duration:0
},
title: {
display: true,
text: graphTitle
},
legend: {
display: false
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
type: 'linear',
position: 'bottom',
scaleLabel: {
display: true,
labelString: xTitle
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: yTitle
}
}]
}
}
});**/
}
});
}
Loading

0 comments on commit eda5bec

Please sign in to comment.