[FIX] fix eslint errors

This commit is contained in:
Bo Chen
2015-02-27 15:36:54 -08:00
parent 8da6ec5fa3
commit 4d06ce7454
3 changed files with 151 additions and 149 deletions

View File

@@ -8,6 +8,8 @@
// - trade
// - transaction
'use strict';
var _ = require('lodash');
var util = require('util');
var extend = require('extend');
@@ -19,6 +21,10 @@ var UInt160 = require('./uint160').UInt160;
var Currency = require('./currency').Currency;
var log = require('./log').internal.sub('orderbook');
function assertValidNumber(number, message) {
assert(!_.isNull(number) && !isNaN(number), message);
}
/**
* @constructor OrderBook
* @param {Remote} remote
@@ -73,11 +79,11 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) {
break;
}
}
};
}
function updateFundedAmountsWrapper (transaction) {
self.updateFundedAmounts(transaction);
};
}
this.on('newListener', function(event) {
listenersModified('add', event);
@@ -107,7 +113,7 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) {
});
return this;
};
}
util.inherits(OrderBook, EventEmitter);
@@ -181,9 +187,7 @@ OrderBook.prototype.subscribe = function() {
}
];
async.series(steps, function(err, res) {
//XXX What now?
});
async.series(steps);
};
/**
@@ -246,7 +250,7 @@ OrderBook.prototype.requestOffers = function(callback) {
self.emit('model', self._offers);
callback(null, self._offers);
};
}
function handleError(err) {
// XXX What now?
@@ -255,7 +259,7 @@ OrderBook.prototype.requestOffers = function(callback) {
}
callback(err);
};
}
var request = this._remote.requestBookOffers(this.toJSON());
request.once('success', handleOffers);
@@ -294,7 +298,7 @@ OrderBook.prototype.subscribeTransactions = function(callback) {
self._subscribed = true;
callback(null, res);
};
}
function handleError(err) {
if (self._remote.trace) {
@@ -302,7 +306,7 @@ OrderBook.prototype.subscribeTransactions = function(callback) {
}
callback(err);
};
}
var request = this._remote.requestSubscribe();
request.addStream('transactions');
@@ -332,7 +336,7 @@ OrderBook.prototype.resetCache = function() {
OrderBook.prototype.hasOwnerFunds = function(account) {
assert(UInt160.is_valid(account), 'Account is invalid');
return this._ownerFunds[account] !== void(0);
return this._ownerFunds[account] !== undefined;
};
/**
@@ -366,7 +370,9 @@ OrderBook.prototype.getOwnerFunds = function(account) {
if (this._currencyGets.is_native()) {
amount = Amount.from_json(this._ownerFunds[account]);
} else {
amount = Amount.from_json(this._ownerFunds[account] + OrderBook.IOU_SUFFIX);
amount = Amount.from_json(
this._ownerFunds[account] + OrderBook.IOU_SUFFIX
);
}
}
@@ -393,7 +399,7 @@ OrderBook.prototype.getUnadjustedOwnerFunds = function(account) {
OrderBook.prototype.deleteOwnerFunds = function(account) {
assert(UInt160.is_valid(account), 'Account is invalid');
this._ownerFunds[account] = void(0);
this._ownerFunds[account] = undefined;
};
/**
@@ -446,23 +452,27 @@ OrderBook.prototype.decrementOwnerOfferCount = function(account) {
* Add amount sum being offered for owner
*
* @param {String} account - owner's account address
* @param {Object|String} amount - offer amount as native string or IOU currency format
* @param {Object|String} amount - offer amount as native string or IOU
* currency format
* @return {Amount}
*/
OrderBook.prototype.addOwnerOfferTotal = function(account, amount) {
assert(UInt160.is_valid(account), 'Account is invalid');
var previousAmount = this.getOwnerOfferTotal(account);
this._ownerOffersTotal[account] = previousAmount.add(Amount.from_json(amount));
var currentAmount = previousAmount.add(Amount.from_json(amount));
return this._ownerOffersTotal[account];
this._ownerOffersTotal[account] = currentAmount;
return currentAmount;
};
/**
* Subtract amount sum being offered for owner
*
* @param {String} account - owner's account address
* @param {Object|String} amount - offer amount as native string or IOU currency format
* @param {Object|String} amount - offer amount as native string or IOU
* currency format
* @return {Amount}
*/
@@ -529,13 +539,14 @@ OrderBook.prototype.resetOwnerOfferTotal = function(account) {
*/
OrderBook.prototype.getOfferTakerGetsFunded = function(offer) {
assert(!_.isNull(offer.taker_gets_funded) && !_.isNaN(offer.taker_gets_funded));
assertValidNumber(offer.taker_gets_funded, 'Taker gets funded is invalid');
return Amount.from_json(offer.taker_gets_funded + OrderBook.IOU_SUFFIX);
};
/**
* Compute adjusted balance that would be left after issuer's transfer fee is deducted
* Compute adjusted balance that would be left after issuer's transfer fee is
* deducted
*
* @param {String} balance
* @return {String}
@@ -543,7 +554,7 @@ OrderBook.prototype.getOfferTakerGetsFunded = function(offer) {
OrderBook.prototype.applyTransferRate = function(balance) {
assert(!isNaN(balance), 'Balance is invalid');
assert(!_.isNull(this._issuerTransferRate) && !isNaN(this._issuerTransferRate), 'Transfer rate is invalid');
assertValidNumber(this._issuerTransferRate, 'Transfer rate is invalid');
var adjustedBalance = Amount.from_json(balance + OrderBook.IOU_SUFFIX)
.divide(this._issuerTransferRate)
@@ -577,24 +588,31 @@ OrderBook.prototype.requestTransferRate = function(callback) {
return callback(null, this._issuerTransferRate);
}
this._remote.requestAccountInfo({ account: this._issuerGets }, function(err, info) {
function handleAccountInfo(err, info) {
if (err) {
return callback(err);
}
// When transfer rate is not explicitly set on account, it implies the default transfer rate
self._issuerTransferRate = info.account_data.TransferRate || OrderBook.DEFAULT_TRANSFER_RATE;
// When transfer rate is not explicitly set on account, it implies the
// default transfer rate
self._issuerTransferRate = info.account_data.TransferRate ||
OrderBook.DEFAULT_TRANSFER_RATE;
callback(null, self._issuerTransferRate);
});
}
this._remote.requestAccountInfo(
{account: this._issuerGets},
handleAccountInfo
);
};
/**
* Set funded amount on offer with its owner's cached funds
*
* Offers have is_fully_funded, indicating whether these funds are sufficient for the offer placed.
* Offers have taker_gets_funded, reflecting the amount this account can afford to offer.
* Offers have taker_pays_funded, reflecting an adjusted TakerPays in the case of a partially funded order.
* is_fully_funded indicates if these funds are sufficient for the offer placed.
* taker_gets_funded indicates the amount this account can afford to offer.
* taker_pays_funded indicates adjusted TakerPays for partially funded offer.
*
* @param {Object} offer
* @return offer
@@ -617,9 +635,13 @@ OrderBook.prototype.setOfferFundedAmount = function(offer) {
} else if (previousOfferSum.compareTo(fundedAmount) < 0) {
offer.taker_gets_funded = fundedAmount.subtract(previousOfferSum).to_text();
var takerPaysFunded = this.getOfferQuality(offer).multiply(this.getOfferTakerGetsFunded(offer));
var takerPaysFunded = this.getOfferQuality(offer).multiply(
this.getOfferTakerGetsFunded(offer)
);
offer.taker_pays_funded = this._currencyPays.is_native() ? String(parseInt(takerPaysFunded.to_json().value, 10)) : takerPaysFunded.to_json().value;
offer.taker_pays_funded = this._currencyPays.is_native()
? String(parseInt(takerPaysFunded.to_json().value, 10))
: takerPaysFunded.to_json().value;
} else {
offer.taker_gets_funded = '0';
offer.taker_pays_funded = '0';
@@ -637,8 +659,8 @@ OrderBook.prototype.setOfferFundedAmount = function(offer) {
OrderBook.prototype.parseAccountBalanceFromNode = function(node) {
var result = {
account: void(0),
balance: void(0)
account: undefined,
balance: undefined
};
switch (node.entryType) {
@@ -757,7 +779,9 @@ OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
var self = this;
if (this._remote.trace) {
log.info('updating offer funds', this._key, account, this.getOwnerFunds(account).to_text());
var ownerFunds = this.getOwnerFunds(account).to_text();
log.info('updating offer funds', this._key, account, ownerFunds);
}
this.resetOwnerOfferTotal(account);
@@ -772,18 +796,24 @@ OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
var previousFundedGets;
if (_.isString(offer.taker_gets_funded)) {
// Offer is not new, so we should consider it for offer_changed and offer_funds_changed events
// Offer is not new, so we should consider it for offer_changed and
// offer_funds_changed events
previousFundedGets = self.getOfferTakerGetsFunded(offer);
}
self.setOfferFundedAmount(offer);
self.addOwnerOfferTotal(offer.Account, offer.TakerGets);
var areFundsChanged = previousFundedGets && !self.getOfferTakerGetsFunded(offer).equals(previousFundedGets);
var areFundsChanged = previousFundedGets
&& !self.getOfferTakerGetsFunded(offer).equals(previousFundedGets);
if (areFundsChanged) {
self.emit('offer_changed', previousOffer, offer);
self.emit('offer_funds_changed', offer, previousOffer.taker_gets_funded, offer.taker_gets_funded);
self.emit('offer_funds_changed',
offer,
previousOffer.taker_gets_funded,
offer.taker_gets_funded
);
}
});
};
@@ -827,9 +857,10 @@ OrderBook.prototype.notify = function(transaction) {
: ('/' + this._currencyPays.to_human() + '/' + this._issuerPays))
);
function handleNode(node) {
var isOfferCancel = transaction.transaction.TransactionType === 'OfferCancel';
var transactionOwnerFunds = transaction.transaction.owner_funds;
function handleNode(node) {
switch (node.nodeType) {
case 'DeletedNode':
self.deleteOffer(node, isOfferCancel);
@@ -844,16 +875,21 @@ OrderBook.prototype.notify = function(transaction) {
case 'ModifiedNode':
self.modifyOffer(node);
takerGetsTotal = takerGetsTotal.add(node.fieldsPrev.TakerGets).subtract(node.fieldsFinal.TakerGets);
takerPaysTotal = takerPaysTotal.add(node.fieldsPrev.TakerPays).subtract(node.fieldsFinal.TakerPays);
takerGetsTotal = takerGetsTotal
.add(node.fieldsPrev.TakerGets)
.subtract(node.fieldsFinal.TakerGets);
takerPaysTotal = takerPaysTotal
.add(node.fieldsPrev.TakerPays)
.subtract(node.fieldsFinal.TakerPays);
break;
case 'CreatedNode':
self.setOwnerFunds(node.fields.Account, transaction.transaction.owner_funds);
self.setOwnerFunds(node.fields.Account, transactionOwnerFunds);
self.insertOffer(node);
break;
}
};
}
_.each(affectedNodes, handleNode);
@@ -867,8 +903,8 @@ OrderBook.prototype.notify = function(transaction) {
/**
* Insert an offer into the orderbook
*
* NOTE: We *MUST* update offers' funded amounts when a new offer is placed because funds go
* to the highest quality offers first.
* NOTE: We *MUST* update offers' funded amounts when a new offer is placed
* because funds go to the highest quality offers first.
*
* @param {Object} node - Offer node
*/
@@ -948,7 +984,7 @@ OrderBook.prototype.getOfferQuality = function(offer) {
OrderBook.prototype.normalizeAmount = function(currency, amountObj) {
var value = currency.is_native()
? amountObj
: amountObj.value
: amountObj.value;
return Amount.from_json(value + OrderBook.IOU_SUFFIX);
};
@@ -987,7 +1023,7 @@ OrderBook.prototype.modifyOffer = function(node) {
* other existing offers in the book
*
* @param {Object} node - Offer node
* @param {Boolean} isOfferCancel - whether node came from an OfferCancel transaction
* @param {Boolean} isOfferCancel - whether node came from an OfferCancel
*/
OrderBook.prototype.deleteOffer = function(node, isOfferCancel) {
@@ -1034,7 +1070,8 @@ OrderBook.prototype.setOffers = function(offers) {
var offer = OrderBook.offerRewrite(rawOffer);
if (offer.hasOwnProperty('owner_funds')) {
// The first offer of each owner from book_offers contains owner balance of offer's output
// The first offer of each owner from book_offers contains owner balance
// of offer's output
self.setOwnerFunds(offer.Account, offer.owner_funds);
}
@@ -1065,8 +1102,6 @@ OrderBook.prototype.offers =
OrderBook.prototype.getOffers = function(callback) {
assert.strictEqual(typeof callback, 'function', 'Callback missing');
var self = this;
if (this._synchronized) {
callback(null, this._offers);
} else {

View File

@@ -1,3 +1,7 @@
/*eslint-disable max-len */
'use strict';
var _ = require('lodash');
var addresses = require('./addresses');
var Meta = require('ripple-lib').Meta;
@@ -93,7 +97,7 @@ module.exports.fiatOffers = function (options) {
index: module.exports.OTHER_LEDGER_INDEX,
owner_funds: options.other_account_funds,
quality: '195796912.5171664'
},
}
];
};
@@ -161,7 +165,7 @@ module.exports.NATIVE_OFFERS = [
index: 'D3338DA77BA23122FB5647B74B53636AB54BE246D4B21707C9D6887DEB334252',
owner_funds: '235.0194163432668',
quality: '195796912.5171664'
},
}
];
module.exports.REQUEST_OFFERS = [
@@ -638,7 +642,7 @@ module.exports.transactionWithInvalidAccountRoot = function(options) {
options = options || {};
_.defaults(options, {
account: addresses.ACCOUNT,
balance: module.exports.NATIVE_BALANCE,
balance: module.exports.NATIVE_BALANCE
});
return {
@@ -700,7 +704,7 @@ module.exports.transactionWithCreatedOffer = function(options) {
TransactionType: 'OfferCreate',
owner_funds: '2010.027702881682'
}
}
};
};
module.exports.transactionWithDeletedOffer = function(options) {
@@ -745,7 +749,7 @@ module.exports.transactionWithDeletedOffer = function(options) {
TransactionType: options.transaction_type,
owner_funds: '2010.027702881682'
}
}
};
};
module.exports.transactionWithModifiedOffer = function() {
@@ -761,7 +765,7 @@ module.exports.transactionWithModifiedOffer = function() {
TransactionType: 'OfferCreate',
owner_funds: '2010.027702881682'
}
}
};
};
module.exports.transactionWithModifiedOffers = function() {
@@ -777,7 +781,7 @@ module.exports.transactionWithModifiedOffers = function() {
TransactionType: 'OfferCreate',
owner_funds: '2010.027702881682'
}
}
};
};
module.exports.transactionWithNoNodes = function() {
@@ -793,7 +797,7 @@ module.exports.transactionWithNoNodes = function() {
TransactionType: 'OfferCreate',
owner_funds: '2010.027702881682'
}
}
};
};
module.exports.accountInfoResponse = function(options) {

View File

@@ -1,3 +1,7 @@
/*eslint-disable max-len */
'use strict';
var assert = require('assert-diff');
var Remote = require('ripple-lib').Remote;
var Currency = require('ripple-lib').Currency;
@@ -441,7 +445,7 @@ describe('OrderBook', function() {
remote.request = function(request) {
assert.deepEqual(request.message, {
command: 'account_info',
id: void(0),
id: undefined,
account: addresses.ISSUER
});
@@ -469,7 +473,7 @@ describe('OrderBook', function() {
remote.request = function(request) {
assert.deepEqual(request.message, {
command: 'account_info',
id: void(0),
id: undefined,
account: addresses.ISSUER
});
@@ -495,7 +499,7 @@ describe('OrderBook', function() {
book._issuerTransferRate = 1002000000;
remote.request = function(request) {
remote.request = function() {
assert(false);
};
@@ -513,7 +517,7 @@ describe('OrderBook', function() {
currency_pays: 'BTC'
});
remote.request = function(request) {
remote.request = function() {
assert(false);
};
@@ -1116,7 +1120,7 @@ describe('OrderBook', function() {
book._offers = fixtures.fiatOffers();
book.on('offer_changed', function(offer) {
book.on('offer_changed', function() {
receivedChangedEvents += 1;
});
@@ -1150,7 +1154,6 @@ describe('OrderBook', function() {
});
it('Update funded amounts - increase funds', function() {
var receivedChangedEvents = 0;
var receivedFundsChangedEvents = 0;
var remote = new Remote();
@@ -1264,7 +1267,7 @@ describe('OrderBook', function() {
book._synchronized = true;
book._offers = fixtures.NATIVE_OFFERS;
book.on('offer_changed', function(offer) {
book.on('offer_changed', function() {
receivedChangedEvents += 1;
});
@@ -1533,13 +1536,6 @@ describe('OrderBook', function() {
it('Notify - created node', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
@@ -1637,13 +1633,6 @@ describe('OrderBook', function() {
it('Notify - deleted node', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
@@ -1667,13 +1656,6 @@ describe('OrderBook', function() {
it('Notify - deleted node - last offer', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
@@ -1772,13 +1754,6 @@ describe('OrderBook', function() {
it('Notify - deleted node - offer cancel', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
@@ -1804,13 +1779,6 @@ describe('OrderBook', function() {
it('Notify - deleted node - offer cancel - last offer', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
@@ -1835,13 +1803,6 @@ describe('OrderBook', function() {
it('Notify - modified node', function() {
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
currency_pays: 'XRP'
});
var remote = new Remote();
var book = remote.createOrderBook({
currency_gets: 'USD',
issuer_gets: addresses.ISSUER,
@@ -2223,7 +2184,7 @@ describe('OrderBook', function() {
case 'book_offers':
assert.deepEqual(request.message, {
command: 'book_offers',
id: void(0),
id: undefined,
taker_gets: {
currency: '0000000000000000000000004254430000000000',
issuer: addresses.ISSUER
@@ -2260,7 +2221,8 @@ describe('OrderBook', function() {
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000000',
Sequence: 195,
TakerGets: { currency: 'BTC',
TakerGets: {
currency: 'BTC',
issuer: addresses.ISSUER,
value: '0.1129232560043778'
},
@@ -2311,7 +2273,8 @@ describe('OrderBook', function() {
LedgerEntryType: 'Offer',
OwnerNode: '0000000000000144',
Sequence: 29356,
TakerGets: { currency: 'BTC',
TakerGets: {
currency: 'BTC',
issuer: addresses.ISSUER,
value: '0.5'
},
@@ -2464,9 +2427,9 @@ describe('OrderBook', function() {
case 'book_offers':
assert.deepEqual(request.message, {
command: 'book_offers',
id: void(0),
id: undefined,
taker_gets: {
currency: '0000000000000000000000000000000000000000',
currency: '0000000000000000000000000000000000000000'
},
taker_pays: {
currency: '0000000000000000000000005553440000000000',