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 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/0x.js/src/0x.ts') 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); -- cgit 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 ++++ 1 file changed, 4 insertions(+) (limited to 'packages/0x.js/src/0x.ts') 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; } /** -- cgit