split fetching tokens function

This commit is contained in:
Mikhail Mikheev 2019-04-09 19:53:41 +04:00
parent 1125536412
commit 56c6892651
5 changed files with 23 additions and 52 deletions

View File

@ -1,5 +1,5 @@
// @flow
import { List, Map } from 'immutable'
import { Map } from 'immutable'
import contract from 'truffle-contract'
import axios from 'axios'
import type { Dispatch as ReduxDispatch } from 'redux'
@ -9,8 +9,6 @@ import { getWeb3 } from '~/logic/wallets/getWeb3'
import { type GlobalState } from '~/store/index'
import { makeToken, type Token, type TokenProps } from '~/logic/tokens/store/model/token'
import { ensureOnce } from '~/utils/singleton'
import { getActiveTokens, getTokens } from '~/logic/tokens/utils/tokensStorage'
import { getEthAsToken } from '~/logic/tokens/utils/tokenHelpers'
import saveTokens from './saveTokens'
import { getRelayUrl } from '~/config/index'
@ -33,7 +31,7 @@ export const getHumanFriendlyToken = ensureOnce(createHumanFriendlyTokenContract
export const getStandardTokenContract = ensureOnce(createStandardTokenContract)
export const fetchTokensData = async () => {
const fetchTokenList = async () => {
const apiUrl = getRelayUrl()
const url = `${apiUrl}/tokens`
const errMsg = 'Error querying safe balances'
@ -41,44 +39,22 @@ export const fetchTokensData = async () => {
}
export const fetchTokens = (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
const tokens: List<TokenProps> = await getActiveTokens(safeAddress)
const ethBalance = await getEthAsToken(safeAddress)
const customTokens = await getTokens(safeAddress)
const {
data: { results },
} = await fetchTokensData()
try {
const balancesRecords = await Promise.all(
results.map(async (item: TokenProps) => {
const status = tokens.findIndex(activeToken => activeToken.name === item.name) !== -1
const funds = status ? await calculateBalanceOf(item.address, safeAddress, item.decimals) : '0'
const {
data: { results: tokenList },
} = await fetchTokenList()
return makeToken({ ...item, status, funds })
}),
)
const customTokenRecords = await Promise.all(
customTokens.map(async (item: TokenProps) => {
const status = tokens.findIndex(activeToken => activeToken.name === item.name) !== -1
const funds = status ? await calculateBalanceOf(item.address, safeAddress, item.decimals) : '0'
return makeToken({ ...item, status, funds })
}),
)
const balances: Map<string, Token> = Map().withMutations((map) => {
balancesRecords.forEach(record => map.set(record.address, record))
customTokenRecords.forEach(record => map.set(record.address, record))
map.set(ethBalance.address, ethBalance)
const tokensMap: Map<string, Token> = Map().withMutations((map) => {
tokenList.forEach((token: TokenProps) => map.set(token.address, makeToken(token)))
})
return dispatch(saveTokens(safeAddress, balances))
dispatch(saveTokens(safeAddress, tokensMap))
} catch (err) {
// eslint-disable-next-line
console.log('Error fetching tokens... ' + err)
console.log('Error fetching token list ' + err)
return Promise.resolve()
}
}
export default fetchTokens

View File

@ -10,8 +10,6 @@ import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelper
export const ADD_TOKENS = 'ADD_TOKENS'
const setTokensOnce = ensureOnceAsync(setActiveTokens)
type TokenProps = {
safeAddress: string,
tokens: Map<string, Token>,
@ -25,13 +23,4 @@ export const addTokens = createAction<string, *, *>(
}),
)
const saveTokens = (safeAddress: string, tokens: Map<string, Token>) => async (
dispatch: ReduxDispatch<GlobalState>,
) => {
dispatch(addTokens(safeAddress, tokens))
const activeAddresses: List<Token> = calculateActiveErc20TokensFrom(tokens.toList())
await setTokensOnce(safeAddress, activeAddresses)
}
export default saveTokens
export default addTokens

View File

@ -1,13 +1,16 @@
// @flow
import enableToken from '~/logic/tokens/store/actions/enableToken'
import disableToken from '~/logic/tokens/store/actions/disableToken'
import fetchTokens from '~/logic/tokens/store/actions/fetchTokens'
export type Actions = {
enableToken: typeof enableToken,
disableToken: typeof disableToken,
enableToken: Function,
disableToken: Function,
fetchTokens: Function
}
export default {
enableToken,
disableToken,
fetchTokens,
}

View File

@ -51,6 +51,12 @@ class Tokens extends React.Component<Props, State> {
filter: '',
}
componentDidMount() {
const { fetchTokens } = this.props
fetchTokens()
}
onCancelSearch = () => {
this.setState(() => ({ filter: '' }))
}

View File

@ -1,19 +1,16 @@
// @flow
import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
import { fetchTokens } from '~/logic/tokens/store/actions/fetchTokens'
import loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'
import fetchTokenBalances from '~/logic/tokens/store/actions/fetchTokenBalances'
export type Actions = {
fetchSafe: typeof fetchSafe,
fetchTokens: typeof fetchTokens,
loadActiveTokens: typeof loadActiveTokens,
fetchTokenBalances: typeof fetchTokenBalances
}
export default {
fetchSafe,
fetchTokens,
loadActiveTokens,
fetchTokenBalances,
}