diff --git a/integration/regtest-node.js b/integration/regtest-node.js index 8cd91990..18724c15 100644 --- a/integration/regtest-node.js +++ b/integration/regtest-node.js @@ -293,6 +293,25 @@ describe('Node Functionality', function() { done(); }); }); + it('correctly give the summary for the address', function(done) { + var options = { + queryMempool: false + }; + node.services.address.getAddressSummary(address, options, function(err, results) { + if (err) { + throw err; + } + + results.totalReceived.should.equal(1000000000); + results.totalSpent.should.equal(0); + results.balance.should.equal(1000000000); + results.unconfirmedBalance.should.equal(1000000000); + results.appearances.should.equal(1); + results.unconfirmedAppearances.should.equal(0); + results.txids.length.should.equal(1); + done(); + }); + }); describe('History', function() { this.timeout(20000); diff --git a/lib/services/address/index.js b/lib/services/address/index.js index f08becf5..5761c98d 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -890,6 +890,8 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba * txids - list of txids (unless noTxList is set) * * @param {String} address + * @param {Object} options + * @param {Boolean} options.noTxList - if set, txid array will not be included * @param {Function} callback */ AddressService.prototype.getAddressSummary = function(address, options, callback) { diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index 9bf9622d..8dd3df87 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -31,7 +31,7 @@ describe('Address Service', function() { it('should return the correct methods', function() { var am = new AddressService({node: mocknode}); var methods = am.getAPIMethods(); - methods.length.should.equal(5); + methods.length.should.equal(6); }); }); @@ -954,4 +954,81 @@ describe('Address Service', function() { }); }); }); + describe('#getAddressSummary', function() { + var node = { + services: { + bitcoind: { + isSpent: sinon.stub().returns(false), + on: sinon.spy() + } + } + }; + var inputs = [ + { + "txid": "9f183412de12a6c1943fc86c390174c1cde38d709217fdb59dcf540230fa58a6", + "height": -1, + "confirmations": 0, + "addresses": { + "mpkDdnLq26djg17s6cYknjnysAm3QwRzu2": { + "outputIndexes": [], + "inputIndexes": [ + 3 + ] + } + }, + "address": "mpkDdnLq26djg17s6cYknjnysAm3QwRzu2" + } + ]; + + var outputs = [ + { + "address": "mpkDdnLq26djg17s6cYknjnysAm3QwRzu2", + "txid": "689e9f543fa4aa5b2daa3b5bb65f9a00ad5aa1a2e9e1fc4e11061d85f2aa9bc5", + "outputIndex": 0, + "height": 556351, + "satoshis": 3487110, + "script": "76a914653b58493c2208481e0902a8ffb97b8112b13fe188ac", + "confirmations": 13190 + } + ]; + + var as = new AddressService({node: node}); + as.getInputs = sinon.stub().callsArgWith(2, null, inputs); + as.getOutputs = sinon.stub().callsArgWith(2, null, outputs); + as.mempoolSpentIndex = { + '689e9f543fa4aa5b2daa3b5bb65f9a00ad5aa1a2e9e1fc4e11061d85f2aa9bc5-0': true + }; + + it('should handle unconfirmed and confirmed outputs and inputs', function(done) { + as.getAddressSummary('mpkDdnLq26djg17s6cYknjnysAm3QwRzu2', {}, function(err, summary) { + should.not.exist(err); + summary.totalReceived.should.equal(3487110); + summary.totalSpent.should.equal(0); + summary.balance.should.equal(3487110); + summary.unconfirmedBalance.should.equal(0); + summary.appearances.should.equal(1); + summary.unconfirmedAppearances.should.equal(1); + summary.txids.should.deep.equal( + [ + '9f183412de12a6c1943fc86c390174c1cde38d709217fdb59dcf540230fa58a6', + '689e9f543fa4aa5b2daa3b5bb65f9a00ad5aa1a2e9e1fc4e11061d85f2aa9bc5' + ] + ); + done(); + }); + }); + it('noTxList should not include txids array', function(done) { + as.getAddressSummary('mpkDdnLq26djg17s6cYknjnysAm3QwRzu2', {noTxList: true}, function(err, summary) { + should.not.exist(err); + summary.totalReceived.should.equal(3487110); + summary.totalSpent.should.equal(0); + summary.balance.should.equal(3487110); + summary.unconfirmedBalance.should.equal(0); + summary.appearances.should.equal(1); + summary.unconfirmedAppearances.should.equal(1); + should.not.exist(summary.txids); + done(); + }); + }); + }); });