Skip to content

Commit

Permalink
Add API call to get transactions count
Browse files Browse the repository at this point in the history
Allows to see how many confirmed, unconfirmed, multisignature and queued transactions exists in particular moment via API call.

closes LiskArchive#341
  • Loading branch information
MaciejBaj committed Jan 3, 2017
1 parent 86d79a3 commit c7c0564
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ __private.attachApi = function () {
router.map(shared, {
'get /': 'getTransactions',
'get /get': 'getTransaction',
'get /count': 'getTransactionsCount',
'get /queued/get': 'getQueuedTransaction',
'get /queued': 'getQueuedTransactions',
'get /multisignatures/get': 'getMultisignatureTransaction',
Expand Down Expand Up @@ -417,6 +418,20 @@ shared.getTransaction = function (req, cb) {
});
};

shared.getTransactionsCount = function (req, cb) {
library.db.query(sql.count).then(function (transactionsCount) {
return setImmediate(cb, null, {
confirmed: transactionsCount[0].count,
multisignature: __private.transactionPool.multisignature.transactions.length,
unconfirmed: __private.transactionPool.unconfirmed.transactions.length,
queued: __private.transactionPool.queued.transactions.length
});

}, function (err) {
return setImmediate(cb, 'Unable to count transactions');
});
};

shared.getQueuedTransaction = function (req, cb) {
return __private.getPooledTransaction('getQueuedTransaction', req, cb);
};
Expand Down
2 changes: 2 additions & 0 deletions sql/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var TransactionsSql = {
'height'
],

count: 'SELECT COUNT("id")::int AS "count" FROM trs',

countById: 'SELECT COUNT("id")::int AS "count" FROM trs WHERE "id" = ${id}',

countList: function (params) {
Expand Down
14 changes: 14 additions & 0 deletions test/api/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,20 @@ describe('GET /api/transactions/get?id=', function () {
});
});

describe('GET /api/transactions/count', function () {

it('should be ok', function (done) {
node.get('/api/transactions/count', function (err, res) {
node.expect(res.body).to.have.property('success').to.be.ok;
node.expect(res.body).to.have.property('confirmed').that.is.an('number');
node.expect(res.body).to.have.property('queued').that.is.an('number');
node.expect(res.body).to.have.property('multisignature').that.is.an('number');
node.expect(res.body).to.have.property('unconfirmed').that.is.an('number');
done();
});
});
});

describe('GET /api/transactions/queued/get?id=', function () {

it('using unknown id should be ok', function (done) {
Expand Down

0 comments on commit c7c0564

Please sign in to comment.