diff --git a/src/js/ripple/amount.js b/src/js/ripple/amount.js index dfc700d9..2a3add34 100644 --- a/src/js/ripple/amount.js +++ b/src/js/ripple/amount.js @@ -10,14 +10,6 @@ var Currency = require('./currency').Currency; var BigNumber = require('./bignumber'); -function isInteger(number) { - return parseInt(number) === number; -} - -function ensureDecimalPoint(value) { - return isInteger(value) ? String(value) + '.0' : value; -} - function inverse(number) { return (new BigNumber(number)).toPower(-1); } @@ -192,7 +184,7 @@ Amount.prototype.ratio_human = function(denominator, opts) { opts = extend({ }, opts); var numerator = this; - denominator = Amount.from_json(ensureDecimalPoint(denominator)); + denominator = Amount.from_json(denominator); // If either operand is NaN, the result is NaN. if (!numerator.is_valid() || !denominator.is_valid()) { @@ -252,7 +244,7 @@ Amount.prototype.ratio_human = function(denominator, opts) { Amount.prototype.product_human = function(factor, opts) { opts = opts || {}; - factor = Amount.from_json(ensureDecimalPoint(factor)); + factor = Amount.from_json(factor); // If either operand is NaN, the result is NaN. if (!this.is_valid() || !factor.is_valid()) { @@ -687,14 +679,13 @@ Amount.prototype.parse_json = function(j) { // - float = with precision 6 // XXX Improvements: disallow leading zeros. Amount.prototype.parse_native = function(j) { - if (typeof j === 'string' && j.match(/^-?\d*(\.\d{0,6})?$/)) { + if (typeof j === 'string' && !isNaN(parseInt(j))) { + if (j.indexOf('.') >= 0) { + throw new Error('Native amounts must be specified in integer drops') + } var value = new BigNumber(j); this._is_native = true; - if (j.indexOf('.') >= 0) { - this._set_value(value); - } else { - this._set_value(value.dividedBy(Amount.bi_xns_unit)); - } + this._set_value(value.dividedBy(Amount.bi_xns_unit)); } else { this._set_value(new BigNumber(NaN)); } diff --git a/test/amount-test.js b/test/amount-test.js index 7b001006..9fd277df 100644 --- a/test/amount-test.js +++ b/test/amount-test.js @@ -362,22 +362,26 @@ describe('Amount', function() { assert.strictEqual('0/XRP', Amount.from_json('0').to_text_full()); }); it('Parse native 0.0', function () { - assert.strictEqual('0/XRP', Amount.from_json('0.0').to_text_full()); + assert.throws(function() { + Amount.from_json('0.0'); + }); }); it('Parse native -0', function () { assert.strictEqual('0/XRP', Amount.from_json('-0').to_text_full()); }); it('Parse native -0.0', function () { - assert.strictEqual('0/XRP', Amount.from_json('-0.0').to_text_full()); + assert.throws(function() { + Amount.from_json('-0.0'); + }); }); it('Parse native 1000', function () { assert.strictEqual('0.001/XRP', Amount.from_json('1000').to_text_full()); }); - it('Parse native 12.3', function () { - assert.strictEqual('12.3/XRP', Amount.from_json('12.3').to_text_full()); + it('Parse native 12300000', function () { + assert.strictEqual('12.3/XRP', Amount.from_json('12300000').to_text_full()); }); - it('Parse native -12.3', function () { - assert.strictEqual('-12.3/XRP', Amount.from_json('-12.3').to_text_full()); + it('Parse native -12300000', function () { + assert.strictEqual('-12.3/XRP', Amount.from_json('-12300000').to_text_full()); }); it('Parse 123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', function () { assert.strictEqual('123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_text_full()); @@ -406,23 +410,17 @@ describe('Amount', function() { it('Parse native 0 human', function () { assert.strictEqual('0/XRP', Amount.from_json('0').to_human_full()); }); - it('Parse native 0.0 human', function () { - assert.strictEqual('0/XRP', Amount.from_json('0.0').to_human_full()); - }); it('Parse native -0 human', function () { assert.strictEqual('0/XRP', Amount.from_json('-0').to_human_full()); }); - it('Parse native -0.0 human', function () { - assert.strictEqual('0/XRP', Amount.from_json('-0.0').to_human_full()); - }); it('Parse native 1000 human', function () { assert.strictEqual('0.001/XRP', Amount.from_json('1000').to_human_full()); }); - it('Parse native 12.3 human', function () { - assert.strictEqual('12.3/XRP', Amount.from_json('12.3').to_human_full()); + it('Parse native 12300000 human', function () { + assert.strictEqual('12.3/XRP', Amount.from_json('12300000').to_human_full()); }); - it('Parse native -12.3 human', function () { - assert.strictEqual('-12.3/XRP', Amount.from_json('-12.3').to_human_full()); + it('Parse native -12300000 human', function () { + assert.strictEqual('-12.3/XRP', Amount.from_json('-12300000').to_human_full()); }); it('Parse 123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh human', function () { assert.strictEqual('123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', Amount.from_json('123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh').to_human_full()); @@ -790,7 +788,7 @@ describe('Amount', function() { }); it('0 XRP == 0 XRP', function () { var a = Amount.from_json('0'); - var b = Amount.from_json('0.0'); + var b = Amount.from_json('0'); assert(a.equals(b)); assert(!a.not_equals_why(b)); }); @@ -819,8 +817,8 @@ describe('Amount', function() { assert(!a.not_equals_why(b)); }); it('1.1 XRP == 1.1 XRP', function () { - var a = Amount.from_json('1.1'); - var b = Amount.from_json('11.0').ratio_human(10); + var a = Amount.from_json('1100000'); + var b = Amount.from_json('11000000').ratio_human('10/XRP'); assert(a.equals(b)); assert(!a.not_equals_why(b)); }); diff --git a/test/orderbook-test.js b/test/orderbook-test.js index 5a473237..b0f8f177 100644 --- a/test/orderbook-test.js +++ b/test/orderbook-test.js @@ -630,7 +630,7 @@ describe('OrderBook', function() { } }; - book.setOwnerFunds(addresses.ACCOUNT, '100.1'); + book.setOwnerFunds(addresses.ACCOUNT, '100100000'); book.setOfferFundedAmount(offer); var expected = { @@ -640,7 +640,7 @@ describe('OrderBook', function() { is_fully_funded: true, taker_gets_funded: '100', taker_pays_funded: '123.456', - owner_funds: '100.1' + owner_funds: '100100000' }; assert.deepEqual(offer, expected); @@ -2191,4 +2191,4 @@ describe('OrderBook', function() { done(); }); }); -}); \ No newline at end of file +});