From b9b936dd51c3f69656c059a6f22fc2748037db89 Mon Sep 17 00:00:00 2001 From: mmv Date: Tue, 16 Apr 2019 14:28:16 +0400 Subject: [PATCH] activeTokens refactor (again) --- src/routes/safe/container/selector.js | 16 ++++++++++------ .../safe/store/actions/fetchTokenBalances.js | 4 ++-- .../safe/store/actions/updateActiveTokens.js | 13 +++++-------- src/routes/safe/store/models/safe.js | 10 +++++----- src/routes/safe/store/models/safeToken.js | 17 ----------------- src/routes/safe/store/models/tokenBalance.js | 17 +++++++++++++++++ src/routes/safe/store/selectors/index.js | 19 ++++++++++++++----- 7 files changed, 53 insertions(+), 43 deletions(-) delete mode 100644 src/routes/safe/store/models/safeToken.js create mode 100644 src/routes/safe/store/models/tokenBalance.js diff --git a/src/routes/safe/container/selector.js b/src/routes/safe/container/selector.js index 54aa8b1a..2317fd7a 100644 --- a/src/routes/safe/container/selector.js +++ b/src/routes/safe/container/selector.js @@ -3,7 +3,8 @@ import { List, Map } from 'immutable' import { createSelector, createStructuredSelector, type Selector } from 'reselect' import { safeSelector, - safeTokensSelector, + safeActiveTokensSelector, + safeBalancesSelector, type RouterProps, type SafeSelectorProps, } from '~/routes/safe/store/selectors' @@ -14,6 +15,7 @@ import { type GlobalState } from '~/store' import { sameAddress } from '~/logic/wallets/ethAddresses' import { orderedTokenListSelector, tokensSelector } from '~/logic/tokens/store/selectors' import { type Token } from '~/logic/tokens/store/model/token' +import { type TokenBalance } from '~/routes/safe/store/models/tokenBalance' import { safeParamAddressSelector } from '../store/selectors' import { getEthAsToken } from '~/logic/tokens/utils/tokenHelpers' @@ -65,16 +67,18 @@ const safeEthAsTokenSelector: Selector = creat ) const extendedSafeTokensSelector: Selector> = createSelector( - safeTokensSelector, + safeActiveTokensSelector, + safeBalancesSelector, tokensSelector, safeEthAsTokenSelector, - (safeTokens: List, tokensList: Map, ethAsToken: Token) => { + (safeTokens: List, balances: List, tokensList: Map, ethAsToken: Token) => { const extendedTokens = Map().withMutations((map) => { - safeTokens.forEach((token: { address: string, balance: string }) => { - const baseToken = tokensList.get(token.address) + safeTokens.forEach((tokenAddress: string) => { + const baseToken = tokensList.get(tokenAddress) + const tokenBalance = balances.find(tknBalance => tknBalance.address === tokenAddress) || '0' if (baseToken) { - map.set(token.address, baseToken.set('balance', token.balance)) + map.set(tokenAddress, baseToken.set('balance', tokenBalance)) } }) diff --git a/src/routes/safe/store/actions/fetchTokenBalances.js b/src/routes/safe/store/actions/fetchTokenBalances.js index 6f6badcc..93b301a2 100644 --- a/src/routes/safe/store/actions/fetchTokenBalances.js +++ b/src/routes/safe/store/actions/fetchTokenBalances.js @@ -4,7 +4,7 @@ import { List } from 'immutable' import { BigNumber } from 'bignumber.js' import { type GlobalState } from '~/store/index' import { type Token } from '~/logic/tokens/store/model/token' -import SafeTokenRecord from '~/routes/safe/store/models/safeToken' +import TokenBalanceRecord from '~/routes/safe/store/models/tokenBalance' import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens' import updateSafe from './updateSafe' import { ETH_ADDRESS } from '~/logic/tokens/utils/tokenHelpers' @@ -38,7 +38,7 @@ const fetchTokenBalances = (safeAddress: string, tokens: List) => async ( const withBalances = await Promise.all( tokens .filter(token => token.address !== ETH_ADDRESS) - .map(async token => SafeTokenRecord({ + .map(async token => TokenBalanceRecord({ address: token.address, balance: await calculateBalanceOf(token.address, safeAddress, token.decimals), })), diff --git a/src/routes/safe/store/actions/updateActiveTokens.js b/src/routes/safe/store/actions/updateActiveTokens.js index 6acc7a09..08ddf94c 100644 --- a/src/routes/safe/store/actions/updateActiveTokens.js +++ b/src/routes/safe/store/actions/updateActiveTokens.js @@ -2,9 +2,7 @@ import { List } from 'immutable' import type { Dispatch as ReduxDispatch, GetState } from 'redux' import { type GlobalState } from '~/store' -import type { SafeToken } from '~/routes/safe/store/models/safeToken' -import SafeTokenRecord from '~/routes/safe/store/models/safeToken' -import { safeTokensSelector } from '~/routes/safe/store/selectors/index' +import { safeActiveTokensSelector } from '~/routes/safe/store/selectors' import { SAFE_PARAM_ADDRESS } from '~/routes/routes' import updateSafe from './updateSafe' @@ -22,19 +20,18 @@ const updateActiveTokens = (safeAddress: string, tokenAddress: string) => async dispatch: ReduxDispatch, getState: GetState, ) => { - const token: SafeToken = SafeTokenRecord({ address: tokenAddress }) const state = getState() - const safeTokens: List = safeTokensSelector(state, generateMatchProps(safeAddress)) - const index = safeTokens.findIndex(safeToken => safeToken.address === token.address) + const safeTokens: List = safeActiveTokensSelector(state, generateMatchProps(safeAddress)) + const index = safeTokens.findIndex(safeToken => safeToken === tokenAddress) let updatedTokens if (index !== -1) { updatedTokens = safeTokens.delete(index) } else { - updatedTokens = safeTokens.push(token) + updatedTokens = safeTokens.push(tokenAddress) } - dispatch(updateSafe({ address: safeAddress, tokens: updatedTokens })) + dispatch(updateSafe({ address: safeAddress, activeTokens: updatedTokens })) } export default updateActiveTokens diff --git a/src/routes/safe/store/models/safe.js b/src/routes/safe/store/models/safe.js index 32bfdcd6..480f95df 100644 --- a/src/routes/safe/store/models/safe.js +++ b/src/routes/safe/store/models/safe.js @@ -2,14 +2,15 @@ import { List, Record } from 'immutable' import type { RecordFactory, RecordOf } from 'immutable' import type { Owner } from '~/routes/safe/store/models/owner' -import type { SafeToken } from '~/routes/safe/store/models/safeToken' +import TokenBalance from '~/routes/safe/store/models/tokenBalance' export type SafeProps = { name: string, address: string, threshold: number, owners: List, - tokens?: List, + balances?: List, + activeTokens?: List, ethBalance?: string, } @@ -19,11 +20,10 @@ const SafeRecord: RecordFactory = Record({ threshold: 0, ethBalance: 0, owners: List([]), - tokens: List([]), + activeTokens: List([]), + balances: List([]), }) -// Tokens is a list of currently enabled tokens for the safe with balances - export type Safe = RecordOf export default SafeRecord diff --git a/src/routes/safe/store/models/safeToken.js b/src/routes/safe/store/models/safeToken.js deleted file mode 100644 index 8d4b3e05..00000000 --- a/src/routes/safe/store/models/safeToken.js +++ /dev/null @@ -1,17 +0,0 @@ -// @flow -import { Record } from 'immutable' -import type { RecordFactory, RecordOf } from 'immutable' - -export type SafeTokenProps = { - address: string, - balance: string, -} - -const SafeTokenRecord: RecordFactory = Record({ - address: '', - balance: '0', -}) - -export type SafeToken = RecordOf - -export default SafeTokenRecord diff --git a/src/routes/safe/store/models/tokenBalance.js b/src/routes/safe/store/models/tokenBalance.js new file mode 100644 index 00000000..b8f1ed80 --- /dev/null +++ b/src/routes/safe/store/models/tokenBalance.js @@ -0,0 +1,17 @@ +// @flow +import { Record } from 'immutable' +import type { RecordFactory, RecordOf } from 'immutable' + +export type TokenBalanceProps = { + address: string, + balance: string, +} + +const TokenBalanceRecord: RecordFactory = Record({ + address: '', + balance: '0', +}) + +export type TokenBalance = RecordOf + +export default TokenBalanceRecord diff --git a/src/routes/safe/store/selectors/index.js b/src/routes/safe/store/selectors/index.js index 470915b6..100e3cbd 100644 --- a/src/routes/safe/store/selectors/index.js +++ b/src/routes/safe/store/selectors/index.js @@ -78,20 +78,29 @@ export const safeSelector: Selector }, ) -export const safeTokensSelector: Selector> = createSelector( +export const safeActiveTokensSelector: Selector> = createSelector( safeSelector, (safe: Safe) => { if (!safe) { - return Map() + return List() } - const tokens = safe.get('tokens') + return safe.activeTokens + }, +) - return tokens +export const safeBalancesSelector: Selector> = createSelector( + safeSelector, + (safe: Safe) => { + if (!safe) { + return List() + } + + return safe.balances }, ) export default createStructuredSelector({ safe: safeSelector, - tokens: safeTokensSelector, + tokens: safeActiveTokensSelector, })