From 9512978de9aee1953930d4f16a105ec5ef7f048e Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 26 Oct 2018 12:43:44 -0700 Subject: feat(instant): Show message if user doesn't have enough ETH --- .../instant/src/containers/selected_asset_amount_input.ts | 14 +++++++++++++- packages/instant/src/types.ts | 1 + packages/instant/src/util/address.ts | 6 ++++++ packages/instant/src/util/error.ts | 5 ++++- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 packages/instant/src/util/address.ts (limited to 'packages/instant') diff --git a/packages/instant/src/containers/selected_asset_amount_input.ts b/packages/instant/src/containers/selected_asset_amount_input.ts index e9dbc61ce..504f51535 100644 --- a/packages/instant/src/containers/selected_asset_amount_input.ts +++ b/packages/instant/src/containers/selected_asset_amount_input.ts @@ -10,11 +10,15 @@ import { Dispatch } from 'redux'; import { Action, actions } from '../redux/actions'; import { State } from '../redux/reducer'; import { ColorOption } from '../style/theme'; -import { ERC20Asset, OrderProcessState } from '../types'; +import { ERC20Asset, OrderProcessState, ZeroExInstantError } from '../types'; +import { getBestAddress } from '../util/address'; import { errorUtil } from '../util/error'; +import { web3Wrapper } from '../util/web3_wrapper'; import { AssetAmountInput } from '../components/asset_amount_input'; +import { ETH_DECIMALS } from '../constants'; + export interface SelectedAssetAmountInputProps { fontColor?: ColorOption; fontSize?: string; @@ -76,6 +80,14 @@ const updateBuyQuoteAsync = async ( errorUtil.errorFlasher.clearError(dispatch); // invalidate the last buy quote. dispatch(actions.updateLatestBuyQuote(newBuyQuote)); + + // set error if user doesn't have appropriate balance + const takerAddress = await getBestAddress(); + const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress); + if (balanceWei < newBuyQuote.worstCaseQuoteInfo.totalEthAmount) { + const balanceError = new Error(ZeroExInstantError.InsufficientBalance); + errorUtil.errorFlasher.flashNewError(dispatch, balanceError); + } }; const debouncedUpdateBuyQuoteAsync = _.debounce(updateBuyQuoteAsync, 200, { trailing: true }); diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts index c63371fb4..174ad86e6 100644 --- a/packages/instant/src/types.ts +++ b/packages/instant/src/types.ts @@ -72,4 +72,5 @@ export enum Network { export enum ZeroExInstantError { AssetMetaDataNotAvailable = 'ASSET_META_DATA_NOT_AVAILABLE', + InsufficientBalance = 'INSUFFICIENT_BALANCE', } diff --git a/packages/instant/src/util/address.ts b/packages/instant/src/util/address.ts new file mode 100644 index 000000000..97ed30a00 --- /dev/null +++ b/packages/instant/src/util/address.ts @@ -0,0 +1,6 @@ +import { web3Wrapper } from '../util/web3_wrapper'; + +export const getBestAddress = async (): Promise => { + const addresses = await web3Wrapper.getAvailableAddressesAsync(); + return addresses[0]; +}; diff --git a/packages/instant/src/util/error.ts b/packages/instant/src/util/error.ts index 64c1f4885..5db0c66d2 100644 --- a/packages/instant/src/util/error.ts +++ b/packages/instant/src/util/error.ts @@ -2,7 +2,7 @@ import { AssetBuyerError } from '@0x/asset-buyer'; import { Dispatch } from 'redux'; import { Action, actions } from '../redux/actions'; -import { Asset } from '../types'; +import { Asset, ZeroExInstantError } from '../types'; import { assetUtils } from './asset'; @@ -49,6 +49,9 @@ const humanReadableMessageForError = (error: Error, asset?: Asset): string | und if (error.message === AssetBuyerError.SignatureRequestDenied) { return 'You denied this transaction'; } + if (error.message === ZeroExInstantError.InsufficientBalance) { + return "You don't have enough ETH"; + } return undefined; }; -- cgit