split fetching tokens function
This commit is contained in:
parent
1125536412
commit
56c6892651
|
@ -1,5 +1,5 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { List, Map } from 'immutable'
|
import { Map } from 'immutable'
|
||||||
import contract from 'truffle-contract'
|
import contract from 'truffle-contract'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import type { Dispatch as ReduxDispatch } from 'redux'
|
import type { Dispatch as ReduxDispatch } from 'redux'
|
||||||
|
@ -9,8 +9,6 @@ import { getWeb3 } from '~/logic/wallets/getWeb3'
|
||||||
import { type GlobalState } from '~/store/index'
|
import { type GlobalState } from '~/store/index'
|
||||||
import { makeToken, type Token, type TokenProps } from '~/logic/tokens/store/model/token'
|
import { makeToken, type Token, type TokenProps } from '~/logic/tokens/store/model/token'
|
||||||
import { ensureOnce } from '~/utils/singleton'
|
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 saveTokens from './saveTokens'
|
||||||
import { getRelayUrl } from '~/config/index'
|
import { getRelayUrl } from '~/config/index'
|
||||||
|
|
||||||
|
@ -33,7 +31,7 @@ export const getHumanFriendlyToken = ensureOnce(createHumanFriendlyTokenContract
|
||||||
|
|
||||||
export const getStandardTokenContract = ensureOnce(createStandardTokenContract)
|
export const getStandardTokenContract = ensureOnce(createStandardTokenContract)
|
||||||
|
|
||||||
export const fetchTokensData = async () => {
|
const fetchTokenList = async () => {
|
||||||
const apiUrl = getRelayUrl()
|
const apiUrl = getRelayUrl()
|
||||||
const url = `${apiUrl}/tokens`
|
const url = `${apiUrl}/tokens`
|
||||||
const errMsg = 'Error querying safe balances'
|
const errMsg = 'Error querying safe balances'
|
||||||
|
@ -41,44 +39,22 @@ export const fetchTokensData = async () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const fetchTokens = (safeAddress: string) => async (dispatch: ReduxDispatch<GlobalState>) => {
|
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 {
|
try {
|
||||||
const balancesRecords = await Promise.all(
|
const {
|
||||||
results.map(async (item: TokenProps) => {
|
data: { results: tokenList },
|
||||||
const status = tokens.findIndex(activeToken => activeToken.name === item.name) !== -1
|
} = await fetchTokenList()
|
||||||
const funds = status ? await calculateBalanceOf(item.address, safeAddress, item.decimals) : '0'
|
|
||||||
|
|
||||||
return makeToken({ ...item, status, funds })
|
const tokensMap: Map<string, Token> = Map().withMutations((map) => {
|
||||||
}),
|
tokenList.forEach((token: TokenProps) => map.set(token.address, makeToken(token)))
|
||||||
)
|
|
||||||
|
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return dispatch(saveTokens(safeAddress, balances))
|
dispatch(saveTokens(safeAddress, tokensMap))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
console.log('Error fetching tokens... ' + err)
|
console.log('Error fetching token list ' + err)
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default fetchTokens
|
||||||
|
|
|
@ -10,8 +10,6 @@ import { calculateActiveErc20TokensFrom } from '~/logic/tokens/utils/tokenHelper
|
||||||
|
|
||||||
export const ADD_TOKENS = 'ADD_TOKENS'
|
export const ADD_TOKENS = 'ADD_TOKENS'
|
||||||
|
|
||||||
const setTokensOnce = ensureOnceAsync(setActiveTokens)
|
|
||||||
|
|
||||||
type TokenProps = {
|
type TokenProps = {
|
||||||
safeAddress: string,
|
safeAddress: string,
|
||||||
tokens: Map<string, Token>,
|
tokens: Map<string, Token>,
|
||||||
|
@ -25,13 +23,4 @@ export const addTokens = createAction<string, *, *>(
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
const saveTokens = (safeAddress: string, tokens: Map<string, Token>) => async (
|
export default addTokens
|
||||||
dispatch: ReduxDispatch<GlobalState>,
|
|
||||||
) => {
|
|
||||||
dispatch(addTokens(safeAddress, tokens))
|
|
||||||
|
|
||||||
const activeAddresses: List<Token> = calculateActiveErc20TokensFrom(tokens.toList())
|
|
||||||
await setTokensOnce(safeAddress, activeAddresses)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default saveTokens
|
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
// @flow
|
// @flow
|
||||||
import enableToken from '~/logic/tokens/store/actions/enableToken'
|
import enableToken from '~/logic/tokens/store/actions/enableToken'
|
||||||
import disableToken from '~/logic/tokens/store/actions/disableToken'
|
import disableToken from '~/logic/tokens/store/actions/disableToken'
|
||||||
|
import fetchTokens from '~/logic/tokens/store/actions/fetchTokens'
|
||||||
|
|
||||||
export type Actions = {
|
export type Actions = {
|
||||||
enableToken: typeof enableToken,
|
enableToken: Function,
|
||||||
disableToken: typeof disableToken,
|
disableToken: Function,
|
||||||
|
fetchTokens: Function
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
enableToken,
|
enableToken,
|
||||||
disableToken,
|
disableToken,
|
||||||
|
fetchTokens,
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,6 +51,12 @@ class Tokens extends React.Component<Props, State> {
|
||||||
filter: '',
|
filter: '',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
const { fetchTokens } = this.props
|
||||||
|
|
||||||
|
fetchTokens()
|
||||||
|
}
|
||||||
|
|
||||||
onCancelSearch = () => {
|
onCancelSearch = () => {
|
||||||
this.setState(() => ({ filter: '' }))
|
this.setState(() => ({ filter: '' }))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,16 @@
|
||||||
// @flow
|
// @flow
|
||||||
import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
|
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 loadActiveTokens from '~/logic/tokens/store/actions/loadActiveTokens'
|
||||||
import fetchTokenBalances from '~/logic/tokens/store/actions/fetchTokenBalances'
|
import fetchTokenBalances from '~/logic/tokens/store/actions/fetchTokenBalances'
|
||||||
|
|
||||||
export type Actions = {
|
export type Actions = {
|
||||||
fetchSafe: typeof fetchSafe,
|
fetchSafe: typeof fetchSafe,
|
||||||
fetchTokens: typeof fetchTokens,
|
|
||||||
loadActiveTokens: typeof loadActiveTokens,
|
loadActiveTokens: typeof loadActiveTokens,
|
||||||
fetchTokenBalances: typeof fetchTokenBalances
|
fetchTokenBalances: typeof fetchTokenBalances
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
fetchSafe,
|
fetchSafe,
|
||||||
fetchTokens,
|
|
||||||
loadActiveTokens,
|
loadActiveTokens,
|
||||||
fetchTokenBalances,
|
fetchTokenBalances,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue