From 1dda10b4f34eb19b731e58c26f13ecc08e367bbe Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 2 Jan 2019 16:52:15 -0800 Subject: Add recursive MultiAsset decoding, store first address in pipeline --- packages/0x.js/src/index.ts | 1 + packages/order-utils/src/asset_data_utils.ts | 29 +++++++++++++++++++++- .../order-utils/src/exchange_transfer_simulator.ts | 2 +- packages/order-utils/src/index.ts | 1 + packages/pipeline/package.json | 2 +- .../pipeline/src/parsers/events/exchange_events.ts | 26 +++++++++++-------- packages/pipeline/src/parsers/sra_orders/index.ts | 14 ++++++----- packages/types/src/index.ts | 8 +++++- yarn.lock | 4 +-- 9 files changed, 64 insertions(+), 23 deletions(-) diff --git a/packages/0x.js/src/index.ts b/packages/0x.js/src/index.ts index 375843741..01742f1b9 100644 --- a/packages/0x.js/src/index.ts +++ b/packages/0x.js/src/index.ts @@ -83,6 +83,7 @@ export { ERC20AssetData, ERC721AssetData, MultiAssetData, + MultiAssetDataWithRecursiveDecoding, SignatureType, OrderRelevantState, Stats, diff --git a/packages/order-utils/src/asset_data_utils.ts b/packages/order-utils/src/asset_data_utils.ts index 18c1f93b6..3741a683e 100644 --- a/packages/order-utils/src/asset_data_utils.ts +++ b/packages/order-utils/src/asset_data_utils.ts @@ -1,4 +1,11 @@ -import { AssetProxyId, ERC20AssetData, ERC721AssetData, MultiAssetData, SingleAssetData } from '@0x/types'; +import { + AssetProxyId, + ERC20AssetData, + ERC721AssetData, + MultiAssetData, + MultiAssetDataWithRecursiveDecoding, + SingleAssetData, +} from '@0x/types'; import { AbiEncoder, BigNumber } from '@0x/utils'; import { MethodAbi } from 'ethereum-types'; import * as _ from 'lodash'; @@ -114,6 +121,26 @@ export const assetDataUtils = { nestedAssetData, }; }, + /** + * Decodes a MultiAsset assetData hex string into it's corresponding amounts and decoded nestedAssetData elements (all nested elements are flattened) + * @param assetData Hex encoded assetData string to decode + * @return An object containing the decoded amounts and nestedAssetData + */ + decodeMultiAssetDataRecursively(assetData: string): MultiAssetDataWithRecursiveDecoding { + const decodedAssetData = assetDataUtils.decodeMultiAssetData(assetData); + const decodedNestedAssetData = _.map(decodedAssetData.nestedAssetData as string[], nestedAssetDataElement => { + const decodedNestedAssetDataElement = assetDataUtils.decodeAssetDataOrThrow(nestedAssetDataElement); + return decodedNestedAssetDataElement.assetProxyId === AssetProxyId.MultiAsset + ? assetDataUtils.decodeMultiAssetDataRecursively(nestedAssetDataElement).nestedAssetData + : (decodedNestedAssetDataElement as SingleAssetData); + }); + const flattenedDecodedNestedAssetData = _.flattenDeep(decodedNestedAssetData); + return { + assetProxyId: decodedAssetData.assetProxyId, + amounts: decodedAssetData.amounts, + nestedAssetData: flattenedDecodedNestedAssetData as SingleAssetData[], + }; + }, /** * Decode and return the assetProxyId from the assetData * @param assetData Hex encoded assetData string to decode diff --git a/packages/order-utils/src/exchange_transfer_simulator.ts b/packages/order-utils/src/exchange_transfer_simulator.ts index 06621fd9e..0a948fd1f 100644 --- a/packages/order-utils/src/exchange_transfer_simulator.ts +++ b/packages/order-utils/src/exchange_transfer_simulator.ts @@ -108,7 +108,7 @@ export class ExchangeTransferSimulator { const amountsElement = decodedAssetData.amounts[index]; const totalAmount = amountInBaseUnits.times(amountsElement); await this.transferFromAsync( - nestedAssetDataElement, + nestedAssetDataElement as string, from, to, totalAmount, diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts index e098f23c3..398188433 100644 --- a/packages/order-utils/src/index.ts +++ b/packages/order-utils/src/index.ts @@ -38,6 +38,7 @@ export { ERC20AssetData, ERC721AssetData, MultiAssetData, + MultiAssetDataWithRecursiveDecoding, AssetProxyId, SignatureType, OrderStateValid, diff --git a/packages/pipeline/package.json b/packages/pipeline/package.json index a40f3d21c..ab73642ec 100644 --- a/packages/pipeline/package.json +++ b/packages/pipeline/package.json @@ -44,7 +44,7 @@ "@0x/contract-artifacts": "^1.0.1", "@0x/contract-wrappers": "^3.0.0", "@0x/dev-utils": "^1.0.21", - "@0x/order-utils": "^2.0.0", + "@0x/order-utils": "^3.0.7", "@0x/subproviders": "^2.1.8", "@0x/types": "^1.4.1", "@0x/utils": "^2.0.8", diff --git a/packages/pipeline/src/parsers/events/exchange_events.ts b/packages/pipeline/src/parsers/events/exchange_events.ts index 16b05260b..50fad7e00 100644 --- a/packages/pipeline/src/parsers/events/exchange_events.ts +++ b/packages/pipeline/src/parsers/events/exchange_events.ts @@ -1,6 +1,6 @@ import { ExchangeCancelEventArgs, ExchangeCancelUpToEventArgs, ExchangeFillEventArgs } from '@0x/contract-wrappers'; import { assetDataUtils } from '@0x/order-utils'; -import { AssetProxyId, ERC20AssetData, ERC721AssetData } from '@0x/types'; +import { AssetProxyId, ERC721AssetData, SingleAssetData } from '@0x/types'; import { LogWithDecodedArgs } from 'ethereum-types'; import * as R from 'ramda'; @@ -62,8 +62,9 @@ export function _convertToExchangeFillEvent(eventLog: LogWithDecodedArgs