diff --git a/src/api/ledger/orders.js b/src/api/ledger/orders.js index 9f5893da..7eafea95 100644 --- a/src/api/ledger/orders.js +++ b/src/api/ledger/orders.js @@ -10,7 +10,7 @@ function requestAccountOffers(remote, address, ledgerVersion, options, remote.requestAccountOffers({ account: address, marker: marker, - limit: limit, + limit: utils.clamp(limit, 10, 400), ledger: ledgerVersion }, composeAsync((data) => ({ @@ -23,13 +23,11 @@ function getOrders(account, options, callback) { validate.address(account); validate.getOrdersOptions(options); - const defaultLimit = 100; - const limit = options.limit || defaultLimit; const ledgerVersion = options.ledgerVersion || this.remote.getLedgerSequence(); const getter = _.partial(requestAccountOffers, this.remote, account, ledgerVersion, options); - utils.getRecursive(getter, limit, + utils.getRecursive(getter, options.limit, composeAsync((orders) => _.sortBy(orders, (order) => order.properties.sequence), callback)); } diff --git a/src/api/ledger/transactions.js b/src/api/ledger/transactions.js index e2eea955..781a7686 100644 --- a/src/api/ledger/transactions.js +++ b/src/api/ledger/transactions.js @@ -5,7 +5,6 @@ const parseTransaction = require('./parse/transaction'); const getTransaction = require('./transaction'); const validate = utils.common.validate; const composeAsync = utils.common.composeAsync; -const DEFAULT_LIMIT = 100; function parseAccountTxTransaction(tx) { // rippled uses a different response format for 'account_tx' than 'tx' @@ -47,7 +46,7 @@ function getAccountTx(remote, address, options, marker, limit, callback) { ledger_index_max: options.maxLedgerVersion || -1, forward: options.earliestFirst, binary: options.binary, - limit: Math.max(limit || DEFAULT_LIMIT, 10), + limit: utils.clamp(limit, 10, 400), marker: marker }; @@ -64,11 +63,10 @@ function getAccountTx(remote, address, options, marker, limit, callback) { } function getTransactionsInternal(remote, address, options, callback) { - const limit = options.limit || DEFAULT_LIMIT; const compare = options.earliestFirst ? utils.compareTransactions : _.rearg(utils.compareTransactions, 1, 0); const getter = _.partial(getAccountTx, remote, address, options); - utils.getRecursive(getter, limit, + utils.getRecursive(getter, options.limit, composeAsync((txs) => txs.sort(compare), callback)); } diff --git a/src/api/ledger/trustlines.js b/src/api/ledger/trustlines.js index 0a1be196..d73eb7c9 100644 --- a/src/api/ledger/trustlines.js +++ b/src/api/ledger/trustlines.js @@ -16,7 +16,7 @@ function getAccountLines(remote, address, ledgerVersion, options, marker, limit, account: address, ledger: ledgerVersion, marker: marker, - limit: Math.max(limit, 10), + limit: utils.clamp(limit, 10, 400), peer: options.counterparty }; @@ -40,13 +40,11 @@ function getTrustlines( validate.address(account); validate.getTrustlinesOptions(options); - const defaultLimit = 100; - const limit = options.limit || defaultLimit; const ledgerVersion = options.ledgerVersion || this.remote.getLedgerSequence(); const getter = _.partial(getAccountLines, this.remote, account, ledgerVersion, options); - utils.getRecursive(getter, limit, callback); + utils.getRecursive(getter, options.limit, callback); } module.exports = utils.wrapCatch(getTrustlines); diff --git a/src/api/ledger/utils.js b/src/api/ledger/utils.js index efed8d22..83aa20c4 100644 --- a/src/api/ledger/utils.js +++ b/src/api/ledger/utils.js @@ -1,9 +1,15 @@ 'use strict'; const _ = require('lodash'); +const assert = require('assert'); const common = require('../common'); const dropsToXrp = common.dropsToXrp; const composeAsync = common.composeAsync; +function clamp(value, min, max) { + assert(min <= max, 'Illegal clamp bounds'); + return Math.min(Math.max(value, min), max); +} + function getXRPBalance(remote, address, ledgerVersion, callback) { remote.requestAccountInfo({account: address, ledger: ledgerVersion}, composeAsync((data) => dropsToXrp(data.account_data.Balance), callback)); @@ -29,7 +35,7 @@ function getRecursiveRecur(getter, marker, limit, callback) { } function getRecursive(getter, limit, callback) { - getRecursiveRecur(getter, undefined, limit, callback); + getRecursiveRecur(getter, undefined, limit || Infinity, callback); } function renameCounterpartyToIssuer(amount) { @@ -86,6 +92,7 @@ module.exports = { getRecursive, hasCompleteLedgerRange, wrapCatch: common.wrapCatch, + clamp: clamp, common: common };