Merge pull request #258 from isocolsky/paginate_txs

various fixes + readme.md
This commit is contained in:
Matias Alejo Garcia 2014-11-20 15:17:25 -03:00
commit 5cd0cbdb04
3 changed files with 47 additions and 14 deletions

View File

@ -255,8 +255,8 @@ addrs: 2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f
### Transactions for multiple addresses ### Transactions for multiple addresses
GET method: GET method:
``` ```
/api/addrs/[:addrs]/txs /api/addrs/[:addrs]/txs[?from=&to=]
/api/addrs/2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f/txs /api/addrs/2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f/txs?from=0&to=20
``` ```
POST method: POST method:
@ -267,8 +267,41 @@ POST method:
POST params: POST params:
``` ```
addrs: 2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f addrs: 2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f
from (optional): 0
to (optional): 20
``` ```
Sample output:
```
{ totalItems: 100,
from: 0,
to: 20,
items:
[ { txid: '3e81723d069b12983b2ef694c9782d32fca26cc978de744acbc32c3d3496e915',
version: 1,
locktime: 0,
vin: [Object],
vout: [Object],
blockhash: '00000000011a135e5277f5493c52c66829792392632b8b65429cf07ad3c47a6c',
confirmations: 109367,
time: 1393659685,
blocktime: 1393659685,
valueOut: 0.3453,
size: 225,
firstSeenTs: undefined,
valueIn: 0.3454,
fees: 0.0001 },
{ ... },
{ ... },
...
{ ... }
]
}
```
Note: if pagination params are not specified, the result is an array of transactions.
### Transaction broadcasting ### Transaction broadcasting
POST method: POST method:
``` ```

View File

@ -108,11 +108,14 @@ exports.multitxs = function(req, res, next) {
txs = txs.slice(start, end); txs = txs.slice(start, end);
} }
var txIndex = {};
_.each(txs, function (tx) { txIndex[tx.txid] = tx; });
async.each(txs, function (tx, callback) { async.each(txs, function (tx, callback) {
tDb.fromIdWithInfo(tx.txid, function(err, tx) { tDb.fromIdWithInfo(tx.txid, function(err, tx) {
if (err) console.log(err); if (err) console.log(err);
if (tx && tx.info) { if (tx && tx.info) {
_.find(txs, { txid: tx.txid }).info = tx.info; txIndex[tx.txid].info = tx.info;
} }
callback(); callback();
}); });
@ -132,8 +135,8 @@ exports.multitxs = function(req, res, next) {
}); });
}; };
var from = req.query.from; var from = req.param('from');
var to = req.query.to; var to = req.param('to');
var as = getAddrs(req, res, next); var as = getAddrs(req, res, next);
if (as) { if (as) {

View File

@ -34,22 +34,19 @@ describe('Transactions for multiple addresses', function() {
beforeEach(function(c) { beforeEach(function(c) {
req = {}; req = {};
res = {}; res = {};
req.query = {};
res.jsonp = sinon.spy(); res.jsonp = sinon.spy();
return c(); return c();
}); });
describe('All', function () { describe('Transactions from multiple addresses', function () {
_.each(fixture, function (f) { _.each(fixture, function (f) {
it(f.test, function (done) { it(f.test, function (done) {
req.param = sinon.stub().withArgs('addrs').returns(f.addrs.join(',')); req.param = sinon.stub();
req.param.withArgs('addrs').returns(f.addrs.join(','));
req.param.withArgs('from').returns(f.from);
req.param.withArgs('to').returns(f.to);
var paginated = !_.isUndefined(f.from) || !_.isUndefined(f.to); var paginated = !_.isUndefined(f.from) || !_.isUndefined(f.to);
if (paginated) {
req.query = {
from: f.from,
to: f.to
};
}
addresses.multitxs(req, res, function() { addresses.multitxs(req, res, function() {
var txs = res.jsonp.getCall(0).args[0]; var txs = res.jsonp.getCall(0).args[0];
txs.should.exist; txs.should.exist;