tokens balance refactor wip
This commit is contained in:
parent
f94e598609
commit
be295d1ef5
|
@ -1,7 +1,7 @@
|
|||
// @flow
|
||||
import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
|
||||
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 = {
|
||||
fetchSafe: typeof fetchSafe,
|
||||
|
|
|
@ -16,12 +16,12 @@ const TIMEOUT = process.env.NODE_ENV === 'test' ? 1500 : 15000
|
|||
class SafeView extends React.Component<Props> {
|
||||
componentDidMount() {
|
||||
const {
|
||||
fetchSafe, loadActiveTokens, activeTokens, safeUrl, fetchTokenBalances,
|
||||
fetchSafe, loadActiveTokens, activeTokens, safeUrl, fetchTokenBalances, safe,
|
||||
} = this.props
|
||||
|
||||
fetchSafe(safeUrl)
|
||||
loadActiveTokens(safeUrl)
|
||||
fetchTokenBalances(safeUrl, activeTokens)
|
||||
fetchTokenBalances(safe, activeTokens)
|
||||
|
||||
this.intervalId = setInterval(() => {
|
||||
// update in another function so it uses up-to-date props values
|
||||
|
@ -35,11 +35,11 @@ class SafeView extends React.Component<Props> {
|
|||
|
||||
checkForUpdates() {
|
||||
const {
|
||||
safeUrl, activeTokens, fetchSafe, fetchTokenBalances,
|
||||
safeUrl, activeTokens, fetchSafe, fetchTokenBalances, safe,
|
||||
} = this.props
|
||||
|
||||
fetchSafe(safeUrl)
|
||||
fetchTokenBalances(safeUrl, activeTokens)
|
||||
fetchTokenBalances(safe, activeTokens)
|
||||
}
|
||||
|
||||
intervalId: IntervalID
|
||||
|
|
|
@ -6,8 +6,9 @@ import { type GlobalState } from '~/store/index'
|
|||
import { type Token } from '~/logic/tokens/store/model/token'
|
||||
import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers'
|
||||
import { getBalanceInEtherOf } from '~/logic/wallets/getWeb3'
|
||||
import { getStandardTokenContract } from './fetchTokens'
|
||||
import saveTokens from './saveTokens'
|
||||
import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens'
|
||||
import type { Safe } from '~/routes/safe/store/model/safe'
|
||||
import updateSafe from './updateSafe'
|
||||
|
||||
export const calculateBalanceOf = async (tokenAddress: string, safeAddress: string, decimals: number = 18) => {
|
||||
if (tokenAddress === ETH_ADDRESS) {
|
||||
|
@ -28,23 +29,24 @@ export const calculateBalanceOf = async (tokenAddress: string, safeAddress: stri
|
|||
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>,
|
||||
) => {
|
||||
if (!safeAddress || !tokens || !tokens.size) {
|
||||
if (!safe || !tokens || !tokens.size) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
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) => {
|
||||
withBalances.forEach(token => map.set(token.address, token))
|
||||
})
|
||||
const safeWithBalances = safe.set('tokens', List(withBalances))
|
||||
|
||||
dispatch(saveTokens(safeAddress, tokensMap))
|
||||
dispatch(updateSafe(safeWithBalances))
|
||||
} catch (err) {
|
||||
// eslint-disable-next-line
|
||||
console.error('Error while loading active tokens from storage:', err)
|
|
@ -9,7 +9,7 @@ export type SafeProps = {
|
|||
address: string,
|
||||
threshold: number,
|
||||
owners: List<Owner>,
|
||||
tokens?: List<Map<string, string>>,
|
||||
tokens?: List<Object>,
|
||||
}
|
||||
|
||||
const SafeRecord: RecordFactory<SafeProps> = Record({
|
||||
|
|
|
@ -21,8 +21,7 @@ export const buildSafe = (storedSafe: SafeProps) => {
|
|||
address: storedSafe.address,
|
||||
name: storedSafe.name,
|
||||
threshold: storedSafe.threshold,
|
||||
activeTokens: storedSafe.activeTokens,
|
||||
balances: storedSafe.balances,
|
||||
tokens: storedSafe.tokens,
|
||||
owners,
|
||||
}
|
||||
|
||||
|
@ -58,7 +57,7 @@ export default handleActions<State, *>(
|
|||
{
|
||||
[UPDATE_SAFE]: (state: State, action: ActionType<Function>): State => {
|
||||
const safe = action.payload
|
||||
const safeAddress = safe.get('address')
|
||||
const safeAddress = safe.address
|
||||
|
||||
const hasSafe = !!state.get(safeAddress)
|
||||
if (hasSafe) {
|
||||
|
|
Loading…
Reference in New Issue