[TASK] Allow limit option in remote.requestBookOffers

This commit is contained in:
Alan Cohen
2014-12-23 11:13:51 -08:00
parent 9bf3724ce6
commit d1d4452217
2 changed files with 65 additions and 8 deletions

View File

@@ -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;
};

View File

@@ -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() {