diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-11-09 09:38:46 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-11-09 09:38:46 +0800 |
commit | a50f0ca997bf3ea09a4ac2c12e406385ba252eb7 (patch) | |
tree | 3172c4a04a06d85555ffc44d43d4e833c2d460b6 /packages/instant/src/redux/async_data.ts | |
parent | 39ae21d6939b43fbae0669219801b2bab140da9c (diff) | |
parent | 12bc6f5d5816a3c1c5ed03b23ce67a1be4e72f39 (diff) | |
download | dexon-sol-tools-a50f0ca997bf3ea09a4ac2c12e406385ba252eb7.tar dexon-sol-tools-a50f0ca997bf3ea09a4ac2c12e406385ba252eb7.tar.gz dexon-sol-tools-a50f0ca997bf3ea09a4ac2c12e406385ba252eb7.tar.bz2 dexon-sol-tools-a50f0ca997bf3ea09a4ac2c12e406385ba252eb7.tar.lz dexon-sol-tools-a50f0ca997bf3ea09a4ac2c12e406385ba252eb7.tar.xz dexon-sol-tools-a50f0ca997bf3ea09a4ac2c12e406385ba252eb7.tar.zst dexon-sol-tools-a50f0ca997bf3ea09a4ac2c12e406385ba252eb7.zip |
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/instant/metamask-connect-flow
Diffstat (limited to 'packages/instant/src/redux/async_data.ts')
-rw-r--r-- | packages/instant/src/redux/async_data.ts | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/packages/instant/src/redux/async_data.ts b/packages/instant/src/redux/async_data.ts index 839a90778..a8f632009 100644 --- a/packages/instant/src/redux/async_data.ts +++ b/packages/instant/src/redux/async_data.ts @@ -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) => { + 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; |