From 61f99d94b515e490a5843e04f38963a7a5ccba0e Mon Sep 17 00:00:00 2001 From: eordano Date: Fri, 20 Mar 2015 13:19:40 -0300 Subject: [PATCH] Drop duplicated code on addressService --- lib/services/address.js | 56 +++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/services/address.js b/lib/services/address.js index b5f2e862..47c37896 100644 --- a/lib/services/address.js +++ b/lib/services/address.js @@ -53,41 +53,37 @@ AddressService.processOutput = function(data) { return output; }; -AddressService.prototype.getAllOutputs = function(address) { - var results = []; - var self = this; +var retrieveOutputs = function(indexFunction, processElement) { + return function(address) { + var results = []; + var self = this; - return new Promise(function(resolve, reject) { - self.database.createReadStream({ - gte: TransactionService.Index.getOutputsForAddress(address, NULLTXHASH, 0), - lte: TransactionService.Index.getOutputsForAddress(address, LASTTXHASH, MAXOUTPUT) - }).on('data', function(element) { - results.push(AddressService.processOutput(element)); - }).on('error', function() { - return reject(); - }).on('end', function() { - return resolve(results); + return new Promise(function(resolve, reject) { + self.database.createReadStream({ + gte: indexFunction(address, NULLTXHASH, 0), + lte: indexFunction(address, LASTTXHASH, MAXOUTPUT) + }).on('data', function(element) { + results.push(processElement(element)); + }).on('error', reject).on('end', function() { + return resolve(results); + }); }); - }); + }; }; -AddressService.prototype.getSpent = function(address) { - var results = []; - var self = this; +AddressService.prototype.getAllOutputs = retrieveOutputs( + TransactionService.Index.getOutputsForAddress, + function(e) { + return AddressService.processOutput(e); + } +); - return new Promise(function(resolve, reject) { - self.database.createReadStream({ - gte: TransactionService.Index.getSpentOutputsForAddress(address, NULLTXHASH, 0), - lte: TransactionService.Index.getSpentOutputsForAddress(address, LASTTXHASH, MAXOUTPUT) - }).on('data', function(element) { - results.push(JSON.parse(element.value)); - }).on('error', function(err) { - return reject(err); - }).on('end', function() { - return resolve(results); - }); - }); -}; +AddressService.prototype.getSpent = retrieveOutputs( + TransactionService.Index.getSpentOutputsForAddress, + function(element) { + return JSON.parse(element.value); + } +); AddressService.prototype.buildAddressSummary = function(address, tip, allOutputs, spent, confirmations) {