Remove assetDataUtils everywhere (#2373)

* remove assetDataUtils everywhere

* export IAssetDataContract from @0x/contract-wrappers to allow @0x/instant to decode asset data  synchronously

* export generic function `decodeAssetDataOrThrow` and add ERC20Bridge support

* export `hexUtils` from order-utils instead of contracts-test-utils
This commit is contained in:
Xianny
2019-12-04 13:08:08 -08:00
committed by GitHub
parent b86d19028c
commit fcbcbac889
70 changed files with 1498 additions and 1129 deletions

View File

@@ -45,8 +45,8 @@
"dependencies": {
"@0x/assert": "^3.0.0",
"@0x/asset-swapper": "^3.0.0",
"@0x/contract-wrappers": "13.0.0",
"@0x/json-schemas": "^5.0.0",
"@0x/order-utils": "^9.0.0",
"@0x/subproviders": "^6.0.0",
"@0x/utils": "^5.0.0",
"@0x/web3-wrapper": "^7.0.0",

View File

@@ -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';
@@ -19,6 +18,7 @@ import { ZeroExInstantOverlay, ZeroExInstantOverlayProps } from './index';
import { Network, OrderSource } from './types';
import { analytics } from './util/analytics';
import { assert } from './util/assert';
import { assetDataEncoder } from './util/asset_data_encoder';
import { orderCoercionUtil } from './util/order_coercion';
import { providerFactory } from './util/provider_factory';
import { util } from './util/util';
@@ -158,12 +158,12 @@ export const ERC20_PROXY_ID = AssetProxyId.ERC20;
export const assetDataForERC20TokenAddress = (tokenAddress: string): string => {
assert.isETHAddressHex('tokenAddress', tokenAddress);
return assetDataUtils.encodeERC20AssetData(tokenAddress);
return assetDataEncoder.ERC20Token(tokenAddress).getABIEncodedTransactionData();
};
export const assetDataForERC721TokenAddress = (tokenAddress: string, tokenId: string | number): string => {
assert.isETHAddressHex('tokenAddress', tokenAddress);
return assetDataUtils.encodeERC721AssetData(tokenAddress, new BigNumber(tokenId));
return assetDataEncoder.ERC721Token(tokenAddress, new BigNumber(tokenId)).getABIEncodedTransactionData();
};
export const hasMetaDataForAssetData = (assetData: string): boolean => {

View File

@@ -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 { decodeAssetProxyId } from './asset_data_encoder';
export const assert = {
...sharedAssert,
isValidOrderSource(variableName: string, orderSource: string | SignedOrder[]): void {
@@ -22,7 +23,7 @@ export const assert = {
_.forEach(metaDataMap, (metaData, assetData) => {
assert.isHexString(`key ${assetData} of ${variableName}`, assetData);
assert.isValidAssetMetaData(`${variableName}.${assetData}`, metaData);
const assetDataProxyId = assetDataUtils.decodeAssetProxyId(assetData);
const assetDataProxyId = decodeAssetProxyId(assetData);
assert.assert(
metaData.assetProxyId === assetDataProxyId,
`Expected meta data for assetData ${assetData} to have asset proxy id of ${assetDataProxyId}, but instead got ${

View File

@@ -0,0 +1,19 @@
import { IAssetDataContract } from '@0x/contract-wrappers';
import { hexUtils, NULL_ADDRESS } from '@0x/utils';
const fakeProvider = { isEIP1193: true } as any;
// instantiate once per app to be more performant
export const assetDataEncoder = new IAssetDataContract(NULL_ADDRESS, fakeProvider);
/**
* Returns the first four bytes of a given hex string.
* Does not have any validations, so the hex string MUST
* be a valid asset data, or this function returns garbage
* without throwing errors.
* @param assetData A hex string where the first four bytes are a valid Asset Proxy Id
*
*/
export function decodeAssetProxyId(assetData: string): string {
return hexUtils.slice(assetData, 0, 4);
}