From d1d4452217c878d0b377d24830b4cd8b3162f6e0 Mon Sep 17 00:00:00 2001 From: Alan Cohen Date: Tue, 23 Dec 2014 11:13:51 -0800 Subject: [PATCH] [TASK] Allow `limit` option in remote.requestBookOffers --- src/js/ripple/remote.js | 25 ++++++++++++++++++--- test/remote-test.js | 48 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/js/ripple/remote.js b/src/js/ripple/remote.js index cdcc8297..efb25a1f 100644 --- a/src/js/ripple/remote.js +++ b/src/js/ripple/remote.js @@ -1258,7 +1258,7 @@ Remote.accountRequest = function(type, options, callback) { request.message.peer = UInt160.json_rewrite(peer); } - if (!isNaN(Number(limit))) { + if (!isNaN(limit)) { limit = Number(limit); // max for 32-bit unsigned int is 4294967295 @@ -1481,16 +1481,18 @@ Remote.prototype.requestTxHistory = function(start, callback) { * @param {Object} options.pays - taker_pays with issuer and currency * @param {String} [options.taker] * @param {String} [options.ledger] + * @param {String|Number} [options.limit] * @param [Function] callback * @return {Request} */ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) { + var ledger; + var limit; var lastArg = arguments[arguments.length - 1]; if (gets.hasOwnProperty('gets') || gets.hasOwnProperty('taker_gets')) { var options = gets; - var ledger; // This would mutate the `lastArg` in `arguments` to be `null` and is // redundant. Once upon a time, some awkward code was written f(g, null, // null, cb) ... @@ -1499,6 +1501,7 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) { pays = options.pays || options.taker_pays; gets = options.gets || options.taker_gets; ledger = options.ledger; + limit = options.limit; } if (typeof lastArg === 'function') { @@ -1524,9 +1527,25 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) { } request.message.taker = taker ? taker : UInt160.ACCOUNT_ONE; - request.ledgerSelect(ledger); + if (!isNaN(limit)) { + limit = Number(limit); + + // max for 32-bit unsigned int is 4294967295 + // we'll clamp to 1e9 + if (limit > 1e9) { + limit = 1e9; + } + // min for 32-bit unsigned int is 0 + // we'll clamp to 0 + if (limit < 0) { + limit = 0; + } + + request.message.limit = limit; + } + request.callback(callback); return request; }; diff --git a/test/remote-test.js b/test/remote-test.js index 16683874..9631269b 100644 --- a/test/remote-test.js +++ b/test/remote-test.js @@ -335,7 +335,7 @@ describe('Remote', function () { }); it('requestAccountLines, account and callback', function() { - var callback = function() {}; + function callback() {} var remote = new Remote({ servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] }); @@ -354,7 +354,7 @@ describe('Remote', function () { }); it('requestAccountLines, ledger, peer', function() { - var callback = function() {}; + function callback() {} var remote = new Remote({ servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] }); @@ -379,7 +379,7 @@ describe('Remote', function () { }); it('requestAccountLines, ledger, peer, limit and marker', function() { - var callback = function() {}; + function callback() {} var remote = new Remote({ servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] }); @@ -408,7 +408,7 @@ describe('Remote', function () { }); it('requestAccountOffers, ledger, peer, limit and marker', function() { - var callback = function() {}; + function callback() {} var remote = new Remote({ servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] }); @@ -437,7 +437,7 @@ describe('Remote', function () { }); it('requestBookOffers, ledger', function() { - var callback = function() {}; + function callback() {} var remote = new Remote({ servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] }); @@ -472,6 +472,44 @@ describe('Remote', function () { assert(request.requested); }); + it('requestBookOffers, ledger and limit', function() { + function callback() {} + + var remote = new Remote({ + servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] + }); + var request = remote.requestBookOffers( + { + gets: { + currency: 'USD', + issuer: ADDRESS + }, + pays: { + currency: 'XRP' + }, + ledger: LEDGER_HASH, + limit: 10 + }, + callback + ); + + assert.deepEqual(request.message, { + command: 'book_offers', + id: undefined, + taker_gets: { + currency: Currency.from_human('USD').to_hex(), + issuer: ADDRESS + }, + taker_pays: { + currency: Currency.from_human('XRP').to_hex() + }, + taker: UInt160.ACCOUNT_ONE, + ledger_hash: LEDGER_HASH, + limit: 10 + }); + + assert(request.requested); + }); it('create remote and get pending transactions', function() { before(function() {