aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/asset.ts
blob: ec22276aef9ac025a8df203754a62fba963fde33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
    },
};