diff --git a/README.md b/README.md index 4d5208ce8..bea57cdc1 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,8 @@ Example response: /insight-api/addr/[:addr]/totalReceived /insight-api/addr/[:addr]/totalSent /insight-api/addr/[:addr]/unconfirmedBalance + + /insight-api/addrs/[:addrs]/summary ``` The response contains the value in Satoshis. diff --git a/lib/addresses.js b/lib/addresses.js index 7fb8f796a..8cbd333b6 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -278,6 +278,64 @@ AddressController.prototype.multitxs = function(req, res) { }); }; +// this call could take a while to run depending on what addresses are used +// considering memory constraints, we will streaming out the results for addresses +// not necessarily in the order we received them + + +AddressController.prototype.multisummary = function(req, res) { + + var self = this; + + var addresses; + + if (_.isArray(req.addrs)) { + addresses = _.uniq(req.addrs); + } else { + addresses = _.compact(req.addrs.split(',')); + } + + var addressesLeft = addresses.length; + var startedWriting = false; + var cache = []; + + res.write('['); + + var sep = ','; + + async.eachLimit(addresses, 4, function(addr, next) { + + self._address.getAddressSummary(addr, {}, function(err, summary) { + + if (err) { + return next(err); + } + + if (addressesLeft-- > 0 && startedWriting) { + res.write(sep); + } + startedWriting = true; + sep = ''; + cache.push(summary); + res.write(JSON.stringify(summary) + sep); + sep = ','; + next(); + }); + + }, function(err) { + + if (err) { + return self.common.handleErrors(err, res); + } + + res.write(']'); + res.end(); + }); + +}; + + + AddressController.prototype.transformAddressHistoryForMultiTxs = function(txs, options, callback) { var self = this; diff --git a/lib/index.js b/lib/index.js index c6519cf9f..6de8b561a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -227,6 +227,8 @@ InsightAPI.prototype.setupRoutes = function(app) { app.get('/addr/:addr/totalReceived', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.totalReceived.bind(addresses)); app.get('/addr/:addr/totalSent', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.totalSent.bind(addresses)); app.get('/addr/:addr/unconfirmedBalance', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.unconfirmedBalance.bind(addresses)); + + app.get('/addrs/:addrs/summary', this.cacheShort(), addresses.checkAddrs.bind(addresses), addresses.multisummary.bind(addresses)); // Status route var status = new StatusController(this.node);