Merge pull request #1232 from 0xProject/feature/instant/account-state-change
[instant] Request account address and balance at mount
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer';
|
import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer';
|
||||||
|
import { BigNumber } from '@0x/utils';
|
||||||
import { Web3Wrapper } from '@0x/web3-wrapper';
|
import { Web3Wrapper } from '@0x/web3-wrapper';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
@@ -7,16 +8,17 @@ import { oc } from 'ts-optchain';
|
|||||||
import { WEB_3_WRAPPER_TRANSACTION_FAILED_ERROR_MSG_PREFIX } from '../constants';
|
import { WEB_3_WRAPPER_TRANSACTION_FAILED_ERROR_MSG_PREFIX } from '../constants';
|
||||||
import { ColorOption } from '../style/theme';
|
import { ColorOption } from '../style/theme';
|
||||||
import { AffiliateInfo, ZeroExInstantError } from '../types';
|
import { AffiliateInfo, ZeroExInstantError } from '../types';
|
||||||
import { getBestAddress } from '../util/address';
|
|
||||||
import { balanceUtil } from '../util/balance';
|
|
||||||
import { gasPriceEstimator } from '../util/gas_price_estimator';
|
import { gasPriceEstimator } from '../util/gas_price_estimator';
|
||||||
import { util } from '../util/util';
|
import { util } from '../util/util';
|
||||||
|
|
||||||
import { Button } from './ui/button';
|
import { Button } from './ui/button';
|
||||||
|
|
||||||
export interface BuyButtonProps {
|
export interface BuyButtonProps {
|
||||||
|
accountAddress?: string;
|
||||||
|
accountEthBalanceInWei?: BigNumber;
|
||||||
buyQuote?: BuyQuote;
|
buyQuote?: BuyQuote;
|
||||||
assetBuyer: AssetBuyer;
|
assetBuyer: AssetBuyer;
|
||||||
|
web3Wrapper: Web3Wrapper;
|
||||||
affiliateInfo?: AffiliateInfo;
|
affiliateInfo?: AffiliateInfo;
|
||||||
onValidationPending: (buyQuote: BuyQuote) => void;
|
onValidationPending: (buyQuote: BuyQuote) => void;
|
||||||
onValidationFail: (buyQuote: BuyQuote, errorMessage: AssetBuyerError | ZeroExInstantError) => void;
|
onValidationFail: (buyQuote: BuyQuote, errorMessage: AssetBuyerError | ZeroExInstantError) => void;
|
||||||
@@ -33,7 +35,8 @@ export class BuyButton extends React.Component<BuyButtonProps> {
|
|||||||
onBuyFailure: util.boundNoop,
|
onBuyFailure: util.boundNoop,
|
||||||
};
|
};
|
||||||
public render(): React.ReactNode {
|
public render(): React.ReactNode {
|
||||||
const shouldDisableButton = _.isUndefined(this.props.buyQuote);
|
const { buyQuote, accountAddress } = this.props;
|
||||||
|
const shouldDisableButton = _.isUndefined(buyQuote) || _.isUndefined(accountAddress);
|
||||||
return (
|
return (
|
||||||
<Button
|
<Button
|
||||||
width="100%"
|
width="100%"
|
||||||
@@ -48,30 +51,25 @@ export class BuyButton extends React.Component<BuyButtonProps> {
|
|||||||
}
|
}
|
||||||
private readonly _handleClick = async () => {
|
private readonly _handleClick = async () => {
|
||||||
// The button is disabled when there is no buy quote anyway.
|
// The button is disabled when there is no buy quote anyway.
|
||||||
const { buyQuote, assetBuyer, affiliateInfo } = this.props;
|
const { buyQuote, assetBuyer, affiliateInfo, accountAddress, accountEthBalanceInWei, web3Wrapper } = this.props;
|
||||||
if (_.isUndefined(buyQuote)) {
|
if (_.isUndefined(buyQuote) || _.isUndefined(accountAddress)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.props.onValidationPending(buyQuote);
|
this.props.onValidationPending(buyQuote);
|
||||||
|
const ethNeededForBuy = buyQuote.worstCaseQuoteInfo.totalEthAmount;
|
||||||
// TODO(bmillman): move address and balance fetching to the async state
|
// if we don't have a balance for the user, let the transaction through, it will be handled by the wallet
|
||||||
const web3Wrapper = new Web3Wrapper(assetBuyer.provider);
|
const hasSufficientEth = _.isUndefined(accountEthBalanceInWei) || accountEthBalanceInWei.gte(ethNeededForBuy);
|
||||||
const takerAddress = await getBestAddress(web3Wrapper);
|
|
||||||
|
|
||||||
const hasSufficientEth = await balanceUtil.hasSufficientEth(takerAddress, buyQuote, web3Wrapper);
|
|
||||||
if (!hasSufficientEth) {
|
if (!hasSufficientEth) {
|
||||||
this.props.onValidationFail(buyQuote, ZeroExInstantError.InsufficientETH);
|
this.props.onValidationFail(buyQuote, ZeroExInstantError.InsufficientETH);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let txHash: string | undefined;
|
let txHash: string | undefined;
|
||||||
const gasInfo = await gasPriceEstimator.getGasInfoAsync();
|
const gasInfo = await gasPriceEstimator.getGasInfoAsync();
|
||||||
const feeRecipient = oc(affiliateInfo).feeRecipient();
|
const feeRecipient = oc(affiliateInfo).feeRecipient();
|
||||||
try {
|
try {
|
||||||
txHash = await assetBuyer.executeBuyQuoteAsync(buyQuote, {
|
txHash = await assetBuyer.executeBuyQuoteAsync(buyQuote, {
|
||||||
feeRecipient,
|
feeRecipient,
|
||||||
takerAddress,
|
takerAddress: accountAddress,
|
||||||
gasPrice: gasInfo.gasPriceInWei,
|
gasPrice: gasInfo.gasPriceInWei,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -86,7 +84,6 @@ export class BuyButton extends React.Component<BuyButtonProps> {
|
|||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
const startTimeUnix = new Date().getTime();
|
const startTimeUnix = new Date().getTime();
|
||||||
const expectedEndTimeUnix = startTimeUnix + gasInfo.estimatedTimeMs;
|
const expectedEndTimeUnix = startTimeUnix + gasInfo.estimatedTimeMs;
|
||||||
this.props.onBuyProcessing(buyQuote, txHash, startTimeUnix, expectedEndTimeUnix);
|
this.props.onBuyProcessing(buyQuote, txHash, startTimeUnix, expectedEndTimeUnix);
|
||||||
@@ -99,7 +96,6 @@ export class BuyButton extends React.Component<BuyButtonProps> {
|
|||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.props.onBuySuccess(buyQuote, txHash);
|
this.props.onBuySuccess(buyQuote, txHash);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer';
|
import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer';
|
||||||
|
import { BigNumber } from '@0x/utils';
|
||||||
|
import { Web3Wrapper } from '@0x/web3-wrapper';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
|
||||||
import { ColorOption } from '../style/theme';
|
import { ColorOption } from '../style/theme';
|
||||||
@@ -12,9 +14,12 @@ import { Button } from './ui/button';
|
|||||||
import { Flex } from './ui/flex';
|
import { Flex } from './ui/flex';
|
||||||
|
|
||||||
export interface BuyOrderStateButtonProps {
|
export interface BuyOrderStateButtonProps {
|
||||||
|
accountAddress?: string;
|
||||||
|
accountEthBalanceInWei?: BigNumber;
|
||||||
buyQuote?: BuyQuote;
|
buyQuote?: BuyQuote;
|
||||||
buyOrderProcessingState: OrderProcessState;
|
buyOrderProcessingState: OrderProcessState;
|
||||||
assetBuyer: AssetBuyer;
|
assetBuyer: AssetBuyer;
|
||||||
|
web3Wrapper: Web3Wrapper;
|
||||||
affiliateInfo?: AffiliateInfo;
|
affiliateInfo?: AffiliateInfo;
|
||||||
onViewTransaction: () => void;
|
onViewTransaction: () => void;
|
||||||
onValidationPending: (buyQuote: BuyQuote) => void;
|
onValidationPending: (buyQuote: BuyQuote) => void;
|
||||||
@@ -49,8 +54,11 @@ export const BuyOrderStateButtons: React.StatelessComponent<BuyOrderStateButtonP
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<BuyButton
|
<BuyButton
|
||||||
|
accountAddress={props.accountAddress}
|
||||||
|
accountEthBalanceInWei={props.accountEthBalanceInWei}
|
||||||
buyQuote={props.buyQuote}
|
buyQuote={props.buyQuote}
|
||||||
assetBuyer={props.assetBuyer}
|
assetBuyer={props.assetBuyer}
|
||||||
|
web3Wrapper={props.web3Wrapper}
|
||||||
affiliateInfo={props.affiliateInfo}
|
affiliateInfo={props.affiliateInfo}
|
||||||
onValidationPending={props.onValidationPending}
|
onValidationPending={props.onValidationPending}
|
||||||
onValidationFail={props.onValidationFail}
|
onValidationFail={props.onValidationFail}
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider
|
|||||||
asyncData.fetchAvailableAssetDatasAndDispatchToStore(this._store);
|
asyncData.fetchAvailableAssetDatasAndDispatchToStore(this._store);
|
||||||
}
|
}
|
||||||
// tslint:disable-next-line:no-floating-promises
|
// tslint:disable-next-line:no-floating-promises
|
||||||
|
asyncData.fetchAccountInfoAndDispatchToStore(this._store);
|
||||||
|
// tslint:disable-next-line:no-floating-promises
|
||||||
asyncData.fetchCurrentBuyQuoteAndDispatchToStore(this._store);
|
asyncData.fetchCurrentBuyQuoteAndDispatchToStore(this._store);
|
||||||
// warm up the gas price estimator cache just in case we can't
|
// warm up the gas price estimator cache just in case we can't
|
||||||
// grab the gas price estimate when submitting the transaction
|
// grab the gas price estimate when submitting the transaction
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { BigNumber } from '@0x/utils';
|
import { BigNumber } from '@0x/utils';
|
||||||
|
|
||||||
import { Network } from './types';
|
import { AccountNotReady, AccountState, Network } from './types';
|
||||||
|
|
||||||
export const BIG_NUMBER_ZERO = new BigNumber(0);
|
export const BIG_NUMBER_ZERO = new BigNumber(0);
|
||||||
export const ETH_DECIMALS = 18;
|
export const ETH_DECIMALS = 18;
|
||||||
@@ -22,3 +22,15 @@ export const ETHEREUM_NODE_URL_BY_NETWORK = {
|
|||||||
[Network.Kovan]: 'https://kovan.infura.io/',
|
[Network.Kovan]: 'https://kovan.infura.io/',
|
||||||
};
|
};
|
||||||
export const BLOCK_POLLING_INTERVAL_MS = 10000; // 10s
|
export const BLOCK_POLLING_INTERVAL_MS = 10000; // 10s
|
||||||
|
export const NO_ACCOUNT: AccountNotReady = {
|
||||||
|
state: AccountState.None,
|
||||||
|
};
|
||||||
|
export const LOADING_ACCOUNT: AccountNotReady = {
|
||||||
|
state: AccountState.Loading,
|
||||||
|
};
|
||||||
|
export const LOCKED_ACCOUNT: AccountNotReady = {
|
||||||
|
state: AccountState.Locked,
|
||||||
|
};
|
||||||
|
export const ERROR_ACCOUNT: AccountNotReady = {
|
||||||
|
state: AccountState.Error,
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer';
|
import { AssetBuyer, AssetBuyerError, BuyQuote } from '@0x/asset-buyer';
|
||||||
|
import { BigNumber } from '@0x/utils';
|
||||||
|
import { Web3Wrapper } from '@0x/web3-wrapper';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
@@ -7,14 +9,17 @@ import { Dispatch } from 'redux';
|
|||||||
import { BuyOrderStateButtons } from '../components/buy_order_state_buttons';
|
import { BuyOrderStateButtons } from '../components/buy_order_state_buttons';
|
||||||
import { Action, actions } from '../redux/actions';
|
import { Action, actions } from '../redux/actions';
|
||||||
import { State } from '../redux/reducer';
|
import { State } from '../redux/reducer';
|
||||||
import { AffiliateInfo, OrderProcessState, ZeroExInstantError } from '../types';
|
import { AccountState, AffiliateInfo, OrderProcessState, ZeroExInstantError } from '../types';
|
||||||
import { errorFlasher } from '../util/error_flasher';
|
import { errorFlasher } from '../util/error_flasher';
|
||||||
import { etherscanUtil } from '../util/etherscan';
|
import { etherscanUtil } from '../util/etherscan';
|
||||||
|
|
||||||
interface ConnectedState {
|
interface ConnectedState {
|
||||||
|
accountAddress?: string;
|
||||||
|
accountEthBalanceInWei?: BigNumber;
|
||||||
buyQuote?: BuyQuote;
|
buyQuote?: BuyQuote;
|
||||||
buyOrderProcessingState: OrderProcessState;
|
buyOrderProcessingState: OrderProcessState;
|
||||||
assetBuyer: AssetBuyer;
|
assetBuyer: AssetBuyer;
|
||||||
|
web3Wrapper: Web3Wrapper;
|
||||||
affiliateInfo?: AffiliateInfo;
|
affiliateInfo?: AffiliateInfo;
|
||||||
onViewTransaction: () => void;
|
onViewTransaction: () => void;
|
||||||
}
|
}
|
||||||
@@ -31,9 +36,16 @@ interface ConnectedDispatch {
|
|||||||
export interface SelectedAssetBuyOrderStateButtons {}
|
export interface SelectedAssetBuyOrderStateButtons {}
|
||||||
const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyOrderStateButtons): ConnectedState => {
|
const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyOrderStateButtons): ConnectedState => {
|
||||||
const assetBuyer = state.providerState.assetBuyer;
|
const assetBuyer = state.providerState.assetBuyer;
|
||||||
|
const web3Wrapper = state.providerState.web3Wrapper;
|
||||||
|
const account = state.providerState.account;
|
||||||
|
const accountAddress = account.state === AccountState.Ready ? account.address : undefined;
|
||||||
|
const accountEthBalanceInWei = account.state === AccountState.Ready ? account.ethBalanceInWei : undefined;
|
||||||
return {
|
return {
|
||||||
|
accountAddress,
|
||||||
|
accountEthBalanceInWei,
|
||||||
buyOrderProcessingState: state.buyOrderState.processState,
|
buyOrderProcessingState: state.buyOrderState.processState,
|
||||||
assetBuyer,
|
assetBuyer,
|
||||||
|
web3Wrapper,
|
||||||
buyQuote: state.latestBuyQuote,
|
buyQuote: state.latestBuyQuote,
|
||||||
affiliateInfo: state.affiliateInfo,
|
affiliateInfo: state.affiliateInfo,
|
||||||
onViewTransaction: () => {
|
onViewTransaction: () => {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { BuyQuote } from '@0x/asset-buyer';
|
|||||||
import { BigNumber } from '@0x/utils';
|
import { BigNumber } from '@0x/utils';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import { ActionsUnion, Asset } from '../types';
|
import { ActionsUnion, AddressAndEthBalanceInWei, Asset } from '../types';
|
||||||
|
|
||||||
export interface PlainAction<T extends string> {
|
export interface PlainAction<T extends string> {
|
||||||
type: T;
|
type: T;
|
||||||
@@ -21,6 +21,11 @@ function createAction<T extends string, P>(type: T, data?: P): PlainAction<T> |
|
|||||||
}
|
}
|
||||||
|
|
||||||
export enum ActionTypes {
|
export enum ActionTypes {
|
||||||
|
SET_ACCOUNT_STATE_LOADING = 'SET_ACCOUNT_STATE_LOADING',
|
||||||
|
SET_ACCOUNT_STATE_LOCKED = 'SET_ACCOUNT_STATE_LOCKED',
|
||||||
|
SET_ACCOUNT_STATE_ERROR = 'SET_ACCOUNT_STATE_ERROR',
|
||||||
|
SET_ACCOUNT_STATE_READY = 'SET_ACCOUNT_STATE_READY',
|
||||||
|
UPDATE_ACCOUNT_ETH_BALANCE = 'UPDATE_ACCOUNT_ETH_BALANCE',
|
||||||
UPDATE_ETH_USD_PRICE = 'UPDATE_ETH_USD_PRICE',
|
UPDATE_ETH_USD_PRICE = 'UPDATE_ETH_USD_PRICE',
|
||||||
UPDATE_SELECTED_ASSET_AMOUNT = 'UPDATE_SELECTED_ASSET_AMOUNT',
|
UPDATE_SELECTED_ASSET_AMOUNT = 'UPDATE_SELECTED_ASSET_AMOUNT',
|
||||||
SET_BUY_ORDER_STATE_NONE = 'SET_BUY_ORDER_STATE_NONE',
|
SET_BUY_ORDER_STATE_NONE = 'SET_BUY_ORDER_STATE_NONE',
|
||||||
@@ -40,6 +45,12 @@ export enum ActionTypes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
|
setAccountStateLoading: () => createAction(ActionTypes.SET_ACCOUNT_STATE_LOADING),
|
||||||
|
setAccountStateLocked: () => createAction(ActionTypes.SET_ACCOUNT_STATE_LOCKED),
|
||||||
|
setAccountStateError: () => createAction(ActionTypes.SET_ACCOUNT_STATE_ERROR),
|
||||||
|
setAccountStateReady: (address: string) => createAction(ActionTypes.SET_ACCOUNT_STATE_READY, address),
|
||||||
|
updateAccountEthBalance: (addressAndBalance: AddressAndEthBalanceInWei) =>
|
||||||
|
createAction(ActionTypes.UPDATE_ACCOUNT_ETH_BALANCE, addressAndBalance),
|
||||||
updateEthUsdPrice: (price?: BigNumber) => createAction(ActionTypes.UPDATE_ETH_USD_PRICE, price),
|
updateEthUsdPrice: (price?: BigNumber) => createAction(ActionTypes.UPDATE_ETH_USD_PRICE, price),
|
||||||
updateSelectedAssetAmount: (amount?: BigNumber) => createAction(ActionTypes.UPDATE_SELECTED_ASSET_AMOUNT, amount),
|
updateSelectedAssetAmount: (amount?: BigNumber) => createAction(ActionTypes.UPDATE_SELECTED_ASSET_AMOUNT, amount),
|
||||||
setBuyOrderStateNone: () => createAction(ActionTypes.SET_BUY_ORDER_STATE_NONE),
|
setBuyOrderStateNone: () => createAction(ActionTypes.SET_BUY_ORDER_STATE_NONE),
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { AssetProxyId } from '@0x/types';
|
|||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import { BIG_NUMBER_ZERO } from '../constants';
|
import { BIG_NUMBER_ZERO } from '../constants';
|
||||||
import { ERC20Asset } from '../types';
|
import { AccountState, ERC20Asset } from '../types';
|
||||||
import { assetUtils } from '../util/asset';
|
import { assetUtils } from '../util/asset';
|
||||||
import { buyQuoteUpdater } from '../util/buy_quote_updater';
|
import { buyQuoteUpdater } from '../util/buy_quote_updater';
|
||||||
import { coinbaseApi } from '../util/coinbase_api';
|
import { coinbaseApi } from '../util/coinbase_api';
|
||||||
@@ -36,6 +36,44 @@ export const asyncData = {
|
|||||||
store.dispatch(actions.setAvailableAssets([]));
|
store.dispatch(actions.setAvailableAssets([]));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
fetchAccountInfoAndDispatchToStore: async (store: Store) => {
|
||||||
|
const { providerState } = store.getState();
|
||||||
|
const web3Wrapper = providerState.web3Wrapper;
|
||||||
|
if (providerState.account.state !== AccountState.Loading) {
|
||||||
|
store.dispatch(actions.setAccountStateLoading());
|
||||||
|
}
|
||||||
|
let availableAddresses: string[];
|
||||||
|
try {
|
||||||
|
availableAddresses = await web3Wrapper.getAvailableAddressesAsync();
|
||||||
|
} catch (e) {
|
||||||
|
store.dispatch(actions.setAccountStateError());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_.isEmpty(availableAddresses)) {
|
||||||
|
const activeAddress = availableAddresses[0];
|
||||||
|
store.dispatch(actions.setAccountStateReady(activeAddress));
|
||||||
|
// tslint:disable-next-line:no-floating-promises
|
||||||
|
asyncData.fetchAccountBalanceAndDispatchToStore(store);
|
||||||
|
} else {
|
||||||
|
store.dispatch(actions.setAccountStateLocked());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
fetchAccountBalanceAndDispatchToStore: async (store: Store) => {
|
||||||
|
const { providerState } = store.getState();
|
||||||
|
const web3Wrapper = providerState.web3Wrapper;
|
||||||
|
const account = providerState.account;
|
||||||
|
if (account.state !== AccountState.Ready) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const address = account.address;
|
||||||
|
const ethBalanceInWei = await web3Wrapper.getBalanceInWeiAsync(address);
|
||||||
|
store.dispatch(actions.updateAccountEthBalance({ address, ethBalanceInWei }));
|
||||||
|
} catch (e) {
|
||||||
|
// leave balance as is
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
},
|
||||||
fetchCurrentBuyQuoteAndDispatchToStore: async (store: Store) => {
|
fetchCurrentBuyQuoteAndDispatchToStore: async (store: Store) => {
|
||||||
const { providerState, selectedAsset, selectedAssetAmount, affiliateInfo } = store.getState();
|
const { providerState, selectedAsset, selectedAssetAmount, affiliateInfo } = store.getState();
|
||||||
const assetBuyer = providerState.assetBuyer;
|
const assetBuyer = providerState.assetBuyer;
|
||||||
|
|||||||
@@ -4,8 +4,12 @@ import { BigNumber } from '@0x/utils';
|
|||||||
import { Web3Wrapper } from '@0x/web3-wrapper';
|
import { Web3Wrapper } from '@0x/web3-wrapper';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
|
import { ERROR_ACCOUNT, LOADING_ACCOUNT, LOCKED_ACCOUNT } from '../constants';
|
||||||
import { assetMetaDataMap } from '../data/asset_meta_data_map';
|
import { assetMetaDataMap } from '../data/asset_meta_data_map';
|
||||||
import {
|
import {
|
||||||
|
Account,
|
||||||
|
AccountReady,
|
||||||
|
AccountState,
|
||||||
AffiliateInfo,
|
AffiliateInfo,
|
||||||
Asset,
|
Asset,
|
||||||
AssetMetaData,
|
AssetMetaData,
|
||||||
@@ -57,6 +61,32 @@ export const DEFAULT_STATE: DefaultState = {
|
|||||||
export const createReducer = (initialState: State) => {
|
export const createReducer = (initialState: State) => {
|
||||||
const reducer = (state: State = initialState, action: Action): State => {
|
const reducer = (state: State = initialState, action: Action): State => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
|
case ActionTypes.SET_ACCOUNT_STATE_LOADING:
|
||||||
|
return reduceStateWithAccount(state, LOADING_ACCOUNT);
|
||||||
|
case ActionTypes.SET_ACCOUNT_STATE_LOCKED:
|
||||||
|
return reduceStateWithAccount(state, LOCKED_ACCOUNT);
|
||||||
|
case ActionTypes.SET_ACCOUNT_STATE_ERROR:
|
||||||
|
return reduceStateWithAccount(state, ERROR_ACCOUNT);
|
||||||
|
case ActionTypes.SET_ACCOUNT_STATE_READY: {
|
||||||
|
const account: AccountReady = {
|
||||||
|
state: AccountState.Ready,
|
||||||
|
address: action.data,
|
||||||
|
};
|
||||||
|
return reduceStateWithAccount(state, account);
|
||||||
|
}
|
||||||
|
case ActionTypes.UPDATE_ACCOUNT_ETH_BALANCE: {
|
||||||
|
const { address, ethBalanceInWei } = action.data;
|
||||||
|
const currentAccount = state.providerState.account;
|
||||||
|
if (currentAccount.state !== AccountState.Ready || currentAccount.address !== address) {
|
||||||
|
return state;
|
||||||
|
} else {
|
||||||
|
const newAccount: AccountReady = {
|
||||||
|
...currentAccount,
|
||||||
|
ethBalanceInWei,
|
||||||
|
};
|
||||||
|
return reduceStateWithAccount(state, newAccount);
|
||||||
|
}
|
||||||
|
}
|
||||||
case ActionTypes.UPDATE_ETH_USD_PRICE:
|
case ActionTypes.UPDATE_ETH_USD_PRICE:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -80,7 +110,6 @@ export const createReducer = (initialState: State) => {
|
|||||||
} else {
|
} else {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
case ActionTypes.SET_QUOTE_REQUEST_STATE_PENDING:
|
case ActionTypes.SET_QUOTE_REQUEST_STATE_PENDING:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@@ -191,6 +220,18 @@ export const createReducer = (initialState: State) => {
|
|||||||
return reducer;
|
return reducer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const reduceStateWithAccount = (state: State, account: Account) => {
|
||||||
|
const oldProviderState = state.providerState;
|
||||||
|
const newProviderState: ProviderState = {
|
||||||
|
...oldProviderState,
|
||||||
|
account,
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
providerState: newProviderState,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const doesBuyQuoteMatchState = (buyQuote: BuyQuote, state: State): boolean => {
|
const doesBuyQuoteMatchState = (buyQuote: BuyQuote, state: State): boolean => {
|
||||||
const selectedAssetIfExists = state.selectedAsset;
|
const selectedAssetIfExists = state.selectedAsset;
|
||||||
const selectedAssetAmountIfExists = state.selectedAssetAmount;
|
const selectedAssetAmountIfExists = state.selectedAssetAmount;
|
||||||
|
|||||||
@@ -120,3 +120,8 @@ export interface AccountNotReady {
|
|||||||
export type Account = AccountReady | AccountNotReady;
|
export type Account = AccountReady | AccountNotReady;
|
||||||
|
|
||||||
export type OrderSource = string | SignedOrder[];
|
export type OrderSource = string | SignedOrder[];
|
||||||
|
|
||||||
|
export interface AddressAndEthBalanceInWei {
|
||||||
|
address: string;
|
||||||
|
ethBalanceInWei: BigNumber;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
import { BuyQuote } from '@0x/asset-buyer';
|
|
||||||
import { Web3Wrapper } from '@0x/web3-wrapper';
|
|
||||||
import * as _ from 'lodash';
|
|
||||||
|
|
||||||
export const balanceUtil = {
|
|
||||||
hasSufficientEth: async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => {
|
|
||||||
if (_.isUndefined(takerAddress)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
|
|
||||||
return balanceWei.gte(buyQuote.worstCaseQuoteInfo.totalEthAmount);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -2,18 +2,12 @@ import { Web3Wrapper } from '@0x/web3-wrapper';
|
|||||||
import { Provider } from 'ethereum-types';
|
import { Provider } from 'ethereum-types';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import { AccountNotReady, AccountState, Maybe, Network, OrderSource, ProviderState } from '../types';
|
import { LOADING_ACCOUNT, NO_ACCOUNT } from '../constants';
|
||||||
|
import { Maybe, Network, OrderSource, ProviderState } from '../types';
|
||||||
|
|
||||||
import { assetBuyerFactory } from './asset_buyer_factory';
|
import { assetBuyerFactory } from './asset_buyer_factory';
|
||||||
import { providerFactory } from './provider_factory';
|
import { providerFactory } from './provider_factory';
|
||||||
|
|
||||||
const LOADING_ACCOUNT: AccountNotReady = {
|
|
||||||
state: AccountState.Loading,
|
|
||||||
};
|
|
||||||
const NO_ACCOUNT: AccountNotReady = {
|
|
||||||
state: AccountState.None,
|
|
||||||
};
|
|
||||||
|
|
||||||
export const providerStateFactory = {
|
export const providerStateFactory = {
|
||||||
getInitialProviderState: (orderSource: OrderSource, network: Network, provider?: Provider): ProviderState => {
|
getInitialProviderState: (orderSource: OrderSource, network: Network, provider?: Provider): ProviderState => {
|
||||||
if (!_.isUndefined(provider)) {
|
if (!_.isUndefined(provider)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user