Validate enough ETH when user clicks buy

This commit is contained in:
Steve Klebanoff
2018-10-26 15:51:23 -07:00
parent 6ad8ac6a48
commit 667b1e03dd
3 changed files with 16 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import * as React from 'react';
import { WEB_3_WRAPPER_TRANSACTION_FAILED_ERROR_MSG_PREFIX } from '../constants';
import { ColorOption } from '../style/theme';
import { getBestAddress } from '../util/address';
import { balanceUtil } from '../util/balance';
import { util } from '../util/util';
import { web3Wrapper } from '../util/web3_wrapper';
@@ -18,6 +19,7 @@ export interface BuyButtonProps {
onBuyProcessing: (buyQuote: BuyQuote, txHash: string) => void;
onBuySuccess: (buyQuote: BuyQuote, txHash: string) => void;
onBuyFailure: (buyQuote: BuyQuote, txHash: string) => void;
validateWalletBeforeBuy: (buyQuote: BuyQuote, takerAddress: string | undefined) => Promise<boolean>;
}
export class BuyButton extends React.Component<BuyButtonProps> {
@@ -43,10 +45,15 @@ export class BuyButton extends React.Component<BuyButtonProps> {
return;
}
const takerAddress = await getBestAddress();
const validWallet = await this.props.validateWalletBeforeBuy(buyQuote, takerAddress);
if (!validWallet) {
return;
}
let txHash: string | undefined;
this.props.onAwaitingSignature(buyQuote);
try {
const takerAddress = await getBestAddress();
txHash = await assetBuyer.executeBuyQuoteAsync(buyQuote, { takerAddress });
} catch (e) {
if (e instanceof Error && e.message === AssetBuyerError.SignatureRequestDenied) {

View File

@@ -23,6 +23,7 @@ export interface BuyOrderStateButtonProps {
onBuySuccess: (buyQuote: BuyQuote, txHash: string) => void;
onBuyFailure: (buyQuote: BuyQuote, txHash: string) => void;
onRetry: () => void;
validateWalletBeforeBuy: (buyQuote: BuyQuote, takerAddress: string | undefined) => Promise<boolean>;
}
// TODO: rename to buttons
@@ -58,6 +59,7 @@ export const BuyOrderStateButtons: React.StatelessComponent<BuyOrderStateButtonP
onBuyProcessing={props.onBuyProcessing}
onBuySuccess={props.onBuySuccess}
onBuyFailure={props.onBuyFailure}
validateWalletBeforeBuy={props.validateWalletBeforeBuy}
/>
);
};

View File

@@ -7,7 +7,9 @@ import { Dispatch } from 'redux';
import { Action, actions } from '../redux/actions';
import { State } from '../redux/reducer';
import { OrderProcessState, OrderState } 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';
@@ -25,6 +27,7 @@ interface ConnectedDispatch {
onBuySuccess: (buyQuote: BuyQuote, txHash: string) => void;
onBuyFailure: (buyQuote: BuyQuote, txHash: string) => void;
onRetry: () => void;
validateWalletBeforeBuy: (buyQuote: BuyQuote, takerAddress: string | undefined) => Promise<boolean>;
}
export interface SelectedAssetBuyOrderStateButtons {}
const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyOrderStateButtons): ConnectedState => ({
@@ -73,6 +76,9 @@ const mapDispatchToProps = (
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(