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