From ea82c8cce3a3688aab9d2ac90cdc6fefa1d34ff2 Mon Sep 17 00:00:00 2001 From: Geert Weening Date: Tue, 24 Jun 2014 16:33:05 -0700 Subject: [PATCH] [FEATURE] use Currency object and hex format in orderbook instead of checking the value of the currency string, create a Currency object and work with that the json format going to rippled will contain the hex value of the currency --- src/js/ripple/orderbook.js | 26 ++++++++++++++------------ src/js/ripple/remote.js | 6 +++--- src/js/ripple/request.js | 2 +- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/js/ripple/orderbook.js b/src/js/ripple/orderbook.js index 0799f697..99d30cee 100644 --- a/src/js/ripple/orderbook.js +++ b/src/js/ripple/orderbook.js @@ -21,9 +21,9 @@ function OrderBook(remote, currency_gets, issuer_gets, currency_pays, issuer_pay var self = this; this._remote = remote; - this._currency_gets = currency_gets; + this._currency_gets = Currency.from_json(currency_gets); this._issuer_gets = issuer_gets; - this._currency_pays = currency_pays; + this._currency_pays = Currency.from_json(currency_pays); this._issuer_pays = issuer_pays; this._subs = 0; this._key = key; @@ -132,18 +132,18 @@ OrderBook.prototype._prepareSubscribe = function (request) { OrderBook.prototype.to_json = function () { var json = { taker_gets: { - currency: this._currency_gets + currency: this._currency_gets.to_hex() }, taker_pays: { - currency: this._currency_pays + currency: this._currency_pays.to_hex() } }; - if (this._currency_gets !== 'XRP') { + if (!this._currency_gets.is_native()) { json.taker_gets.issuer = this._issuer_gets; } - if (this._currency_pays !== 'XRP') { + if (!this._currency_pays.is_native()) { json.taker_pays.issuer = this._issuer_pays; } @@ -159,17 +159,19 @@ OrderBook.prototype.to_json = function () { OrderBook.prototype.is_valid = function () { // XXX Should check for same currency (non-native) && same issuer return ( - Currency.is_valid(this._currency_pays) && - (this._currency_pays === 'XRP' || UInt160.is_valid(this._issuer_pays)) && - Currency.is_valid(this._currency_gets) && - (this._currency_gets === 'XRP' || UInt160.is_valid(this._issuer_gets)) && - !(this._currency_pays === 'XRP' && this._currency_gets === 'XRP') + this._currency_pays && this._currency_pays.is_valid() && + (this._currency_pays.is_native() || UInt160.is_valid(this._issuer_pays)) && + + this._currency_gets && this._currency_gets.is_valid() && + (this._currency_gets.is_native() || UInt160.is_valid(this._issuer_gets)) && + + !(this._currency_pays.is_native() && this._currency_gets.is_native()) ); }; OrderBook.prototype.trade = function(type) { var tradeStr = '0' - + ((this['_currency_' + type] === 'XRP') ? '' : '/' + + ((Currency.from_json(this['_currency_' + type]).is_native()) ? '' : '/' + this['_currency_' + type ] + '/' + this['_issuer_' + type]); return Amount.from_json(tradeStr); diff --git a/src/js/ripple/remote.js b/src/js/ripple/remote.js index 76f21771..eed4d4ef 100644 --- a/src/js/ripple/remote.js +++ b/src/js/ripple/remote.js @@ -1439,7 +1439,7 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) { currency: Currency.json_rewrite(gets.currency, {force_hex:true}) }; - if (request.message.taker_gets.currency !== 'XRP') { + if (!Currency.from_json(request.message.taker_gets.currency).is_native()) { request.message.taker_gets.issuer = UInt160.json_rewrite(gets.issuer); } @@ -1447,7 +1447,7 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) { currency: Currency.json_rewrite(pays.currency, {force_hex:true}) }; - if (request.message.taker_pays.currency !== 'XRP') { + if (!Currency.from_json(request.message.taker_pays.currency).is_native()) { request.message.taker_pays.issuer = UInt160.json_rewrite(pays.issuer); } @@ -1757,7 +1757,7 @@ Remote.prototype.createPathFind = function(src_account, dst_account, dst_amount, }; Remote.prepareTrade = function(currency, issuer) { - return currency + (currency === 'XRP' ? '' : ('/' + issuer)); + return currency + (Currency.from_json(currency).is_native() ? '' : ('/' + issuer)); }; /** diff --git a/src/js/ripple/request.js b/src/js/ripple/request.js index d058b3c9..7e20ec64 100644 --- a/src/js/ripple/request.js +++ b/src/js/ripple/request.js @@ -343,7 +343,7 @@ Request.prototype.addBook = function(book, snapshot) { currency: Currency.json_rewrite(book[side].currency, {force_hex: true}) }; - if (obj.currency !== 'XRP') { + if (!Currency.from_json(obj.currency).is_native()) { obj.issuer = UInt160.json_rewrite(book[side].issuer); } }