Skip to content

Commit d1d4452

Browse files
author
Alan Cohen
committed
[TASK] Allow limit option in remote.requestBookOffers
1 parent 9bf3724 commit d1d4452

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

src/js/ripple/remote.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ Remote.accountRequest = function(type, options, callback) {
12581258
request.message.peer = UInt160.json_rewrite(peer);
12591259
}
12601260

1261-
if (!isNaN(Number(limit))) {
1261+
if (!isNaN(limit)) {
12621262
limit = Number(limit);
12631263

12641264
// max for 32-bit unsigned int is 4294967295
@@ -1481,16 +1481,18 @@ Remote.prototype.requestTxHistory = function(start, callback) {
14811481
* @param {Object} options.pays - taker_pays with issuer and currency
14821482
* @param {String} [options.taker]
14831483
* @param {String} [options.ledger]
1484+
* @param {String|Number} [options.limit]
14841485
* @param [Function] callback
14851486
* @return {Request}
14861487
*/
14871488

14881489
Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) {
1490+
var ledger;
1491+
var limit;
14891492
var lastArg = arguments[arguments.length - 1];
14901493

14911494
if (gets.hasOwnProperty('gets') || gets.hasOwnProperty('taker_gets')) {
14921495
var options = gets;
1493-
var ledger;
14941496
// This would mutate the `lastArg` in `arguments` to be `null` and is
14951497
// redundant. Once upon a time, some awkward code was written f(g, null,
14961498
// null, cb) ...
@@ -1499,6 +1501,7 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) {
14991501
pays = options.pays || options.taker_pays;
15001502
gets = options.gets || options.taker_gets;
15011503
ledger = options.ledger;
1504+
limit = options.limit;
15021505
}
15031506

15041507
if (typeof lastArg === 'function') {
@@ -1524,9 +1527,25 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) {
15241527
}
15251528

15261529
request.message.taker = taker ? taker : UInt160.ACCOUNT_ONE;
1527-
15281530
request.ledgerSelect(ledger);
15291531

1532+
if (!isNaN(limit)) {
1533+
limit = Number(limit);
1534+
1535+
// max for 32-bit unsigned int is 4294967295
1536+
// we'll clamp to 1e9
1537+
if (limit > 1e9) {
1538+
limit = 1e9;
1539+
}
1540+
// min for 32-bit unsigned int is 0
1541+
// we'll clamp to 0
1542+
if (limit < 0) {
1543+
limit = 0;
1544+
}
1545+
1546+
request.message.limit = limit;
1547+
}
1548+
15301549
request.callback(callback);
15311550
return request;
15321551
};

test/remote-test.js

+43-5
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ describe('Remote', function () {
335335
});
336336

337337
it('requestAccountLines, account and callback', function() {
338-
var callback = function() {};
338+
function callback() {}
339339
var remote = new Remote({
340340
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
341341
});
@@ -354,7 +354,7 @@ describe('Remote', function () {
354354
});
355355

356356
it('requestAccountLines, ledger, peer', function() {
357-
var callback = function() {};
357+
function callback() {}
358358
var remote = new Remote({
359359
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
360360
});
@@ -379,7 +379,7 @@ describe('Remote', function () {
379379
});
380380

381381
it('requestAccountLines, ledger, peer, limit and marker', function() {
382-
var callback = function() {};
382+
function callback() {}
383383
var remote = new Remote({
384384
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
385385
});
@@ -408,7 +408,7 @@ describe('Remote', function () {
408408
});
409409

410410
it('requestAccountOffers, ledger, peer, limit and marker', function() {
411-
var callback = function() {};
411+
function callback() {}
412412
var remote = new Remote({
413413
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
414414
});
@@ -437,7 +437,7 @@ describe('Remote', function () {
437437
});
438438

439439
it('requestBookOffers, ledger', function() {
440-
var callback = function() {};
440+
function callback() {}
441441
var remote = new Remote({
442442
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
443443
});
@@ -472,6 +472,44 @@ describe('Remote', function () {
472472
assert(request.requested);
473473
});
474474

475+
it('requestBookOffers, ledger and limit', function() {
476+
function callback() {}
477+
478+
var remote = new Remote({
479+
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
480+
});
481+
var request = remote.requestBookOffers(
482+
{
483+
gets: {
484+
currency: 'USD',
485+
issuer: ADDRESS
486+
},
487+
pays: {
488+
currency: 'XRP'
489+
},
490+
ledger: LEDGER_HASH,
491+
limit: 10
492+
},
493+
callback
494+
);
495+
496+
assert.deepEqual(request.message, {
497+
command: 'book_offers',
498+
id: undefined,
499+
taker_gets: {
500+
currency: Currency.from_human('USD').to_hex(),
501+
issuer: ADDRESS
502+
},
503+
taker_pays: {
504+
currency: Currency.from_human('XRP').to_hex()
505+
},
506+
taker: UInt160.ACCOUNT_ONE,
507+
ledger_hash: LEDGER_HASH,
508+
limit: 10
509+
});
510+
511+
assert(request.requested);
512+
});
475513

476514
it('create remote and get pending transactions', function() {
477515
before(function() {

0 commit comments

Comments
 (0)