diff options
author | fragosti <francesco.agosti93@gmail.com> | 2019-01-25 04:05:44 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2019-01-25 04:05:44 +0800 |
commit | 5b06595a6b6d459d53840d066fb204c0a9e3ed02 (patch) | |
tree | a5e9ed5c805cd69bd2f78525ac5b02cb9cdebdbf /packages/instant/src | |
parent | 44aafe4d78059267c9279fdf747fd51e6c3b26e1 (diff) | |
parent | 92cbff67d196abc7755e82087dbb1831485485d1 (diff) | |
download | dexon-0x-contracts-5b06595a6b6d459d53840d066fb204c0a9e3ed02.tar.gz dexon-0x-contracts-5b06595a6b6d459d53840d066fb204c0a9e3ed02.tar.zst dexon-0x-contracts-5b06595a6b6d459d53840d066fb204c0a9e3ed02.zip |
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into development
Diffstat (limited to 'packages/instant/src')
-rw-r--r-- | packages/instant/src/components/order_details.tsx | 4 | ||||
-rw-r--r-- | packages/instant/src/constants.ts | 2 | ||||
-rw-r--r-- | packages/instant/src/containers/selected_erc20_asset_amount_input.ts | 2 | ||||
-rw-r--r-- | packages/instant/src/index.umd.ts | 42 | ||||
-rw-r--r-- | packages/instant/src/redux/async_data.ts | 2 | ||||
-rw-r--r-- | packages/instant/src/util/asset.ts | 9 | ||||
-rw-r--r-- | packages/instant/src/util/format.ts | 8 | ||||
-rw-r--r-- | packages/instant/src/util/gas_price_estimator.ts | 2 | ||||
-rw-r--r-- | packages/instant/src/util/maybe_big_number.ts | 2 |
9 files changed, 59 insertions, 14 deletions
diff --git a/packages/instant/src/components/order_details.tsx b/packages/instant/src/components/order_details.tsx index 4db20b13e..3ded50652 100644 --- a/packages/instant/src/components/order_details.tsx +++ b/packages/instant/src/components/order_details.tsx @@ -77,7 +77,7 @@ export class OrderDetails extends React.PureComponent<OrderDetailsProps> { } private _hadErrorFetchingUsdPrice(): boolean { - return this.props.ethUsdPrice ? this.props.ethUsdPrice.equals(BIG_NUMBER_ZERO) : false; + return this.props.ethUsdPrice ? this.props.ethUsdPrice.isEqualTo(BIG_NUMBER_ZERO) : false; } private _totalCostSecondaryValue(): React.ReactNode { @@ -156,7 +156,7 @@ export class OrderDetails extends React.PureComponent<OrderDetailsProps> { return !_.isUndefined(assetTotalInWei) && !_.isUndefined(selectedAssetUnitAmount) && !selectedAssetUnitAmount.eq(BIG_NUMBER_ZERO) - ? assetTotalInWei.div(selectedAssetUnitAmount).ceil() + ? assetTotalInWei.div(selectedAssetUnitAmount).integerValue(BigNumber.ROUND_CEIL) : undefined; } diff --git a/packages/instant/src/constants.ts b/packages/instant/src/constants.ts index bfd9e9098..d407925a3 100644 --- a/packages/instant/src/constants.ts +++ b/packages/instant/src/constants.ts @@ -21,7 +21,7 @@ export const NPM_PACKAGE_VERSION = process.env.NPM_PACKAGE_VERSION; export const DEFAULT_UNKOWN_ASSET_NAME = '???'; export const ACCOUNT_UPDATE_INTERVAL_TIME_MS = ONE_SECOND_MS * 5; export const BUY_QUOTE_UPDATE_INTERVAL_TIME_MS = ONE_SECOND_MS * 15; -export const DEFAULT_GAS_PRICE = GWEI_IN_WEI.mul(6); +export const DEFAULT_GAS_PRICE = GWEI_IN_WEI.multipliedBy(6); export const DEFAULT_ESTIMATED_TRANSACTION_TIME_MS = ONE_MINUTE_MS * 2; export const MAGIC_TRIGGER_ERROR_INPUT = '0€'; export const MAGIC_TRIGGER_ERROR_MESSAGE = 'Triggered error'; diff --git a/packages/instant/src/containers/selected_erc20_asset_amount_input.ts b/packages/instant/src/containers/selected_erc20_asset_amount_input.ts index cb9df527e..4b9993332 100644 --- a/packages/instant/src/containers/selected_erc20_asset_amount_input.ts +++ b/packages/instant/src/containers/selected_erc20_asset_amount_input.ts @@ -84,7 +84,7 @@ const mapDispatchToProps = ( // reset our buy state dispatch(actions.setBuyOrderStateNone()); - if (!_.isUndefined(value) && value.greaterThan(0) && !_.isUndefined(asset)) { + if (!_.isUndefined(value) && value.isGreaterThan(0) && !_.isUndefined(asset)) { // even if it's debounced, give them the illusion it's loading dispatch(actions.setQuoteRequestStatePending()); // tslint:disable-next-line:no-floating-promises diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index d172f4145..0acf3f2ad 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -1,3 +1,6 @@ +import { AssetBuyer, BigNumber } from '@0x/asset-buyer'; +import { assetDataUtils } from '@0x/order-utils'; +import { Provider } from 'ethereum-types'; import * as _ from 'lodash'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; @@ -9,9 +12,12 @@ import { INJECTED_DIV_ID, NPM_PACKAGE_VERSION, } from './constants'; +import { assetMetaDataMap } from './data/asset_meta_data_map'; import { ZeroExInstantOverlay, ZeroExInstantOverlayProps } from './index'; +import { Network, OrderSource } from './types'; import { analytics } from './util/analytics'; import { assert } from './util/assert'; +import { providerFactory } from './util/provider_factory'; import { util } from './util/util'; const isInstantRendered = (): boolean => !!document.getElementById(INJECTED_DIV_ID); @@ -122,6 +128,42 @@ export const render = (config: ZeroExInstantConfig, selector: string = DEFAULT_Z window.onpopstate = onPopStateHandler; }; +export const assetDataForERC20TokenAddress = (tokenAddress: string): string => { + assert.isETHAddressHex('tokenAddress', tokenAddress); + return assetDataUtils.encodeERC20AssetData(tokenAddress); +}; + +export const hasMetaDataForAssetData = (assetData: string): boolean => { + assert.isHexString('assetData', assetData); + return assetMetaDataMap[assetData] !== undefined; +}; + +export const hasLiquidityForAssetDataAsync = async ( + assetData: string, + orderSource: OrderSource, + networkId: Network = Network.Mainnet, + provider?: Provider, +): Promise<boolean> => { + assert.isHexString('assetData', assetData); + assert.isValidOrderSource('orderSource', orderSource); + assert.isNumber('networkId', networkId); + + if (provider !== undefined) { + assert.isWeb3Provider('provider', provider); + } + + const bestProvider: Provider = provider || providerFactory.getFallbackNoSigningProvider(networkId); + + const assetBuyerOptions = { networkId }; + + const assetBuyer = _.isString(orderSource) + ? AssetBuyer.getAssetBuyerForStandardRelayerAPIUrl(bestProvider, orderSource, assetBuyerOptions) + : AssetBuyer.getAssetBuyerForProvidedOrders(bestProvider, orderSource, assetBuyerOptions); + + const liquidity = await assetBuyer.getLiquidityForAssetDataAsync(assetData); + return liquidity.ethValueAvailableInWei.gt(new BigNumber(0)); +}; + // Write version info to the exported object for debugging export const GIT_SHA = GIT_SHA_FROM_CONSTANT; export const NPM_VERSION = NPM_PACKAGE_VERSION; diff --git a/packages/instant/src/redux/async_data.ts b/packages/instant/src/redux/async_data.ts index 884ab103d..f20fe319f 100644 --- a/packages/instant/src/redux/async_data.ts +++ b/packages/instant/src/redux/async_data.ts @@ -99,7 +99,7 @@ export const asyncData = { if ( !_.isUndefined(selectedAssetUnitAmount) && !_.isUndefined(selectedAsset) && - selectedAssetUnitAmount.greaterThan(BIG_NUMBER_ZERO) && + selectedAssetUnitAmount.isGreaterThan(BIG_NUMBER_ZERO) && buyOrderState.processState === OrderProcessState.None && selectedAsset.metaData.assetProxyId === AssetProxyId.ERC20 ) { diff --git a/packages/instant/src/util/asset.ts b/packages/instant/src/util/asset.ts index 709561dbc..b5c97913d 100644 --- a/packages/instant/src/util/asset.ts +++ b/packages/instant/src/util/asset.ts @@ -114,15 +114,18 @@ export const assetUtils = { const assetName = assetUtils.bestNameForAsset(asset, 'of this asset'); if ( error instanceof InsufficientAssetLiquidityError && - error.amountAvailableToFill.greaterThan(BIG_NUMBER_ZERO) + error.amountAvailableToFill.isGreaterThan(BIG_NUMBER_ZERO) ) { const unitAmountAvailableToFill = Web3Wrapper.toUnitAmount( error.amountAvailableToFill, asset.metaData.decimals, ); - const roundedUnitAmountAvailableToFill = unitAmountAvailableToFill.round(2, BigNumber.ROUND_DOWN); + const roundedUnitAmountAvailableToFill = unitAmountAvailableToFill.decimalPlaces( + 2, + BigNumber.ROUND_DOWN, + ); - if (roundedUnitAmountAvailableToFill.greaterThan(BIG_NUMBER_ZERO)) { + if (roundedUnitAmountAvailableToFill.isGreaterThan(BIG_NUMBER_ZERO)) { return `There are only ${roundedUnitAmountAvailableToFill} ${assetName} available to buy`; } } diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts index 4adb63e21..61aeb839f 100644 --- a/packages/instant/src/util/format.ts +++ b/packages/instant/src/util/format.ts @@ -25,16 +25,16 @@ export const format = { if (_.isUndefined(ethUnitAmount)) { return defaultText; } - let roundedAmount = ethUnitAmount.round(decimalPlaces).toDigits(decimalPlaces); + let roundedAmount = ethUnitAmount.decimalPlaces(decimalPlaces).precision(decimalPlaces); - if (roundedAmount.eq(BIG_NUMBER_ZERO) && ethUnitAmount.greaterThan(BIG_NUMBER_ZERO)) { + if (roundedAmount.eq(BIG_NUMBER_ZERO) && ethUnitAmount.isGreaterThan(BIG_NUMBER_ZERO)) { // Sometimes for small ETH amounts (i.e. 0.000045) the amount rounded to 4 decimalPlaces is 0 // If that is the case, show to 1 significant digit roundedAmount = new BigNumber(ethUnitAmount.toPrecision(1)); } const displayAmount = - roundedAmount.greaterThan(BIG_NUMBER_ZERO) && roundedAmount.lessThan(minUnitAmountToDisplay) + roundedAmount.isGreaterThan(BIG_NUMBER_ZERO) && roundedAmount.isLessThan(minUnitAmountToDisplay) ? `< ${minUnitAmountToDisplay.toString()}` : roundedAmount.toString(); @@ -62,7 +62,7 @@ export const format = { if (_.isUndefined(ethUnitAmount) || _.isUndefined(ethUsdPrice)) { return defaultText; } - const rawUsdPrice = ethUnitAmount.mul(ethUsdPrice); + const rawUsdPrice = ethUnitAmount.multipliedBy(ethUsdPrice); const roundedUsdPrice = rawUsdPrice.toFixed(decimalPlaces); if (roundedUsdPrice === '0.00' && rawUsdPrice.gt(BIG_NUMBER_ZERO)) { return '<$0.01'; diff --git a/packages/instant/src/util/gas_price_estimator.ts b/packages/instant/src/util/gas_price_estimator.ts index 332c8d00a..9792b60ba 100644 --- a/packages/instant/src/util/gas_price_estimator.ts +++ b/packages/instant/src/util/gas_price_estimator.ts @@ -35,7 +35,7 @@ const fetchFastAmountInWeiAsync = async (): Promise<GasInfo> => { const gasPriceInGwei = new BigNumber(gasInfo.fast / 10); // Time is in minutes const estimatedTimeMs = gasInfo.fastWait * 60 * 1000; // Minutes to MS - return { gasPriceInWei: gasPriceInGwei.mul(GWEI_IN_WEI), estimatedTimeMs }; + return { gasPriceInWei: gasPriceInGwei.multipliedBy(GWEI_IN_WEI), estimatedTimeMs }; }; export class GasPriceEstimator { diff --git a/packages/instant/src/util/maybe_big_number.ts b/packages/instant/src/util/maybe_big_number.ts index 9d3746e10..f48473389 100644 --- a/packages/instant/src/util/maybe_big_number.ts +++ b/packages/instant/src/util/maybe_big_number.ts @@ -18,7 +18,7 @@ export const maybeBigNumberUtil = { }, areMaybeBigNumbersEqual: (val1: Maybe<BigNumber>, val2: Maybe<BigNumber>): boolean => { if (!_.isUndefined(val1) && !_.isUndefined(val2)) { - return val1.equals(val2); + return val1.isEqualTo(val2); } return _.isUndefined(val1) && _.isUndefined(val2); }, |