diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-10-27 06:20:04 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-10-27 06:20:04 +0800 |
commit | e1ae551560ad12501256fd5702dcd929baa84100 (patch) | |
tree | d02f498fe7a14222b18cef3476cb7aa8a54cbc5e | |
parent | 476cbbb6cb32db5ca72cc9d200b3ead152ae0e33 (diff) | |
download | dexon-sol-tools-e1ae551560ad12501256fd5702dcd929baa84100.tar.gz dexon-sol-tools-e1ae551560ad12501256fd5702dcd929baa84100.tar.zst dexon-sol-tools-e1ae551560ad12501256fd5702dcd929baa84100.zip |
move funct into util
-rw-r--r-- | packages/instant/src/containers/selected_erc20_asset_amount_input.ts | 7 | ||||
-rw-r--r-- | packages/instant/src/util/balance.ts | 30 |
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; + }, +}; |