From 33ede5c65e25ee34f2c7b777774d40a27efedb8f Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 31 Mar 2015 16:26:46 -0300 Subject: [PATCH] add getActivity(addresses) to blockchain explorer --- lib/blockchainexplorer.js | 18 ++++++++++++++++-- lib/server.js | 2 +- test/blockchainexplorer.js | 1 + test/integration/server.js | 2 +- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/blockchainexplorer.js b/lib/blockchainexplorer.js index c11a82c..f32772f 100644 --- a/lib/blockchainexplorer.js +++ b/lib/blockchainexplorer.js @@ -29,6 +29,7 @@ function BlockChainExplorer(opts) { } var explorer = new Explorers.Insight(url, network); explorer.getTransactions = _.bind(getTransactionsInsight, explorer, url); + explorer.getActivity = _.bind(getActivityInsight, explorer, url); explorer.initSocket = _.bind(initSocketInsight, explorer, url); return explorer; default: @@ -36,10 +37,16 @@ function BlockChainExplorer(opts) { }; }; -function getTransactionsInsight(url, addresses, cb) { +function getTransactionsInsight(url, addresses, from, to, cb) { + var qs = []; + if (_.isNumber(from)) qs.push('from=' + from); + if (_.isNumber(to)) qs.push('to=' + to); + + var url = url + '/api/addrs/txs' + (qs.length > 0 ? '?' + qs.join('&') : ''); + request({ method: "POST", - url: url + '/api/addrs/txs', + url: url, json: { addrs: [].concat(addresses).join(',') } @@ -49,6 +56,13 @@ function getTransactionsInsight(url, addresses, cb) { }); }; +function getActivityInsight(url, addresses, cb) { + getTransactionsInsight(url, addresses, 0, 0, function(err, result) { + if (err) return cb(err); + return cb(null, result.items > 0); + }); +}; + function initSocketInsight(url) { var socket = io.connect(url, {}); return socket; diff --git a/lib/server.js b/lib/server.js index 33ec1e3..4784e72 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1024,7 +1024,7 @@ WalletService.prototype.getTxHistory = function(opts, cb) { }); }, function(next) { - bc.getTransactions(addressStrs, function(err, txs) { + bc.getTransactions(addressStrs, null, null, function(err, txs) { if (err) return next(err); next(null, self._normalizeTxHistory(txs)); diff --git a/test/blockchainexplorer.js b/test/blockchainexplorer.js index b1fbd3d..b57e33a 100644 --- a/test/blockchainexplorer.js +++ b/test/blockchainexplorer.js @@ -16,6 +16,7 @@ describe('Blockchain explorer', function() { should.exist(exp); exp.should.respondTo('broadcast'); exp.should.respondTo('getTransactions'); + exp.should.respondTo('getActivity'); exp.should.respondTo('getUnspentUtxos'); exp.should.respondTo('initSocket'); var exp = BlockchainExplorer({ diff --git a/test/integration/server.js b/test/integration/server.js index bd700e2..5aa0fd9 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -166,7 +166,7 @@ helpers.stubBroadcastFail = function() { }; helpers.stubHistory = function(txs) { - blockchainExplorer.getTransactions = sinon.stub().callsArgWith(1, null, txs); + blockchainExplorer.getTransactions = sinon.stub().callsArgWith(3, null, txs); }; helpers.clientSign = WalletUtils.signTxp;