diff --git a/src/api/ledger/balances.js b/src/api/ledger/balances.js index 856031bd..f690b282 100644 --- a/src/api/ledger/balances.js +++ b/src/api/ledger/balances.js @@ -19,13 +19,22 @@ function getTrustlineBalanceAmount(trustline) { }; } -function formatBalances(balances) { - const xrpBalance = { - currency: 'XRP', - value: balances.xrp - }; - return [xrpBalance].concat( - balances.trustlines.map(getTrustlineBalanceAmount)); +function formatBalances(options, balances) { + const result = balances.trustlines.map(getTrustlineBalanceAmount); + if (!(options.counterparty || + (options.currency && options.currency !== 'XRP') + )) { + const xrpBalance = { + currency: 'XRP', + value: balances.xrp + }; + result.unshift(xrpBalance); + } + if (options.limit && result.length > options.limit) { + const toRemove = result.length - options.limit; + result.splice(-toRemove, toRemove); + } + return result; } function getTrustlinesAsync(account, options, callback) { @@ -54,7 +63,7 @@ function getBalancesAsync(account, options, callback) { _.partial(utils.getXRPBalance, this.remote, account) ), trustlines: _.partial(getTrustlinesAsync.bind(this), account, options) - }, composeAsync(formatBalances, convertErrors(callback))); + }, composeAsync(_.partial(formatBalances, options), convertErrors(callback))); } function getBalances(account: string, options = {}) { diff --git a/test/api-test.js b/test/api-test.js index 53fc4370..9eddf337 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -196,9 +196,45 @@ describe('RippleAPI', function() { }); }); - it('getBalances', function() { - return this.api.getBalances(address).then( - _.partial(checkResult, responses.getBalances, 'getBalances')); + describe('RippleAPI', function() { + + it('getBalances', function() { + return this.api.getBalances(address).then( + _.partial(checkResult, responses.getBalances, 'getBalances')); + }); + + it('getBalances - limit', function() { + const options = { + limit: 3 + }; + const expectedResponse = responses.getBalances.slice(0, 3); + return this.api.getBalances(address, options).then( + _.partial(checkResult, expectedResponse, 'getBalances')); + }); + + it('getBalances - limit & currency', function() { + const options = { + currency: 'USD', + limit: 3 + }; + const expectedResponse = _.filter(responses.getBalances, + item => item.currency === 'USD').slice(0, 3); + return this.api.getBalances(address, options).then( + _.partial(checkResult, expectedResponse, 'getBalances')); + }); + + it('getBalances - limit & currency & issuer', function() { + const options = { + currency: 'USD', + counterparty: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', + limit: 3 + }; + const expectedResponse = _.filter(responses.getBalances, + item => item.currency === 'USD' && + item.counterparty === 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B').slice(0, 3); + return this.api.getBalances(address, options).then( + _.partial(checkResult, expectedResponse, 'getBalances')); + }); }); it('getBalanceSheet', function() { diff --git a/test/fixtures/api/rippled/account-lines.js b/test/fixtures/api/rippled/account-lines.js index f12b7b6e..31b87037 100644 --- a/test/fixtures/api/rippled/account-lines.js +++ b/test/fixtures/api/rippled/account-lines.js @@ -2,7 +2,7 @@ const _ = require('lodash'); const BASE_LEDGER_INDEX = 8819951; -module.exports.normal = function(request, options={}) { +module.exports.normal = function(request, options = {}) { _.defaults(options, { ledger: BASE_LEDGER_INDEX }); @@ -16,8 +16,7 @@ module.exports.normal = function(request, options={}) { marker: options.marker, limit: request.limit, ledger_index: options.ledger, - lines: [ - { + lines: _.filter([{ account: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z', balance: '0', currency: 'ASP', @@ -252,12 +251,12 @@ module.exports.normal = function(request, options={}) { quality_out: 0, freeze: true } - ] + ], item => !request.peer || item.account === request.peer) } }); }; -module.exports.counterparty = function(request, options={}) { +module.exports.counterparty = function(request, options = {}) { _.defaults(options, { ledger: BASE_LEDGER_INDEX }); @@ -271,8 +270,7 @@ module.exports.counterparty = function(request, options={}) { marker: options.marker, limit: request.limit, ledger_index: options.ledger, - lines: [ - { + lines: [{ account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', balance: '0.3488146605801446', currency: 'CHF',