aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils/asset_data_utils.ts
blob: d05ff250488114ba40f6dd24334c5c4cf9f2a4df (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
import { ContractWrappers } from '@0xproject/contract-wrappers';
import { assetDataUtils as sharedAssetDataUtils } from '@0xproject/order-utils';
import * as _ from 'lodash';

import { AssetBuyerError } from '../types';

export const assetDataUtils = {
    ...sharedAssetDataUtils,
    getEtherTokenAssetDataOrThrow(contractWrappers: ContractWrappers): string {
        const etherTokenAddressIfExists = contractWrappers.etherToken.getContractAddressIfExists();
        if (_.isUndefined(etherTokenAddressIfExists)) {
            throw new Error(AssetBuyerError.NoEtherTokenContractFound);
        }
        const etherTokenAssetData = sharedAssetDataUtils.encodeERC20AssetData(etherTokenAddressIfExists);
        return etherTokenAssetData;
    },
    getZrxTokenAssetDataOrThrow(contractWrappers: ContractWrappers): string {
        let zrxTokenAssetData: string;
        try {
            zrxTokenAssetData = contractWrappers.exchange.getZRXAssetData();
        } catch (err) {
            throw new Error(AssetBuyerError.NoZrxTokenContractFound);
        }
        return zrxTokenAssetData;
    },
};