mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 12:45:50 +00:00
Merge pull request #596 from darkdarkdragon/develop-RLJS-520
getBalances fixes:
This commit is contained in:
@@ -28,13 +28,22 @@ function getTrustlineBalanceAmount(trustline: Trustline) {
|
||||
};
|
||||
}
|
||||
|
||||
function formatBalances(balances) {
|
||||
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
|
||||
};
|
||||
return [xrpBalance].concat(
|
||||
balances.trustlines.map(getTrustlineBalanceAmount));
|
||||
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,
|
||||
@@ -67,7 +76,7 @@ function getBalancesAsync(account: string, options: TrustlinesOptions,
|
||||
_.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: TrustlinesOptions = {}
|
||||
|
||||
@@ -196,11 +196,47 @@ describe('RippleAPI', function() {
|
||||
});
|
||||
});
|
||||
|
||||
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() {
|
||||
return this.api.getBalanceSheet(address).then(
|
||||
_.partial(checkResult, responses.getBalanceSheet, 'getBalanceSheet'));
|
||||
|
||||
8
test/fixtures/api/rippled/account-lines.js
vendored
8
test/fixtures/api/rippled/account-lines.js
vendored
@@ -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,7 +251,7 @@ module.exports.normal = function(request, options={}) {
|
||||
quality_out: 0,
|
||||
freeze: true
|
||||
}
|
||||
]
|
||||
], item => !request.peer || item.account === request.peer)
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user