balance fetch fixes

This commit is contained in:
Mikhail Mikheev 2019-04-11 18:12:27 +04:00
parent fe35b811c4
commit f94e598609
5 changed files with 17 additions and 34 deletions

View File

@ -7,7 +7,7 @@ import { type Token } from '~/logic/tokens/store/model/token'
import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers' import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers'
import { getBalanceInEtherOf } from '~/logic/wallets/getWeb3' import { getBalanceInEtherOf } from '~/logic/wallets/getWeb3'
import { getStandardTokenContract } from './fetchTokens' import { getStandardTokenContract } from './fetchTokens'
import { addTokens } from './saveTokens' import saveTokens from './saveTokens'
export const calculateBalanceOf = async (tokenAddress: string, safeAddress: string, decimals: number = 18) => { export const calculateBalanceOf = async (tokenAddress: string, safeAddress: string, decimals: number = 18) => {
if (tokenAddress === ETH_ADDRESS) { if (tokenAddress === ETH_ADDRESS) {
@ -37,14 +37,14 @@ const fetchTokenBalances = (safeAddress: string, tokens: List<Token>) => async (
try { try {
const withBalances = await Promise.all( const withBalances = await Promise.all(
tokens.map(async token => token.set('funds', await calculateBalanceOf(token.address, safeAddress, token.decimals))), tokens.map(async token => token.set('balance', await calculateBalanceOf(token.address, safeAddress, token.decimals))),
) )
const tokensMap = Map().withMutations((map) => { const tokensMap = Map().withMutations((map) => {
withBalances.forEach(token => map.set(token.address, token)) withBalances.forEach(token => map.set(token.address, token))
}) })
dispatch(addTokens(safeAddress, tokensMap)) dispatch(saveTokens(safeAddress, tokensMap))
} catch (err) { } catch (err) {
// eslint-disable-next-line // eslint-disable-next-line
console.error('Error while loading active tokens from storage:', err) console.error('Error while loading active tokens from storage:', err)

View File

@ -15,7 +15,7 @@ type TokenProps = {
tokens: Map<string, Token>, tokens: Map<string, Token>,
} }
export const addTokens = createAction<string, *, *>( const addTokens = createAction<string, *, *>(
ADD_TOKENS, ADD_TOKENS,
(safeAddress: string, tokens: Map<string, Token>): TokenProps => ({ (safeAddress: string, tokens: Map<string, Token>): TokenProps => ({
safeAddress, safeAddress,

View File

@ -1,27 +1,15 @@
// @flow // @flow
import { List, Map } from 'immutable' import { List, Map } from 'immutable'
import { createSelector, type Selector } from 'reselect' import { createSelector, type Selector } from 'reselect'
import { safeParamAddressSelector, type RouterProps } from '~/routes/safe/store/selectors' import { type RouterProps } from '~/routes/safe/store/selectors'
import { type GlobalState } from '~/store' import { type GlobalState } from '~/store'
import { TOKEN_REDUCER_ID } from '~/logic/tokens/store/reducer/tokens' import { TOKEN_REDUCER_ID } from '~/logic/tokens/store/reducer/tokens'
import { type Token } from '~/logic/tokens/store/model/token' import { type Token } from '~/logic/tokens/store/model/token'
const tokensStateSelector = (state: GlobalState) => state[TOKEN_REDUCER_ID] export const tokensSelector = (state: GlobalState) => state[TOKEN_REDUCER_ID]
export const tokensSelector: Selector<GlobalState, RouterProps, Map<string, Token>> = createSelector(
tokensStateSelector,
safeParamAddressSelector,
(tokens: Map<string, Map<string, Token>>, address: string) => {
if (!address) {
return Map()
}
return tokens.get(address) || Map()
},
)
export const tokenListSelector: Selector<GlobalState, Map<string, Token>, List<Token>> = createSelector( export const tokenListSelector: Selector<GlobalState, Map<string, Token>, List<Token>> = createSelector(
tokensStateSelector, tokensSelector,
(tokens: Map<string, Token>) => tokens.toList(), (tokens: Map<string, Token>) => tokens.toList(),
) )
@ -29,12 +17,3 @@ export const orderedTokenListSelector: Selector<GlobalState, RouterProps, List<T
tokenListSelector, tokenListSelector,
(tokens: List<Token>) => tokens.sortBy((token: Token) => token.get('symbol')), (tokens: List<Token>) => tokens.sortBy((token: Token) => token.get('symbol')),
) )
export const tokenAddressesSelector: Selector<GlobalState, RouterProps, List<string>> = createSelector(
tokenListSelector,
(tokens: List<Token>) => {
const addresses = List().withMutations(list => tokens.map(token => list.push(token.address)))
return addresses
},
)

View File

@ -17,9 +17,9 @@ export type BalanceRow = SortRow<BalanceData>
export const getBalanceData = (activeTokens: List<Token>): Array<BalanceRow> => { export const getBalanceData = (activeTokens: List<Token>): Array<BalanceRow> => {
const rows = activeTokens.map((token: Token) => ({ const rows = activeTokens.map((token: Token) => ({
[BALANCE_TABLE_ASSET_ID]: token.get('name'), [BALANCE_TABLE_ASSET_ID]: token.name,
[BALANCE_TABLE_BALANCE_ID]: `${token.get('funds')} ${token.get('symbol')}`, [BALANCE_TABLE_BALANCE_ID]: `${token.balance} ${token.symbol}`,
[buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)]: Number(token.get('funds')), [buildOrderFieldFrom(BALANCE_TABLE_BALANCE_ID)]: Number(token.balance),
[FIXED]: token.get('symbol') === 'ETH', [FIXED]: token.get('symbol') === 'ETH',
})) }))

View File

@ -47,11 +47,15 @@ export const grantedSelector: Selector<GlobalState, RouterProps, boolean> = crea
}, },
) )
type UserToken = {
address: string,
balance: string,
}
const extendedSafeTokensSelector: Selector<GlobalState, RouterProps, List<Token>> = createSelector( const extendedSafeTokensSelector: Selector<GlobalState, RouterProps, List<Token>> = createSelector(
safeTokensSelector, safeTokensSelector,
tokensSelector, tokensSelector,
(safeTokens: Map<string, string>, tokensList: Map<string, Token>) => { (safeTokens: List<UserToken>, tokensList: Map<string, Token>) => {
// const extendedTokens = safeTokens.map(token => tokensList.get(token.address).set('balance', token.balance))
const extendedTokens = Map().withMutations((map) => { const extendedTokens = Map().withMutations((map) => {
safeTokens.forEach((token: { address: string, balance: string }) => { safeTokens.forEach((token: { address: string, balance: string }) => {
const baseToken = tokensList.get(token.address) const baseToken = tokensList.get(token.address)
@ -62,7 +66,7 @@ const extendedSafeTokensSelector: Selector<GlobalState, RouterProps, List<Token>
}) })
}) })
return extendedTokens return extendedTokens.toList()
}, },
) )