diff options
author | Jacob Evans <jacob@dekz.net> | 2018-01-18 15:17:22 +0800 |
---|---|---|
committer | Jacob Evans <jacob@dekz.net> | 2018-01-19 07:31:06 +0800 |
commit | cfc868bf4dc371b299801dd24917a695387546e2 (patch) | |
tree | d4385425a85a1ef669ad1b58d71b3ad20b97a2b7 | |
parent | 7b4e2257d8316ea12c1874b03d148a458318dbc1 (diff) | |
download | dexon-sol-tools-cfc868bf4dc371b299801dd24917a695387546e2.tar dexon-sol-tools-cfc868bf4dc371b299801dd24917a695387546e2.tar.gz dexon-sol-tools-cfc868bf4dc371b299801dd24917a695387546e2.tar.bz2 dexon-sol-tools-cfc868bf4dc371b299801dd24917a695387546e2.tar.lz dexon-sol-tools-cfc868bf4dc371b299801dd24917a695387546e2.tar.xz dexon-sol-tools-cfc868bf4dc371b299801dd24917a695387546e2.tar.zst dexon-sol-tools-cfc868bf4dc371b299801dd24917a695387546e2.zip |
Reject negative amounts in isValidBaseUnitAmount
-rw-r--r-- | packages/0x.js/test/exchange_wrapper_test.ts | 47 | ||||
-rw-r--r-- | packages/assert/src/index.ts | 2 | ||||
-rw-r--r-- | packages/assert/test/assert_test.ts | 10 |
3 files changed, 59 insertions, 0 deletions
diff --git a/packages/0x.js/test/exchange_wrapper_test.ts b/packages/0x.js/test/exchange_wrapper_test.ts index d2a2149a0..7e0ffd818 100644 --- a/packages/0x.js/test/exchange_wrapper_test.ts +++ b/packages/0x.js/test/exchange_wrapper_test.ts @@ -390,6 +390,29 @@ describe('ExchangeWrapper', () => { ).to.not.be.rejectedWith(ExchangeContractErrs.OrderFillAmountZero); }); }); + describe('negative fill amount', async () => { + let signedOrder: SignedOrder; + const negativeFillTakerAmount = new BigNumber(-100); + beforeEach(async () => { + signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, + takerTokenAddress, + makerAddress, + takerAddress, + fillableAmount, + ); + }); + it('should not allow the exchange wrapper to fill if amount is negative', async () => { + return expect( + zeroEx.exchange.fillOrderAsync( + signedOrder, + negativeFillTakerAmount, + shouldThrowOnInsufficientBalanceOrAllowance, + takerAddress, + ), + ).to.be.rejected(); + }); + }); }); describe('#batchFillOrdersAsync', () => { let signedOrder: SignedOrder; @@ -498,6 +521,30 @@ describe('ExchangeWrapper', () => { ).to.not.be.rejectedWith(ExchangeContractErrs.OrderFillAmountZero); }); }); + describe('negative batch fill amount', async () => { + beforeEach(async () => { + const negativeFillTakerAmount = new BigNumber(-100); + orderFillBatch = [ + { + signedOrder, + takerTokenFillAmount, + }, + { + signedOrder: anotherSignedOrder, + takerTokenFillAmount: negativeFillTakerAmount, + }, + ]; + }); + it('should not allow the exchange wrapper to batch fill if any amount is negative', async () => { + return expect( + zeroEx.exchange.batchFillOrdersAsync( + orderFillBatch, + shouldThrowOnInsufficientBalanceOrAllowance, + takerAddress, + ), + ).to.be.rejected(); + }); + }); }); describe('#fillOrdersUpTo', () => { let signedOrder: SignedOrder; diff --git a/packages/assert/src/index.ts b/packages/assert/src/index.ts index 56f663780..015ffe579 100644 --- a/packages/assert/src/index.ts +++ b/packages/assert/src/index.ts @@ -12,6 +12,8 @@ export const assert = { }, isValidBaseUnitAmount(variableName: string, value: BigNumber) { assert.isBigNumber(variableName, value); + const isNegative = value.lessThan(0); + this.assert(!isNegative, `${variableName} should not be a negative number, found value: ${value.toNumber()}` ); const hasDecimals = value.decimalPlaces() !== 0; this.assert( !hasDecimals, diff --git a/packages/assert/test/assert_test.ts b/packages/assert/test/assert_test.ts index ff337196d..732e99b3e 100644 --- a/packages/assert/test/assert_test.ts +++ b/packages/assert/test/assert_test.ts @@ -22,6 +22,16 @@ describe('Assertions', () => { invalidInputs.forEach(input => expect(assert.isBigNumber.bind(assert, variableName, input)).to.throw()); }); }); + describe('#isValidBaseUnitAmount', () => { + it('should not throw for valid input', () => { + const validInputs = [new BigNumber(23), new BigNumber('45000000')]; + validInputs.forEach(input => expect(assert.isValidBaseUnitAmount.bind(assert, variableName, input)).to.not.throw()); + }); + it('should throw for invalid input', () => { + const invalidInputs = [0, undefined, new BigNumber(3.145), 3.145, new BigNumber(-400)]; + invalidInputs.forEach(input => expect(assert.isValidBaseUnitAmount.bind(assert, variableName, input)).to.throw()); + }); + }); describe('#isString', () => { it('should not throw for valid input', () => { const validInputs = ['hello', 'goodbye']; |