From 56c6892651f1e4bd967d399c091a1bcf37c4731f Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Tue, 9 Apr 2019 19:53:41 +0400 Subject: [PATCH] split fetching tokens function --- src/logic/tokens/store/actions/fetchTokens.js | 46 +++++-------------- src/logic/tokens/store/actions/saveTokens.js | 13 +----- .../safe/component/Balances/Tokens/actions.js | 7 ++- .../safe/component/Balances/Tokens/index.jsx | 6 +++ src/routes/safe/container/actions.js | 3 -- 5 files changed, 23 insertions(+), 52 deletions(-) diff --git a/src/logic/tokens/store/actions/fetchTokens.js b/src/logic/tokens/store/actions/fetchTokens.js index 164f7b06..0c4a205c 100644 --- a/src/logic/tokens/store/actions/fetchTokens.js +++ b/src/logic/tokens/store/actions/fetchTokens.js @@ -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) => { - const tokens: List = 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 = 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 = 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 diff --git a/src/logic/tokens/store/actions/saveTokens.js b/src/logic/tokens/store/actions/saveTokens.js index f2399b85..0fca88b8 100644 --- a/src/logic/tokens/store/actions/saveTokens.js +++ b/src/logic/tokens/store/actions/saveTokens.js @@ -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, @@ -25,13 +23,4 @@ export const addTokens = createAction( }), ) -const saveTokens = (safeAddress: string, tokens: Map) => async ( - dispatch: ReduxDispatch, -) => { - dispatch(addTokens(safeAddress, tokens)) - - const activeAddresses: List = calculateActiveErc20TokensFrom(tokens.toList()) - await setTokensOnce(safeAddress, activeAddresses) -} - -export default saveTokens +export default addTokens diff --git a/src/routes/safe/component/Balances/Tokens/actions.js b/src/routes/safe/component/Balances/Tokens/actions.js index 971349a5..dfede044 100644 --- a/src/routes/safe/component/Balances/Tokens/actions.js +++ b/src/routes/safe/component/Balances/Tokens/actions.js @@ -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, } diff --git a/src/routes/safe/component/Balances/Tokens/index.jsx b/src/routes/safe/component/Balances/Tokens/index.jsx index f43251d9..8504902b 100644 --- a/src/routes/safe/component/Balances/Tokens/index.jsx +++ b/src/routes/safe/component/Balances/Tokens/index.jsx @@ -51,6 +51,12 @@ class Tokens extends React.Component { filter: '', } + componentDidMount() { + const { fetchTokens } = this.props + + fetchTokens() + } + onCancelSearch = () => { this.setState(() => ({ filter: '' })) } diff --git a/src/routes/safe/container/actions.js b/src/routes/safe/container/actions.js index 21b11b96..3fb578b6 100644 --- a/src/routes/safe/container/actions.js +++ b/src/routes/safe/container/actions.js @@ -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, }