From 50aeb9d3214f86b9118f76ae9b1d0fe8e8e8229b Mon Sep 17 00:00:00 2001 From: joeuhren <46763106+joeuhren@users.noreply.github.com> Date: Thu, 10 Dec 2020 17:51:46 -0700 Subject: [PATCH] Fix getlasttxs api -Completely removed the /ext/getlasttxsajax api and replaced with /ext/getlasttxs -Fixed record count for movement page --- app.js | 6 ------ lib/database.js | 35 +++++++++++++++++++++++++---------- views/index.pug | 2 +- views/movement.pug | 2 +- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app.js b/app.js index 6bc64b0..4c709fa 100644 --- a/app.js +++ b/app.js @@ -210,12 +210,6 @@ app.use('/ext/getlasttxs/:min', function(req, res) { }); }); -app.use('/ext/getlasttxsajax/:min', function(req, res){ - db.get_last_txs(req, function(data, draw, count) { - res.json({"data":data, "draw": draw, "recordsTotal": count, "recordsFiltered": count}); - }); -}); - app.use('/ext/getaddresstxsajax/:address', function(req,res){ req.query.length = parseInt(req.query.length); if(isNaN(req.query.length) || req.query.length > settings.txcount){ diff --git a/lib/database.js b/lib/database.js index 2d4b7f9..1013f49 100644 --- a/lib/database.js +++ b/lib/database.js @@ -422,17 +422,32 @@ module.exports = { }, get_last_txs_ajax: function(start, length, min, cb) { - // Lookup the coin stats to get the txes value which is used to determine the total # of records - Stats.findOne({coin:settings.coin}, function(err, stats) { - // Get last transactions where there is at least 1 vout - Tx.find({'total': {$gte: min}, 'vout': { $gte: { $size: 1 }}}).sort({blockindex: -1}).skip(Number(start)).limit(Number(length)).exec(function(err, txs) { - if (err) { - return cb(err); - } else { - return cb(txs, stats.txes); - } + // check if min is greater than zero + if (min > 0) { + // min is greater than zero which means we must pull record count from the txes collection + Tx.find({'total': {$gte: min}}).countDocuments(function(err, count) { + // Get last transactions where there is at least 1 vout + Tx.find({'total': {$gte: min}, 'vout': { $gte: { $size: 1 }}}).sort({blockindex: -1}).skip(Number(start)).limit(Number(length)).exec(function(err, txs) { + if (err) { + return cb(err); + } else { + return cb(txs, count); + } + }); }); - }); + } else { + // min is zero (shouldn't ever be negative) which means we must pull record count from the coinstats collection (pulling from txes could potentially take a long time because it would include coinbase txes) + Stats.findOne({coin:settings.coin}, function(err, stats) { + // Get last transactions where there is at least 1 vout + Tx.find({'total': {$gte: min}, 'vout': { $gte: { $size: 1 }}}).sort({blockindex: -1}).skip(Number(start)).limit(Number(length)).exec(function(err, txs) { + if (err) { + return cb(err); + } else { + return cb(txs, stats.txes); + } + }); + }); + } }, get_address_txs_ajax: function(hash, start, length, cb) { diff --git a/views/index.pug b/views/index.pug index d710a5c..892d764 100644 --- a/views/index.pug +++ b/views/index.pug @@ -28,7 +28,7 @@ block content serverSide: true, iDisplayLength: displayLengthMax, lengthMenu: lengthMenuOpts, - ajax: '/ext/getlasttxsajax/0', + ajax: '/ext/getlasttxs/0', language: { paginate: { previous: '<', diff --git a/views/movement.pug b/views/movement.pug index 99998ba..fbeb24a 100644 --- a/views/movement.pug +++ b/views/movement.pug @@ -29,7 +29,7 @@ block content serverSide: true, iDisplayLength: displayLengthMax, lengthMenu: lengthMenuOpts, - ajax: '/ext/getlasttxsajax/#{min_amount}', + ajax: '/ext/getlasttxs/#{min_amount}', language: { paginate: { previous: '<',