Browse Source

optimize activity check for single address

activeAddress
Ivan Socolsky 9 years ago
parent
commit
89575da4e8
  1. 20
      lib/blockchainexplorers/insight.js

20
lib/blockchainexplorers/insight.js

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var _ = require('lodash'); var _ = require('lodash');
var async = require('async');
var $ = require('preconditions').singleton(); var $ = require('preconditions').singleton();
var log = require('npmlog'); var log = require('npmlog');
log.debug = log.verbose; log.debug = log.verbose;
@ -114,8 +115,10 @@ Insight.prototype.getTransactions = function(addresses, from, to, cb) {
}; };
Insight.prototype.getAddressActivity = function(addresses, cb) { Insight.prototype.getAddressActivity = function(addresses, cb) {
function getOneAddressActivity(address, cb) { var self = this;
var url = this.url + this.apiPrefix + '/addr/' + address;
function getOneAddressActivity(address, cb2) {
var url = self.url + self.apiPrefix + '/addr/' + address;
var args = { var args = {
method: 'GET', method: 'GET',
url: url, url: url,
@ -123,21 +126,26 @@ Insight.prototype.getAddressActivity = function(addresses, cb) {
}; };
request(args, function(err, res, result) { request(args, function(err, res, result) {
if (res && res.statusCode == 404) return cb(); if (res && res.statusCode == 404) return cb2();
if (err || res.statusCode !== 200) if (err || res.statusCode !== 200)
return cb(_parseErr(err, res)); return cb2(_parseErr(err, res));
var nbTxs = result.unconfirmedTxApperances + result.txApperances; var nbTxs = result.unconfirmedTxApperances + result.txApperances;
return cb(null, nbTxs > 0); return cb2(null, nbTxs > 0);
}); });
}; };
addresses = [].concat(addresses); addresses = [].concat(addresses);
if (addresses.length == 1) {
return getOneAddressActivity(addresses[0], cb);
}
var activityFound = false; var activityFound = false;
var i = 0; var i = 0;
async.until(function() { async.until(function() {
return activityFound; return i == addresses.length || activityFound;
}, function(next) { }, function(next) {
getOneAddressActivity(addresses[i++], function(err, res) { getOneAddressActivity(addresses[i++], function(err, res) {
if (err) return next(err); if (err) return next(err);

Loading…
Cancel
Save