Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
Add query parameter for only showing frozen balances
Browse files Browse the repository at this point in the history
  • Loading branch information
wltsmrz committed Dec 8, 2014
1 parent 65be807 commit a9b246b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
18 changes: 13 additions & 5 deletions api/balances.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ var respond = require('../lib/response-handler.js');
var errors = require('./../lib/errors.js');
var validator = require('./../lib/schema-validator.js');

module.exports = {
get: getBalances
};

var InvalidRequestError = errors.InvalidRequestError;

/**
Expand All @@ -36,7 +32,8 @@ function getBalances(request, response, next) {
var options = {
account: request.params.account,
currency: request.query.currency,
counterparty: request.query.counterparty
counterparty: request.query.counterparty,
frozen: request.query.frozen === 'true'
};

var currencyRE = new RegExp(options.currency ? ('^' + options.currency.toUpperCase() + '$') : /./);
Expand All @@ -60,6 +57,10 @@ function getBalances(request, response, next) {
};

function getXRPBalance(callback) {
if (options.frozen) {
return callback();
}

var ledger = validator.isValid(request.query.ledger, 'UINT32') ? Number(request.query.ledger) : 'validated';

var accountInfoRequest = remote.requestAccountInfo({
Expand Down Expand Up @@ -108,6 +109,10 @@ function getBalances(request, response, next) {
accountLinesRequest.once('error', callback);
accountLinesRequest.once('success', function(result) {
result.lines.forEach(function(line) {
if (options.frozen && !line.freeze) {
return;
}

if (currencyRE.test(line.currency)) {
balances.push({
value: line.balance,
Expand Down Expand Up @@ -160,3 +165,6 @@ function getBalances(request, response, next) {
}
});
};

module.exports.get = getBalances;

20 changes: 20 additions & 0 deletions test/balances-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,24 @@ suite('get balances', function() {
})))
.end(done);
});

test('/accounts/:account/balances?frozen', function(done) {
self.wss.once('request_account_info', function(message, conn) {
assert(false, 'Should not request account_info');
});

self.wss.once('request_account_lines', function(message, conn) {
assert.strictEqual(message.command, 'account_lines');
assert.strictEqual(message.account, addresses.VALID);
assert.strictEqual(message.ledger_index, 'validated');
conn.send(fixtures.accountLinesResponse(message));
});

self.app
.get(requestPath(addresses.VALID, '?frozen=true'))
.expect(testutils.checkBody(fixtures.RESTAccountBalancesFrozenResponse()))
.expect(testutils.checkStatus(200))
.expect(testutils.checkHeaders)
.end(done);
});
});
31 changes: 28 additions & 3 deletions test/fixtures/balances.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ module.exports.accountLinesResponse = function(request, options) {
no_ripple: true,
no_ripple_peer: true,
quality_in: 0,
quality_out: 0
quality_out: 0,
freeze: true
},
{
account: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q',
Expand All @@ -104,7 +105,8 @@ module.exports.accountLinesResponse = function(request, options) {
limit_peer: '0',
no_ripple: true,
quality_in: 0,
quality_out: 0
quality_out: 0,
freeze: true
},
{
account: 'rHpXfibHgSb64n8kK9QWDpdbfqSpYbM9a4',
Expand Down Expand Up @@ -306,7 +308,8 @@ module.exports.accountLinesResponse = function(request, options) {
limit: '0',
limit_peer: '1',
quality_in: 0,
quality_out: 0
quality_out: 0,
freeze: true
}
]
}
Expand Down Expand Up @@ -528,3 +531,25 @@ module.exports.RESTAccountBalancesCounterpartyCurrencyResponse = function(option
]
});
};

module.exports.RESTAccountBalancesFrozenResponse = function(options) {
options = options || {};

_.defaults(options, {
validated: true
});

return JSON.stringify({
success: true,
marker: options.marker,
limit: options.limit,
ledger: options.ledger,
validated: options.validated,
balances: [
{ value: '0', currency: 'XAU', counterparty: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z' },
{ value: '2.497605752725159', currency: 'USD', counterparty: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q' },
{ value: '0', currency: 'USD', counterparty: 'rEhDDUUNxpXgEHVJtC2cjXAgyx5VCFxdMF' }
]
});
};

0 comments on commit a9b246b

Please sign in to comment.