-
Notifications
You must be signed in to change notification settings - Fork 467
[instant] Request account address and balance at mount #1232
Changes from all commits
d0c009a
cde192d
c84e163
211163b
b147cd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import { AssetProxyId } from '@0x/types'; | |
import * as _ from 'lodash'; | ||
|
||
import { BIG_NUMBER_ZERO } from '../constants'; | ||
import { ERC20Asset } from '../types'; | ||
import { AccountState, ERC20Asset } from '../types'; | ||
import { assetUtils } from '../util/asset'; | ||
import { buyQuoteUpdater } from '../util/buy_quote_updater'; | ||
import { coinbaseApi } from '../util/coinbase_api'; | ||
|
@@ -36,6 +36,44 @@ export const asyncData = { | |
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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that we'd have a concurrency issue in which we'd dispatch a balance for an account that they have since changed? I feel like we may need a similar check here as we have for the buy quote. Alternatively, part of me feels like balance fetching could be paired with address fetching. This could be easier to reason about -- we always get the address and the balance together, or the whole thing is considered "failed". I'm not married to this though, what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are correct wrt to the balance update coming late issue |
||
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) => { | ||
const { providerState, selectedAsset, selectedAssetAmount, affiliateInfo } = store.getState(); | ||
const assetBuyer = providerState.assetBuyer; | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super nit (not necessary to change) but I'd prefer for this to follow the
return
pattern above, i.e.