Skip to content

Commit

Permalink
getBalances fixes:
Browse files Browse the repository at this point in the history
obey limit, do not return XRP if currency or issuer specified
  • Loading branch information
darkdarkdragon committed Oct 15, 2015
1 parent 462e440 commit 965b609
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/api/ledger/balances.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {}) {
Expand Down

0 comments on commit 965b609

Please sign in to comment.