aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/src
diff options
context:
space:
mode:
authorAmir Bandeali <abandeali1@gmail.com>2019-01-03 08:52:15 +0800
committerAmir Bandeali <abandeali1@gmail.com>2019-01-08 02:31:11 +0800
commit1dda10b4f34eb19b731e58c26f13ecc08e367bbe (patch)
treea33ff795d65bcbb67379414e29138cb364c02fe0 /packages/order-utils/src
parent0fba0b1a1bbe1192802ebcf4b88437b94d6a1c18 (diff)
downloaddexon-0x-contracts-1dda10b4f34eb19b731e58c26f13ecc08e367bbe.tar.gz
dexon-0x-contracts-1dda10b4f34eb19b731e58c26f13ecc08e367bbe.tar.zst
dexon-0x-contracts-1dda10b4f34eb19b731e58c26f13ecc08e367bbe.zip
Add recursive MultiAsset decoding, store first address in pipeline
Diffstat (limited to 'packages/order-utils/src')
-rw-r--r--packages/order-utils/src/asset_data_utils.ts29
-rw-r--r--packages/order-utils/src/exchange_transfer_simulator.ts2
-rw-r--r--packages/order-utils/src/index.ts1
3 files changed, 30 insertions, 2 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
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,