From 85e16c1233fc2c7020c2556cd79ac7d53cee4c98 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 21 Dec 2017 09:33:57 +0100 Subject: Make assertion stricter so that one cannot submit invalid baseUnit amounts to `toUnitAmount` --- packages/0x.js/src/0x.ts | 2 +- packages/0x.js/test/0x.js_test.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'packages') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 7393cc814..596d6ffba 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -128,7 +128,7 @@ export class ZeroEx { * @return The amount in units. */ public static toUnitAmount(amount: BigNumber, decimals: number): BigNumber { - assert.isBigNumber('amount', amount); + assert.isValidBaseUnitAmount('amount', amount); assert.isNumber('decimals', decimals); const aUnit = new BigNumber(10).pow(decimals); diff --git a/packages/0x.js/test/0x.js_test.ts b/packages/0x.js/test/0x.js_test.ts index 8d62b3518..9bf504909 100644 --- a/packages/0x.js/test/0x.js_test.ts +++ b/packages/0x.js/test/0x.js_test.ts @@ -114,6 +114,12 @@ describe('ZeroEx library', () => { }); }); describe('#toUnitAmount', () => { + it('should throw if invalid baseUnit amount supplied as argument', () => { + const invalidBaseUnitAmount = new BigNumber(1000000000.4); + const decimals = 6; + expect(() => ZeroEx.toUnitAmount(invalidBaseUnitAmount, decimals)) + .to.throw('amount should be in baseUnits (no decimals), found value: 1000000000.4'); + }); it('Should return the expected unit amount for the decimals passed in', () => { const baseUnitAmount = new BigNumber(1000000000); const decimals = 6; -- cgit v1.2.3 From b94d13b413c9eef7b4afd7244cbc48167239f629 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 21 Dec 2017 09:34:50 +0100 Subject: Refactor toBaseUnitAmount so that it throws if user supplies unitAmount with too many decimals --- packages/0x.js/src/0x.ts | 4 ++++ packages/0x.js/test/0x.js_test.ts | 6 ++++++ 2 files changed, 10 insertions(+) (limited to 'packages') diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 596d6ffba..a18f1fc55 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -149,6 +149,10 @@ export class ZeroEx { const unit = new BigNumber(10).pow(decimals); const baseUnitAmount = amount.times(unit); + const hasDecimals = baseUnitAmount.decimalPlaces() !== 0; + if (hasDecimals) { + throw new Error(`Invalid unit amount: ${amount.toString()} - Too many decimal places`); + } return baseUnitAmount; } /** diff --git a/packages/0x.js/test/0x.js_test.ts b/packages/0x.js/test/0x.js_test.ts index 9bf504909..245946a89 100644 --- a/packages/0x.js/test/0x.js_test.ts +++ b/packages/0x.js/test/0x.js_test.ts @@ -136,6 +136,12 @@ describe('ZeroEx library', () => { const expectedUnitAmount = new BigNumber(1000000000); expect(baseUnitAmount).to.be.bignumber.equal(expectedUnitAmount); }); + it('should throw if unitAmount has more decimals then specified as the max decimal precision', () => { + const unitAmount = new BigNumber(0.823091); + const decimals = 5; + expect(() => ZeroEx.toBaseUnitAmount(unitAmount, decimals)) + .to.throw('Invalid unit amount: 0.823091 - Too many decimal places'); + }); }); describe('#getOrderHashHex', () => { const expectedOrderHash = '0x39da987067a3c9e5f1617694f1301326ba8c8b0498ebef5df4863bed394e3c83'; -- cgit v1.2.3 From fffe8c355e9c8d9e9ad4e0d7cdf9822f4df5f399 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 21 Dec 2017 14:40:18 +0100 Subject: Add to CHANGELOG --- packages/0x.js/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'packages') diff --git a/packages/0x.js/CHANGELOG.md b/packages/0x.js/CHANGELOG.md index d85ace89f..536b66fce 100644 --- a/packages/0x.js/CHANGELOG.md +++ b/packages/0x.js/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +vx.x.x - _TBD_ +------------------------ + * Assert baseUnit amount supplied to `toUnitAmount` is integer amount. (#287) + * `toBaseUnitAmount` throws if amount supplied has too many decimals (#287) + v0.28.0 - _December 20, 2017_ ------------------------ * Add `etherTokenAddress` arg to `depositAsync` and `withdrawAsync` methods on `zeroEx.etherToken` (#267) -- cgit v1.2.3