diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2019-01-04 03:18:33 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2019-01-08 02:31:11 +0800 |
commit | 5720589515d321e2d2db87c8337ee3f6340a0ef8 (patch) | |
tree | 86529a52a614ac4abf4c1342b558b8f28b0bc778 /packages/order-utils/src/asset_data_utils.ts | |
parent | 70508f52a1dd6bfcc891f145c3a06f1e66c3cda5 (diff) | |
download | dexon-sol-tools-5720589515d321e2d2db87c8337ee3f6340a0ef8.tar dexon-sol-tools-5720589515d321e2d2db87c8337ee3f6340a0ef8.tar.gz dexon-sol-tools-5720589515d321e2d2db87c8337ee3f6340a0ef8.tar.bz2 dexon-sol-tools-5720589515d321e2d2db87c8337ee3f6340a0ef8.tar.lz dexon-sol-tools-5720589515d321e2d2db87c8337ee3f6340a0ef8.tar.xz dexon-sol-tools-5720589515d321e2d2db87c8337ee3f6340a0ef8.tar.zst dexon-sol-tools-5720589515d321e2d2db87c8337ee3f6340a0ef8.zip |
Add tests for recursive MAP decoding
Diffstat (limited to 'packages/order-utils/src/asset_data_utils.ts')
-rw-r--r-- | packages/order-utils/src/asset_data_utils.ts | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/packages/order-utils/src/asset_data_utils.ts b/packages/order-utils/src/asset_data_utils.ts index 3741a683e..b5ce87e3e 100644 --- a/packages/order-utils/src/asset_data_utils.ts +++ b/packages/order-utils/src/asset_data_utils.ts @@ -128,16 +128,32 @@ export const assetDataUtils = { */ 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 amounts: any[] = []; + const decodedNestedAssetData = _.map( + decodedAssetData.nestedAssetData as string[], + (nestedAssetDataElement, index) => { + const decodedNestedAssetDataElement = assetDataUtils.decodeAssetDataOrThrow(nestedAssetDataElement); + if (decodedNestedAssetDataElement.assetProxyId === AssetProxyId.MultiAsset) { + const recursivelyDecodedAssetData = assetDataUtils.decodeMultiAssetDataRecursively( + nestedAssetDataElement, + ); + amounts.push( + _.map(recursivelyDecodedAssetData.amounts as BigNumber[], amountElement => + amountElement.times(decodedAssetData.amounts[index]), + ), + ); + return recursivelyDecodedAssetData.nestedAssetData; + } else { + amounts.push(decodedAssetData.amounts[index]); + return decodedNestedAssetDataElement as SingleAssetData; + } + }, + ); + const flattenedAmounts = _.flattenDeep(amounts); const flattenedDecodedNestedAssetData = _.flattenDeep(decodedNestedAssetData); return { assetProxyId: decodedAssetData.assetProxyId, - amounts: decodedAssetData.amounts, + amounts: flattenedAmounts, nestedAssetData: flattenedDecodedNestedAssetData as SingleAssetData[], }; }, |