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

Commit

Permalink
export generic function decodeAssetDataOrThrow and add ERC20Bridge …
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
xianny committed Dec 3, 2019
1 parent 3d99df1 commit 4e464d3
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 33 deletions.
3 changes: 2 additions & 1 deletion contracts/test-utils/src/address_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { hexRandom } from '@0x/utils';

import { constants } from './constants';
import { hexRandom } from './hex_utils';

/**
* Generates a random address.
Expand Down
11 changes: 0 additions & 11 deletions contracts/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ export { OrderFactory } from './order_factory';
export { bytes32Values, testCombinatoriallyWithReferenceFunc, uint256Values } from './combinatorial_utils';
export { TransactionFactory } from './transaction_factory';
export { testWithReferenceFuncAsync } from './test_with_reference';
export {
hexConcat,
hexHash,
hexLeftPad,
hexInvert,
hexSlice,
hexRandom,
hexRightPad,
hexSize,
toHex,
} from './hex_utils';
export {
BatchMatchOrder,
ContractName,
Expand Down
3 changes: 1 addition & 2 deletions contracts/test-utils/src/order_utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { generatePseudoRandomSalt } from '@0x/order-utils';
import { Order, SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils';
import { BigNumber, hexHash } from '@0x/utils';
import * as _ from 'lodash';

import { constants } from './constants';
import { hexHash } from './hex_utils';
import { BatchMatchOrder, CancelOrder, MatchOrder } from './types';

export const orderUtils = {
Expand Down
17 changes: 2 additions & 15 deletions packages/instant/src/util/asset_data_encoder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IAssetDataContract } from '@0x/contract-wrappers';
import { NULL_ADDRESS } from '@0x/utils';
import { hexSlice, 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);

/**
Expand All @@ -14,19 +15,5 @@ export const assetDataEncoder = new IAssetDataContract(NULL_ADDRESS, fakeProvide
*
*/
export function decodeAssetProxyId(assetData: string): string {
/**
* Slices a hex number.
* Copied from @0x/contracts-test-utils
* Consider moving hex_utils into @0x/utils if this is needed in more places
*/
function hexSlice(n: string, start: number, end?: number): string {
const hex = n.substr(2); // removed assertions that n is a hex string
const sliceStart = start >= 0 ? start * 2 : Math.max(0, hex.length + start * 2);
let sliceEnd = hex.length;
if (end !== undefined) {
sliceEnd = end >= 0 ? end * 2 : Math.max(0, hex.length + end * 2);
}
return '0x'.concat(hex.substring(sliceStart, sliceEnd));
}
return hexSlice(assetData, 0, 4);
}
74 changes: 74 additions & 0 deletions packages/order-utils/src/decode_asset_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { IAssetDataContract } from '@0x/contract-wrappers';
import { AssetData, AssetProxyId } from '@0x/types';
import { BigNumber, hexSlice, NULL_ADDRESS } from '@0x/utils';

const fakeProvider = { isEIP1193: true } as any;
const assetDataDecoder = new IAssetDataContract(NULL_ADDRESS, fakeProvider);
export function decodeAssetDataOrThrow(assetData: string): AssetData {
const assetProxyId = hexSlice(assetData, 0, 4); // tslint:disable-line:custom-no-magic-numbers
switch (assetProxyId) {
case AssetProxyId.ERC20: {
const tokenAddress = assetDataDecoder.getABIDecodedTransactionData<string>('ERC20Token', assetData);
return {
assetProxyId,
tokenAddress,
};
}
case AssetProxyId.ERC20Bridge: {
const [tokenAddress, bridgeAddress, bridgeData] = assetDataDecoder.getABIDecodedTransactionData<
[string, string, string]
>('ERC20Bridge', assetData);
return {
assetProxyId,
tokenAddress,
bridgeAddress,
bridgeData,
};
}
case AssetProxyId.ERC721: {
const [tokenAddress, tokenId] = assetDataDecoder.getABIDecodedTransactionData<[string, BigNumber]>(
'ERC721Token',
assetData,
);
return {
assetProxyId,
tokenAddress,
tokenId,
};
}
case AssetProxyId.ERC1155: {
const [tokenAddress, tokenIds, tokenValues] = assetDataDecoder.getABIDecodedTransactionData<
[string, BigNumber[], BigNumber[]]
>('ERC1155Assets', assetData);
return {
assetProxyId,
tokenAddress,
tokenIds,
tokenValues,
};
}
case AssetProxyId.MultiAsset: {
const [amounts, nestedAssetData] = assetDataDecoder.getABIDecodedTransactionData<[BigNumber[], string[]]>(
'MultiAsset',
assetData,
);
return {
assetProxyId,
amounts,
nestedAssetData,
};
}
case AssetProxyId.StaticCall:
const [callTarget, staticCallData, callResultHash] = assetDataDecoder.getABIDecodedTransactionData<
[string, string, string]
>('StaticCall', assetData);
return {
assetProxyId,
callTarget,
staticCallData,
callResultHash,
};
default:
throw new Error(`Unhandled asset proxy ID: ${assetProxyId}`);
}
}
14 changes: 13 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ export interface ERC20AssetData {
tokenAddress: string;
}

export interface ERC20BridgeAssetData {
assetProxyId: string;
tokenAddress: string;
bridgeAddress: string;
bridgeData: string;
}

export interface ERC721AssetData {
assetProxyId: string;
tokenAddress: string;
Expand Down Expand Up @@ -201,7 +208,12 @@ export interface ERC1155AssetDataNoProxyId {
callbackData: string;
}

export type SingleAssetData = ERC20AssetData | ERC721AssetData | ERC1155AssetData | StaticCallAssetData;
export type SingleAssetData =
| ERC20AssetData
| ERC20BridgeAssetData
| ERC721AssetData
| ERC1155AssetData
| StaticCallAssetData;

export interface MultiAssetData {
assetProxyId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { BigNumber } from '@0x/utils';
import * as crypto from 'crypto';
import * as ethUtil from 'ethereumjs-util';

import { constants } from './constants';
import { BigNumber } from './index';
import { Numberish } from './types';

const { WORD_LENGTH } = constants;
// tslint:disable:custom-no-magic-numbers

const WORD_LENGTH = 32;
const WORD_CEIL = new BigNumber(2).pow(WORD_LENGTH * 8);

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ export {
AnyRevertError,
} from './revert_error';

export {
hexConcat,
hexHash,
hexLeftPad,
hexInvert,
hexSlice,
hexRandom,
hexRightPad,
hexSize,
toHex,
} from './hex_utils';

export import CoordinatorRevertErrors = require('./revert_errors/coordinator/revert_errors');
export import ExchangeForwarderRevertErrors = require('./revert_errors/exchange-forwarder/revert_errors');
export import LibMathRevertErrors = require('./revert_errors/exchange-libs/lib_math_revert_errors');
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AbiEncoder } from '.';
import { BigNumber } from './index';

export interface FunctionInfo {
functionSignature: string;
Expand All @@ -17,3 +18,5 @@ export interface DecodedCalldata {
functionSignature: string;
functionArguments: any;
}

export type Numberish = BigNumber | string | number;

0 comments on commit 4e464d3

Please sign in to comment.