@0x/contracts-exchange
: Add more tests to wrapper_unit_tests
.
This commit is contained in:
@@ -5,8 +5,10 @@ import {
|
||||
expect,
|
||||
hexRandom,
|
||||
} from '@0x/contracts-test-utils';
|
||||
import { ExchangeRevertErrors } from '@0x/order-utils';
|
||||
import { OrderInfo, OrderStatus, OrderWithoutDomain as Order } from '@0x/types';
|
||||
import { BigNumber } from '@0x/utils';
|
||||
import { TransactionReceipt } from 'ethereum-types';
|
||||
import * as ethjs from 'ethereumjs-util';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
@@ -20,7 +22,8 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
|
||||
const { ONE_ETHER } = constants;
|
||||
const randomAddress = () => hexRandom(constants.ADDRESS_LENGTH);
|
||||
const randomAssetData = () => hexRandom(34);
|
||||
const randomAmount = () => ONE_ETHER.times(_.random(0, 100, true).toFixed(10));
|
||||
const randomSignature = () => `${hexRandom(65)}02`;
|
||||
const randomAmount = (maxAmount: BigNumber = ONE_ETHER) => maxAmount.times(_.random(0, 100, true).toFixed(12));
|
||||
const randomTimestamp = () => new BigNumber(Math.floor(_.now() / 1000) + _.random(0, 34560));
|
||||
const randomSalt = () => new BigNumber(hexRandom(constants.WORD_LENGTH).substr(2), 16);
|
||||
let testContract: TestWrapperFunctionsContract;
|
||||
@@ -55,7 +58,36 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
|
||||
// Computes the expected (fake) order hash generated by the `TestWrapperFunctions` contract.
|
||||
function getExpectedOrderHash(order: Order): string {
|
||||
// It's just `keccak256(order.salt)`.
|
||||
return ethjs.bufferToHex(ethjs.sha3(ethjs.toBuffer(`0x${order.salt.toString(16)}`)));
|
||||
return ethjs.bufferToHex(ethjs.sha3(ethjs.setLengthLeft(
|
||||
`0x${order.salt.toString(16)}`, constants.WORD_LENGTH)));
|
||||
}
|
||||
|
||||
type AsyncFunction<TArgs extends any[], TResult> = (...args: TArgs) => Promise<TResult>;
|
||||
|
||||
interface MutableContractFunction<
|
||||
TCallAsyncArgs extends any[],
|
||||
TAwaitTransactionSuccessAsyncArgs extends any[],
|
||||
TCallAsyncResult,
|
||||
> {
|
||||
callAsync: AsyncFunction<TCallAsyncArgs, TCallAsyncResult>;
|
||||
awaitTransactionSuccessAsync: AsyncFunction<TAwaitTransactionSuccessAsyncArgs, TransactionReceipt>;
|
||||
}
|
||||
|
||||
async function getResultAndTransactAsync<
|
||||
TCallAsyncArgs extends any[],
|
||||
TAwaitTransactionSuccessAsyncArgs extends any[],
|
||||
TCallAsyncResult,
|
||||
>(
|
||||
contractFunction: MutableContractFunction<TCallAsyncArgs, TAwaitTransactionSuccessAsyncArgs, TCallAsyncResult>,
|
||||
// tslint:disable-next-line: trailing-comma
|
||||
...args: TAwaitTransactionSuccessAsyncArgs
|
||||
): Promise<[TCallAsyncResult, TransactionReceipt]> {
|
||||
// HACK(dorothy-zbornak): We take advantage of the general rule that
|
||||
// the parameters for `callAsync()` are a subset of the
|
||||
// parameters for `awaitTransactionSuccessAsync()`.
|
||||
const result = await contractFunction.callAsync(...args as any as TCallAsyncArgs);
|
||||
const receipt = await contractFunction.awaitTransactionSuccessAsync(...args);
|
||||
return [ result, receipt ];
|
||||
}
|
||||
|
||||
describe('getOrdersInfo', () => {
|
||||
@@ -84,12 +116,67 @@ blockchainTests.only('Exchange wrapper functions unit tests.', env => {
|
||||
expect(actual).to.deep.eq(expected);
|
||||
});
|
||||
|
||||
it('works with multiple orders', async () => {
|
||||
const NUM_ORDERS = 7;
|
||||
it('works with many orders', async () => {
|
||||
const NUM_ORDERS = 16;
|
||||
const orders = _.times(NUM_ORDERS, () => createRandomOrder());
|
||||
const expected = orders.map(getExpectedOrderInfo);
|
||||
const actual = await testContract.getOrdersInfo.callAsync(orders);
|
||||
expect(actual).to.deep.eq(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fillOrKillOrder', () => {
|
||||
it('reverts if the order is filled by less than `takerAssetFillAmount`', async () => {
|
||||
const fillAmount = randomAmount();
|
||||
const order = createRandomOrder({
|
||||
// `_fillOrder()` is overridden to always return `order.takerAssetAmount` as
|
||||
// the `takerAssetFilledAmount`.
|
||||
takerAssetAmount: fillAmount.minus(1),
|
||||
});
|
||||
const expectedError = new ExchangeRevertErrors.IncompleteFillError(
|
||||
getExpectedOrderHash(order),
|
||||
);
|
||||
const tx = testContract.fillOrKillOrder.awaitTransactionSuccessAsync(
|
||||
order,
|
||||
fillAmount,
|
||||
randomSignature(),
|
||||
);
|
||||
return expect(tx).to.revertWith(expectedError);
|
||||
});
|
||||
|
||||
it('reverts if the order is filled by greater than `takerAssetFillAmount`', async () => {
|
||||
const fillAmount = randomAmount();
|
||||
const order = createRandomOrder({
|
||||
// `_fillOrder()` is overridden to always return `order.takerAssetAmount` as
|
||||
// the `takerAssetFilledAmount`.
|
||||
takerAssetAmount: fillAmount.plus(1),
|
||||
});
|
||||
const expectedError = new ExchangeRevertErrors.IncompleteFillError(
|
||||
getExpectedOrderHash(order),
|
||||
);
|
||||
const tx = testContract.fillOrKillOrder.awaitTransactionSuccessAsync(
|
||||
order,
|
||||
fillAmount,
|
||||
randomSignature(),
|
||||
);
|
||||
return expect(tx).to.revertWith(expectedError);
|
||||
});
|
||||
|
||||
it('works if the order is filled by exactly `takerAssetFillAmount`', async () => {
|
||||
const fillAmount = randomAmount();
|
||||
const order = createRandomOrder({
|
||||
// `_fillOrder()` is overridden to always return `order.takerAssetAmount` as
|
||||
// the `takerAssetFilledAmount`.
|
||||
takerAssetAmount: fillAmount,
|
||||
});
|
||||
// const expected = getExpectedFillResults(order, fillAmount);
|
||||
const actual = await getResultAndTransactAsync(
|
||||
testContract.fillOrKillOrder,
|
||||
order,
|
||||
fillAmount,
|
||||
randomSignature(),
|
||||
);
|
||||
// expect(actual).to.deep.eq(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user