diff --git a/server/.eslintrc.json b/server/.eslintrc.json index e642fb4..5139d8a 100644 --- a/server/.eslintrc.json +++ b/server/.eslintrc.json @@ -9,6 +9,8 @@ "no-use-before-define": 1, "object-shorthand": 1, "key-spacing": 0, - "no-plusplus": 0 + "no-plusplus": 0, + "no-unused-vars": 1, + "no-param-reassign": 1 } } \ No newline at end of file diff --git a/server/index.js b/server/index.js index 23d3125..47b2141 100644 --- a/server/index.js +++ b/server/index.js @@ -10,7 +10,6 @@ logger.log('debug', db.connect(config.mongodb.uri, config.mongodb.options); db.connection.once('open', () => { - if (config.start_node) Bcoin.start(); Api.listen(config.api.port, () => { diff --git a/server/lib/api/address.js b/server/lib/api/address.js index c2a9e1a..ed0cab9 100644 --- a/server/lib/api/address.js +++ b/server/lib/api/address.js @@ -9,14 +9,15 @@ module.exports = function AddressAPI(router) { const addr = req.params.addr || ''; // Get Bcoin data return request(`${API_URL}/tx/address/${addr}`, - (error, bcoinRes, txs) => { + (error, bcoinRes, bcoinTxs) => { if (error) { logger.log('error', `${error}`); return res.status(404).send({}); } + let txs = {}; try { - txs = JSON.parse(txs); + txs = JSON.parse(bcoinTxs); } catch (e) { logger.log('error', `${e}`); @@ -24,7 +25,7 @@ module.exports = function AddressAPI(router) { } // Sum the matching outputs for every tx - const totalReceived = txs.reduce((sum, tx) => sum + tx.outputs.reduce((sum, output) => { + const totalReceived = txs.reduce((total, tx) => total + tx.outputs.reduce((sum, output) => { if (output.address === req.params.addr) { return sum + output.value; } @@ -32,7 +33,7 @@ module.exports = function AddressAPI(router) { }, 0), 0) || 0; // Sum the matching inputs for every tx - const totalSpent = txs.reduce((sum, tx) => sum + tx.inputs.reduce((sum, input) => { + const totalSpent = txs.reduce((total, tx) => total + tx.inputs.reduce((sum, input) => { if (input.coin && input.coin.address === req.params.addr) { return sum + input.coin.value; } diff --git a/server/lib/api/block.js b/server/lib/api/block.js index 9c3af3c..482ea0e 100644 --- a/server/lib/api/block.js +++ b/server/lib/api/block.js @@ -37,7 +37,7 @@ module.exports = function BlockAPI(router) { }); router.get('/blocks', (req, res) => { - const limit = parseInt(req.query.limit) || 100; + const limit = parseInt(req.query.limit, 10) || 100; // Pass Mongo params, fields and limit to db api. db.blocks.getBlocks( {}, @@ -89,7 +89,7 @@ module.exports = function BlockAPI(router) { }); router.get('/block-index/:height', (req, res) => { - const blockHeight = parseInt(req.params.height) || 1; + const blockHeight = parseInt(req.params.height, 10) || 1; // Pass Mongo params, fields and limit to db api. db.blocks.getBlock( { height: blockHeight }, diff --git a/server/lib/api/cors.js b/server/lib/api/cors.js index 1e4bdcd..42c9833 100644 --- a/server/lib/api/cors.js +++ b/server/lib/api/cors.js @@ -1,5 +1,5 @@ module.exports = function (req, res, next) { - let allowed = { + const allowed = { origins: [ '*', diff --git a/server/lib/api/currency.js b/server/lib/api/currency.js index 80af7aa..1f03b89 100644 --- a/server/lib/api/currency.js +++ b/server/lib/api/currency.js @@ -10,11 +10,15 @@ const refreshInterval = config.api.currency_refresh >= 1 ? 60 * 1000; let lastRate = 0; -getRate(); +init(); -setInterval(() => { +function init() { getRate(); -}, refreshInterval); + + setInterval(() => { + getRate(); + }, refreshInterval); +} // Make the request to the remote API function getRate() { @@ -38,7 +42,7 @@ function getRate() { module.exports = function currencyAPI(app) { // Return the ticker price app.get('/currency', (req, res) => { - const data = {} + const data = {}; data[config.api.ticker_prop] = lastRate; res.json({ diff --git a/server/lib/api/socket.js b/server/lib/api/socket.js index 7882fc7..fef4ee8 100644 --- a/server/lib/api/socket.js +++ b/server/lib/api/socket.js @@ -50,10 +50,10 @@ function emitTx(transaction) { const txJSON = transaction.toJSON(); io.sockets.emit('tx', { txid: txJSON.hash, - valueOut: transaction.outputs.reduce((sum, tx) => { - tx = tx.toJSON(); + valueOut: transaction.outputs.reduce((sum, output) => { + output = output.toJSON(); - const valB = (tx.value || tx.valueOut.value || 0) / 1e8; + const valB = (output.value || output.valueOut.value || 0) / 1e8; return sum + valB; }, 0), diff --git a/server/lib/api/status.js b/server/lib/api/status.js index f273e9d..514d5db 100644 --- a/server/lib/api/status.js +++ b/server/lib/api/status.js @@ -53,11 +53,11 @@ module.exports = function statusAPI(router) { return res.status(404).send(err); } if (!status) { - logger.log('err' - `/status getStatus: no Status`); + logger.log('err', + '/status getStatus: no Status'); return res.status(404).send(); } - res.json({ + return res.json({ info: { version: status.version, protocolversion: netCfg.PROTOCOL_VERSION, @@ -88,8 +88,8 @@ module.exports = function statusAPI(router) { '/sync: no status'); return res.status(404).send(); } - res.json({ - status: status.chain.progress === 100 ? 'synced': 'syncing', + return res.json({ + status: status.chain.progress === 100 ? 'synced' : 'syncing', blockChainHeight: status.chain.height, syncPercentage: Math.round(status.chain.progress * 100), height: status.chain.height, diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index 697ffb2..fb79b87 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -47,7 +47,7 @@ module.exports = function transactionAPI(router) { locktime: tx.locktime, blockhash: tx.block, fees: tx.fee / 1e8, - confirmations: height - tx.height + 1, + confirmations: (height - tx.height) + 1, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, vin: tx.inputs.map(input => ({ addr: input.coin ? input.coin.address : '', @@ -70,12 +70,11 @@ module.exports = function transactionAPI(router) { // query by address // last n txs router.get('/txs', (req, res) => { - const pageNum = parseInt(req.query.pageNum) || 0; + const pageNum = parseInt(req.query.pageNum, 10) || 0; const rangeStart = pageNum * MAX_TXS; const rangeEnd = rangeStart + MAX_TXS; // get txs for blockhash, start with best height to calc confirmations if (req.query.block) { - db.blocks.getBestHeight( (err, blockHeight) => { if (err) { @@ -111,7 +110,7 @@ module.exports = function transactionAPI(router) { txs: block.txs.map(tx => ({ txid: tx.hash, fees: tx.fee / 1e8, - confirmations: height - block.height + 1, + confirmations: (height - block.height) + 1, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, vin: tx.inputs.map(input => ({ addr: input.coin ? input.coin.address : '', @@ -160,7 +159,7 @@ module.exports = function transactionAPI(router) { txs: txs.map(tx => ({ txid: tx.hash, fees: tx.fee / 1e8, - confirmations: height - tx.height + 1, + confirmations: (height - tx.height) + 1, valueOut: tx.outputs.reduce((sum, output) => sum + output.value, 0) / 1e8, vin: tx.inputs.map(input => ({ addr: input.coin ? input.coin.address : '',