fix token balance updating

This commit is contained in:
mmv 2019-04-15 15:40:02 +04:00
parent fc3e1e48df
commit 400a19f901
3 changed files with 24 additions and 14 deletions

View File

@ -16,12 +16,12 @@ const TIMEOUT = process.env.NODE_ENV === 'test' ? 1500 : 15000
class SafeView extends React.Component<Props> { class SafeView extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { const {
fetchSafe, loadActiveTokens, activeTokens, safeUrl, fetchTokenBalances, safe, fetchSafe, loadActiveTokens, activeTokens, safeUrl, fetchTokenBalances,
} = this.props } = this.props
fetchSafe(safeUrl) fetchSafe(safeUrl)
// loadActiveTokens(safeUrl) // loadActiveTokens(safeUrl)
fetchTokenBalances(safe, activeTokens) fetchTokenBalances(safeUrl, activeTokens)
this.intervalId = setInterval(() => { this.intervalId = setInterval(() => {
// update in another function so it uses up-to-date props values // update in another function so it uses up-to-date props values
@ -29,17 +29,25 @@ class SafeView extends React.Component<Props> {
}, TIMEOUT) }, TIMEOUT)
} }
componentDidUpdate(prevProps) {
const { activeTokens } = this.props
if (!activeTokens.equals(prevProps.activeTokens)) {
this.checkForUpdates()
}
}
componentWillUnmount() { componentWillUnmount() {
clearInterval(this.intervalId) clearInterval(this.intervalId)
} }
checkForUpdates() { checkForUpdates() {
const { const {
safeUrl, activeTokens, fetchSafe, fetchTokenBalances, safe, safeUrl, activeTokens, fetchSafe, fetchTokenBalances,
} = this.props } = this.props
fetchSafe(safeUrl, true) fetchSafe(safeUrl, true)
fetchTokenBalances(safe, activeTokens) fetchTokenBalances(safeUrl, activeTokens)
} }
intervalId: IntervalID intervalId: IntervalID

View File

@ -46,6 +46,7 @@ export default (safeAddress: string, update: boolean = false) => async (dispatch
const safeProps: SafeProps = await buildSafe(safeAddress, safeName) const safeProps: SafeProps = await buildSafe(safeAddress, safeName)
if (update) { if (update) {
console.log(safeProps)
dispatch(updateSafe(safeProps)) dispatch(updateSafe(safeProps))
} else { } else {
dispatch(addSafe(safeProps)) dispatch(addSafe(safeProps))

View File

@ -6,7 +6,6 @@ import { type GlobalState } from '~/store/index'
import { type Token } from '~/logic/tokens/store/model/token' import { type Token } from '~/logic/tokens/store/model/token'
import SafeTokenRecord from '~/routes/safe/store/models/safeToken' import SafeTokenRecord from '~/routes/safe/store/models/safeToken'
import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens' import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens'
import type { Safe } from '~/routes/safe/store/models/safe'
import updateSafe from './updateSafe' import updateSafe from './updateSafe'
import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers' import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers'
@ -28,22 +27,24 @@ export const calculateBalanceOf = async (tokenAddress: string, safeAddress: stri
return new BigNumber(balance).div(10 ** decimals).toString() return new BigNumber(balance).div(10 ** decimals).toString()
} }
const fetchTokenBalances = (safe: Safe, tokens: List<Token>) => async (dispatch: ReduxDispatch<GlobalState>) => { const fetchTokenBalances = (safeAddress: string, tokens: List<Token>) => async (
if (!safe || !tokens || !tokens.size) { dispatch: ReduxDispatch<GlobalState>,
) => {
if (!safeAddress || !tokens || !tokens.size) {
return return
} }
try { try {
const withBalances = await Promise.all( const withBalances = await Promise.all(
tokens.map(async token => SafeTokenRecord({ tokens
.filter(token => token.address !== ETH_ADDRESS)
.map(async token => SafeTokenRecord({
address: token.address, address: token.address,
balance: await calculateBalanceOf(token.address, safe.address, token.decimals), balance: await calculateBalanceOf(token.address, safeAddress, token.decimals),
})), })),
) )
const safeWithBalances = safe.set('tokens', List(withBalances)) dispatch(updateSafe({ address: safeAddress, tokens: List(withBalances) }))
dispatch(updateSafe(safeWithBalances))
} 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)