Addressed review comments and prettified code

This commit is contained in:
Alex Towle
2019-08-13 11:05:37 -07:00
parent 6e1fdda182
commit e4257fb6c7
4 changed files with 85 additions and 95 deletions

View File

@@ -1,6 +1,4 @@
import { blockchainTests, constants, describe, expect, hexRandom } from '@0x/contracts-test-utils';
import { eip712Utils, orderHashUtils } from '@0x/order-utils';
import { Order } from '@0x/types';
import { BigNumber, signTypedDataUtils } from '@0x/utils';
import * as ethUtil from 'ethereumjs-util';
import * as _ from 'lodash';
@@ -13,7 +11,6 @@ blockchainTests('LibEIP712ExchangeDomain', env => {
const CHAIN_ID = 1337;
// Random generator functions
const randomAddress = () => hexRandom(constants.ADDRESS_LENGTH);
const randomHash = () => hexRandom(constants.WORD_LENGTH);
/**

View File

@@ -36,17 +36,14 @@ blockchainTests('LibOrder', env => {
expirationTimeSeconds: constants.ZERO_AMOUNT,
};
/**
* Tests the `_hashOrder()` function against a reference hash.
*/
async function testHashOrderAsync(order: Order): Promise<void> {
const typedData = eip712Utils.createOrderTypedData(order);
const expectedHash = '0x'.concat(
signTypedDataUtils.generateTypedDataHashWithoutDomain(typedData).toString('hex'),
before(async () => {
libsContract = await TestLibsContract.deployFrom0xArtifactAsync(
artifacts.TestLibs,
env.provider,
env.txDefaults,
new BigNumber(CHAIN_ID),
);
const actualHash = await libsContract.hashOrder.callAsync(order);
expect(actualHash).to.be.eq(expectedHash);
}
});
/**
* Tests the `getOrderHash()` function against a reference hash.
@@ -57,15 +54,6 @@ blockchainTests('LibOrder', env => {
expect(actualHash).to.be.eq(expectedHash);
}
before(async () => {
libsContract = await TestLibsContract.deployFrom0xArtifactAsync(
artifacts.TestLibs,
env.provider,
env.txDefaults,
new BigNumber(CHAIN_ID),
);
});
describe('getOrderHash', () => {
it('should correctly hash an empty order', async () => {
await testGetOrderHashAsync({
@@ -101,6 +89,18 @@ blockchainTests('LibOrder', env => {
});
});
/**
* Tests the `_hashOrder()` function against a reference hash.
*/
async function testHashOrderAsync(order: Order): Promise<void> {
const typedData = eip712Utils.createOrderTypedData(order);
const expectedHash = '0x'.concat(
signTypedDataUtils.generateTypedDataHashWithoutDomain(typedData).toString('hex'),
);
const actualHash = await libsContract.hashOrder.callAsync(order);
expect(actualHash).to.be.eq(expectedHash);
}
describe('hashOrder', () => {
it('should correctly hash an empty order', async () => {
await testHashOrderAsync(EMPTY_ORDER);

View File

@@ -26,17 +26,14 @@ blockchainTests('LibZeroExTransaction', env => {
},
};
/**
* Tests the `_hashZeroExTransaction()` function against a reference hash.
*/
async function testHashZeroExTransactionAsync(transaction: ZeroExTransaction): Promise<void> {
const typedData = eip712Utils.createZeroExTransactionTypedData(transaction);
const expectedHash = '0x'.concat(
signTypedDataUtils.generateTypedDataHashWithoutDomain(typedData).toString('hex'),
before(async () => {
libsContract = await TestLibsContract.deployFrom0xArtifactAsync(
artifacts.TestLibs,
env.provider,
env.txDefaults,
new BigNumber(CHAIN_ID),
);
const actualHash = await libsContract.hashZeroExTransaction.callAsync(transaction);
expect(actualHash).to.be.eq(expectedHash);
}
});
/**
* Tests the `getTransactionHash()` function against a reference hash.
@@ -48,15 +45,6 @@ blockchainTests('LibZeroExTransaction', env => {
expect(actualHash).to.be.eq(expectedHash);
}
before(async () => {
libsContract = await TestLibsContract.deployFrom0xArtifactAsync(
artifacts.TestLibs,
env.provider,
env.txDefaults,
new BigNumber(CHAIN_ID),
);
});
describe('getTransactionHash', () => {
it('should correctly hash an empty transaction', async () => {
await testGetTransactionHashAsync({
@@ -82,12 +70,24 @@ blockchainTests('LibZeroExTransaction', env => {
});
});
/**
* Tests the `_hashZeroExTransaction()` function against a reference hash.
*/
async function testHashZeroExTransactionAsync(transaction: ZeroExTransaction): Promise<void> {
const typedData = eip712Utils.createZeroExTransactionTypedData(transaction);
const expectedHash = '0x'.concat(
signTypedDataUtils.generateTypedDataHashWithoutDomain(typedData).toString('hex'),
);
const actualHash = await libsContract.hashZeroExTransaction.callAsync(transaction);
expect(actualHash).to.be.eq(expectedHash);
}
describe('hashOrder', () => {
it('should correctly hash an empty order', async () => {
it('should correctly hash an empty transaction', async () => {
await testHashZeroExTransactionAsync(EMPTY_TRANSACTION);
});
it('should correctly hash a non-empty order', async () => {
it('should correctly hash a non-empty transaction', async () => {
await testHashZeroExTransactionAsync({
salt: randomUint256(),
expirationTimeSeconds: randomUint256(),