diff options
author | Fabio Berger <me@fabioberger.com> | 2017-12-22 01:22:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-22 01:22:28 +0800 |
commit | 734d220d6050ad7b9fa66e5e0695b848501eeff6 (patch) | |
tree | a66808f2d37e852894be4160098566552d3d4f3d /packages | |
parent | dc1d2a33a5e9396a57777c992f6c7b49e75480a0 (diff) | |
parent | fffe8c355e9c8d9e9ad4e0d7cdf9822f4df5f399 (diff) | |
download | dexon-sol-tools-734d220d6050ad7b9fa66e5e0695b848501eeff6.tar dexon-sol-tools-734d220d6050ad7b9fa66e5e0695b848501eeff6.tar.gz dexon-sol-tools-734d220d6050ad7b9fa66e5e0695b848501eeff6.tar.bz2 dexon-sol-tools-734d220d6050ad7b9fa66e5e0695b848501eeff6.tar.lz dexon-sol-tools-734d220d6050ad7b9fa66e5e0695b848501eeff6.tar.xz dexon-sol-tools-734d220d6050ad7b9fa66e5e0695b848501eeff6.tar.zst dexon-sol-tools-734d220d6050ad7b9fa66e5e0695b848501eeff6.zip |
Merge pull request #287 from 0xProject/fix/toBaseUnitAmount
Fix toBaseUnitAmount Issue
Diffstat (limited to 'packages')
-rw-r--r-- | packages/0x.js/CHANGELOG.md | 5 | ||||
-rw-r--r-- | packages/0x.js/src/0x.ts | 6 | ||||
-rw-r--r-- | packages/0x.js/test/0x.js_test.ts | 12 |
3 files changed, 22 insertions, 1 deletions
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) diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 7393cc814..a18f1fc55 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); @@ -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 8d62b3518..245946a89 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; @@ -130,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'; |