tokens balance refactor wip

This commit is contained in:
Mikhail Mikheev 2019-04-11 19:44:48 +04:00
parent f94e598609
commit be295d1ef5
5 changed files with 19 additions and 18 deletions

View File

@ -1,7 +1,7 @@
// @flow // @flow
import fetchSafe from '~/routes/safe/store/actions/fetchSafe' import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
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 '~/routes/safe/store/actions/fetchTokenBalances'
export type Actions = { export type Actions = {
fetchSafe: typeof fetchSafe, fetchSafe: typeof fetchSafe,

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, fetchSafe, loadActiveTokens, activeTokens, safeUrl, fetchTokenBalances, safe,
} = this.props } = this.props
fetchSafe(safeUrl) fetchSafe(safeUrl)
loadActiveTokens(safeUrl) loadActiveTokens(safeUrl)
fetchTokenBalances(safeUrl, activeTokens) fetchTokenBalances(safe, 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
@ -35,11 +35,11 @@ class SafeView extends React.Component<Props> {
checkForUpdates() { checkForUpdates() {
const { const {
safeUrl, activeTokens, fetchSafe, fetchTokenBalances, safeUrl, activeTokens, fetchSafe, fetchTokenBalances, safe,
} = this.props } = this.props
fetchSafe(safeUrl) fetchSafe(safeUrl)
fetchTokenBalances(safeUrl, activeTokens) fetchTokenBalances(safe, activeTokens)
} }
intervalId: IntervalID intervalId: IntervalID

View File

@ -6,8 +6,9 @@ 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 { 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 '~/logic/tokens/store/actions/fetchTokens'
import saveTokens from './saveTokens' import type { Safe } from '~/routes/safe/store/model/safe'
import updateSafe from './updateSafe'
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) {
@ -28,23 +29,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 = (safeAddress: string, tokens: List<Token>) => async ( const fetchTokenBalances = (safe: Safe, tokens: List<Token>) => async (
dispatch: ReduxDispatch<GlobalState>, dispatch: ReduxDispatch<GlobalState>,
) => { ) => {
if (!safeAddress || !tokens || !tokens.size) { if (!safe || !tokens || !tokens.size) {
return return
} }
try { try {
const withBalances = await Promise.all( const withBalances = await Promise.all(
tokens.map(async token => token.set('balance', await calculateBalanceOf(token.address, safeAddress, token.decimals))), tokens.map(async token => ({
address: token.address,
balance: await calculateBalanceOf(token.address, safe.address, token.decimals),
})),
) )
const tokensMap = Map().withMutations((map) => { const safeWithBalances = safe.set('tokens', List(withBalances))
withBalances.forEach(token => map.set(token.address, token))
})
dispatch(saveTokens(safeAddress, tokensMap)) 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)

View File

@ -9,7 +9,7 @@ export type SafeProps = {
address: string, address: string,
threshold: number, threshold: number,
owners: List<Owner>, owners: List<Owner>,
tokens?: List<Map<string, string>>, tokens?: List<Object>,
} }
const SafeRecord: RecordFactory<SafeProps> = Record({ const SafeRecord: RecordFactory<SafeProps> = Record({

View File

@ -21,8 +21,7 @@ export const buildSafe = (storedSafe: SafeProps) => {
address: storedSafe.address, address: storedSafe.address,
name: storedSafe.name, name: storedSafe.name,
threshold: storedSafe.threshold, threshold: storedSafe.threshold,
activeTokens: storedSafe.activeTokens, tokens: storedSafe.tokens,
balances: storedSafe.balances,
owners, owners,
} }
@ -58,7 +57,7 @@ export default handleActions<State, *>(
{ {
[UPDATE_SAFE]: (state: State, action: ActionType<Function>): State => { [UPDATE_SAFE]: (state: State, action: ActionType<Function>): State => {
const safe = action.payload const safe = action.payload
const safeAddress = safe.get('address') const safeAddress = safe.address
const hasSafe = !!state.get(safeAddress) const hasSafe = !!state.get(safeAddress)
if (hasSafe) { if (hasSafe) {