From a254676a9b0e83942b876a40900009bbd7f80505 Mon Sep 17 00:00:00 2001 From: joeuhren <46763106+joeuhren@users.noreply.github.com> Date: Sun, 22 Nov 2020 18:27:04 -0700 Subject: [PATCH] Historical Address Balance --- lib/database.js | 48 +++++++++++++++++++++++++++++---------------- models/addresstx.js | 2 +- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/database.js b/lib/database.js index 7d13433..0a36c1c 100644 --- a/lib/database.js +++ b/lib/database.js @@ -87,11 +87,13 @@ function update_address(hash, blockheight, txid, amount, type, cb) { } else { if ( hash != 'coinbase' ) { AddressTx.findOneAndUpdate({a_id: hash, txid: txid}, { + $inc: { + amount: addr_inc.balance + }, $set: { a_id: hash, blockindex: blockheight, - txid: txid, - amount: address.balance + txid: txid } }, { new: true, @@ -448,42 +450,55 @@ module.exports = { get_address_txs_ajax: function(hash, start, length, cb) { var totalCount = 0; - Address.findOne({a_id: hash}, function(err, addressTotalTxs) { - + AddressTx.find({a_id: hash}).countDocuments({}, function(err, count){ if(err) { return cb(err); } else { - AddressTx.find({a_id: hash}).count({}, function(err, count){ - if(err) { + totalCount = count; + AddressTx.aggregate([ + { $match: { a_id: hash } }, + { $sort: {blockindex: -1} }, + { $skip: Number(start) }, + { + $group: { + _id: '', + balance: { $sum: '$amount' } + } + }, + { + $project: { + _id: 0, + balance: '$balance' + } + }, + { $sort: {blockindex: -1} } + ], function (err,balance_sum) { + if (err) { return cb(err); } else { - totalCount = count; - AddressTx.find({a_id: hash}).sort({blockindex: 'desc'}).skip(Number(start)).limit(Number(length)).exec(function (err, address) { + AddressTx.find({a_id: hash}).sort({blockindex: 'desc'}).skip(Number(start)).limit(Number(length)).exec(function (err, address_tx) { if (err) { return cb(err); } else { var txs = []; - var count = address.length; - var hashes = address; - + var count = address_tx.length; + var running_balance = balance_sum[0].balance; var txs = []; lib.syncLoop(count, function (loop) { var i = loop.iteration(); find_tx(hashes[i].txid, function (tx) { if (tx && !txs.includes(tx)) { - // tx = {...hashes[i], ...tx} - tx.balance = hashes[i].balance; + tx.balance = running_balance; txs.push(tx); loop.next(); } else if (!txs.includes(tx)) { - // tx = {...hashes[i], ...tx} - tx.balance = hashes[i].balance; txs.push("1. Not found"); loop.next(); } else { loop.next(); } + running_balance = running_balance - address_tx[i].amount; }) }, function () { return cb(txs, totalCount); @@ -492,7 +507,6 @@ module.exports = { }); } }); - } }); }, @@ -787,7 +801,7 @@ module.exports = { var task_limit_blocks = settings.block_parallel_tasks; if (typeof start === 'undefined' || start < 1) start = 1; // fix for invalid block height (skip genesis block as it should not have valid txs) if (task_limit_blocks < 1) { task_limit_blocks = 1; } - var task_limit_txs = 1 + var task_limit_txs = 1; for (i=start; i<(end+1); i++) { blocks_to_scan.push(i); } diff --git a/models/addresstx.js b/models/addresstx.js index c1fdb10..2bb7ff4 100644 --- a/models/addresstx.js +++ b/models/addresstx.js @@ -5,7 +5,7 @@ var AddressTXSchema = new Schema({ a_id: { type: String, index: true}, blockindex: {type: Number, default: 0, index: true}, txid: { type: String, lowercase: true, index: true}, - amount: { type: Number, default: 0} + amount: { type: Number, default: 0, index: true} }, {id: false}); module.exports = mongoose.model('AddressTx', AddressTXSchema); \ No newline at end of file