Add tests for batchCancelAsync
This commit is contained in:
@@ -212,7 +212,7 @@ export class ExchangeWrapper extends ContractWrapper {
|
|||||||
orders: Array<Order|SignedOrder>, takerTokenCancelAmounts: BigNumber.BigNumber[]): Promise<void> {
|
orders: Array<Order|SignedOrder>, takerTokenCancelAmounts: BigNumber.BigNumber[]): Promise<void> {
|
||||||
const makers = _.map(orders, order => order.maker);
|
const makers = _.map(orders, order => order.maker);
|
||||||
assert.isSameLength('orders', orders, 'takerTokenCancelAmounts', takerTokenCancelAmounts);
|
assert.isSameLength('orders', orders, 'takerTokenCancelAmounts', takerTokenCancelAmounts);
|
||||||
assert.assert(_.isEmpty(orders), 'Can not cancel an empty batch');
|
assert.assert(!_.isEmpty(orders), 'Can not cancel an empty batch');
|
||||||
assert.assert(_.uniq(makers).length === 1, 'Can not cancel orders from multiple makers in a single batch');
|
assert.assert(_.uniq(makers).length === 1, 'Can not cancel orders from multiple makers in a single batch');
|
||||||
const maker = makers[0];
|
const maker = makers[0];
|
||||||
// _.zip doesn't type check if values have different types :'(
|
// _.zip doesn't type check if values have different types :'(
|
||||||
|
@@ -45,7 +45,7 @@ export const assert = {
|
|||||||
isSameLength(variableName1: string, value1: any[], variableName2: string, value2: any[]) {
|
isSameLength(variableName1: string, value1: any[], variableName2: string, value2: any[]) {
|
||||||
const length1 = value1.length;
|
const length1 = value1.length;
|
||||||
const length2 = value2.length;
|
const length2 = value2.length;
|
||||||
this.assert(length1 === length2, `${variableName1} and ${variableName2} length mismatch.
|
this.assert(length1 === length2, `${variableName1} and ${variableName2} length mismatch. \
|
||||||
${length1} != ${length2}`);
|
${length1} != ${length2}`);
|
||||||
},
|
},
|
||||||
isNumber(variableName: string, value: number): void {
|
isNumber(variableName: string, value: number): void {
|
||||||
|
@@ -323,7 +323,7 @@ describe('ExchangeWrapper', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('#cancelOrderAsync', () => {
|
describe('cancel order(s)', () => {
|
||||||
let makerTokenAddress: string;
|
let makerTokenAddress: string;
|
||||||
let takerTokenAddress: string;
|
let takerTokenAddress: string;
|
||||||
let coinbase: string;
|
let coinbase: string;
|
||||||
@@ -343,6 +343,7 @@ describe('ExchangeWrapper', () => {
|
|||||||
);
|
);
|
||||||
orderHashHex = await zeroEx.getOrderHashHexAsync(signedOrder);
|
orderHashHex = await zeroEx.getOrderHashHexAsync(signedOrder);
|
||||||
});
|
});
|
||||||
|
describe('#cancelOrderAsync', () => {
|
||||||
describe('failed cancels', () => {
|
describe('failed cancels', () => {
|
||||||
it('should throw when cancel amount is zero', async () => {
|
it('should throw when cancel amount is zero', async () => {
|
||||||
const zeroCancelAmount = new BigNumber(0);
|
const zeroCancelAmount = new BigNumber(0);
|
||||||
@@ -352,7 +353,8 @@ describe('ExchangeWrapper', () => {
|
|||||||
it('should throw when order is expired', async () => {
|
it('should throw when order is expired', async () => {
|
||||||
const expirationInPast = new BigNumber(1496826058); // 7th Jun 2017
|
const expirationInPast = new BigNumber(1496826058); // 7th Jun 2017
|
||||||
const expiredSignedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
const expiredSignedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||||
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast,
|
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress,
|
||||||
|
fillableAmount, expirationInPast,
|
||||||
);
|
);
|
||||||
orderHashHex = await zeroEx.getOrderHashHexAsync(expiredSignedOrder);
|
orderHashHex = await zeroEx.getOrderHashHexAsync(expiredSignedOrder);
|
||||||
return expect(zeroEx.exchange.cancelOrderAsync(expiredSignedOrder, cancelAmount))
|
return expect(zeroEx.exchange.cancelOrderAsync(expiredSignedOrder, cancelAmount))
|
||||||
@@ -372,6 +374,46 @@ describe('ExchangeWrapper', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('#batchCancelOrderAsync', () => {
|
||||||
|
let anotherSignedOrder: SignedOrder;
|
||||||
|
let anotherOrderHashHex: string;
|
||||||
|
beforeEach(async () => {
|
||||||
|
anotherSignedOrder = await fillScenarios.createFillableSignedOrderAsync(
|
||||||
|
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
|
||||||
|
);
|
||||||
|
anotherOrderHashHex = await zeroEx.getOrderHashHexAsync(anotherSignedOrder);
|
||||||
|
});
|
||||||
|
describe('failed batch cancels', () => {
|
||||||
|
it('should throw when length of orders and cancelAmounts mismatch', async () => {
|
||||||
|
return expect(zeroEx.exchange.batchCancelOrderAsync([signedOrder], []))
|
||||||
|
.to.be.rejectedWith('orders and takerTokenCancelAmounts length mismatch. 1 != 0');
|
||||||
|
});
|
||||||
|
it('should throw when orders are empty', async () => {
|
||||||
|
return expect(zeroEx.exchange.batchCancelOrderAsync([], []))
|
||||||
|
.to.be.rejectedWith('Can not cancel an empty batch');
|
||||||
|
});
|
||||||
|
it.only('should throw when orders have different makers', async () => {
|
||||||
|
const signedOrderWithADifferentMaker = await fillScenarios.createFillableSignedOrderAsync(
|
||||||
|
makerTokenAddress, takerTokenAddress, takerAddress, takerAddress, fillableAmount,
|
||||||
|
);
|
||||||
|
return expect(zeroEx.exchange.batchCancelOrderAsync(
|
||||||
|
[signedOrder, signedOrderWithADifferentMaker], [cancelAmount, cancelAmount]))
|
||||||
|
.to.be.rejectedWith('Can not cancel orders from multiple makers in a single batch');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('successful batch cancels', () => {
|
||||||
|
it('should cancel a batch of orders', async () => {
|
||||||
|
await zeroEx.exchange.batchCancelOrderAsync(
|
||||||
|
[signedOrder, anotherSignedOrder], [cancelAmount, cancelAmount]);
|
||||||
|
const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHashHex);
|
||||||
|
const anotherCancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(
|
||||||
|
anotherOrderHashHex);
|
||||||
|
expect(cancelledAmount).to.be.bignumber.equal(cancelAmount);
|
||||||
|
expect(anotherCancelledAmount).to.be.bignumber.equal(cancelAmount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
describe('tests that require partially filled order', () => {
|
describe('tests that require partially filled order', () => {
|
||||||
let makerTokenAddress: string;
|
let makerTokenAddress: string;
|
||||||
let takerTokenAddress: string;
|
let takerTokenAddress: string;
|
||||||
|
Reference in New Issue
Block a user