diff --git a/circle.yml b/circle.yml index a0cd477e..f4a4ca29 100644 --- a/circle.yml +++ b/circle.yml @@ -1,7 +1,10 @@ machine: node: version: 0.12.0 +<<<<<<< HEAD test: override: - bin/ci.sh "$CIRCLE_NODE_INDEX" "$CIRCLE_NODE_TOTAL": parallel: true +======= +>>>>>>> upstream/master diff --git a/src/core/amount.js b/src/core/amount.js index 0e50d7f2..8be48118 100644 --- a/src/core/amount.js +++ b/src/core/amount.js @@ -356,7 +356,7 @@ Amount.prototype._copy = function(value) { Amount.prototype.compareTo = function(to) { const toAmount = Amount.from_json(to); if (!this.is_comparable(toAmount)) { - return new Amount(); + return 0; } return this._value.comparedTo(toAmount._value); }; diff --git a/src/core/autobridgecalculator.js b/src/core/autobridgecalculator.js index 746a085d..9448e925 100644 --- a/src/core/autobridgecalculator.js +++ b/src/core/autobridgecalculator.js @@ -20,7 +20,8 @@ function assertValidLegOneOffer(legOneOffer, message) { function AutobridgeCalculator(currencyGets, currencyPays, legOneOffers, legTwoOffers, issuerGets, issuerPays) { this._currencyGets = currencyGets; - this._currencyPays = currencyPays; + this._currencyGetsHex = currencyGets.to_hex(); + this._currencyPaysHex = currencyPays.to_hex(); this._issuerGets = issuerGets; this._issuerPays = issuerPays; this.legOneOffers = _.cloneDeep(legOneOffers); @@ -29,6 +30,8 @@ function AutobridgeCalculator(currencyGets, currencyPays, this._ownerFundsLeftover = {}; } +AutobridgeCalculator.NULL_AMOUNT = Utils.normalizeAmount('0'); + /** * Calculates an ordered array of autobridged offers by quality * @@ -39,7 +42,7 @@ AutobridgeCalculator.prototype.calculate = function() { let legOnePointer = 0; let legTwoPointer = 0; - let offersAutobridged = []; + const offersAutobridged = []; this.clearOwnerFundsLeftover(); @@ -70,14 +73,17 @@ AutobridgeCalculator.prototype.calculate = function() { continue; } - if (legOneTakerGetsFunded.compareTo(legTwoTakerPaysFunded) > 0) { + // using private fields for speed + if (legOneTakerGetsFunded._value.comparedTo( + legTwoTakerPaysFunded._value) > 0) { autobridgedOffer = this.getAutobridgedOfferWithClampedLegOne( legOneOffer, legTwoOffer ); legTwoPointer++; - } else if (legTwoTakerPaysFunded.compareTo(legOneTakerGetsFunded) > 0) { + } else if (legTwoTakerPaysFunded._value.comparedTo( + legOneTakerGetsFunded._value) > 0) { autobridgedOffer = this.getAutobridgedOfferWithClampedLegTwo( legOneOffer, legTwoOffer @@ -210,8 +216,6 @@ AutobridgeCalculator.prototype.clearOwnerFundsLeftover = function() { */ AutobridgeCalculator.prototype.resetOwnerFundsLeftover = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); - this._ownerFundsLeftover[account] = Utils.normalizeAmount('0'); return this._ownerFundsLeftover[account]; @@ -226,12 +230,10 @@ AutobridgeCalculator.prototype.resetOwnerFundsLeftover = function(account) { */ AutobridgeCalculator.prototype.getLeftoverOwnerFunds = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); - let amount = this._ownerFundsLeftover[account]; if (!amount) { - amount = Utils.normalizeAmount('0'); + amount = AutobridgeCalculator.NULL_AMOUNT.clone(); } return amount; @@ -248,7 +250,6 @@ AutobridgeCalculator.prototype.getLeftoverOwnerFunds = function(account) { AutobridgeCalculator.prototype.addLeftoverOwnerFunds = function(account, amount) { - assert(UInt160.is_valid(account), 'Account is invalid'); assert(amount instanceof Amount, 'Amount is invalid'); this._ownerFundsLeftover[account] = this.getLeftoverOwnerFunds(account) @@ -266,7 +267,6 @@ function(account, amount) { AutobridgeCalculator.prototype.setLeftoverOwnerFunds = function(account, amount) { - assert(UInt160.is_valid(account), 'Account is invalid'); assert(amount instanceof Amount, 'Amount is invalid'); this._ownerFundsLeftover[account] = amount; @@ -291,13 +291,13 @@ function(takerGets, takerPays) { autobridgedOffer.TakerGets = { value: takerGets.to_text(), - currency: this._currencyGets.to_hex(), + currency: this._currencyGetsHex, issuer: this._issuerGets }; autobridgedOffer.TakerPays = { value: takerPays.to_text(), - currency: this._currencyPays.to_hex(), + currency: this._currencyPaysHex, issuer: this._issuerPays }; @@ -308,7 +308,8 @@ function(takerGets, takerPays) { autobridgedOffer.autobridged = true; - autobridgedOffer.BookDirectory = Utils.convertOfferQualityToHex(quality); + autobridgedOffer.BookDirectory = Utils.convertOfferQualityToHexFromText(autobridgedOffer.quality); + autobridgedOffer.qualityHex = autobridgedOffer.BookDirectory; return autobridgedOffer; }; diff --git a/src/core/baseconverter.js b/src/core/baseconverter.js index 6c0bc541..ef16e64a 100644 --- a/src/core/baseconverter.js +++ b/src/core/baseconverter.js @@ -1,28 +1,39 @@ 'use strict'; + function normalize(digitArray) { - while (digitArray[0] === 0) { - digitArray.shift(); + let i = 0; + while (digitArray[i] === 0) { + ++i; + } + if (i > 0) { + digitArray.splice(0, i); } return digitArray; } function divmod(digitArray, base, divisor) { - var remainder = 0; - var quotient = []; - for (var j = 0; j < digitArray.length; j++) { - var temp = remainder * base + parseInt(digitArray[j], 10); - quotient.push(Math.floor(temp / divisor)); + let remainder = 0; + let temp, divided; + let j = -1; + + const length = digitArray.length; + const quotient = new Array(length); + + while (++j < length) { + temp = remainder * base + digitArray[j]; + divided = temp / divisor; + quotient[j] = divided << 0; remainder = temp % divisor; } return {quotient: normalize(quotient), remainder: remainder}; } function convertBase(digitArray, fromBase, toBase) { - var result = []; - var dividend = digitArray; + const result = []; + let dividend = digitArray, qr; while (dividend.length > 0) { - var qr = divmod(dividend, fromBase, toBase); + qr = divmod(dividend, fromBase, toBase); result.unshift(qr.remainder); dividend = qr.quotient; } diff --git a/src/core/currency.js b/src/core/currency.js index 66a7e603..d40c4a9a 100644 --- a/src/core/currency.js +++ b/src/core/currency.js @@ -22,7 +22,7 @@ var Currency = extend(function() { this._update(); }, UInt160); -Currency.prototype = extend({}, UInt160.prototype); +Currency.prototype = Object.create(extend({}, UInt160.prototype)); Currency.prototype.constructor = Currency; Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000'; diff --git a/src/core/orderbook.js b/src/core/orderbook.js index 10360bd3..c46c2430 100644 --- a/src/core/orderbook.js +++ b/src/core/orderbook.js @@ -24,10 +24,6 @@ const OrderBookUtils = require('./orderbookutils'); const log = require('./log').internal.sub('orderbook'); const IOUValue = require('./iouvalue').IOUValue; -function assertValidNumber(number, message) { - assert(!_.isNull(number) && !isNaN(number), message); -} - /** * @constructor OrderBook * @param {Remote} remote @@ -36,11 +32,12 @@ function assertValidNumber(number, message) { * @param {String} bid currency * @param {String} bid issuer * @param {String} orderbook key + * @param {Boolean} fire 'model' event after receiving transaction + only once in 10 seconds */ function OrderBook(remote, - currencyGets, issuerGets, currencyPays, issuerPays, - key) { + currencyGets, issuerGets, currencyPays, issuerPays, key) { EventEmitter.call(this); const self = this; @@ -61,6 +58,8 @@ function OrderBook(remote, this._ownerFundsUnadjusted = {}; this._ownerFunds = {}; this._ownerOffersTotal = {}; + this._validAccounts = {}; + this._validAccountsCount = 0; // We consider ourselves synced if we have a current // copy of the offers, we are online and subscribed to updates @@ -73,13 +72,44 @@ function OrderBook(remote, // books that we must keep track of to compute autobridged offers this._legOneBook = null; this._legTwoBook = null; + this._gotOffersFromLegOne = false; + this._gotOffersFromLegTwo = false; + + this.sortOffers = this._currencyGets.has_interest() ? + _sortOffers.bind(this) : _sortOffersQuick; + + this.notifyDirectOffersChanged = + _.debounce( + this.notifyDirectOffersChangedInternal, + OrderBook.NOTIFY_TIMEOUT, + {maxWait: OrderBook.NOTIFY_MAXWAIT}); this._isAutobridgeable = !this._currencyGets.is_native() && !this._currencyPays.is_native(); - function computeAutobridgedOffersWrapper() { - self.computeAutobridgedOffers(); - self.mergeDirectAndAutobridgedBooks(); + this._autobridgeThrottleTimeMultiplier = 1; + this.createDebouncedOffersWrapper(); + + function computeAutobridgedOffersWrapperOne() { + self._gotOffersFromLegOne = true; + self.computeAutobridgedOffersThrottled(); + } + + function computeAutobridgedOffersWrapperTwo() { + self._gotOffersFromLegTwo = true; + self.computeAutobridgedOffersThrottled(); + } + + function onDisconnect() { + self.resetCache(); + self._gotOffersFromLegOne = false; + self._gotOffersFromLegTwo = false; + if (!self._destroyed) { + self._remote.once('disconnect', onDisconnect); + self._remote.once('connect', function() { + self.subscribe(); + }); + } } if (this._isAutobridgeable) { @@ -89,15 +119,15 @@ function OrderBook(remote, issuer_pays: issuerPays }); - this._legOneBook.on('model', computeAutobridgedOffersWrapper); - this._legTwoBook = remote.createOrderBook({ currency_gets: currencyGets, issuer_gets: issuerGets, currency_pays: 'XRP' }); + } - this._legTwoBook.on('model', computeAutobridgedOffersWrapper); + function updateFundedAmountsWrapper(transaction) { + self.updateFundedAmounts(transaction); } function listenersModified(action, event) { @@ -107,7 +137,16 @@ function OrderBook(remote, switch (action) { case 'add': if (++self._listeners === 1) { + self._shouldSubscribe = true; self.subscribe(); + + self._remote.on('transaction', updateFundedAmountsWrapper); + self._remote.once('disconnect', onDisconnect); + + if (self._isAutobridgeable) { + self._legOneBook.on('model', computeAutobridgedOffersWrapperOne); + self._legTwoBook.on('model', computeAutobridgedOffersWrapperTwo); + } } break; case 'remove': @@ -119,10 +158,6 @@ function OrderBook(remote, } } - function updateFundedAmountsWrapper(transaction) { - self.updateFundedAmounts(transaction); - } - this.on('newListener', function(event) { listenersModified('add', event); }); @@ -131,23 +166,21 @@ function OrderBook(remote, listenersModified('remove', event); }); - this._remote.on('transaction', updateFundedAmountsWrapper); - this.on('unsubscribe', function() { self.resetCache(); self._remote.removeListener('transaction', updateFundedAmountsWrapper); - }); + self._remote.removeListener('disconnect', onDisconnect); - this._remote.once('prepare_subscribe', function() { - self.subscribe(); - }); + self._gotOffersFromLegOne = false; + self._gotOffersFromLegTwo = false; - this._remote.on('disconnect', function() { - self.resetCache(); - self._remote.once('prepare_subscribe', function() { - self.subscribe(); - }); + if (self._isAutobridgeable) { + self._legOneBook.removeListener('model', + computeAutobridgedOffersWrapperOne); + self._legTwoBook.removeListener('model', + computeAutobridgedOffersWrapperTwo); + } }); return this; @@ -165,7 +198,21 @@ OrderBook.EVENTS = [ 'offer_changed', 'offer_funds_changed' ]; -OrderBook.DEFAULT_TRANSFER_RATE = 1000000000; +OrderBook.DEFAULT_TRANSFER_RATE = Amount.from_json(1000000000); + +OrderBook.NOTIFY_TIMEOUT = 100; + +OrderBook.NOTIFY_MAXWAIT = 250; + +OrderBook.AUTOBRIDGE_CALCULATE_THROTTLE_TIME = 1000; + +OrderBook.AUTOBRIDGE_CALCULATE_DEBOUNCE_TIME = 250; + +OrderBook.AUTOBRIDGE_CALCULATE_DEBOUNCE_MAXWAIT = 500; + +OrderBook.ZERO_NATIVE_AMOUNT = Amount.from_json('0'); + +OrderBook.ZERO_NORMALIZED_AMOUNT = OrderBookUtils.normalizeAmount('0'); /** * Normalize offers from book_offers and transaction stream @@ -192,18 +239,20 @@ OrderBook.offerRewrite = function(offer) { result.Flags = result.Flags || 0; result.OwnerNode = result.OwnerNode || new Array(16 + 1).join('0'); result.BookNode = result.BookNode || new Array(16 + 1).join('0'); + result.qualityHex = result.BookDirectory.slice(-16); return result; }; /** * Initialize orderbook. Get orderbook offers and subscribe to transactions + * @api private */ OrderBook.prototype.subscribe = function() { const self = this; - if (!this._shouldSubscribe) { + if (!this._shouldSubscribe || this._destroyed) { return; } @@ -216,7 +265,7 @@ OrderBook.prototype.subscribe = function() { self.requestTransferRate(callback); }, function(callback) { - self.requestOffers(callback); + self.requestOffers(callback, true); }, function(callback) { self.subscribeTransactions(callback); @@ -229,6 +278,7 @@ OrderBook.prototype.subscribe = function() { /** * Unhook event listeners and prevent ripple-lib from further work on this * orderbook. There is no more orderbook stream, so "unsubscribe" is nominal + * @api private */ OrderBook.prototype.unsubscribe = function() { @@ -250,33 +300,75 @@ OrderBook.prototype.unsubscribe = function() { this.emit('unsubscribe'); }; +/** + * After that you can't use this object. + */ + +OrderBook.prototype.destroy = function() { + this._destroyed = true; + if (this._subscribed) { + this.unsubscribe(); + } + + if (this._remote._books.hasOwnProperty(this._key)) { + delete this._remote._books[this._key]; + } + + if (this._isAutobridgeable) { + this._legOneBook.destroy(); + this._legTwoBook.destroy(); + } +}; + /** * Request orderbook entries from server * * @param {Function} callback */ -OrderBook.prototype.requestOffers = function(callback=function() {}) { +OrderBook.prototype.requestOffers = function(callback = function() {}, + internal = false) { const self = this; + if (!this._remote.isConnected()) { + // do not make request if not online. + // that requests will be queued and + // eventually all of them will fire back + return undefined; + } + if (!this._shouldSubscribe) { - return callback(new Error('Should not request offers')); + callback(new Error('Should not request offers')); + return undefined; } if (this._remote.trace) { log.info('requesting offers', this._key); } + if (this._isAutobridgeable && !internal) { + this._gotOffersFromLegOne = false; + this._gotOffersFromLegTwo = false; + + this._legOneBook.requestOffers(); + this._legTwoBook.requestOffers(); + } + + function handleOffers(res) { + if (self._destroyed) { + return; + } + if (!Array.isArray(res.offers)) { // XXX What now? - return callback(new Error('Invalid response')); + callback(new Error('Invalid response')); + return; } if (self._remote.trace) { log.info('requested offers', self._key, 'offers: ' + res.offers.length); } - self.setOffers(res.offers); self.notifyDirectOffersChanged(); @@ -331,8 +423,9 @@ OrderBook.prototype.requestTransferRate = function(callback) { // 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; + self._issuerTransferRate = + Amount.from_json(info.account_data.TransferRate || + OrderBook.DEFAULT_TRANSFER_RATE); callback(null, self._issuerTransferRate); } @@ -392,7 +485,7 @@ OrderBook.prototype.subscribeTransactions = function(callback) { * books, an additional merge step is also performed */ -OrderBook.prototype.notifyDirectOffersChanged = function() { +OrderBook.prototype.notifyDirectOffersChangedInternal = function() { if (this._isAutobridgeable) { this.mergeDirectAndAutobridgedBooks(); } else { @@ -409,6 +502,12 @@ OrderBook.prototype.resetCache = function() { this._ownerOffersTotal = {}; this._offerCounts = {}; this._synced = false; + this._offers = []; + + if (this._validAccountsCount > 3000) { + this._validAccounts = {}; + this._validAccountsCount = 0; + } }; /** @@ -418,7 +517,6 @@ OrderBook.prototype.resetCache = function() { */ OrderBook.prototype.hasOwnerFunds = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); return this._ownerFunds[account] !== undefined; }; @@ -430,7 +528,6 @@ OrderBook.prototype.hasOwnerFunds = function(account) { */ OrderBook.prototype.setOwnerFunds = function(account, fundedAmount) { - assert(UInt160.is_valid(account), 'Account is invalid'); assert(!isNaN(fundedAmount), 'Funded amount is invalid'); this._ownerFundsUnadjusted[account] = fundedAmount; @@ -447,11 +544,16 @@ OrderBook.prototype.setOwnerFunds = function(account, fundedAmount) { OrderBook.prototype.applyTransferRate = function(balance) { assert(!isNaN(balance), 'Balance is invalid'); - assertValidNumber(this._issuerTransferRate, 'Transfer rate is invalid'); + assert(this._issuerTransferRate.is_valid(), 'Transfer rate is invalid'); const adjustedBalance = (new IOUValue(balance)) .divide(new IOUValue(this._issuerTransferRate)) .multiply(new IOUValue(OrderBook.DEFAULT_TRANSFER_RATE)).toString(); + const adjustedBalance = OrderBookUtils.normalizeAmount(balance) + .divide(this._issuerTransferRate) + .multiply(OrderBook.DEFAULT_TRANSFER_RATE) + .to_json() + .value; return adjustedBalance; }; @@ -464,7 +566,6 @@ OrderBook.prototype.applyTransferRate = function(balance) { */ OrderBook.prototype.getOwnerFunds = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); if (this.hasOwnerFunds(account)) { if (this._currencyGets.is_native()) { return Amount.from_json(this._ownerFunds[account]); @@ -481,7 +582,6 @@ OrderBook.prototype.getOwnerFunds = function(account) { */ OrderBook.prototype.getUnadjustedOwnerFunds = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); return this._ownerFundsUnadjusted[account]; }; @@ -492,7 +592,6 @@ OrderBook.prototype.getUnadjustedOwnerFunds = function(account) { */ OrderBook.prototype.deleteOwnerFunds = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); this._ownerFunds[account] = undefined; }; @@ -504,7 +603,6 @@ OrderBook.prototype.deleteOwnerFunds = function(account) { */ OrderBook.prototype.getOwnerOfferCount = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); return this._offerCounts[account] || 0; }; @@ -516,7 +614,6 @@ OrderBook.prototype.getOwnerOfferCount = function(account) { */ OrderBook.prototype.incrementOwnerOfferCount = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); const result = (this._offerCounts[account] || 0) + 1; this._offerCounts[account] = result; return result; @@ -531,7 +628,6 @@ OrderBook.prototype.incrementOwnerOfferCount = function(account) { */ OrderBook.prototype.decrementOwnerOfferCount = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); const result = (this._offerCounts[account] || 1) - 1; this._offerCounts[account] = result; @@ -552,8 +648,6 @@ OrderBook.prototype.decrementOwnerOfferCount = function(account) { */ OrderBook.prototype.addOwnerOfferTotal = function(account, amount) { - assert(UInt160.is_valid(account), 'Account is invalid'); - const previousAmount = this.getOwnerOfferTotal(account); const currentAmount = previousAmount.add(Amount.from_json(amount)); @@ -572,14 +666,12 @@ OrderBook.prototype.addOwnerOfferTotal = function(account, amount) { */ OrderBook.prototype.subtractOwnerOfferTotal = function(account, amount) { - assert(UInt160.is_valid(account), 'Account is invalid'); - const previousAmount = this.getOwnerOfferTotal(account); const newAmount = previousAmount.subtract(Amount.from_json(amount)); + this._ownerOffersTotal[account] = newAmount; assert(!newAmount.is_negative(), 'Offer total cannot be negative'); - return newAmount; }; @@ -591,15 +683,14 @@ OrderBook.prototype.subtractOwnerOfferTotal = function(account, amount) { */ OrderBook.prototype.getOwnerOfferTotal = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); const amount = this._ownerOffersTotal[account]; if (amount) { return amount; } if (this._currencyGets.is_native()) { - return Amount.from_json('0'); + return OrderBook.ZERO_NATIVE_AMOUNT.clone(); } - return OrderBookUtils.normalizeAmount('0'); + return OrderBook.ZERO_NORMALIZED_AMOUNT.clone(); }; /** @@ -610,11 +701,10 @@ OrderBook.prototype.getOwnerOfferTotal = function(account) { */ OrderBook.prototype.resetOwnerOfferTotal = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); if (this._currencyGets.is_native()) { - this._ownerOffersTotal[account] = Amount.from_json('0'); + this._ownerOffersTotal[account] = OrderBook.ZERO_NATIVE_AMOUNT.clone(); } else { - this._ownerOffersTotal[account] = OrderBookUtils.normalizeAmount('0'); + this._ownerOffersTotal[account] = OrderBook.ZERO_NORMALIZED_AMOUNT.clone(); } }; @@ -632,17 +722,18 @@ OrderBook.prototype.resetOwnerOfferTotal = function(account) { OrderBook.prototype.setOfferFundedAmount = function(offer) { assert.strictEqual(typeof offer, 'object', 'Offer is invalid'); + const takerGets = Amount.from_json(offer.TakerGets); const fundedAmount = this.getOwnerFunds(offer.Account); const previousOfferSum = this.getOwnerOfferTotal(offer.Account); - const currentOfferSum = previousOfferSum.add( - Amount.from_json(offer.TakerGets)); + const currentOfferSum = previousOfferSum.add(takerGets); offer.owner_funds = this.getUnadjustedOwnerFunds(offer.Account); - offer.is_fully_funded = fundedAmount.compareTo(currentOfferSum) >= 0; + offer.is_fully_funded = fundedAmount.is_comparable(currentOfferSum) && + fundedAmount.compareTo(currentOfferSum) >= 0; if (offer.is_fully_funded) { - offer.taker_gets_funded = Amount.from_json(offer.TakerGets).to_text(); + offer.taker_gets_funded = takerGets.to_text(); offer.taker_pays_funded = Amount.from_json(offer.TakerPays).to_text(); } else if (previousOfferSum.compareTo(fundedAmount) < 0) { offer.taker_gets_funded = fundedAmount.subtract(previousOfferSum).to_text(); @@ -698,7 +789,11 @@ OrderBook.prototype.parseAccountBalanceFromNode = function(node) { } assert(!isNaN(result.balance), 'node has an invalid balance'); - assert(UInt160.is_valid(result.account), 'node has an invalid account'); + if (this._validAccounts[result.Account] === undefined) { + assert(UInt160.is_valid(result.account), 'node has an invalid account'); + this._validAccounts[result.Account] = true; + this._validAccountsCount++; + } return result; }; @@ -786,6 +881,7 @@ OrderBook.prototype.updateFundedAmounts = function(transaction) { }); }; + /** * Update offers' funded amount with their owner's funds * @@ -793,10 +889,15 @@ OrderBook.prototype.updateFundedAmounts = function(transaction) { */ OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) { - assert(UInt160.is_valid(account), 'Account is invalid'); + // assert(UInt160.is_valid(account), 'Account is invalid'); const self = this; + if (!this.hasOwnerFunds(account)) { + // We are only updating owner funds that are already cached + return; + } + if (this._remote.trace) { const ownerFunds = this.getOwnerFunds(account); log.info('updating offer funds', this._key, account, @@ -848,7 +949,7 @@ OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) { OrderBook.prototype.notify = function(transaction) { const self = this; - if (!(this._subscribed && this._synced)) { + if (!(this._subscribed && this._synced) || this._destroyed) { return; } @@ -884,6 +985,12 @@ OrderBook.prototype.notify = function(transaction) { function handleNode(node) { switch (node.nodeType) { case 'DeletedNode': + if (self._validAccounts[node.fields.Account] === undefined) { + assert(UInt160.is_valid(node.fields.Account), + 'node has an invalid account'); + self._validAccounts[node.fields.Account] = true; + self._validAccountsCount++; + } self.deleteOffer(node, isOfferCancel); // We don't want to count an OfferCancel as a trade @@ -894,6 +1001,12 @@ OrderBook.prototype.notify = function(transaction) { break; case 'ModifiedNode': + if (self._validAccounts[node.fields.Account] === undefined) { + assert(UInt160.is_valid(node.fields.Account), + 'node has an invalid account'); + self._validAccounts[node.fields.Account] = true; + self._validAccountsCount++; + } self.modifyOffer(node); takerGetsTotal = takerGetsTotal @@ -906,6 +1019,12 @@ OrderBook.prototype.notify = function(transaction) { break; case 'CreatedNode': + if (self._validAccounts[node.fields.Account] === undefined) { + assert(UInt160.is_valid(node.fields.Account), + 'node has an invalid account'); + self._validAccounts[node.fields.Account] = true; + self._validAccountsCount++; + } // rippled does not set owner_funds if the order maker is the issuer // because the value would be infinite const fundedAmount = transactionOwnerFunds !== undefined ? @@ -919,7 +1038,9 @@ OrderBook.prototype.notify = function(transaction) { _.each(affectedNodes, handleNode); this.emit('transaction', transaction); + this.notifyDirectOffersChanged(); + if (!takerGetsTotal.is_zero()) { this.emit('trade', takerPaysTotal, takerGetsTotal); } @@ -951,17 +1072,27 @@ OrderBook.prototype.insertOffer = function(node) { const originalLength = this._offers.length; - for (let i = 0; i < originalLength; i++) { - const quality = OrderBookUtils.getOfferQuality(offer, this._currencyGets); - const existingOfferQuality = OrderBookUtils.getOfferQuality( - this._offers[i], - this._currencyGets - ); + if (!this._currencyGets.has_interest()) { + // use fast path + for (let i = 0; i < originalLength; i++) { + if (offer.qualityHex <= this._offers[i].qualityHex) { + this._offers.splice(i, 0, offer); + break; + } + } + } else { + for (let i = 0; i < originalLength; i++) { + const quality = OrderBookUtils.getOfferQuality(offer, this._currencyGets); + const existingOfferQuality = OrderBookUtils.getOfferQuality( + this._offers[i], + this._currencyGets + ); - if (quality.compareTo(existingOfferQuality) <= 0) { - this._offers.splice(i, 0, offer); + if (quality.compareTo(existingOfferQuality) <= 0) { + this._offers.splice(i, 0, offer); - break; + break; + } } } @@ -1067,28 +1198,33 @@ OrderBook.prototype.deleteOffer = function(node, isOfferCancel) { OrderBook.prototype.setOffers = function(offers) { assert(Array.isArray(offers), 'Offers is not an array'); - const self = this; - this.resetCache(); - const newOffers = _.map(offers, function(rawOffer) { - const offer = OrderBook.offerRewrite(rawOffer); + let i = -1, offer; + const l = offers.length; - if (offer.hasOwnProperty('owner_funds')) { + while (++i < l) { + offer = OrderBook.offerRewrite(offers[i]); + + if (this._validAccounts[offer.Account] === undefined) { + assert(UInt160.is_valid(offer.Account), 'Account is invalid'); + this._validAccounts[offer.Account] = true; + this._validAccountsCount++; + } + if (offer.owner_funds !== undefined) { // The first offer of each owner from book_offers contains owner balance // of offer's output - self.setOwnerFunds(offer.Account, offer.owner_funds); + this.setOwnerFunds(offer.Account, offer.owner_funds); } - self.incrementOwnerOfferCount(offer.Account); + this.incrementOwnerOfferCount(offer.Account); - self.setOfferFundedAmount(offer); - self.addOwnerOfferTotal(offer.Account, offer.TakerGets); + this.setOfferFundedAmount(offer); + this.addOwnerOfferTotal(offer.Account, offer.TakerGets); + offers[i] = offer; + } - return offer; - }); - - this._offers = newOffers; + this._offers = offers; this._synced = true; }; @@ -1191,6 +1327,14 @@ OrderBook.prototype.computeAutobridgedOffers = function() { assert(!this._currencyGets.is_native() && !this._currencyPays.is_native(), 'Autobridging is only for IOU:IOU orderbooks'); + if (this._destroyed) { + return; + } + + if (!this._gotOffersFromLegOne || !this._gotOffersFromLegTwo) { + return; + } + const autobridgeCalculator = new AutobridgeCalculator( this._currencyGets, this._currencyPays, @@ -1203,6 +1347,44 @@ OrderBook.prototype.computeAutobridgedOffers = function() { this._offersAutobridged = autobridgeCalculator.calculate(); }; +OrderBook.prototype.computeAutobridgedOffersWrapper = function() { + var startTime = Date.now(); + this.computeAutobridgedOffers(); + this.mergeDirectAndAutobridgedBooks(); + var lasted = (Date.now() - startTime); + + const newMult = + ((lasted * 2 / OrderBook.AUTOBRIDGE_CALCULATE_THROTTLE_TIME) << 0) + 1; + if (newMult !== this._autobridgeThrottleTimeMultiplier) { + this._autobridgeThrottleTimeMultiplier = newMult; + this.createDebouncedOffersWrapper(); + } +} + +OrderBook.prototype.createDebouncedOffersWrapper = function() { + const m = this._autobridgeThrottleTimeMultiplier; + this.computeAutobridgedOffersThrottled = + _.debounce( + _.throttle( + this.computeAutobridgedOffersWrapper, + OrderBook.AUTOBRIDGE_CALCULATE_THROTTLE_TIME * m, + {leading: true, trailing: true}), + OrderBook.AUTOBRIDGE_CALCULATE_DEBOUNCE_TIME, + {maxWait: OrderBook.AUTOBRIDGE_CALCULATE_DEBOUNCE_MAXWAIT}); +} + + +function _sortOffers(a, b) { + const aQuality = OrderBookUtils.getOfferQuality(a, this._currencyGets); + const bQuality = OrderBookUtils.getOfferQuality(b, this._currencyGets); + + return aQuality._value.comparedTo(bQuality._value); +} + +function _sortOffersQuick(a, b) { + return a.qualityHex.localeCompare(b.qualityHex); +} + /** * Merge direct and autobridged offers into a combined orderbook * @@ -1210,22 +1392,24 @@ OrderBook.prototype.computeAutobridgedOffers = function() { */ OrderBook.prototype.mergeDirectAndAutobridgedBooks = function() { - const self = this; + + if (this._destroyed) { + return; + } if (_.isEmpty(this._offers) && _.isEmpty(this._offersAutobridged)) { - // still emit empty offers list to indicate that load is completed - this.emit('model', []); + if (this._synced && this._gotOffersFromLegOne && + this._gotOffersFromLegTwo) { + // emit empty model to indicate to listeners that we've got offers, + // just there was no one + this.emit('model', []); + } return; } this._mergedOffers = this._offers .concat(this._offersAutobridged) - .sort(function(a, b) { - const aQuality = OrderBookUtils.getOfferQuality(a, self._currencyGets); - const bQuality = OrderBookUtils.getOfferQuality(b, self._currencyGets); - - return aQuality.compareTo(bQuality); - }); + .sort(this.sortOffers); this.emit('model', this._mergedOffers); }; diff --git a/src/core/seed.js b/src/core/seed.js index 2e0f0115..e44c7a51 100644 --- a/src/core/seed.js +++ b/src/core/seed.js @@ -19,7 +19,7 @@ const Seed = extend(function() { }, UInt); Seed.width = 16; -Seed.prototype = extend({}, UInt.prototype); +Seed.prototype = Object.create(extend({}, UInt.prototype)); Seed.prototype.constructor = Seed; // value = NaN on error. diff --git a/src/core/uint128.js b/src/core/uint128.js index f3de0f96..13974730 100644 --- a/src/core/uint128.js +++ b/src/core/uint128.js @@ -13,7 +13,7 @@ var UInt128 = extend(function() { }, UInt); UInt128.width = 16; -UInt128.prototype = extend({}, UInt.prototype); +UInt128.prototype = Object.create(extend({}, UInt.prototype)); UInt128.prototype.constructor = UInt128; var HEX_ZERO = UInt128.HEX_ZERO = '00000000000000000000000000000000'; diff --git a/src/core/uint160.js b/src/core/uint160.js index 70cd4e97..94031bdf 100644 --- a/src/core/uint160.js +++ b/src/core/uint160.js @@ -17,7 +17,7 @@ var UInt160 = extend(function() { }, UInt); UInt160.width = 20; -UInt160.prototype = extend({}, UInt.prototype); +UInt160.prototype = Object.create(extend({}, UInt.prototype)); UInt160.prototype.constructor = UInt160; var HEX_ZERO = UInt160.HEX_ZERO = '0000000000000000000000000000000000000000'; diff --git a/src/core/uint256.js b/src/core/uint256.js index 47cd1cd0..b9a04236 100644 --- a/src/core/uint256.js +++ b/src/core/uint256.js @@ -13,7 +13,7 @@ var UInt256 = extend(function() { }, UInt); UInt256.width = 32; -UInt256.prototype = extend({}, UInt.prototype); +UInt256.prototype = Object.create(extend({}, UInt.prototype)); UInt256.prototype.constructor = UInt256; var HEX_ZERO = UInt256.HEX_ZERO = '00000000000000000000000000000000' + diff --git a/test/fixtures/orderbook.js b/test/fixtures/orderbook.js index 31ee8db3..307de420 100644 --- a/test/fixtures/orderbook.js +++ b/test/fixtures/orderbook.js @@ -5,6 +5,9 @@ const _ = require('lodash'); const addresses = require('./addresses'); const Meta = require('ripple-lib').Meta; +const Amount = require('ripple-lib').Amount; +const SerializedObject = require('ripple-lib').SerializedObject; +const Types = require('ripple-lib').types; module.exports.FIAT_BALANCE = '10'; module.exports.NATIVE_BALANCE = '55'; @@ -809,6 +812,8 @@ module.exports.transactionWithInvalidAccountRoot = function(options) { }; }; +const IOU_SUFFIX = '/000/rrrrrrrrrrrrrrrrrrrrrhoLvTp'; + module.exports.transactionWithCreatedOffer = function(options) { options = options || {}; _.defaults(options, { @@ -816,7 +821,16 @@ module.exports.transactionWithCreatedOffer = function(options) { amount: '1.9951' }); - const meta = new Meta({ + const takerGets = Amount.from_json(options.amount + IOU_SUFFIX); + const takerPays = Amount.from_json(module.exports.TAKER_PAYS + IOU_SUFFIX); + const quality = takerPays.divide(takerGets); + + const so = new SerializedObject(); + Types.Quality.serialize(so, quality); + + const BookDirectory = so.to_hex(); + + var meta = new Meta({ AffectedNodes: [ { CreatedNode: { @@ -824,7 +838,7 @@ module.exports.transactionWithCreatedOffer = function(options) { LedgerIndex: 'AF3C702057C9C47DB9E809FD8C76CD22521012C5CC7AE95D914EC9E226F1D7E5', NewFields: { Account: options.account, - BookDirectory: '7B73A610A009249B0CC0D4311E8BA7927B5A34D86634581C5F211CEE1E0697A0', + BookDirectory: BookDirectory, Flags: 131072, Sequence: 1404, TakerGets: { diff --git a/test/orderbook-autobridge-test.js b/test/orderbook-autobridge-test.js index 8d07d037..c30ea7e3 100644 --- a/test/orderbook-autobridge-test.js +++ b/test/orderbook-autobridge-test.js @@ -1,19 +1,30 @@ -/*eslint-disable max-len */ +/* eslint-disable max-len */ 'use strict'; -var _ = require('lodash'); -var assert = require('assert-diff'); -var Remote = require('ripple-lib').Remote; -var Currency = require('ripple-lib').Currency; -var addresses = require('./fixtures/addresses'); -var fixtures = require('./fixtures/orderbook'); +const _ = require('lodash'); +const assert = require('assert-diff'); +const Remote = require('ripple-lib').Remote; +const Currency = require('ripple-lib').Currency; +const Amount = require('ripple-lib').Amount; +const addresses = require('./fixtures/addresses'); +const fixtures = require('./fixtures/orderbook'); describe('OrderBook Autobridging', function() { this.timeout(0); + function createRemote() { + const remote = new Remote(); + + remote.isConnected = function() { + return true; + }; + + return remote; + } + it('Initialize IOU/IOU', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', @@ -27,23 +38,24 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); @@ -58,25 +70,26 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - leg one partially funded', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); legOneOffers[0].owner_funds = '2105863129'; book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); @@ -88,25 +101,26 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - leg two partially funded', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); legTwoOffers[0].owner_funds = '10'; book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); @@ -118,25 +132,26 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - leg two transfer rate', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1002000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); legTwoOffers[0].owner_funds = '10'; book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged[0].TakerGets.value, '9.980039920159681'); @@ -146,19 +161,19 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - taker funds < leg two in', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); legOneOffers[0].owner_funds = '33461561812'; @@ -169,6 +184,7 @@ describe('OrderBook Autobridging', function() { book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); @@ -180,19 +196,19 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - leg one partially funded - owners equal', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); legOneOffers[0].owner_funds = '2105863129'; @@ -201,6 +217,7 @@ describe('OrderBook Autobridging', function() { book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); @@ -212,19 +229,19 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - leg one partially funded - owners equal - leg two in > leg one out', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); legOneOffers[0].owner_funds = '2105863129'; @@ -236,6 +253,7 @@ describe('OrderBook Autobridging', function() { book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); @@ -247,23 +265,24 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - leg one consumes leg two fully', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2)); book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 2); @@ -280,19 +299,19 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - leg two consumes first leg one offer fully', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 2)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 2)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 1)); legTwoOffers[0].TakerGets.value = '170.7639524223001'; legTwoOffers[0].TakerPays = '49439476610'; @@ -301,6 +320,7 @@ describe('OrderBook Autobridging', function() { book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 2); @@ -317,19 +337,19 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - owners equal', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1002000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2)); legOneOffers[0].owner_funds = '2105863129'; legTwoOffers[1].owner_funds = '19.32660005780981'; @@ -339,6 +359,7 @@ describe('OrderBook Autobridging', function() { book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 2); @@ -355,19 +376,19 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - owners equal - leg one overfunded', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1002000000); - var legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); - var legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2)); + const legOneOffers = _.cloneDeep(fixtures.LEG_ONE_OFFERS.slice(0, 1)); + const legTwoOffers = _.cloneDeep(fixtures.LEG_TWO_OFFERS.slice(0, 2)); legOneOffers[0].owner_funds = '41461561812'; @@ -378,6 +399,7 @@ describe('OrderBook Autobridging', function() { book._legOneBook.setOffers(legOneOffers); book._legTwoBook.setOffers(legTwoOffers); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 2); @@ -394,20 +416,21 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - TakerPays < Quality * TakerGets', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); book._legOneBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '75', TakerPays: { value: '50', @@ -422,6 +445,7 @@ describe('OrderBook Autobridging', function() { book._legTwoBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '90', issuer: addresses.ISSUER, @@ -433,6 +457,7 @@ describe('OrderBook Autobridging', function() { } ]); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); @@ -444,20 +469,21 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - update funded amount', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); book._legOneBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '100', TakerPays: { value: '100', @@ -469,6 +495,7 @@ describe('OrderBook Autobridging', function() { }, { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '50', TakerPays: { value: '100', @@ -482,6 +509,7 @@ describe('OrderBook Autobridging', function() { book._legTwoBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '90', issuer: addresses.ISSUER, @@ -493,6 +521,7 @@ describe('OrderBook Autobridging', function() { }, { Account: addresses.OTHER_ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '30', issuer: addresses.ISSUER, @@ -504,6 +533,7 @@ describe('OrderBook Autobridging', function() { } ]); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 3); @@ -525,20 +555,21 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - update funded amount - owners equal', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); book._legOneBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '100', TakerPays: { value: '100', @@ -550,6 +581,7 @@ describe('OrderBook Autobridging', function() { }, { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '20', TakerPays: { value: '100', @@ -563,6 +595,7 @@ describe('OrderBook Autobridging', function() { book._legTwoBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '90', issuer: addresses.ISSUER, @@ -574,6 +607,7 @@ describe('OrderBook Autobridging', function() { }, { Account: addresses.OTHER_ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '30', issuer: addresses.ISSUER, @@ -585,6 +619,7 @@ describe('OrderBook Autobridging', function() { } ]); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 3); @@ -606,20 +641,21 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - update funded amount - first two owners equal', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); book._legOneBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '100', TakerPays: { value: '100', @@ -631,6 +667,7 @@ describe('OrderBook Autobridging', function() { }, { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '100', TakerPays: { value: '200', @@ -644,6 +681,7 @@ describe('OrderBook Autobridging', function() { book._legTwoBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '90', issuer: addresses.ISSUER, @@ -655,6 +693,7 @@ describe('OrderBook Autobridging', function() { }, { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '30', issuer: addresses.ISSUER, @@ -665,6 +704,7 @@ describe('OrderBook Autobridging', function() { }, { Account: addresses.OTHER_ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '20', issuer: addresses.ISSUER, @@ -676,6 +716,7 @@ describe('OrderBook Autobridging', function() { } ]); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 4); @@ -702,20 +743,21 @@ describe('OrderBook Autobridging', function() { }); it('Compute autobridged offers - unfunded offer - owners equal', function() { - var book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'EUR', issuer_gets: addresses.ISSUER, currency_pays: 'USD', issuer_pays: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; - book._legOneBook._issuerTransferRate = 1000000000; - book._legTwoBook._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); + book._legOneBook._issuerTransferRate = Amount.from_json(1000000000); + book._legTwoBook._issuerTransferRate = Amount.from_json(1000000000); book._legOneBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: '75', TakerPays: { value: '75', @@ -730,6 +772,7 @@ describe('OrderBook Autobridging', function() { book._legTwoBook.setOffers([ { Account: addresses.ACCOUNT, + BookDirectory: '3B95C29205977C2136BBC70F21895F8C8F471C8522BF446E570463F9CDB31517', TakerGets: { value: '90', issuer: addresses.ISSUER, @@ -741,6 +784,7 @@ describe('OrderBook Autobridging', function() { } ]); + book._gotOffersFromLegOne = book._gotOffersFromLegTwo = true; book.computeAutobridgedOffers(); assert.strictEqual(book._offersAutobridged.length, 1); diff --git a/test/orderbook-test.js b/test/orderbook-test.js index caefa353..7f382a5a 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -1,4 +1,4 @@ -/*eslint-disable max-len */ +/* eslint-disable max-len */ 'use strict'; @@ -13,8 +13,18 @@ const fixtures = require('./fixtures/orderbook'); describe('OrderBook', function() { this.timeout(0); + function createRemote() { + const remote = new Remote(); + + remote.isConnected = function() { + return true; + }; + + return remote; + } + it('toJSON', function() { - let book = new Remote().createOrderBook({ + let book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -30,7 +40,7 @@ describe('OrderBook', function() { } }); - book = new Remote().createOrderBook({ + book = createRemote().createOrderBook({ issuer_gets: addresses.ISSUER, currency_gets: 'BTC', currency_pays: 'XRP' @@ -48,7 +58,7 @@ describe('OrderBook', function() { }); it('Check orderbook validity', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -58,7 +68,7 @@ describe('OrderBook', function() { }); it('Automatic subscription (based on listeners)', function(done) { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -72,7 +82,7 @@ describe('OrderBook', function() { }); it('Subscribe', function(done) { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -94,7 +104,7 @@ describe('OrderBook', function() { }); it('Unsubscribe', function(done) { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -114,7 +124,7 @@ describe('OrderBook', function() { }); it('Automatic unsubscription - remove all listeners', function(done) { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -129,7 +139,7 @@ describe('OrderBook', function() { }); it('Automatic unsubscription - once listener', function(done) { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -144,33 +154,33 @@ describe('OrderBook', function() { }); it('Set owner funds', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOwnerFunds(addresses.ACCOUNT, '1'); assert.strictEqual(book.getOwnerFunds(addresses.ACCOUNT).to_text(), '1'); }); it('Set owner funds - unadjusted funds', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); book.setOwnerFunds(addresses.ACCOUNT, '1'); assert.strictEqual(book._ownerFundsUnadjusted[addresses.ACCOUNT], '1'); }); it('Set owner funds - invalid account', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -182,7 +192,7 @@ describe('OrderBook', function() { }); it('Set owner funds - invalid amount', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -194,7 +204,7 @@ describe('OrderBook', function() { }); it('Has owner funds', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -205,7 +215,7 @@ describe('OrderBook', function() { }); it('Delete owner funds', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -219,7 +229,7 @@ describe('OrderBook', function() { }); it('Delete owner funds', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -234,7 +244,7 @@ describe('OrderBook', function() { }); it('Increment owner offer count', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -244,20 +254,8 @@ describe('OrderBook', function() { assert.strictEqual(book.getOwnerOfferCount(addresses.ACCOUNT), 1); }); - it('Increment owner offer count - invalid address', function() { - const book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: addresses.ISSUER, - currency_pays: 'XRP' - }); - - assert.throws(function() { - book.incrementOwnerOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); - }); - }); - it('Decrement owner offer count', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -270,7 +268,7 @@ describe('OrderBook', function() { }); it('Decrement owner offer count - no more offers', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -283,20 +281,8 @@ describe('OrderBook', function() { assert.strictEqual(book.getOwnerFunds(addresses.ACCOUNT), undefined); }); - it('Decrement owner offer count - invalid address', function() { - const book = new Remote().createOrderBook({ - currency_gets: 'BTC', - issuer_gets: addresses.ISSUER, - currency_pays: 'XRP' - }); - - assert.throws(function() { - book.decrementOwnerOfferCount('zrrrrrrrrrrrrrrrrrrrBZbvji'); - }); - }); - it('Subtract owner offer total', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -324,7 +310,7 @@ describe('OrderBook', function() { }); it('Subtract owner offer total - negative total', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -340,7 +326,7 @@ describe('OrderBook', function() { }); it('Get owner offer total', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -355,7 +341,7 @@ describe('OrderBook', function() { }); it('Get owner offer total - native', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'XRP' @@ -367,7 +353,7 @@ describe('OrderBook', function() { }); it('Get owner offer total - no total', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -377,7 +363,7 @@ describe('OrderBook', function() { }); it('Get owner offer total - native - no total', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' @@ -387,31 +373,31 @@ describe('OrderBook', function() { }); it('Apply transfer rate - cached transfer rate', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); assert.strictEqual(book.applyTransferRate('1'), '0.9980039920159681'); }); it('Apply transfer rate - native currency', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); assert.strictEqual(book.applyTransferRate('0.9980039920159681'), '0.9980039920159681'); }); it('Apply transfer rate - invalid balance', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -423,7 +409,7 @@ describe('OrderBook', function() { }); it('Apply transfer rate - invalid transfer rate', function() { - const book = new Remote().createOrderBook({ + const book = createRemote().createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' @@ -435,7 +421,7 @@ describe('OrderBook', function() { }); it('Request transfer rate', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, @@ -451,19 +437,19 @@ describe('OrderBook', function() { request.emit('success', { account_data: { - TransferRate: 1002000000 + TransferRate: Amount.from_json(1002000000) } }); }; book.requestTransferRate(function(err, rate) { assert.ifError(err); - assert.strictEqual(rate, 1002000000); + assert(rate.equals(1002000000)); }); }); it('Request transfer rate - not set', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, @@ -485,19 +471,19 @@ describe('OrderBook', function() { book.requestTransferRate(function(err, rate) { assert.ifError(err); - assert.strictEqual(rate, 1000000000); + assert(rate.equals(1000000000)); }); }); it('Request transfer rate - cached transfer rate', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'BTC', issuer_gets: addresses.ISSUER, currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); remote.request = function() { assert(false); @@ -505,12 +491,12 @@ describe('OrderBook', function() { book.requestTransferRate(function(err, rate) { assert.ifError(err); - assert.strictEqual(rate, 1002000000); + assert(rate.equals(1002000000)); }); }); it('Request transfer rate - native currency', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, @@ -523,20 +509,20 @@ describe('OrderBook', function() { book.requestTransferRate(function(err, rate) { assert.ifError(err); - assert.strictEqual(rate, 1000000000); - assert.strictEqual(book._issuerTransferRate, 1000000000); + assert(rate.equals(1000000000)); + assert(book._issuerTransferRate.equals(1000000000)); }); }); it('Set offer funded amount - iou/xrp - fully funded', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'BTC', currency_pays: 'XRP', issuer_gets: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); const offer = { Account: addresses.ACCOUNT, @@ -565,14 +551,14 @@ describe('OrderBook', function() { }); it('Set offer funded amount - iou/xrp - unfunded', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'BTC', currency_pays: 'XRP', issuer_gets: addresses.ISSUER }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); const offer = { Account: addresses.ACCOUNT, @@ -603,14 +589,14 @@ describe('OrderBook', function() { }); it('Set offer funded amount - xrp/iou - funded', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); const offer = { Account: addresses.ACCOUNT, @@ -639,14 +625,14 @@ describe('OrderBook', function() { }); it('Set offer funded amount - xrp/iou - unfunded', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); const offer = { Account: addresses.ACCOUNT, @@ -677,14 +663,14 @@ describe('OrderBook', function() { }); it('Set offer funded amount - zero funds', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', issuer_pays: addresses.ISSUER, currency_pays: 'BTC' }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); const offer = { Account: addresses.ACCOUNT, @@ -711,7 +697,7 @@ describe('OrderBook', function() { }); it('Check is balance change node', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -761,7 +747,7 @@ describe('OrderBook', function() { }); it('Check is balance change node - not balance change', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', @@ -804,7 +790,7 @@ describe('OrderBook', function() { }); it('Check is balance change node - different currency', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'BTC', @@ -854,7 +840,7 @@ describe('OrderBook', function() { }); it('Check is balance change node - different issuer', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -904,7 +890,7 @@ describe('OrderBook', function() { }); it('Check is balance change node - native currency', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', @@ -939,7 +925,7 @@ describe('OrderBook', function() { }); it('Check is balance change node - native currency - not balance change', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', @@ -969,7 +955,7 @@ describe('OrderBook', function() { }); it('Parse account balance from node', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1064,7 +1050,7 @@ describe('OrderBook', function() { }); it('Parse account balance from node - native currency', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1105,7 +1091,7 @@ describe('OrderBook', function() { let receivedChangedEvents = 0; let receivedFundsChangedEvents = 0; - const remote = new Remote(); + const remote = createRemote(); const message = fixtures.transactionWithRippleState(); @@ -1115,7 +1101,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book._offers = fixtures.fiatOffers(); @@ -1155,7 +1141,7 @@ describe('OrderBook', function() { it('Update funded amounts - increase funds', function() { let receivedFundsChangedEvents = 0; - const remote = new Remote(); + const remote = createRemote(); const message = fixtures.transactionWithRippleState({ balance: '50' @@ -1167,7 +1153,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers({ account_funds: '19' @@ -1196,7 +1182,7 @@ describe('OrderBook', function() { }); it('Update funded amounts - owner_funds', function(done) { - const remote = new Remote(); + const remote = createRemote(); const message = fixtures.transactionWithRippleState(); @@ -1206,7 +1192,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); book._offers = fixtures.fiatOffers(); @@ -1222,7 +1208,7 @@ describe('OrderBook', function() { }); it('Update funded amounts - issuer transfer rate set', function(done) { - const remote = new Remote(); + const remote = createRemote(); const message = fixtures.transactionWithRippleState(); @@ -1232,7 +1218,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); book._ownerFunds[addresses.ACCOUNT] = '100'; book._offers = fixtures.fiatOffers(); @@ -1250,7 +1236,7 @@ describe('OrderBook', function() { let receivedChangedEvents = 0; let receivedFundsChangedEvents = 0; - const remote = new Remote(); + const remote = createRemote(); const message = fixtures.transactionWithAccountRoot(); @@ -1292,7 +1278,7 @@ describe('OrderBook', function() { }); it('Update funded amounts - no affected account', function(done) { - const remote = new Remote(); + const remote = createRemote(); const message = fixtures.transactionWithAccountRoot({ account: addresses.ACCOUNT @@ -1324,7 +1310,7 @@ describe('OrderBook', function() { }); it('Update funded amounts - no balance change', function(done) { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'XRP', @@ -1357,7 +1343,7 @@ describe('OrderBook', function() { }); it('Update funded amounts - deferred TransferRate', function(done) { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1376,7 +1362,7 @@ describe('OrderBook', function() { request.emit('success', fixtures.accountInfoResponse()); - assert.strictEqual(book._issuerTransferRate, fixtures.TRANSFER_RATE); + assert(book._issuerTransferRate.equals(fixtures.TRANSFER_RATE)); done(); }; @@ -1385,7 +1371,7 @@ describe('OrderBook', function() { }); it('Set offers - issuer transfer rate set - iou/xrp', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1393,7 +1379,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); const offers = fixtures.bookOffersResponse().offers; @@ -1418,7 +1404,7 @@ describe('OrderBook', function() { }); it('Set offers - issuer transfer rate set - iou/xrp - funded amounts', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1426,7 +1412,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); const offers = fixtures.bookOffersResponse({ account_funds: '233.13532' @@ -1491,7 +1477,7 @@ describe('OrderBook', function() { }); it('Set offers - multiple calls', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1499,7 +1485,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); const offers = fixtures.bookOffersResponse().offers; @@ -1525,7 +1511,7 @@ describe('OrderBook', function() { }); it('Set offers - incorrect taker pays funded', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1533,7 +1519,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); const offers = fixtures.DECIMAL_TAKER_PAYS_FUNDED_OFFERS; @@ -1546,7 +1532,7 @@ describe('OrderBook', function() { }); it('Notify - created node', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1554,7 +1540,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); book._subscribed = book._synced = true; const message = fixtures.transactionWithCreatedOffer(); @@ -1568,7 +1554,7 @@ describe('OrderBook', function() { }); it('Notify - created nodes - correct sorting', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1576,7 +1562,7 @@ describe('OrderBook', function() { currency_pays: 'XRP' }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); book._subscribed = book._synced = true; const offer = fixtures.transactionWithCreatedOffer(); @@ -1601,12 +1587,12 @@ describe('OrderBook', function() { assert.strictEqual(book._offers[2].Account, addresses.OTHER_ACCOUNT); }); - it('Notify - created nodes - events', function() { + it('Notify - created nodes - events', function(done) { let numTransactionEvents = 0; let numModelEvents = 0; let numOfferAddedEvents = 0; - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1626,7 +1612,7 @@ describe('OrderBook', function() { numOfferAddedEvents += 1; }); - book._issuerTransferRate = 1002000000; + book._issuerTransferRate = Amount.from_json(1002000000); book._subscribed = book._synced = true; const offer = fixtures.transactionWithCreatedOffer(); @@ -1638,12 +1624,15 @@ describe('OrderBook', function() { book.notify(offer3); assert.strictEqual(numTransactionEvents, 3); - assert.strictEqual(numModelEvents, 3); assert.strictEqual(numOfferAddedEvents, 3); + setTimeout(function() { + assert.strictEqual(numModelEvents, 1); + done(); + }, 300); }); it('Notify - deleted node', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1652,7 +1641,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1666,7 +1655,7 @@ describe('OrderBook', function() { }); it('Notify - deleted node - last offer', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1675,7 +1664,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers().slice(0, 1)); @@ -1687,13 +1676,13 @@ describe('OrderBook', function() { assert.strictEqual(book.getOwnerFunds(addresses.ACCOUNT), undefined); }); - it('Notify - deleted node - events', function() { + it('Notify - deleted node - events', function(done) { let numTransactionEvents = 0; let numModelEvents = 0; let numTradeEvents = 0; let numOfferRemovedEvents = 0; - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -1717,7 +1706,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1726,13 +1715,16 @@ describe('OrderBook', function() { book.notify(message); assert.strictEqual(numTransactionEvents, 1); - assert.strictEqual(numModelEvents, 1); assert.strictEqual(numTradeEvents, 1); assert.strictEqual(numOfferRemovedEvents, 1); + setTimeout(function() { + assert.strictEqual(numModelEvents, 1); + done(); + }, 300) }); it('Notify - deleted node - trade', function(done) { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -1754,7 +1746,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1764,7 +1756,7 @@ describe('OrderBook', function() { }); it('Notify - deleted node - offer cancel', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1773,7 +1765,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1789,7 +1781,7 @@ describe('OrderBook', function() { }); it('Notify - deleted node - offer cancel - last offer', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1798,7 +1790,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers().slice(0, 1)); @@ -1813,7 +1805,7 @@ describe('OrderBook', function() { }); it('Notify - modified node', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', @@ -1822,7 +1814,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1843,13 +1835,13 @@ describe('OrderBook', function() { assert.strictEqual(book._offers[1].taker_pays_funded, '972251352'); }); - it('Notify - modified node - events', function() { + it('Notify - modified node - events', function(done) { let numTransactionEvents = 0; let numModelEvents = 0; let numTradeEvents = 0; let numOfferChangedEvents = 0; - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -1873,7 +1865,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1882,13 +1874,16 @@ describe('OrderBook', function() { book.notify(message); assert.strictEqual(numTransactionEvents, 1); - assert.strictEqual(numModelEvents, 1); assert.strictEqual(numTradeEvents, 1); assert.strictEqual(numOfferChangedEvents, 1); + setTimeout(function() { + assert.strictEqual(numModelEvents, 1); + done(); + }, 300); }); it('Notify - modified node - trade', function(done) { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -1910,7 +1905,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1920,7 +1915,7 @@ describe('OrderBook', function() { }); it('Notify - modified nodes - trade', function(done) { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -1942,7 +1937,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1957,7 +1952,7 @@ describe('OrderBook', function() { let numTradeEvents = 0; let numOfferChangedEvents = 0; - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -1981,7 +1976,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -1998,7 +1993,7 @@ describe('OrderBook', function() { it('Notify - in disconnected state', function(done) { this.timeout(100); - const remote = new Remote(); + const remote = createRemote(); const transaction = fixtures.transactionWithDeletedOfferR(); const offers = { offers: fixtures.REQUEST_OFFERS_NATIVE @@ -2043,7 +2038,7 @@ describe('OrderBook', function() { }); }); it('Delete offer - offer cancel - funded after delete', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -2051,7 +2046,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers({ account_funds: '20' @@ -2070,7 +2065,7 @@ describe('OrderBook', function() { }); it('Delete offer - offer cancel - not fully funded after delete', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -2078,7 +2073,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers({ account_funds: '4.5' @@ -2099,7 +2094,7 @@ describe('OrderBook', function() { }); it('Insert offer - best quality', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -2107,7 +2102,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.QUALITY_OFFERS); @@ -2123,7 +2118,7 @@ describe('OrderBook', function() { }); it('Insert offer - best quality - insufficient funds for all offers', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -2131,7 +2126,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers()); @@ -2158,7 +2153,7 @@ describe('OrderBook', function() { }); it('Insert offer - worst quality - insufficient funds for all orders', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -2166,7 +2161,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers({ account_funds: '25' @@ -2195,7 +2190,7 @@ describe('OrderBook', function() { }); it('Insert offer - middle quality - insufficient funds for all offers', function() { - const remote = new Remote(); + const remote = createRemote(); const book = remote.createOrderBook({ currency_gets: 'USD', issuer_gets: addresses.ISSUER, @@ -2203,7 +2198,7 @@ describe('OrderBook', function() { }); book._subscribed = true; - book._issuerTransferRate = 1000000000; + book._issuerTransferRate = Amount.from_json(1000000000); book.setOffers(fixtures.fiatOffers({ account_funds: '30' @@ -2232,7 +2227,7 @@ describe('OrderBook', function() { }); it('Request offers - native currency', function(done) { - const remote = new Remote(); + const remote = createRemote(); const offers = { offers: fixtures.REQUEST_OFFERS_NATIVE @@ -2258,6 +2253,7 @@ describe('OrderBook', function() { is_fully_funded: false, taker_gets_funded: '600', taker_pays_funded: '33.6398379637041', + qualityHex: '5711A3A4254F5000', quality: '.0560663966061735' }, { @@ -2280,6 +2276,7 @@ describe('OrderBook', function() { is_fully_funded: true, taker_gets_funded: '2000', taker_pays_funded: '99.72233516476456', + qualityHex: '5711B6D8C62EF414', quality: '0.049861167582382' }, { @@ -2302,6 +2299,7 @@ describe('OrderBook', function() { is_fully_funded: true, taker_gets_funded: '2000', taker_pays_funded: '99.72233516476456', + qualityHex: '5711B6D8C62EF414', quality: '0.049861167582382' }, { @@ -2324,6 +2322,7 @@ describe('OrderBook', function() { taker_gets_funded: '1900', taker_pays_funded: '94.7362184065258', owner_funds: '3900', + qualityHex: '5711B6D8C62EF414', quality: '0.049861167582382' } ];