aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/asset.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/instant/src/util/asset.ts')
-rw-r--r--packages/instant/src/util/asset.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/instant/src/util/asset.ts b/packages/instant/src/util/asset.ts
new file mode 100644
index 000000000..ec22276ae
--- /dev/null
+++ b/packages/instant/src/util/asset.ts
@@ -0,0 +1,34 @@
+import { assetDataUtils } from '@0xproject/order-utils';
+import { AssetProxyId, ObjectMap } from '@0xproject/types';
+import * as _ from 'lodash';
+
+import { assetDataNetworkMapping } from '../data/asset_data_network_mapping';
+import { Asset, AssetMetaData, Network, ZeroExInstantError } from '../types';
+
+export const assetUtils = {
+ createAssetFromAssetData: (assetData: string, assetMetaDataMap: ObjectMap<AssetMetaData>): Asset => {
+ return {
+ assetProxyId: assetDataUtils.decodeAssetProxyId(assetData),
+ assetData,
+ metaData: assetUtils.getMetaDataOrThrow(assetData, assetMetaDataMap),
+ };
+ },
+ getMetaDataOrThrow: (
+ assetData: string,
+ metaDataMap: ObjectMap<AssetMetaData>,
+ network: Network = Network.Mainnet,
+ ): AssetMetaData => {
+ let mainnetAssetData: string | undefined = assetData;
+ if (network !== Network.Mainnet) {
+ mainnetAssetData = assetDataNetworkMapping.getAssociatedAssetDataIfExists(assetData, network);
+ }
+ if (_.isUndefined(mainnetAssetData)) {
+ throw new Error(ZeroExInstantError.AssetMetaDataNotAvailable);
+ }
+ const metaData = metaDataMap[mainnetAssetData];
+ if (_.isUndefined(metaData)) {
+ throw new Error();
+ }
+ return metaData;
+ },
+};