diff options
Diffstat (limited to 'packages/order-utils/src/asset_data_utils.ts')
-rw-r--r-- | packages/order-utils/src/asset_data_utils.ts | 29 |
1 files changed, 28 insertions, 1 deletions
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'; @@ -115,6 +122,26 @@ export const assetDataUtils = { }; }, /** + * 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 * @return The assetProxyId |