From f10106f9a0e8c031b1d4db5290d8245946851be0 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Sat, 7 Oct 2017 15:48:11 -0400 Subject: [PATCH] Added sort options to get address history. --- lib/services/address/index.js | 20 ++++---------------- lib/utils.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/services/address/index.js b/lib/services/address/index.js index 6d3c63f8..b5bad5e3 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -12,6 +12,7 @@ var Encoding = require('./encoding'); var Transform = require('stream').Transform; var assert = require('assert'); var Stream = require('stream'); +var utils = require('../../utils'); var AddressService = function(options) { @@ -54,8 +55,6 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba options.to = options.to || 0xffffffff; options.queryMempool = _.isUndefined(options.queryMempool) ? true : false; - var txIdList = []; - if (_.isString(addresses)) { addresses = [addresses]; } @@ -64,25 +63,14 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba self._getAddressHistory(address, options, next); - }, function(err, txLists) { + }, function(err, txList) { if(err) { return callback(err); } - txLists = _.compact(_.flattenDeep(txLists)); - var txList = []; - - for(var i = 0; i < txLists.length; i++) { - var tx = txLists[i]; - if (txIdList.indexOf(tx.txid()) !== -1) { - continue; - } - txIdList.push(tx.txid()); - txList.push(tx); - } - - txList.reverse(); + txList = utils.dedupByTxid(txList); + txList = utils.orderByConfirmationsDesc(txList); var results = { totalCount: txList.length, diff --git a/lib/utils.js b/lib/utils.js index 90d27de0..41b231f5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -149,4 +149,20 @@ utils.convertMillisecondsToHumanReadable = function(ms) { }; +utils.dedupByTxid = function(list) { + var used = []; + return _.compact(_.flattenDeep(list)).filter(function(item) { + var pass = used.indexOf(item.txid()) === -1; + used.push(item.txid()); + return pass; + }); +}; + +utils.orderByConfirmations = function(list) { + // newly confirmed first + return _.sortBy(list, function(item) { + return item.confirmations; + }); +}; + module.exports = utils;