From cde192df0d48bb45f57c319179dbd9f64abd13e9 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Wed, 7 Nov 2018 23:36:00 -0800 Subject: feat(instant): fetch balance at startup --- packages/instant/src/redux/actions.ts | 2 ++ packages/instant/src/redux/async_data.ts | 16 ++++++++++++++++ packages/instant/src/redux/reducer.ts | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) (limited to 'packages/instant/src/redux') diff --git a/packages/instant/src/redux/actions.ts b/packages/instant/src/redux/actions.ts index 084360629..9b0d80152 100644 --- a/packages/instant/src/redux/actions.ts +++ b/packages/instant/src/redux/actions.ts @@ -25,6 +25,7 @@ export enum ActionTypes { 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_SELECTED_ASSET_AMOUNT = 'UPDATE_SELECTED_ASSET_AMOUNT', SET_BUY_ORDER_STATE_NONE = 'SET_BUY_ORDER_STATE_NONE', @@ -48,6 +49,7 @@ 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), 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 e27ec1dc3..862f60e78 100644 --- a/packages/instant/src/redux/async_data.ts +++ b/packages/instant/src/redux/async_data.ts @@ -50,8 +50,24 @@ export const asyncData = { if (!_.isEmpty(availableAddresses)) { const activeAddress = availableAddresses[0]; store.dispatch(actions.setAccountStateReady(activeAddress)); + await 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 ethBalanceInWei = await web3Wrapper.getBalanceInWeiAsync(account.address); + store.dispatch(actions.updateAccountEthBalance(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 9bbd114a2..961e29619 100644 --- a/packages/instant/src/redux/reducer.ts +++ b/packages/instant/src/redux/reducer.ts @@ -67,12 +67,25 @@ export const createReducer = (initialState: State) => { return reduceStateWithAccount(state, LOCKED_ACCOUNT); case ActionTypes.SET_ACCOUNT_STATE_ERROR: return reduceStateWithAccount(state, ERROR_ACCOUNT); - case ActionTypes.SET_ACCOUNT_STATE_READY: + 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 account = state.providerState.account; + if (account.state !== AccountState.Ready) { + return state; + } else { + const newAccount: AccountReady = { + ...account, + ethBalanceInWei: action.data, + }; + return reduceStateWithAccount(state, newAccount); + } + } case ActionTypes.UPDATE_ETH_USD_PRICE: return { ...state, -- cgit v1.2.3