diff --git a/src/js/ripple/orderbook.js b/src/js/ripple/orderbook.js index b43643a518..98db75cc17 100644 --- a/src/js/ripple/orderbook.js +++ b/src/js/ripple/orderbook.js @@ -43,8 +43,9 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) { this._shouldSubscribe = true; this._listeners = 0; this._offers = [ ]; - this._ownerFunds = { }; this._offerCounts = { }; + this._ownerFunds = { }; + this._ownerOffers = { }; // We consider ourselves synchronized if we have a current // copy of the offers, we are online and subscribed to updates. @@ -180,6 +181,7 @@ OrderBook.prototype.unsubscribe = function() { OrderBook.prototype.resetCache = function() { this._ownerFunds = { }; + this._ownerOffers = { }; this._offerCounts = { }; this._synchronized = false; }; @@ -232,6 +234,8 @@ OrderBook.prototype.removeCachedFunds = function(account) { /** * Get offer count for offer owner + * + * @param {String} account address */ OrderBook.prototype.getOfferCount = function(account) { @@ -241,6 +245,8 @@ OrderBook.prototype.getOfferCount = function(account) { /** * Increment offer count for offer owner + * + * @param {String} account address */ OrderBook.prototype.incrementOfferCount = function(account) { @@ -252,6 +258,8 @@ OrderBook.prototype.incrementOfferCount = function(account) { /** * Decrement offer count for offer owner + * + * @param {String} account address */ OrderBook.prototype.decrementOfferCount = function(account) { @@ -261,6 +269,56 @@ OrderBook.prototype.decrementOfferCount = function(account) { return result; }; +/** + * Add offer amount to sum amount being offered by an account + * + * @param {String} account address + * @param {Object|String} offer amount + * @return sum + */ + +OrderBook.prototype.addOwnerOffer = function(account, amount) { + assert(UInt160.is_valid(account), 'Account is invalid'); + var previousAmount = this.getOwnerOfferSum(account); + var newAmount = previousAmount.add(Amount.from_json(amount)); + this._ownerOffers[account] = newAmount; + return newAmount; +}; + +/** + * Subtract offer amount from sum amount being offered by an account + * + * @param {String} account address + * @param {Object|String} offer amount + * @return sum + */ + +OrderBook.prototype.subtractOwnerOffer = function(account, amount) { + assert(UInt160.is_valid(account), 'Account is invalid'); + this._ownerOffers[account].subtract(Amount.from_json(amount)); + return this._ownerOffers[account]; +}; + +/** + * Get sum amount for offers by an account + * + * @param {String} account address + * @return sum + */ + +OrderBook.prototype.getOwnerOfferSum = function(account) { + assert(UInt160.is_valid(account), 'Account is invalid'); + var amount = this._ownerOffers[account]; + if (!amount) { + if (typeof amount === 'string') { + amount = Amount.from_json('0'); + } else { + amount = Amount.from_json('0' + OrderBook.IOU_SUFFIX); + } + } + return amount; +}; + /** * Compute funded amount for a balance/transferRate * @@ -358,11 +416,20 @@ OrderBook.prototype.setFundedAmount = function(offer, fundedAmount) { return offer; } + // Sum of offer amounts by an account + var offerSum = this.getOwnerOfferSum(offer.Account); + + if (offerSum.is_zero()) { + // If there are no cached offer amounts for the account, default to + // TakerGets of this offer + offerSum = Amount.from_json(offer.TakerGets); + } + offer.is_fully_funded = Amount.from_json( this._currencyGets.is_native() ? fundedAmount : fundedAmount + OrderBook.IOU_SUFFIX - ).compareTo(Amount.from_json(offer.TakerGets)) >= 0; + ).compareTo(offerSum) >= 0; if (offer.is_fully_funded) { offer.taker_gets_funded = Amount.from_json(offer.TakerGets).to_text(); @@ -376,12 +443,8 @@ OrderBook.prototype.setFundedAmount = function(offer, fundedAmount) { ? offer.TakerPays.value : offer.TakerPays; - var takerGetsValue = (typeof offer.TakerGets === 'object') - ? offer.TakerGets.value - : offer.TakerGets; - var takerPays = Amount.from_json(takerPaysValue + OrderBook.IOU_SUFFIX); - var takerGets = Amount.from_json(takerGetsValue + OrderBook.IOU_SUFFIX); + var takerGets = Amount.from_json(offerSum); var fundedPays = Amount.from_json(fundedAmount + OrderBook.IOU_SUFFIX); var rate = takerPays.divide(takerGets); @@ -662,9 +725,16 @@ OrderBook.offerRewrite = function(offer) { OrderBook.prototype.setOffers = function(offers) { assert(Array.isArray(offers)); + var l = offers.length; var newOffers = [ ]; - for (var i=0, l=offers.length; i