Reject negative amounts in isValidBaseUnitAmount

This commit is contained in:
Jacob Evans 2018-01-18 18:17:22 +11:00
parent 7b4e2257d8
commit cfc868bf4d
3 changed files with 59 additions and 0 deletions

View File

@ -390,6 +390,29 @@ describe('ExchangeWrapper', () => {
).to.not.be.rejectedWith(ExchangeContractErrs.OrderFillAmountZero); ).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', () => { describe('#batchFillOrdersAsync', () => {
let signedOrder: SignedOrder; let signedOrder: SignedOrder;
@ -498,6 +521,30 @@ describe('ExchangeWrapper', () => {
).to.not.be.rejectedWith(ExchangeContractErrs.OrderFillAmountZero); ).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', () => { describe('#fillOrdersUpTo', () => {
let signedOrder: SignedOrder; let signedOrder: SignedOrder;

View File

@ -12,6 +12,8 @@ export const assert = {
}, },
isValidBaseUnitAmount(variableName: string, value: BigNumber) { isValidBaseUnitAmount(variableName: string, value: BigNumber) {
assert.isBigNumber(variableName, value); 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; const hasDecimals = value.decimalPlaces() !== 0;
this.assert( this.assert(
!hasDecimals, !hasDecimals,

View File

@ -22,6 +22,16 @@ describe('Assertions', () => {
invalidInputs.forEach(input => expect(assert.isBigNumber.bind(assert, variableName, input)).to.throw()); 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', () => { describe('#isString', () => {
it('should not throw for valid input', () => { it('should not throw for valid input', () => {
const validInputs = ['hello', 'goodbye']; const validInputs = ['hello', 'goodbye'];