Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
remove assetDataUtils everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
xianny committed Nov 27, 2019
1 parent 6ac5bcc commit 79fa0c0
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 762 deletions.
2 changes: 2 additions & 0 deletions contracts/extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
"chai-as-promised": "^7.1.0",
"chai-bignumber": "^3.0.0",
"dirty-chai": "^2.0.1",
"ethereumjs-abi": "0.6.5",
"ethereumjs-util": "^5.1.1",
"lodash": "^4.17.11",
"make-promises-safe": "^1.1.0",
"mocha": "^6.2.0",
Expand Down
16 changes: 9 additions & 7 deletions contracts/extensions/test/dutch_auction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
web3Wrapper,
} from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { assetDataUtils, generatePseudoRandomSalt } from '@0x/order-utils';
import { generatePseudoRandomSalt } from '@0x/order-utils';
import { RevertReason, SignedOrder } from '@0x/types';
import { BigNumber, providerUtils } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
Expand All @@ -26,6 +26,8 @@ import { DutchAuctionContract, DutchAuctionTestWrapper, WETH9Contract } from './

import { artifacts } from './artifacts';

import { encodeDutchAuctionAssetData } from './utils';

chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
Expand Down Expand Up @@ -158,7 +160,7 @@ describe(ContractName.DutchAuction, () => {
feeRecipientAddress,
// taker address or sender address should be set to the ducth auction contract
takerAddress: dutchAuctionContract.address,
makerAssetData: assetDataUtils.encodeDutchAuctionAssetData(
makerAssetData: encodeDutchAuctionAssetData(
await devUtils.encodeERC20AssetData(defaultMakerAssetAddress).callAsync(),
auctionBeginTimeSeconds,
auctionBeginAmount,
Expand Down Expand Up @@ -202,7 +204,7 @@ describe(ContractName.DutchAuction, () => {
describe('matchOrders', () => {
it('should be worth the begin price at the begining of the auction', async () => {
auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp + 2);
const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData(
const makerAssetData = encodeDutchAuctionAssetData(
defaultERC20MakerAssetData,
auctionBeginTimeSeconds,
auctionBeginAmount,
Expand All @@ -216,7 +218,7 @@ describe(ContractName.DutchAuction, () => {
it('should be be worth the end price at the end of the auction', async () => {
auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds * 2);
auctionEndTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds);
const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData(
const makerAssetData = encodeDutchAuctionAssetData(
defaultERC20MakerAssetData,
auctionBeginTimeSeconds,
auctionBeginAmount,
Expand Down Expand Up @@ -282,7 +284,7 @@ describe(ContractName.DutchAuction, () => {
it('should revert when auction expires', async () => {
auctionBeginTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds * 2);
auctionEndTimeSeconds = new BigNumber(currentBlockTimestamp - tenMinutesInSeconds);
const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData(
const makerAssetData = encodeDutchAuctionAssetData(
defaultERC20MakerAssetData,
auctionBeginTimeSeconds,
auctionBeginAmount,
Expand Down Expand Up @@ -310,7 +312,7 @@ describe(ContractName.DutchAuction, () => {
});
it('begin time is less than end time', async () => {
auctionBeginTimeSeconds = new BigNumber(auctionEndTimeSeconds).plus(tenMinutesInSeconds);
const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData(
const makerAssetData = encodeDutchAuctionAssetData(
defaultERC20MakerAssetData,
auctionBeginTimeSeconds,
auctionBeginAmount,
Expand All @@ -336,7 +338,7 @@ describe(ContractName.DutchAuction, () => {
const erc721MakerAssetData = await devUtils
.encodeERC721AssetData(erc721Token.address, makerAssetId)
.callAsync();
const makerAssetData = assetDataUtils.encodeDutchAuctionAssetData(
const makerAssetData = encodeDutchAuctionAssetData(
erc721MakerAssetData,
auctionBeginTimeSeconds,
auctionBeginAmount,
Expand Down
21 changes: 21 additions & 0 deletions contracts/extensions/test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
import { BigNumber } from '@0x/utils';
import * as ethAbi from 'ethereumjs-abi';
import * as ethUtil from 'ethereumjs-util';

export * from './balance_threshold_wrapper';
export * from './dutch_auction_test_wrapper';

// tslint:disable-next-line:completed-docs
export function encodeDutchAuctionAssetData(
assetData: string,
beginTimeSeconds: BigNumber,
beginAmount: BigNumber,
): string {
const assetDataBuffer = ethUtil.toBuffer(assetData);
const abiEncodedAuctionData = (ethAbi as any).rawEncode(
['uint256', 'uint256'],
[beginTimeSeconds.toString(), beginAmount.toString()],
);
const abiEncodedAuctionDataBuffer = ethUtil.toBuffer(abiEncodedAuctionData);
const dutchAuctionDataBuffer = Buffer.concat([assetDataBuffer, abiEncodedAuctionDataBuffer]);
const dutchAuctionData = ethUtil.bufferToHex(dutchAuctionDataBuffer);
return dutchAuctionData;
}
13 changes: 7 additions & 6 deletions packages/asset-swapper/test/order_prune_utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ContractAddresses } from '@0x/contract-addresses';
import { constants as devConstants, getLatestBlockTimestampAsync, OrderFactory } from '@0x/contracts-test-utils';
import { BlockchainLifecycle, tokenUtils } from '@0x/dev-utils';
import { migrateOnceAsync } from '@0x/migrations';
import { assetDataUtils } from '@0x/order-utils';
import { SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
Expand Down Expand Up @@ -70,13 +69,15 @@ describe('OrderPruner', () => {
[makerTokenAddress, takerTokenAddress] = tokenUtils.getDummyERC20TokenAddresses();
erc20MakerTokenContract = new ERC20TokenContract(makerTokenAddress, provider);
erc20TakerTokenContract = new ERC20TokenContract(takerTokenAddress, provider);
[makerAssetData, takerAssetData, wethAssetData] = [
assetDataUtils.encodeERC20AssetData(makerTokenAddress),
assetDataUtils.encodeERC20AssetData(takerTokenAddress),
assetDataUtils.encodeERC20AssetData(contractAddresses.etherToken),
];
exchangeContract = new ExchangeContract(contractAddresses.exchange, provider);
devUtilsContract = new DevUtilsContract(contractAddresses.devUtils, provider);

[makerAssetData, takerAssetData, wethAssetData] = [
await devUtilsContract.encodeERC20AssetData(makerTokenAddress).callAsync(),
await devUtilsContract.encodeERC20AssetData(takerTokenAddress).callAsync(),
await devUtilsContract.encodeERC20AssetData(contractAddresses.etherToken).callAsync(),
];

// Configure order defaults
const defaultOrderParams = {
...devConstants.STATIC_ORDER_PARAMS,
Expand Down
1 change: 1 addition & 0 deletions packages/instant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"dependencies": {
"@0x/assert": "^2.2.0-beta.2",
"@0x/asset-swapper": "^2.1.0-beta.3",
"@0x/contract-wrappers": "^12.2.0-beta.3",
"@0x/json-schemas": "^4.1.0-beta.2",
"@0x/order-utils": "^8.5.0-beta.3",
"@0x/subproviders": "^5.1.0-beta.2",
Expand Down
21 changes: 12 additions & 9 deletions packages/instant/src/index.umd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BigNumber, SwapQuoter } from '@0x/asset-swapper';
import { assetDataUtils } from '@0x/order-utils';
import { AssetProxyId } from '@0x/types';
import { providerUtils } from '@0x/utils';
import { SupportedProvider, ZeroExProvider } from 'ethereum-types';
Expand All @@ -19,19 +18,20 @@ import { ZeroExInstantOverlay, ZeroExInstantOverlayProps } from './index';
import { Network, OrderSource } from './types';
import { analytics } from './util/analytics';
import { assert } from './util/assert';
import { devUtilsContract } from './util/dev_utils_contract';
import { orderCoercionUtil } from './util/order_coercion';
import { providerFactory } from './util/provider_factory';
import { util } from './util/util';

const isInstantRendered = (): boolean => !!document.getElementById(INJECTED_DIV_ID);

const validateInstantRenderConfig = (config: ZeroExInstantConfig, selector: string) => {
const validateInstantRenderConfigAsync = async (config: ZeroExInstantConfig, selector: string) => {
assert.isValidOrderSource('orderSource', config.orderSource);
if (config.defaultSelectedAssetData !== undefined) {
assert.isHexString('defaultSelectedAssetData', config.defaultSelectedAssetData);
}
if (config.additionalAssetMetaDataMap !== undefined) {
assert.isValidAssetMetaDataMap('additionalAssetMetaDataMap', config.additionalAssetMetaDataMap);
await assert.isValidAssetMetaDataMapAsync('additionalAssetMetaDataMap', config.additionalAssetMetaDataMap);
}
if (config.defaultAssetBuyAmount !== undefined) {
assert.isNumber('defaultAssetBuyAmount', config.defaultAssetBuyAmount);
Expand Down Expand Up @@ -108,15 +108,15 @@ export interface ZeroExInstantConfig extends ZeroExInstantOverlayProps {
shouldDisablePushToHistory?: boolean;
}

export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => {
export const render = async (config: ZeroExInstantConfig, selector: string = DEFAULT_ZERO_EX_CONTAINER_SELECTOR) => {
// Coerces BigNumber provided in config to version utilized by 0x packages
const coercedConfig = _.assign({}, config, {
orderSource: _.isArray(config.orderSource)
? orderCoercionUtil.coerceOrderArrayFieldsToBigNumber(config.orderSource)
: config.orderSource,
});

validateInstantRenderConfig(coercedConfig, selector);
await validateInstantRenderConfigAsync(coercedConfig, selector);

if (coercedConfig.shouldDisablePushToHistory) {
if (!isInstantRendered()) {
Expand Down Expand Up @@ -156,14 +156,17 @@ export const ERC721_PROXY_ID = AssetProxyId.ERC721;

export const ERC20_PROXY_ID = AssetProxyId.ERC20;

export const assetDataForERC20TokenAddress = (tokenAddress: string): string => {
export const assetDataForERC20TokenAddressAsync = async (tokenAddress: string): Promise<string> => {
assert.isETHAddressHex('tokenAddress', tokenAddress);
return assetDataUtils.encodeERC20AssetData(tokenAddress);
return devUtilsContract.encodeERC20AssetData(tokenAddress).callAsync();
};

export const assetDataForERC721TokenAddress = (tokenAddress: string, tokenId: string | number): string => {
export const assetDataForERC721TokenAddressAsync = async (
tokenAddress: string,
tokenId: string | number,
): Promise<string> => {
assert.isETHAddressHex('tokenAddress', tokenAddress);
return assetDataUtils.encodeERC721AssetData(tokenAddress, new BigNumber(tokenId));
return devUtilsContract.encodeERC721AssetData(tokenAddress, new BigNumber(tokenId)).callAsync();
};

export const hasMetaDataForAssetData = (assetData: string): boolean => {
Expand Down
7 changes: 4 additions & 3 deletions packages/instant/src/util/assert.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { assert as sharedAssert } from '@0x/assert';
import { schemas } from '@0x/json-schemas';
import { assetDataUtils } from '@0x/order-utils';
import { AssetProxyId, ObjectMap, SignedOrder } from '@0x/types';
import * as _ from 'lodash';

import { AffiliateInfo, AssetMetaData } from '../types';

import { devUtilsContract } from './dev_utils_contract';

export const assert = {
...sharedAssert,
isValidOrderSource(variableName: string, orderSource: string | SignedOrder[]): void {
Expand All @@ -18,11 +19,11 @@ export const assert = {
areValidAssetDatas(variableName: string, assetDatas: string[]): void {
_.forEach(assetDatas, (assetData, index) => assert.isHexString(`${variableName}[${index}]`, assetData));
},
isValidAssetMetaDataMap(variableName: string, metaDataMap: ObjectMap<AssetMetaData>): void {
async isValidAssetMetaDataMapAsync(variableName: string, metaDataMap: ObjectMap<AssetMetaData>): Promise<void> {
_.forEach(metaDataMap, (metaData, assetData) => {
assert.isHexString(`key ${assetData} of ${variableName}`, assetData);
assert.isValidAssetMetaData(`${variableName}.${assetData}`, metaData);
const assetDataProxyId = assetDataUtils.decodeAssetProxyId(assetData);
const assetDataProxyId = await devUtilsContract.decodeAssetProxyId(assetData).callAsync();
assert.assert(
metaData.assetProxyId === assetDataProxyId,
`Expected meta data for assetData ${assetData} to have asset proxy id of ${assetDataProxyId}, but instead got ${
Expand Down
5 changes: 5 additions & 0 deletions packages/instant/src/util/dev_utils_contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DevUtilsContract } from '@0x/contract-wrappers';
import { NULL_ADDRESS } from '@0x/utils';

const fakeProvider = { isEIP1193: true } as any;
export const devUtilsContract = new DevUtilsContract(NULL_ADDRESS, fakeProvider);
Loading

0 comments on commit 79fa0c0

Please sign in to comment.