From b147cd888505443981781d200a68b949812cb3e9 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Thu, 8 Nov 2018 17:22:21 -0800 Subject: fix(instant): fix bug where we potentially fetch balance for the wrong account --- packages/instant/src/redux/actions.ts | 5 +++-- packages/instant/src/redux/async_data.ts | 8 +++++--- packages/instant/src/redux/reducer.ts | 9 +++++---- packages/instant/src/types.ts | 5 +++++ 4 files changed, 18 insertions(+), 9 deletions(-) (limited to 'packages') diff --git a/packages/instant/src/redux/actions.ts b/packages/instant/src/redux/actions.ts index 9b0d80152..fc89e3d0e 100644 --- a/packages/instant/src/redux/actions.ts +++ b/packages/instant/src/redux/actions.ts @@ -2,7 +2,7 @@ import { BuyQuote } from '@0x/asset-buyer'; import { BigNumber } from '@0x/utils'; import * as _ from 'lodash'; -import { ActionsUnion, Asset } from '../types'; +import { ActionsUnion, AddressAndEthBalanceInWei, Asset } from '../types'; export interface PlainAction { type: T; @@ -49,7 +49,8 @@ export const actions = { 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: (balance: BigNumber) => createAction(ActionTypes.UPDATE_ACCOUNT_ETH_BALANCE, balance), + updateAccountEthBalance: (addressAndBalance: AddressAndEthBalanceInWei) => + createAction(ActionTypes.UPDATE_ACCOUNT_ETH_BALANCE, addressAndBalance), updateEthUsdPrice: (price?: BigNumber) => createAction(ActionTypes.UPDATE_ETH_USD_PRICE, price), updateSelectedAssetAmount: (amount?: BigNumber) => createAction(ActionTypes.UPDATE_SELECTED_ASSET_AMOUNT, amount), setBuyOrderStateNone: () => createAction(ActionTypes.SET_BUY_ORDER_STATE_NONE), diff --git a/packages/instant/src/redux/async_data.ts b/packages/instant/src/redux/async_data.ts index bff9d8814..a8f632009 100644 --- a/packages/instant/src/redux/async_data.ts +++ b/packages/instant/src/redux/async_data.ts @@ -52,7 +52,8 @@ export const asyncData = { if (!_.isEmpty(availableAddresses)) { const activeAddress = availableAddresses[0]; store.dispatch(actions.setAccountStateReady(activeAddress)); - await asyncData.fetchAccountBalanceAndDispatchToStore(store); + // tslint:disable-next-line:no-floating-promises + asyncData.fetchAccountBalanceAndDispatchToStore(store); } else { store.dispatch(actions.setAccountStateLocked()); } @@ -65,8 +66,9 @@ export const asyncData = { return; } try { - const ethBalanceInWei = await web3Wrapper.getBalanceInWeiAsync(account.address); - store.dispatch(actions.updateAccountEthBalance(ethBalanceInWei)); + const address = account.address; + const ethBalanceInWei = await web3Wrapper.getBalanceInWeiAsync(address); + store.dispatch(actions.updateAccountEthBalance({ address, ethBalanceInWei })); } catch (e) { // leave balance as is return; diff --git a/packages/instant/src/redux/reducer.ts b/packages/instant/src/redux/reducer.ts index 961e29619..a5a1b6f7d 100644 --- a/packages/instant/src/redux/reducer.ts +++ b/packages/instant/src/redux/reducer.ts @@ -75,13 +75,14 @@ export const createReducer = (initialState: State) => { return reduceStateWithAccount(state, account); } case ActionTypes.UPDATE_ACCOUNT_ETH_BALANCE: { - const account = state.providerState.account; - if (account.state !== AccountState.Ready) { + const { address, ethBalanceInWei } = action.data; + const currentAccount = state.providerState.account; + if (currentAccount.state !== AccountState.Ready || currentAccount.address !== address) { return state; } else { const newAccount: AccountReady = { - ...account, - ethBalanceInWei: action.data, + ...currentAccount, + ethBalanceInWei, }; return reduceStateWithAccount(state, newAccount); } diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts index d65f70008..20ad2ed95 100644 --- a/packages/instant/src/types.ts +++ b/packages/instant/src/types.ts @@ -120,3 +120,8 @@ export interface AccountNotReady { export type Account = AccountReady | AccountNotReady; export type OrderSource = string | SignedOrder[]; + +export interface AddressAndEthBalanceInWei { + address: string; + ethBalanceInWei: BigNumber; +} -- cgit v1.2.3