pointing to new Insight api for retrieving txs

This commit is contained in:
Ivan Socolsky 2014-11-13 16:32:26 -03:00 committed by Matias Alejo Garcia
commit 8e92864627
2 changed files with 11 additions and 48 deletions

View file

@ -269,39 +269,15 @@ Insight.prototype.getTransaction = function(txid, cb) {
});
};
Insight.prototype.getTransactions = function(addresses, cb) {
Insight.prototype.getTransactions = function (addresses, cb) {
preconditions.shouldBeArray(addresses);
preconditions.shouldBeFunction(cb);
var self = this;
if (!addresses.length) return cb(null, []);
// Iterator: get a list of transaction ids for an address
function getTransactionIds(address, next) {
self.request('/api/addr/' + address, function(err, res, body) {
if (err || res.statusCode != 200 || !body) return next(err || res);
next(null, JSON.parse(body).transactions);
});
}
async.map(addresses, getTransactionIds, function then(err, txids) {
if (err) return cb(err);
// txids it's a list of list, let's fix that:
var txidsList = txids.reduce(function(a, r) {
return r.concat(a);
});
// Remove duplicated txids
txidsList = txidsList.filter(function(elem, pos, self) {
return self.indexOf(elem) == pos;
});
// Now get the transactions for that list of txIds
async.map(txidsList, self.getTransaction.bind(self), function then(err, txs) {
if (err) return cb(err);
cb(null, txs);
});
this.requestPost('/api/addrs/txs', {
addrs: addresses.join(',')
}, function (err, res, txs) {
if (err || res.statusCode != 200) return cb(err || res);
cb(null, txs);
});
};

View file

@ -229,25 +229,12 @@ describe('Insight model', function() {
it('should get a set of transaction by addresses', function(done) {
var blockchain = new Insight(FAKE_OPTS);
sinon.stub(blockchain, "request", function(url, cb) {
var res = {statusCode: 200};
if (url == '/api/addr/2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM') {
return setTimeout(function() {
var body = JSON.stringify({transactions: [1, 2]});
cb(null, res, body);
}, 0);
}
if (url == '/api/addr/2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb') {
return setTimeout(function() {
var body = JSON.stringify({transactions: [3]});
cb(null, res, body);
}, 0);
}
sinon.stub(blockchain, "requestPost", function(url, data, cb) {
url.should.be.equal('/api/addrs/txs');
data.addrs.should.be.equal('2NATQJnaQe2CUKLyhL1zdNkttJM1dUH9HaM,2NE9hTCffeugo5gQtfB4owq98gyTeWC56yb');
setTimeout(function() {
var body = JSON.stringify({txid: '123123'});
var res = {statusCode: 200};
var body = [1, 2, 3];
cb(null, res, body);
}, 0);
});