Merge branch '3.0' into feat/3.0/optimizeConstants

This commit is contained in:
Amir Bandeali
2019-08-15 15:20:48 -07:00
11 changed files with 2101 additions and 171 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,72 +1,157 @@
import { addressUtils, blockchainTests, constants, describe, expect } from '@0x/contracts-test-utils';
import { assetDataUtils, orderHashUtils } from '@0x/order-utils';
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';
import { artifacts, TestLibOrderContract } from '../src';
blockchainTests('LibOrder', env => {
let libOrderContract: TestLibOrderContract;
let order: Order;
const randomAddress = () => hexRandom(constants.ADDRESS_LENGTH);
const randomHash = () => hexRandom(constants.WORD_LENGTH);
const randomUint256 = () => new BigNumber(randomHash());
const randomAssetData = () => hexRandom(36);
const EMPTY_ORDER: Order = {
domain: {
verifyingContractAddress: constants.NULL_ADDRESS,
chainId: 0,
},
senderAddress: constants.NULL_ADDRESS,
makerAddress: constants.NULL_ADDRESS,
takerAddress: constants.NULL_ADDRESS,
makerFee: constants.ZERO_AMOUNT,
takerFee: constants.ZERO_AMOUNT,
makerAssetAmount: constants.ZERO_AMOUNT,
takerAssetAmount: constants.ZERO_AMOUNT,
makerAssetData: constants.NULL_BYTES,
takerAssetData: constants.NULL_BYTES,
makerFeeAssetData: constants.NULL_BYTES,
takerFeeAssetData: constants.NULL_BYTES,
salt: constants.ZERO_AMOUNT,
feeRecipientAddress: constants.NULL_ADDRESS,
expirationTimeSeconds: constants.ZERO_AMOUNT,
};
before(async () => {
libOrderContract = await TestLibOrderContract.deployFrom0xArtifactAsync(
artifacts.TestLibOrder,
env.provider,
env.txDefaults,
);
const domain = {
verifyingContractAddress: libOrderContract.address,
chainId: 1,
};
order = {
...constants.STATIC_ORDER_PARAMS,
makerAddress: addressUtils.generatePseudoRandomAddress(),
takerAddress: addressUtils.generatePseudoRandomAddress(),
senderAddress: addressUtils.generatePseudoRandomAddress(),
feeRecipientAddress: addressUtils.generatePseudoRandomAddress(),
makerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
takerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
makerFeeAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
takerFeeAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
salt: new BigNumber(0),
expirationTimeSeconds: new BigNumber(0),
domain,
};
});
describe('LibOrder', () => {
describe('getOrderHash', () => {
it('should return the correct orderHash', async () => {
const domainHash = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...order.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const orderHashHex = await libOrderContract.getTypedDataHash.callAsync(order, domainHash);
expect(orderHashUtils.getOrderHashHex(order)).to.be.equal(orderHashHex);
/**
* Tests the `getTypedDataHash()` function against a reference hash.
*/
async function testGetTypedDataHashAsync(order: Order): Promise<void> {
const expectedHash = orderHashUtils.getOrderHashHex(order);
const domainHash = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...order.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const actualHash = await libOrderContract.getTypedDataHash.callAsync(order, domainHash);
expect(actualHash).to.be.eq(expectedHash);
}
describe('getTypedDataHash', () => {
it('should correctly hash an empty order', async () => {
await testGetTypedDataHashAsync({
...EMPTY_ORDER,
domain: {
...EMPTY_ORDER.domain,
verifyingContractAddress: libOrderContract.address,
},
});
it('orderHash should differ if the domain hash is different', async () => {
const domainHash1 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...order.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const domainHash2 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...order.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
chainId: 1337,
}),
);
const orderHashHex1 = await libOrderContract.getTypedDataHash.callAsync(order, domainHash1);
const orderHashHex2 = await libOrderContract.getTypedDataHash.callAsync(order, domainHash2);
expect(orderHashHex1).to.be.not.equal(orderHashHex2);
});
it('should correctly hash a non-empty order', async () => {
await testGetTypedDataHashAsync({
domain: {
verifyingContractAddress: libOrderContract.address,
chainId: 1337,
},
senderAddress: randomAddress(),
makerAddress: randomAddress(),
takerAddress: randomAddress(),
makerFee: randomUint256(),
takerFee: randomUint256(),
makerAssetAmount: randomUint256(),
takerAssetAmount: randomUint256(),
makerAssetData: randomAssetData(),
takerAssetData: randomAssetData(),
makerFeeAssetData: randomAssetData(),
takerFeeAssetData: randomAssetData(),
salt: randomUint256(),
feeRecipientAddress: randomAddress(),
expirationTimeSeconds: randomUint256(),
});
});
it('orderHash should differ if the domain hash is different', async () => {
const domainHash1 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...EMPTY_ORDER.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const domainHash2 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...EMPTY_ORDER.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
chainId: 1337,
}),
);
const orderHashHex1 = await libOrderContract.getTypedDataHash.callAsync(EMPTY_ORDER, domainHash1);
const orderHashHex2 = await libOrderContract.getTypedDataHash.callAsync(EMPTY_ORDER, domainHash2);
expect(orderHashHex1).to.be.not.equal(orderHashHex2);
});
});
/**
* Tests the `getStructHash()` function against a reference hash.
*/
async function testGetStructHashAsync(order: Order): Promise<void> {
const typedData = eip712Utils.createOrderTypedData(order);
const expectedHash = ethUtil.bufferToHex(signTypedDataUtils.generateTypedDataHashWithoutDomain(typedData));
const actualHash = await libOrderContract.getStructHash.callAsync(order);
expect(actualHash).to.be.eq(expectedHash);
}
describe('getStructHash', () => {
it('should correctly hash an empty order', async () => {
await testGetStructHashAsync(EMPTY_ORDER);
});
it('should correctly hash a non-empty order', async () => {
await testGetStructHashAsync({
// The domain is not used in this test, so it's okay if it is left empty.
domain: {
verifyingContractAddress: constants.NULL_ADDRESS,
chainId: 0,
},
senderAddress: randomAddress(),
makerAddress: randomAddress(),
takerAddress: randomAddress(),
makerFee: randomUint256(),
takerFee: randomUint256(),
makerAssetAmount: randomUint256(),
takerAssetAmount: randomUint256(),
makerAssetData: randomAssetData(),
takerAssetData: randomAssetData(),
makerFeeAssetData: randomAssetData(),
takerFeeAssetData: randomAssetData(),
salt: randomUint256(),
feeRecipientAddress: randomAddress(),
expirationTimeSeconds: randomUint256(),
});
});
});

View File

@@ -1,74 +1,132 @@
import { addressUtils, blockchainTests, constants, describe, expect } from '@0x/contracts-test-utils';
import { transactionHashUtils } from '@0x/order-utils';
import { blockchainTests, constants, describe, expect, hexRandom } from '@0x/contracts-test-utils';
import { eip712Utils, transactionHashUtils } from '@0x/order-utils';
import { ZeroExTransaction } from '@0x/types';
import { BigNumber, signTypedDataUtils } from '@0x/utils';
import * as ethUtil from 'ethereumjs-util';
import * as _ from 'lodash';
import { artifacts, TestLibZeroExTransactionContract } from '../src';
blockchainTests('LibZeroExTransaction', env => {
let libZeroExTransactionContract: TestLibZeroExTransactionContract;
let zeroExTransaction: ZeroExTransaction;
const randomAddress = () => hexRandom(constants.ADDRESS_LENGTH);
const randomHash = () => hexRandom(constants.WORD_LENGTH);
const randomUint256 = () => new BigNumber(randomHash());
const randomAssetData = () => hexRandom(36);
const EMPTY_TRANSACTION: ZeroExTransaction = {
salt: constants.ZERO_AMOUNT,
expirationTimeSeconds: constants.ZERO_AMOUNT,
signerAddress: constants.NULL_ADDRESS,
data: constants.NULL_BYTES,
domain: {
verifyingContractAddress: constants.NULL_ADDRESS,
chainId: 0,
},
};
before(async () => {
libZeroExTransactionContract = await TestLibZeroExTransactionContract.deployFrom0xArtifactAsync(
artifacts.TestLibZeroExTransaction,
env.provider,
env.txDefaults,
);
const domain = {
verifyingContractAddress: libZeroExTransactionContract.address,
chainId: 1,
};
zeroExTransaction = {
signerAddress: addressUtils.generatePseudoRandomAddress(),
salt: new BigNumber(0),
expirationTimeSeconds: new BigNumber(0),
data: constants.NULL_BYTES,
domain,
};
});
describe('LibZeroExTransaction', () => {
describe('getTransactionHash', () => {
it('should return the correct transactionHash', async () => {
const domainHash = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...zeroExTransaction.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const orderHashHex = await libZeroExTransactionContract.getTypedDataHash.callAsync(
zeroExTransaction,
domainHash,
);
expect(transactionHashUtils.getTransactionHashHex(zeroExTransaction)).to.be.equal(orderHashHex);
/**
* Tests the `getTypedDataHash()` function against a reference hash.
*/
async function testGetTypedDataHashAsync(transaction: ZeroExTransaction): Promise<void> {
const expectedHash = transactionHashUtils.getTransactionHashHex(transaction);
const domainHash = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...transaction.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const actualHash = await libZeroExTransactionContract.getTypedDataHash.callAsync(transaction, domainHash);
expect(actualHash).to.be.eq(expectedHash);
}
describe('getTypedDataHash', () => {
it('should correctly hash an empty transaction', async () => {
await testGetTypedDataHashAsync({
...EMPTY_TRANSACTION,
domain: {
...EMPTY_TRANSACTION.domain,
verifyingContractAddress: libZeroExTransactionContract.address,
},
});
it('transactionHash should differ if the domain hash is different', async () => {
const domainHash1 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...zeroExTransaction.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const domainHash2 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...zeroExTransaction.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
chainId: 1337,
}),
);
const transactionHashHex1 = await libZeroExTransactionContract.getTypedDataHash.callAsync(
zeroExTransaction,
domainHash1,
);
const transactionHashHex2 = await libZeroExTransactionContract.getTypedDataHash.callAsync(
zeroExTransaction,
domainHash2,
);
expect(transactionHashHex1).to.be.not.equal(transactionHashHex2);
});
it('should correctly hash a non-empty transaction', async () => {
await testGetTypedDataHashAsync({
salt: randomUint256(),
expirationTimeSeconds: randomUint256(),
signerAddress: randomAddress(),
data: randomAssetData(),
domain: {
...EMPTY_TRANSACTION.domain,
verifyingContractAddress: libZeroExTransactionContract.address,
},
});
});
it('transactionHash should differ if the domain hash is different', async () => {
const domainHash1 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...EMPTY_TRANSACTION.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
}),
);
const domainHash2 = ethUtil.bufferToHex(
signTypedDataUtils.generateDomainHash({
...EMPTY_TRANSACTION.domain,
name: constants.EIP712_DOMAIN_NAME,
version: constants.EIP712_DOMAIN_VERSION,
chainId: 1337,
}),
);
const transactionHashHex1 = await libZeroExTransactionContract.getTypedDataHash.callAsync(
EMPTY_TRANSACTION,
domainHash1,
);
const transactionHashHex2 = await libZeroExTransactionContract.getTypedDataHash.callAsync(
EMPTY_TRANSACTION,
domainHash2,
);
expect(transactionHashHex1).to.be.not.equal(transactionHashHex2);
});
});
/**
* Tests the `getStructHash()` function against a reference hash.
*/
async function testGetStructHashAsync(transaction: ZeroExTransaction): Promise<void> {
const typedData = eip712Utils.createZeroExTransactionTypedData(transaction);
const expectedHash = ethUtil.bufferToHex(signTypedDataUtils.generateTypedDataHashWithoutDomain(typedData));
const actualHash = await libZeroExTransactionContract.getStructHash.callAsync(transaction);
expect(actualHash).to.be.eq(expectedHash);
}
describe('getStructHash', () => {
it('should correctly hash an empty transaction', async () => {
await testGetStructHashAsync(EMPTY_TRANSACTION);
});
it('should correctly hash a non-empty transaction', async () => {
await testGetStructHashAsync({
salt: randomUint256(),
expirationTimeSeconds: randomUint256(),
signerAddress: randomAddress(),
data: randomAssetData(),
// The domain is not used in this test, so it's okay if it is left empty.
domain: {
verifyingContractAddress: constants.NULL_ADDRESS,
chainId: 0,
},
});
});
});