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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
import { AssetBuyerError } from '@0x/asset-buyer';
import { AssetProxyId, ObjectMap } from '@0x/types';
import * as _ from 'lodash';
import { assetDataNetworkMapping } from '../data/asset_data_network_mapping';
import { Asset, AssetMetaData, ERC20Asset, Network, ZeroExInstantError } from '../types';
export const assetUtils = {
createAssetsFromAssetDatas: (
assetDatas: string[],
assetMetaDataMap: ObjectMap<AssetMetaData>,
network: Network,
): Asset[] => {
const arrayOfAssetOrUndefined = _.map(assetDatas, assetData =>
assetUtils.createAssetFromAssetDataIfExists(assetData, assetMetaDataMap, network),
);
return _.compact(arrayOfAssetOrUndefined);
},
createAssetFromAssetDataIfExists: (
assetData: string,
assetMetaDataMap: ObjectMap<AssetMetaData>,
network: Network,
): Asset | undefined => {
const metaData = assetUtils.getMetaDataIfExists(assetData, assetMetaDataMap, network);
if (_.isUndefined(metaData)) {
return;
}
return {
assetData,
metaData,
};
},
createAssetFromAssetDataOrThrow: (
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 => {
const metaDataIfExists = assetUtils.getMetaDataIfExists(assetData, metaDataMap, network);
if (_.isUndefined(metaDataIfExists)) {
throw new Error(ZeroExInstantError.AssetMetaDataNotAvailable);
}
return metaDataIfExists;
},
getMetaDataIfExists: (
assetData: string,
metaDataMap: ObjectMap<AssetMetaData>,
network: Network,
): AssetMetaData | undefined => {
let mainnetAssetData: string | undefined = assetData;
if (network !== Network.Mainnet) {
const mainnetAssetDataIfExists = assetUtils.getAssociatedAssetDataIfExists(
assetData.toLowerCase(),
network,
);
// Just so we don't fail in the case where we are on a non-mainnet network,
// but pass in a valid mainnet assetData.
mainnetAssetData = mainnetAssetDataIfExists || assetData;
}
if (_.isUndefined(mainnetAssetData)) {
return;
}
const metaData = metaDataMap[mainnetAssetData.toLowerCase()];
if (_.isUndefined(metaData)) {
return;
}
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;
}
},
formattedSymbolForAsset: (asset?: ERC20Asset, defaultName: string = '???'): string => {
if (_.isUndefined(asset)) {
return defaultName;
}
const symbol = asset.metaData.symbol;
if (symbol.length <= 5) {
return symbol;
}
return `${symbol.slice(0, 3)}…`;
},
getAssociatedAssetDataIfExists: (assetData: string, network: Network): string | undefined => {
const assetDataGroupIfExists = _.find(assetDataNetworkMapping, value => value[network] === assetData);
if (_.isUndefined(assetDataGroupIfExists)) {
return;
}
return assetDataGroupIfExists[Network.Mainnet];
},
getERC20AssetsFromAssets: (assets: Asset[]): ERC20Asset[] => {
const erc20sOrUndefined = _.map(
assets,
asset => (asset.metaData.assetProxyId === AssetProxyId.ERC20 ? (asset as ERC20Asset) : undefined),
);
return _.compact(erc20sOrUndefined);
},
assetBuyerErrorMessage: (asset: ERC20Asset, error: Error): string | undefined => {
if (error.message === AssetBuyerError.InsufficientAssetLiquidity) {
const assetName = assetUtils.bestNameForAsset(asset, 'of this asset');
return `Not enough ${assetName} available`;
} else if (error.message === AssetBuyerError.InsufficientZrxLiquidity) {
return 'Not enough ZRX available';
} else if (
error.message === AssetBuyerError.StandardRelayerApiError ||
error.message.startsWith(AssetBuyerError.AssetUnavailable)
) {
const assetName = assetUtils.bestNameForAsset(asset, 'This asset');
return `${assetName} is currently unavailable`;
}
return undefined;
},
};
|