diff options
author | Francesco Agosti <francesco.agosti93@gmail.com> | 2018-10-24 08:00:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-24 08:00:54 +0800 |
commit | 4a72dc6c6f0825232d61e470f5b7975aec2e5c0e (patch) | |
tree | fe6a9ee3ebe2c3a3aa21931261eca3c91f8072d7 /packages/instant/src/util/asset.ts | |
parent | 2110ac32b7fceb230467ae0e2bb2bcd5e582f25f (diff) | |
parent | 864f89c535cf68a9325f27eae7f49a088c481120 (diff) | |
download | dexon-sol-tools-4a72dc6c6f0825232d61e470f5b7975aec2e5c0e.tar dexon-sol-tools-4a72dc6c6f0825232d61e470f5b7975aec2e5c0e.tar.gz dexon-sol-tools-4a72dc6c6f0825232d61e470f5b7975aec2e5c0e.tar.bz2 dexon-sol-tools-4a72dc6c6f0825232d61e470f5b7975aec2e5c0e.tar.lz dexon-sol-tools-4a72dc6c6f0825232d61e470f5b7975aec2e5c0e.tar.xz dexon-sol-tools-4a72dc6c6f0825232d61e470f5b7975aec2e5c0e.tar.zst dexon-sol-tools-4a72dc6c6f0825232d61e470f5b7975aec2e5c0e.zip |
Merge pull request #1159 from 0xProject/feature/instant/beta-render-et-al
[instant] Pass in liquiditySource, assetData and other settings from render
Diffstat (limited to 'packages/instant/src/util/asset.ts')
-rw-r--r-- | packages/instant/src/util/asset.ts | 53 |
1 files changed, 53 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..4e3b2b946 --- /dev/null +++ b/packages/instant/src/util/asset.ts @@ -0,0 +1,53 @@ +import { AssetProxyId, ObjectMap } from '@0x/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>, + network: Network, + ): Asset => { + return { + assetData, + metaData: assetUtils.getMetaDataOrThrow(assetData, assetMetaDataMap, network), + }; + }, + getMetaDataOrThrow: (assetData: string, metaDataMap: ObjectMap<AssetMetaData>, network: Network): AssetMetaData => { + let mainnetAssetData: string | undefined = assetData; + if (network !== Network.Mainnet) { + mainnetAssetData = assetUtils.getAssociatedAssetDataIfExists(assetData, network); + } + if (_.isUndefined(mainnetAssetData)) { + throw new Error(ZeroExInstantError.AssetMetaDataNotAvailable); + } + const metaData = metaDataMap[mainnetAssetData]; + if (_.isUndefined(metaData)) { + throw new Error(ZeroExInstantError.AssetMetaDataNotAvailable); + } + return metaData; + }, + bestNameForAsset: (asset?: Asset, defaultName: string = '???'): string => { + if (_.isUndefined(asset)) { + return defaultName; + } + const metaData = asset.metaData; + switch (metaData.assetProxyId) { + case AssetProxyId.ERC20: + return metaData.symbol.toUpperCase(); + case AssetProxyId.ERC721: + return metaData.name; + default: + return defaultName; + } + }, + getAssociatedAssetDataIfExists: (assetData: string, network: Network): string | undefined => { + const assetDataGroupIfExists = _.find(assetDataNetworkMapping, value => value[network] === assetData); + if (_.isUndefined(assetDataGroupIfExists)) { + return; + } + return assetDataGroupIfExists[Network.Mainnet]; + }, +}; |