Merge pull request #596 from darkdarkdragon/develop-RLJS-520

getBalances fixes:
This commit is contained in:
Chris Clark
2015-10-16 10:54:16 -07:00
3 changed files with 61 additions and 18 deletions

View File

@@ -28,13 +28,22 @@ function getTrustlineBalanceAmount(trustline: Trustline) {
}; };
} }
function formatBalances(balances) { function formatBalances(options, balances) {
const xrpBalance = { const result = balances.trustlines.map(getTrustlineBalanceAmount);
currency: 'XRP', if (!(options.counterparty ||
value: balances.xrp (options.currency && options.currency !== 'XRP')
}; )) {
return [xrpBalance].concat( const xrpBalance = {
balances.trustlines.map(getTrustlineBalanceAmount)); 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: string, options: TrustlinesOptions, function getTrustlinesAsync(account: string, options: TrustlinesOptions,
@@ -67,7 +76,7 @@ function getBalancesAsync(account: string, options: TrustlinesOptions,
_.partial(utils.getXRPBalance, this.remote, account) _.partial(utils.getXRPBalance, this.remote, account)
), ),
trustlines: _.partial(getTrustlinesAsync.bind(this), account, options) trustlines: _.partial(getTrustlinesAsync.bind(this), account, options)
}, composeAsync(formatBalances, convertErrors(callback))); }, composeAsync(_.partial(formatBalances, options), convertErrors(callback)));
} }
function getBalances(account: string, options: TrustlinesOptions = {} function getBalances(account: string, options: TrustlinesOptions = {}

View File

@@ -196,9 +196,45 @@ describe('RippleAPI', function() {
}); });
}); });
it('getBalances', function() { describe('RippleAPI', function() {
return this.api.getBalances(address).then(
_.partial(checkResult, responses.getBalances, 'getBalances')); 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() { it('getBalanceSheet', function() {

View File

@@ -2,7 +2,7 @@
const _ = require('lodash'); const _ = require('lodash');
const BASE_LEDGER_INDEX = 8819951; const BASE_LEDGER_INDEX = 8819951;
module.exports.normal = function(request, options={}) { module.exports.normal = function(request, options = {}) {
_.defaults(options, { _.defaults(options, {
ledger: BASE_LEDGER_INDEX ledger: BASE_LEDGER_INDEX
}); });
@@ -16,8 +16,7 @@ module.exports.normal = function(request, options={}) {
marker: options.marker, marker: options.marker,
limit: request.limit, limit: request.limit,
ledger_index: options.ledger, ledger_index: options.ledger,
lines: [ lines: _.filter([{
{
account: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z', account: 'r3vi7mWxru9rJCxETCyA1CHvzL96eZWx5z',
balance: '0', balance: '0',
currency: 'ASP', currency: 'ASP',
@@ -252,12 +251,12 @@ module.exports.normal = function(request, options={}) {
quality_out: 0, quality_out: 0,
freeze: true freeze: true
} }
] ], item => !request.peer || item.account === request.peer)
} }
}); });
}; };
module.exports.counterparty = function(request, options={}) { module.exports.counterparty = function(request, options = {}) {
_.defaults(options, { _.defaults(options, {
ledger: BASE_LEDGER_INDEX ledger: BASE_LEDGER_INDEX
}); });
@@ -271,8 +270,7 @@ module.exports.counterparty = function(request, options={}) {
marker: options.marker, marker: options.marker,
limit: request.limit, limit: request.limit,
ledger_index: options.ledger, ledger_index: options.ledger,
lines: [ lines: [{
{
account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B', account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
balance: '0.3488146605801446', balance: '0.3488146605801446',
currency: 'CHF', currency: 'CHF',