diff --git a/server/lib/api/address.js b/server/lib/api/address.js index 47cc1dd..c2a9e1a 100644 --- a/server/lib/api/address.js +++ b/server/lib/api/address.js @@ -8,7 +8,7 @@ module.exports = function AddressAPI(router) { router.get('/addr/:addr', (req, res) => { const addr = req.params.addr || ''; // Get Bcoin data - request(`${API_URL}/tx/address/${addr}`, + return request(`${API_URL}/tx/address/${addr}`, (error, bcoinRes, txs) => { if (error) { logger.log('error', @@ -41,17 +41,17 @@ module.exports = function AddressAPI(router) { // Match Insight API return res.json({ - addrStr: req.params.addr, - balance: (totalReceived - totalSpent) / 1e8, - balanceSat: totalReceived - totalSpent, - totalReceived: totalReceived / 1e8, - totalReceivedSat: totalReceived, - totalSent: totalSpent / 1e8, - totalSentSat: totalSpent, - unconfirmedBalance: 0, - unconfirmedBalanceSat: 0, + addrStr: req.params.addr, + balance: (totalReceived - totalSpent) / 1e8, + balanceSat: totalReceived - totalSpent, + totalReceived: totalReceived / 1e8, + totalReceivedSat: totalReceived, + totalSent: totalSpent / 1e8, + totalSentSat: totalSpent, + unconfirmedBalance: 0, + unconfirmedBalanceSat: 0, unconfirmedTxApperances: 0, - txApperances: txs.length, + txApperances: txs.length, }); }); }); diff --git a/server/lib/api/currency.js b/server/lib/api/currency.js index 0e55496..80af7aa 100644 --- a/server/lib/api/currency.js +++ b/server/lib/api/currency.js @@ -16,6 +16,7 @@ setInterval(() => { getRate(); }, refreshInterval); +// Make the request to the remote API function getRate() { request(config.api.ticker_url, (err, res, body) => { if (err) { diff --git a/server/lib/api/index.js b/server/lib/api/index.js index c871e61..3bc0d98 100644 --- a/server/lib/api/index.js +++ b/server/lib/api/index.js @@ -7,9 +7,7 @@ const api = express.Router(); const cors = require('./cors'); app.use(cors); - -app.use(bodyParser.urlencoded({ extended: false })) - +app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); // Serve insight ui front end from root dir public folder @@ -47,6 +45,7 @@ app.use((req, res) => { }); }); +// Socket server const server = require('http').Server(app); const io = require('socket.io')(server); diff --git a/server/lib/api/transaction.js b/server/lib/api/transaction.js index 007c313..4cc6306 100644 --- a/server/lib/api/transaction.js +++ b/server/lib/api/transaction.js @@ -10,10 +10,7 @@ module.exports = function transactionAPI(router) { // Txs by txid router.get('/tx/:txid', (req, res) => { // Get max block height for calculating confirmations - db.blocks.getBlock( - {}, - { height: 1 }, - 1, + db.blocks.getBestHeight( (err, block) => { if (err) { logger.log('err', err); @@ -22,7 +19,7 @@ module.exports = function transactionAPI(router) { const height = block.height; // Bcoin transaction data - request(`${API_URL}/tx/${req.params.txid}`, (error, localRes, tx) => { + return request(`${API_URL}/tx/${req.params.txid}`, (error, localRes, tx) => { if (error) { logger.log('error', `${error}`); @@ -73,10 +70,7 @@ module.exports = function transactionAPI(router) { const rangeEnd = rangeStart + MAX_TXS; // get txs for blockhash if (req.query.block) { - db.blocks.getBlock( - {}, - { height: 1 }, - 1, + db.blocks.getBestHeight( (err, block) => { if (err) { logger.log('err', err); @@ -84,7 +78,7 @@ module.exports = function transactionAPI(router) { } const height = block.height; // Get Bcoin data - request(`${API_URL}/block/${req.query.block}`, (error, localRes, block) => { + return request(`${API_URL}/block/${req.query.block}`, (error, localRes, block) => { if (error) { logger.log('error', `${error}`); @@ -129,9 +123,6 @@ module.exports = function transactionAPI(router) { } else if (req.query.address) { // Get txs by address db.blocks.getBestHeight( - {}, - { height: 1 }, - 1, (err, block) => { if (err) { logger.log('err', err); @@ -141,7 +132,7 @@ module.exports = function transactionAPI(router) { const height = block.height; const addr = req.query.address || ''; - request(`${API_URL}/tx/address/${req.query.address}`, (error, localRes, txs) => { + return request(`${API_URL}/tx/address/${req.query.address}`, (error, localRes, txs) => { if (error) { logger.log('error', `${error}`); diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index 563620c..d0c9a53 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -53,7 +53,7 @@ function getBlock(params, options, limit, cb) { return cb(null, blocks[0]); }); } - +// Highest known height function getBestHeight(cb) { getBlock({}, {}, 1, (err, block) => { if (err) {