Add checks and tests for expired order and zero fill amount

This commit is contained in:
Leonid Logvinov 2017-06-06 16:57:13 +02:00
parent 5412e80315
commit fa910bca0e
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4
3 changed files with 26 additions and 2 deletions

View File

@ -274,7 +274,13 @@ export class ExchangeWrapper extends ContractWrapper {
} }
private async validateCancelOrderAndThrowIfInvalidAsync(order: Order, private async validateCancelOrderAndThrowIfInvalidAsync(order: Order,
cancelAmount: BigNumber.BigNumber): Promise<void> { cancelAmount: BigNumber.BigNumber): Promise<void> {
// TODO if (cancelAmount.eq(0)) {
throw new Error(ExchangeContractErrs.ORDER_CANCEL_AMOUNT_ZERO);
}
const currentUnixTimestampSec = Date.now() / 1000;
if (order.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) {
throw new Error(ExchangeContractErrs.ORDER_CANCEL_EXPIRED);
}
} }
/** /**

View File

@ -121,6 +121,8 @@ export enum ExchangeContractErrCodes {
export const ExchangeContractErrs = strEnum([ export const ExchangeContractErrs = strEnum([
'ORDER_FILL_EXPIRED', 'ORDER_FILL_EXPIRED',
'ORDER_CANCEL_EXPIRED',
'ORDER_CANCEL_AMOUNT_ZERO',
'ORDER_REMAINING_FILL_AMOUNT_ZERO', 'ORDER_REMAINING_FILL_AMOUNT_ZERO',
'ORDER_FILL_ROUNDING_ERROR', 'ORDER_FILL_ROUNDING_ERROR',
'FILL_BALANCE_ALLOWANCE_ERROR', 'FILL_BALANCE_ALLOWANCE_ERROR',

View File

@ -332,6 +332,7 @@ describe('ExchangeWrapper', () => {
const fillableAmount = new BigNumber(5); const fillableAmount = new BigNumber(5);
let signedOrder: SignedOrder; let signedOrder: SignedOrder;
let orderHashHex: string; let orderHashHex: string;
const cancelAmount = new BigNumber(3);
before(async () => { before(async () => {
[coinbase, makerAddress, takerAddress] = userAddresses; [coinbase, makerAddress, takerAddress] = userAddresses;
const [makerToken, takerToken] = tokenUtils.getNonProtocolTokens(); const [makerToken, takerToken] = tokenUtils.getNonProtocolTokens();
@ -342,9 +343,24 @@ describe('ExchangeWrapper', () => {
); );
orderHashHex = await zeroEx.getOrderHashHexAsync(signedOrder); orderHashHex = await zeroEx.getOrderHashHexAsync(signedOrder);
}); });
describe('failed cancels', () => {
it('should throw when cancel amount is zero', async () => {
const zeroCancelAmount = new BigNumber(0);
return expect(zeroEx.exchange.cancelOrderAsync(signedOrder, zeroCancelAmount))
.to.be.rejectedWith(ExchangeContractErrs.ORDER_CANCEL_AMOUNT_ZERO);
});
it('should throw when order is expired', async () => {
const expirationInPast = new BigNumber(42);
const expiredSignedOrder = await fillScenarios.createFillableSignedOrderAsync(
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast,
);
orderHashHex = await zeroEx.getOrderHashHexAsync(expiredSignedOrder);
return expect(zeroEx.exchange.cancelOrderAsync(expiredSignedOrder, cancelAmount))
.to.be.rejectedWith(ExchangeContractErrs.ORDER_CANCEL_EXPIRED);
});
});
describe('successful cancels', () => { describe('successful cancels', () => {
it('should cancel an order', async () => { it('should cancel an order', async () => {
const cancelAmount = new BigNumber(5);
await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmount); await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmount);
const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHashHex); const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHashHex);
expect(cancelledAmount).to.be.bignumber.equal(cancelAmount); expect(cancelledAmount).to.be.bignumber.equal(cancelAmount);