aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-27 10:53:15 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-10-27 10:53:15 +0800
commitff295daa5c56b5c056a5faa5ca8875c317524070 (patch)
tree96e6ee707cf75e3272d90d44a8c8cf1b0567a5ce /packages/instant/src
parentbb307a55d347c34ab51144c75721860e13659ecb (diff)
downloaddexon-sol-tools-ff295daa5c56b5c056a5faa5ca8875c317524070.tar
dexon-sol-tools-ff295daa5c56b5c056a5faa5ca8875c317524070.tar.gz
dexon-sol-tools-ff295daa5c56b5c056a5faa5ca8875c317524070.tar.bz2
dexon-sol-tools-ff295daa5c56b5c056a5faa5ca8875c317524070.tar.lz
dexon-sol-tools-ff295daa5c56b5c056a5faa5ca8875c317524070.tar.xz
dexon-sol-tools-ff295daa5c56b5c056a5faa5ca8875c317524070.tar.zst
dexon-sol-tools-ff295daa5c56b5c056a5faa5ca8875c317524070.zip
Simpler way of validaitng has enough eth
Diffstat (limited to 'packages/instant/src')
-rw-r--r--packages/instant/src/components/buy_button.tsx13
-rw-r--r--packages/instant/src/components/buy_order_state_buttons.tsx12
-rw-r--r--packages/instant/src/containers/selected_asset_buy_order_state_buttons.ts20
-rw-r--r--packages/instant/src/containers/selected_erc20_asset_amount_input.ts4
-rw-r--r--packages/instant/src/types.ts4
-rw-r--r--packages/instant/src/util/balance.ts40
6 files changed, 37 insertions, 56 deletions
diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx
index 86c55dbf9..2f670d387 100644
--- a/packages/instant/src/components/buy_button.tsx
+++ b/packages/instant/src/components/buy_button.tsx
@@ -4,6 +4,7 @@ import * as React from 'react';
import { WEB_3_WRAPPER_TRANSACTION_FAILED_ERROR_MSG_PREFIX } from '../constants';
import { ColorOption } from '../style/theme';
+import { ZeroExInstantError } from '../types';
import { getBestAddress } from '../util/address';
import { balanceUtil } from '../util/balance';
import { util } from '../util/util';
@@ -14,12 +15,12 @@ import { Button, Text } from './ui';
export interface BuyButtonProps {
buyQuote?: BuyQuote;
assetBuyer?: AssetBuyer;
- onAwaitingSignature: (buyQuote: BuyQuote) => void;
+ onPendingValidation: (buyQuote: BuyQuote) => void;
+ onValidationFail: (buyQuote: BuyQuote, error: ZeroExInstantError) => void;
onSignatureDenied: (buyQuote: BuyQuote, preventedError: Error) => void;
onBuyProcessing: (buyQuote: BuyQuote, txHash: string) => void;
onBuySuccess: (buyQuote: BuyQuote, txHash: string) => void;
onBuyFailure: (buyQuote: BuyQuote, txHash: string) => void;
- validateWalletBeforeBuy: (buyQuote: BuyQuote, takerAddress?: string) => Promise<boolean>;
}
export class BuyButton extends React.Component<BuyButtonProps> {
@@ -45,14 +46,16 @@ export class BuyButton extends React.Component<BuyButtonProps> {
return;
}
+ this.props.onPendingValidation(buyQuote);
const takerAddress = await getBestAddress();
- const validWallet = await this.props.validateWalletBeforeBuy(buyQuote, takerAddress);
- if (!validWallet) {
+
+ const hasSufficientFunds = await balanceUtil.hasSufficientFunds(takerAddress, buyQuote, web3Wrapper);
+ if (!hasSufficientFunds) {
+ this.props.onValidationFail(buyQuote, ZeroExInstantError.InsufficientBalance);
return;
}
let txHash: string | undefined;
- this.props.onAwaitingSignature(buyQuote);
try {
txHash = await assetBuyer.executeBuyQuoteAsync(buyQuote, { takerAddress });
} catch (e) {
diff --git a/packages/instant/src/components/buy_order_state_buttons.tsx b/packages/instant/src/components/buy_order_state_buttons.tsx
index cb5654424..8225441f7 100644
--- a/packages/instant/src/components/buy_order_state_buttons.tsx
+++ b/packages/instant/src/components/buy_order_state_buttons.tsx
@@ -7,7 +7,7 @@ import { Flex } from '../components/ui/flex';
import { PlacingOrderButton } from '../components/placing_order_button';
import { ColorOption } from '../style/theme';
-import { OrderProcessState } from '../types';
+import { OrderProcessState, ZeroExInstantError } from '../types';
import { Button } from './ui/button';
import { Text } from './ui/text';
@@ -17,13 +17,13 @@ export interface BuyOrderStateButtonProps {
buyOrderProcessingState: OrderProcessState;
assetBuyer?: AssetBuyer;
onViewTransaction: () => void;
- onAwaitingSignature: (buyQuote: BuyQuote) => void;
+ onPendingValidation: (buyQuote: BuyQuote) => void;
onSignatureDenied: (buyQuote: BuyQuote, error: Error) => void;
onBuyProcessing: (buyQuote: BuyQuote, txHash: string) => void;
onBuySuccess: (buyQuote: BuyQuote, txHash: string) => void;
onBuyFailure: (buyQuote: BuyQuote, txHash: string) => void;
onRetry: () => void;
- validateWalletBeforeBuy: (buyQuote: BuyQuote, takerAddress: string | undefined) => Promise<boolean>;
+ onValidationFail: (buyQuote: BuyQuote, error: ZeroExInstantError) => void;
}
// TODO: rename to buttons
@@ -46,7 +46,7 @@ export const BuyOrderStateButtons: React.StatelessComponent<BuyOrderStateButtonP
props.buyOrderProcessingState === OrderProcessState.PROCESSING
) {
return <SecondaryButton onClick={props.onViewTransaction}>View Transaction</SecondaryButton>;
- } else if (props.buyOrderProcessingState === OrderProcessState.AWAITING_SIGNATURE) {
+ } else if (props.buyOrderProcessingState === OrderProcessState.VALIDATING) {
return <PlacingOrderButton />;
}
@@ -54,12 +54,12 @@ export const BuyOrderStateButtons: React.StatelessComponent<BuyOrderStateButtonP
<BuyButton
buyQuote={props.buyQuote}
assetBuyer={props.assetBuyer}
- onAwaitingSignature={props.onAwaitingSignature}
+ onPendingValidation={props.onPendingValidation}
onSignatureDenied={props.onSignatureDenied}
onBuyProcessing={props.onBuyProcessing}
onBuySuccess={props.onBuySuccess}
onBuyFailure={props.onBuyFailure}
- validateWalletBeforeBuy={props.validateWalletBeforeBuy}
+ onValidationFail={props.onValidationFail}
/>
);
};
diff --git a/packages/instant/src/containers/selected_asset_buy_order_state_buttons.ts b/packages/instant/src/containers/selected_asset_buy_order_state_buttons.ts
index 49b9cf209..dee724e15 100644
--- a/packages/instant/src/containers/selected_asset_buy_order_state_buttons.ts
+++ b/packages/instant/src/containers/selected_asset_buy_order_state_buttons.ts
@@ -6,12 +6,13 @@ import { Dispatch } from 'redux';
import { Action, actions } from '../redux/actions';
import { State } from '../redux/reducer';
-import { OrderProcessState, OrderState } from '../types';
+import { OrderProcessState, OrderState, ZeroExInstantError } from '../types';
import { balanceUtil } from '../util/balance';
import { etherscanUtil } from '../util/etherscan';
import { web3Wrapper } from '../util/web3_wrapper';
import { BuyOrderStateButtons } from '../components/buy_order_state_buttons';
+import { errorUtil } from '../util/error';
interface ConnectedState {
buyQuote?: BuyQuote;
@@ -21,13 +22,13 @@ interface ConnectedState {
}
interface ConnectedDispatch {
- onAwaitingSignature: (buyQuote: BuyQuote) => void;
+ onPendingValidation: (buyQuote: BuyQuote) => void;
onSignatureDenied: (buyQuote: BuyQuote, error: Error) => void;
onBuyProcessing: (buyQuote: BuyQuote, txHash: string) => void;
onBuySuccess: (buyQuote: BuyQuote, txHash: string) => void;
onBuyFailure: (buyQuote: BuyQuote, txHash: string) => void;
onRetry: () => void;
- validateWalletBeforeBuy: (buyQuote: BuyQuote, takerAddress: string | undefined) => Promise<boolean>;
+ onValidationFail: (buyQuote: BuyQuote, error: ZeroExInstantError) => void;
}
export interface SelectedAssetBuyOrderStateButtons {}
const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyOrderStateButtons): ConnectedState => ({
@@ -57,8 +58,8 @@ const mapDispatchToProps = (
dispatch: Dispatch<Action>,
ownProps: SelectedAssetBuyOrderStateButtons,
): ConnectedDispatch => ({
- onAwaitingSignature: (buyQuote: BuyQuote) => {
- const newOrderState: OrderState = { processState: OrderProcessState.AWAITING_SIGNATURE };
+ onPendingValidation: (buyQuote: BuyQuote) => {
+ const newOrderState: OrderState = { processState: OrderProcessState.VALIDATING };
dispatch(actions.updateBuyOrderState(newOrderState));
},
onBuyProcessing: (buyQuote: BuyQuote, txHash: string) => {
@@ -71,14 +72,15 @@ const mapDispatchToProps = (
dispatch(actions.updateBuyOrderState({ processState: OrderProcessState.FAILURE, txHash })),
onSignatureDenied: (buyQuote, error) => {
dispatch(actions.resetAmount());
- dispatch(actions.setError(error));
+ errorUtil.errorFlasher.flashNewError(dispatch, error);
+ },
+ onValidationFail: (buyQuote, error) => {
+ dispatch(actions.updateBuyOrderState({ processState: OrderProcessState.NONE }));
+ errorUtil.errorFlasher.flashNewError(dispatch, new Error(error));
},
onRetry: () => {
dispatch(actions.resetAmount());
},
- validateWalletBeforeBuy: async (buyQuote: BuyQuote, takerAddress: string | undefined) => {
- return balanceUtil.checkInsufficientEthBalanceAndFlashError(takerAddress, buyQuote, web3Wrapper, dispatch);
- },
});
export const SelectedAssetBuyOrderStateButtons: React.ComponentClass<SelectedAssetBuyOrderStateButtons> = connect(
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 325dc0ef2..a090ad8fb 100644
--- a/packages/instant/src/containers/selected_erc20_asset_amount_input.ts
+++ b/packages/instant/src/containers/selected_erc20_asset_amount_input.ts
@@ -79,10 +79,6 @@ 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();
- await balanceUtil.checkInsufficientEthBalanceAndFlashError(takerAddress, newBuyQuote, web3Wrapper, dispatch);
};
const debouncedUpdateBuyQuoteAsync = _.debounce(updateBuyQuoteAsync, 200, { trailing: true });
diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts
index 174ad86e6..9dd5624b6 100644
--- a/packages/instant/src/types.ts
+++ b/packages/instant/src/types.ts
@@ -10,14 +10,14 @@ export enum AsyncProcessState {
export enum OrderProcessState {
NONE = 'None',
- AWAITING_SIGNATURE = 'Awaiting Signature',
+ VALIDATING = 'Validating',
PROCESSING = 'Processing',
SUCCESS = 'Success',
FAILURE = 'Failure',
}
interface OrderStatePreTx {
- processState: OrderProcessState.NONE | OrderProcessState.AWAITING_SIGNATURE;
+ processState: OrderProcessState.NONE | OrderProcessState.VALIDATING;
}
interface OrderStatePostTx {
processState: OrderProcessState.PROCESSING | OrderProcessState.SUCCESS | OrderProcessState.FAILURE;
diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts
index 954dc7156..9cb8b8987 100644
--- a/packages/instant/src/util/balance.ts
+++ b/packages/instant/src/util/balance.ts
@@ -1,38 +1,18 @@
import { BuyQuote } from '@0x/asset-buyer';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as _ from 'lodash';
-import { Dispatch } from 'redux';
-
-import { ZeroExInstantError } from '../types';
-
-import { errorUtil } from './error';
-
-const hasSufficientFunds = async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => {
- if (_.isUndefined(takerAddress)) {
- return false;
- }
- const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
- return balanceWei >= buyQuote.worstCaseQuoteInfo.totalEthAmount;
-};
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
- */
- checkInsufficientEthBalanceAndFlashError: async (
- takerAddress: string | undefined,
- buyQuote: BuyQuote,
- web3Wrapper: Web3Wrapper,
- dispatch: Dispatch,
- ) => {
- const hasEnoughFunds = await hasSufficientFunds(takerAddress, buyQuote, web3Wrapper);
- if (hasEnoughFunds) {
- return true;
+ hasSufficientFunds: async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => {
+ if (_.isUndefined(takerAddress)) {
+ return false;
}
- const balanceError = new Error(ZeroExInstantError.InsufficientBalance);
- errorUtil.errorFlasher.flashNewError(dispatch, balanceError);
- return false;
+ const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
+ console.log('balanceWei', balanceWei.toString());
+ console.log(
+ 'buyQuote.worstCaseQuoteInfo.totalEthAmount',
+ buyQuote.worstCaseQuoteInfo.totalEthAmount.toString(),
+ );
+ return balanceWei >= buyQuote.worstCaseQuoteInfo.totalEthAmount;
},
};