From 6e9f946607034cdf71db87f7b24539c08f936dcd Mon Sep 17 00:00:00 2001 From: sairajzero Date: Sat, 28 Jan 2023 21:01:10 +0530 Subject: [PATCH] Update index.js --- lib/services/address/index.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 14a1a7b4..bee7a4a6 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -207,9 +207,12 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream if(err) return log.error(err); - if(!options.txNotNeeded && results.items.length < MAX_TX_QUERY_LIMIT) - if(!results.items.some(x => x.txid() === tx.txid())) //push only if tx not already in array + if(!options.txNotNeeded) { + let count = self._getOccurrenceCount(tx, address); + results.totalCount += 1 / count; //fix for duplication + if(results.items.length < MAX_TX_QUERY_LIMIT && !results.items.some(x => x.txid() === tx.txid())) //push only if tx not already in array results.items.push(tx); + } streamer(null, tx); @@ -223,7 +226,6 @@ AddressService.prototype.getAddressHistory = function(addresses, options, stream //sort items in desc block-height, then asc txid (if same height) results.items.sort((a, b) => b.__height - a.__height || a.txid().localeCompare(b.txid())); - console.debug("ITEMS:", results.items) //TODO: sorting of tx list (results.items) callback(null, results); @@ -405,6 +407,31 @@ AddressService.prototype._getAddressSummaryResult = function(txs, address, resul return result; }; +AddressService.prototype._getOccurrenceCount = function(tx, address) { + let count = 0; + + for(var i = 0; i < tx.inputs.length; i++) { + + var input = tx.inputs[i]; + + if(utils.getAddress(input, this._network) === address) + count++; + + } + + for(var j = 0; j < tx.outputs.length; j++) { + + var output = tx.outputs[j]; + + if(utils.getAddress(output, this._network) === address) + count++; + + } + + return count; + +} + AddressService.prototype._getOutputResults = function(tx, address) { let result = { value: 0, count:0 };