aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--packages/instant/src/containers/selected_erc20_asset_amount_input.ts7
-rw-r--r--packages/instant/src/util/balance.ts30
2 files changed, 32 insertions, 5 deletions
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 3d0895aac..1fc5b6d8c 100644
--- a/packages/instant/src/containers/selected_erc20_asset_amount_input.ts
+++ b/packages/instant/src/containers/selected_erc20_asset_amount_input.ts
@@ -13,6 +13,7 @@ import { State } from '../redux/reducer';
import { ColorOption } from '../style/theme';
import { ERC20Asset, OrderProcessState, ZeroExInstantError } from '../types';
import { getBestAddress } from '../util/address';
+import { balanceUtil } from '../util/balance';
import { BigNumberInput } from '../util/big_number_input';
import { errorUtil } from '../util/error';
import { web3Wrapper } from '../util/web3_wrapper';
@@ -81,11 +82,7 @@ const updateBuyQuoteAsync = async (
// 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);
- }
+ await balanceUtil.checkSufficientBalanceAndFlashError(takerAddress, newBuyQuote, web3Wrapper, dispatch);
};
const debouncedUpdateBuyQuoteAsync = _.debounce(updateBuyQuoteAsync, 200, { trailing: true });
diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts
new file mode 100644
index 000000000..d442da9e0
--- /dev/null
+++ b/packages/instant/src/util/balance.ts
@@ -0,0 +1,30 @@
+import { BuyQuote } from '@0x/asset-buyer';
+import { Web3Wrapper } from '@0x/web3-wrapper';
+import { Dispatch } from 'redux';
+
+import { ZeroExInstantError } from '../types';
+
+import { errorUtil } from './error';
+
+export const balanceUtil = {
+ /**
+ * Checks to see if user has enough balance to buy assets
+ * If they do not, flash an error and return false
+ * If they do, return true
+ */
+ checkSufficientBalanceAndFlashError: async (
+ takerAddress: string,
+ buyQuote: BuyQuote,
+ web3Wrapper: Web3Wrapper,
+ dispatch: Dispatch,
+ ) => {
+ const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
+
+ if (balanceWei < buyQuote.worstCaseQuoteInfo.totalEthAmount) {
+ const balanceError = new Error(ZeroExInstantError.InsufficientBalance);
+ errorUtil.errorFlasher.flashNewError(dispatch, balanceError);
+ return false;
+ }
+ return true;
+ },
+};