[TASK] change accountRequest method signature

`(type, options, callback)` where the last argument will be considered for callback if it's a function
This commit is contained in:
Geert Weening
2014-11-07 17:08:01 -08:00
parent 3c9660203b
commit 6f5d1104aa
4 changed files with 45 additions and 41 deletions

View File

@@ -211,7 +211,7 @@ Account.prototype.lines = function(callback) {
} }
} }
this._remote.requestAccountLines(this._account_id, accountLines); this._remote.requestAccountLines({account: this._account_id}, accountLines);
return this; return this;
}; };

View File

@@ -344,7 +344,7 @@ OrderBook.prototype.requestTransferRate = function(callback) {
return this.emit('transfer_rate', this._issuerTransferRate); return this.emit('transfer_rate', this._issuerTransferRate);
} }
this._remote.requestAccountInfo(issuer, function(err, info) { this._remote.requestAccountInfo({account: issuer}, function(err, info) {
if (err) { if (err) {
// XXX What now? // XXX What now?
return callback(err); return callback(err);
@@ -447,7 +447,7 @@ OrderBook.prototype.requestFundedAmount = function(account, callback) {
} }
function requestNativeBalance(callback) { function requestNativeBalance(callback) {
self._remote.requestAccountInfo(account, function(err, info) { self._remote.requestAccountInfo({account: account}, function(err, info) {
if (err) { if (err) {
callback(err); callback(err);
} else { } else {
@@ -458,8 +458,8 @@ OrderBook.prototype.requestFundedAmount = function(account, callback) {
function requestLineBalance(callback) { function requestLineBalance(callback) {
var request = self._remote.requestAccountLines( var request = self._remote.requestAccountLines(
account,
{ {
account: account,
ledger: 'validated', ledger: 'validated',
peer: self._issuerGets peer: self._issuerGets
} }

View File

@@ -1218,10 +1218,11 @@ Remote.prototype.requestTx = function(hash, callback) {
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
Remote.accountRequest = function(type, account, options, callback) { Remote.accountRequest = function(type, options, callback) {
var ledger, peer, limit, marker; var account, ledger, peer, limit, marker;
if (typeof options === 'object') { if (typeof options === 'object') {
account = options.account;
ledger = options.ledger; ledger = options.ledger;
peer = options.peer; peer = options.peer;
limit = options.limit; limit = options.limit;
@@ -1231,7 +1232,7 @@ Remote.accountRequest = function(type, account, options, callback) {
// if a marker is given, we need a ledger // if a marker is given, we need a ledger
// check if a valid ledger_index or ledger_hash is provided // check if a valid ledger_index or ledger_hash is provided
if (marker) { if (marker) {
if(!(Number(ledger) > 0) && !UInt256.is_valid(ledger)) { if (!(Number(ledger) > 0) && !UInt256.is_valid(ledger)) {
throw new Error('A ledger_index or ledger_hash must be provided when using a marker'); throw new Error('A ledger_index or ledger_hash must be provided when using a marker');
} }
} }
@@ -1243,9 +1244,12 @@ Remote.accountRequest = function(type, account, options, callback) {
} }
var request = new Request(this, type); var request = new Request(this, type);
var account = UInt160.json_rewrite(account);
request.message.account = account; if (account) {
account = UInt160.json_rewrite(account);
request.message.account = account;
}
request.ledgerSelect(ledger); request.ledgerSelect(ledger);
if (UInt160.is_valid(peer)) { if (UInt160.is_valid(peer)) {
@@ -1280,15 +1284,15 @@ Remote.accountRequest = function(type, account, options, callback) {
/** /**
* Request account_info * Request account_info
* *
* @param {String} account - ripple address
* @param {Object} options * @param {Object} options
* @param {String} account - ripple address
* @param {String} peer - ripple address * @param {String} peer - ripple address
* @param [String|Number] ledger identifier * @param [String|Number] ledger identifier
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
Remote.prototype.requestAccountInfo = function(account, options, callback) { Remote.prototype.requestAccountInfo = function(options, callback) {
var args = Array.prototype.concat.apply(['account_info'], arguments); var args = Array.prototype.concat.apply(['account_info'], arguments);
return Remote.accountRequest.apply(this, args); return Remote.accountRequest.apply(this, args);
}; };
@@ -1296,15 +1300,15 @@ Remote.prototype.requestAccountInfo = function(account, options, callback) {
/** /**
* Request account_currencies * Request account_currencies
* *
* @param {String} account - ripple address
* @param {Object} options * @param {Object} options
* @param {String} account - ripple address
* @param {String} peer - ripple address * @param {String} peer - ripple address
* @param [String|Number] ledger identifier * @param [String|Number] ledger identifier
* @param [Function] callback * @param [Function] callback
* @return {Request} * @return {Request}
*/ */
Remote.prototype.requestAccountCurrencies = function(account, options, callback) { Remote.prototype.requestAccountCurrencies = function(options, callback) {
var args = Array.prototype.concat.apply(['account_currencies'], arguments); var args = Array.prototype.concat.apply(['account_currencies'], arguments);
return Remote.accountRequest.apply(this, args); return Remote.accountRequest.apply(this, args);
}; };
@@ -1319,8 +1323,8 @@ Remote.prototype.requestAccountCurrencies = function(account, options, callback)
* ledger closes. You have to supply a ledger_index or ledger_hash * ledger closes. You have to supply a ledger_index or ledger_hash
* when paging to ensure a complete response * when paging to ensure a complete response
* *
* @param {String} account - ripple address
* @param {Object} options * @param {Object} options
* @param {String} account - ripple address
* @param {String} peer - ripple address * @param {String} peer - ripple address
* @param [String|Number] ledger identifier * @param [String|Number] ledger identifier
* @param [Number] limit - max results per response * @param [Number] limit - max results per response
@@ -1329,7 +1333,7 @@ Remote.prototype.requestAccountCurrencies = function(account, options, callback)
* @return {Request} * @return {Request}
*/ */
Remote.prototype.requestAccountLines = function(account, options, callback) { Remote.prototype.requestAccountLines = function(options, callback) {
// XXX Does this require the server to be trusted? // XXX Does this require the server to be trusted?
//utils.assert(this.trusted); //utils.assert(this.trusted);
var args = Array.prototype.concat.apply(['account_lines'], arguments); var args = Array.prototype.concat.apply(['account_lines'], arguments);
@@ -1346,8 +1350,8 @@ Remote.prototype.requestAccountLines = function(account, options, callback) {
* ledger closes. You have to supply a ledger_index or ledger_hash * ledger closes. You have to supply a ledger_index or ledger_hash
* when paging to ensure a complete response * when paging to ensure a complete response
* *
* @param {String} account - ripple address
* @param {Object} options * @param {Object} options
* @param {String} account - ripple address
* @param [String|Number] ledger identifier * @param [String|Number] ledger identifier
* @param [Number] limit - max results per response * @param [Number] limit - max results per response
* @param {String} marker - start position in response paging * @param {String} marker - start position in response paging
@@ -1355,7 +1359,7 @@ Remote.prototype.requestAccountLines = function(account, options, callback) {
* @return {Request} * @return {Request}
*/ */
Remote.prototype.requestAccountOffers = function(account, options, callback) { Remote.prototype.requestAccountOffers = function(options, callback) {
var args = Array.prototype.concat.apply(['account_offers'], arguments); var args = Array.prototype.concat.apply(['account_offers'], arguments);
return Remote.accountRequest.apply(this, args); return Remote.accountRequest.apply(this, args);
}; };

View File

@@ -193,25 +193,25 @@ describe('Remote', function () {
}); });
it('request account currencies with ledger index', function() { it('request account currencies with ledger index', function() {
var request = remote.requestAccountCurrencies(ADDRESS); var request = remote.requestAccountCurrencies({account: ADDRESS});
assert.strictEqual(request.message.command, 'account_currencies'); assert.strictEqual(request.message.command, 'account_currencies');
assert.strictEqual(request.message.account, ADDRESS); assert.strictEqual(request.message.account, ADDRESS);
}); });
it('request account info with ledger index', function() { it('request account info with ledger index', function() {
var request = remote.requestAccountInfo(ADDRESS, {ledger: 9592219}); var request = remote.requestAccountInfo({account: ADDRESS, ledger: 9592219});
assert.strictEqual(request.message.command, 'account_info'); assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, ADDRESS); assert.strictEqual(request.message.account, ADDRESS);
assert.strictEqual(request.message.ledger_index, 9592219); assert.strictEqual(request.message.ledger_index, 9592219);
}); });
it('request account info with ledger hash', function() { it('request account info with ledger hash', function() {
var request = remote.requestAccountInfo(ADDRESS, {ledger: LEDGER_HASH}); var request = remote.requestAccountInfo({account: ADDRESS, ledger: LEDGER_HASH});
assert.strictEqual(request.message.command, 'account_info'); assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, ADDRESS); assert.strictEqual(request.message.account, ADDRESS);
assert.strictEqual(request.message.ledger_hash, LEDGER_HASH); assert.strictEqual(request.message.ledger_hash, LEDGER_HASH);
}); });
it('request account info with ledger identifier', function() { it('request account info with ledger identifier', function() {
var request = remote.requestAccountInfo(ADDRESS, {ledger: 'validated'}); var request = remote.requestAccountInfo({account: ADDRESS, ledger: 'validated'});
assert.strictEqual(request.message.command, 'account_info'); assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, ADDRESS); assert.strictEqual(request.message.account, ADDRESS);
assert.strictEqual(request.message.ledger_index, 'validated'); assert.strictEqual(request.message.ledger_index, 'validated');
@@ -238,7 +238,7 @@ describe('Remote', function () {
}); });
it('pagingAccountRequest', function() { it('pagingAccountRequest', function() {
var request = Remote.accountRequest('account_lines', ADDRESS); var request = Remote.accountRequest('account_lines', {account: ADDRESS});
assert.deepEqual(request.message, { assert.deepEqual(request.message, {
command: 'account_lines', command: 'account_lines',
id: undefined, id: undefined,
@@ -247,7 +247,7 @@ describe('Remote', function () {
}); });
it('pagingAccountRequest - limit', function() { it('pagingAccountRequest - limit', function() {
var request = Remote.accountRequest('account_lines', ADDRESS, {limit: 100}); var request = Remote.accountRequest('account_lines', {account: ADDRESS, limit: 100});
assert.deepEqual(request.message, { assert.deepEqual(request.message, {
command: 'account_lines', command: 'account_lines',
id: undefined, id: undefined,
@@ -257,7 +257,7 @@ describe('Remote', function () {
}); });
it('pagingAccountRequest - limit, marker', function() { it('pagingAccountRequest - limit, marker', function() {
var request = Remote.accountRequest('account_lines', ADDRESS, {limit: 100, marker: PAGING_MARKER, ledger: 9592219}); var request = Remote.accountRequest('account_lines', {account: ADDRESS, limit: 100, marker: PAGING_MARKER, ledger: 9592219});
assert.deepEqual(request.message, { assert.deepEqual(request.message, {
command: 'account_lines', command: 'account_lines',
id: undefined, id: undefined,
@@ -271,38 +271,38 @@ describe('Remote', function () {
}); });
it('accountRequest - limit min', function() { it('accountRequest - limit min', function() {
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 0}).message.limit, 0); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 0}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: -1}).message.limit, 0); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: -1}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: -1e9}).message.limit, 0); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: -1e9}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: -1e24}).message.limit, 0); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: -1e24}).message.limit, 0);
}); });
it('accountRequest - limit max', function() { it('accountRequest - limit max', function() {
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e9}).message.limit, 1e9); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e9}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e9+1}).message.limit, 1e9); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e9+1}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e10}).message.limit, 1e9); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e10}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e24}).message.limit, 1e9); assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e24}).message.limit, 1e9);
}); });
it('accountRequest - a valid ledger is required when using a marker', function() { it('accountRequest - a valid ledger is required when using a marker', function() {
assert.throws(function() { assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER}) Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER})
},'A ledger_index or ledger_hash must be provided when using a marker'); },'A ledger_index or ledger_hash must be provided when using a marker');
assert.throws(function() { assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:'validated'}) Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:'validated'})
},'A ledger_index or ledger_hash must be provided when using a marker'); },'A ledger_index or ledger_hash must be provided when using a marker');
assert.throws(function() { assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:NaN}) Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:NaN})
},'A ledger_index or ledger_hash must be provided when using a marker'); },'A ledger_index or ledger_hash must be provided when using a marker');
assert.throws(function() { assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:LEDGER_HASH.substr(0,63)}) Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:LEDGER_HASH.substr(0,63)})
},'A ledger_index or ledger_hash must be provided when using a marker'); },'A ledger_index or ledger_hash must be provided when using a marker');
assert.throws(function() { assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:LEDGER_HASH+'F'}) Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:LEDGER_HASH+'F'})
},'A ledger_index or ledger_hash must be provided when using a marker'); },'A ledger_index or ledger_hash must be provided when using a marker');
}); });
@@ -312,7 +312,7 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
}); });
var request = remote.requestAccountLines( var request = remote.requestAccountLines(
ADDRESS, {account: ADDRESS},
callback callback
); );
@@ -331,8 +331,8 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
}); });
var request = remote.requestAccountLines( var request = remote.requestAccountLines(
ADDRESS,
{ {
account: ADDRESS,
ledger: LEDGER_HASH, ledger: LEDGER_HASH,
peer: PEER_ADDRESS peer: PEER_ADDRESS
}, },
@@ -356,8 +356,8 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
}); });
var request = remote.requestAccountLines( var request = remote.requestAccountLines(
ADDRESS,
{ {
account: ADDRESS,
ledger: LEDGER_INDEX, ledger: LEDGER_INDEX,
peer: PEER_ADDRESS, peer: PEER_ADDRESS,
limit: 200, limit: 200,
@@ -385,8 +385,8 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ] servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
}); });
var request = remote.requestAccountOffers( var request = remote.requestAccountOffers(
ADDRESS,
{ {
account: ADDRESS,
ledger: LEDGER_HASH, ledger: LEDGER_HASH,
peer: PEER_ADDRESS, peer: PEER_ADDRESS,
limit: 32, limit: 32,