Skip to content

Commit

Permalink
CP-9361: User can't hide scam tokens with a large non-zero balance (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
atn4z7 authored Nov 5, 2024
1 parent 1e3485c commit 30604e4
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ActivityIndicator } from 'components/ActivityIndicator'
import AnalyticsService from 'services/analytics/AnalyticsService'
import { selectWalletType } from 'store/app'
import { useNetworks } from 'hooks/networks/useNetworks'
import { selectTokenBlacklist } from 'store/portfolio/slice'

type Props = {
account: Account
Expand All @@ -40,8 +41,9 @@ function AccountItem({
const { activeNetwork } = useNetworks()
const walletType = useSelector(selectWalletType)
const context = useApplicationContext()
const tokenBlacklist = useSelector(selectTokenBlacklist)
const accountBalance = useSelector(
selectBalanceTotalInCurrencyForAccount(account.index)
selectBalanceTotalInCurrencyForAccount(account.index, tokenBlacklist)
)
const isBalanceLoaded = useSelector(
selectIsBalanceLoadedForAddress(account.index, activeNetwork.chainId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useTokenPortfolioPriceChange } from 'hooks/balance/useTokenPortfolioPri
import { Space } from 'components/Space'
import { useNetworks } from 'hooks/networks/useNetworks'
import { isAvmNetwork, isPvmNetwork } from 'utils/network/isAvalancheNetwork'
import { selectTokenBlacklist } from 'store/portfolio/slice'
import ZeroState from './ZeroState'
import Tokens from './Tokens'
import { PChainAssetList } from './PChainAssetList'
Expand All @@ -38,10 +39,12 @@ const ActiveNetworkCard = (): JSX.Element => {
const { filteredTokenList: tokens } = useSearchableTokenList()
const { activeNetwork } = useNetworks()
const account = useSelector(selectActiveAccount)
const tokenBlacklist = useSelector(selectTokenBlacklist)
const totalBalanceInCurrency = useSelector(
selectBalanceTotalInCurrencyForNetworkAndAccount(
activeNetwork.chainId,
account?.index
account?.index,
tokenBlacklist
)
)
const { navigate } = useNavigation<NavigationProp>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import TopRightBadge from 'components/TopRightBadge'
import { Text } from '@avalabs/k2-mobile'
import PriceChangeIndicator from 'screens/watchlist/components/PriceChangeIndicator'
import { useTokenPortfolioPriceChange } from 'hooks/balance/useTokenPortfolioPriceChange'
import { selectTokenBlacklist } from 'store/portfolio/slice'

const windowWidth = Dimensions.get('window').width

Expand All @@ -37,10 +38,12 @@ const InactiveNetworkCard: FC<Props> = ({
} = useApplicationContext()
const { theme } = useTheme()
const account = useSelector(selectActiveAccount)
const tokenBlacklist = useSelector(selectTokenBlacklist)
const totalBalance = useSelector(
selectBalanceTotalInCurrencyForNetworkAndAccount(
network.chainId,
account?.index
account?.index,
tokenBlacklist
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
selectIsRefetchingBalances,
selectTokensWithBalanceForAccount
} from 'store/balance/slice'
import { selectActiveAccount } from 'store/account'
import { selectActiveAccount } from 'store/account/slice'
import { ActivityIndicator } from 'components/ActivityIndicator'
import PriceChangeIndicator from 'screens/watchlist/components/PriceChangeIndicator'
import { Icons, Text, useTheme, View } from '@avalabs/k2-mobile'
import { useTokenPortfolioPriceChange } from 'hooks/balance/useTokenPortfolioPriceChange'
import { Tooltip } from 'components/Tooltip'
import { Space } from 'components/Space'
import { RootState } from 'store'
import { selectTokenBlacklist } from 'store/portfolio/slice'
import { PortfolioHeaderLoader } from './Loaders/PortfolioHeaderLoader'

function PortfolioHeader(): JSX.Element {
Expand All @@ -26,8 +27,12 @@ function PortfolioHeader(): JSX.Element {
const activeAccount = useSelector(selectActiveAccount)
const isBalanceLoading = useSelector(selectIsLoadingBalances)
const isRefetchingBalance = useSelector(selectIsRefetchingBalances)
const tokenBlacklist = useSelector(selectTokenBlacklist)
const balanceTotalInCurrency = useSelector(
selectBalanceTotalInCurrencyForAccount(activeAccount?.index ?? 0)
selectBalanceTotalInCurrencyForAccount(
activeAccount?.index ?? 0,
tokenBlacklist
)
)
const balanceAccurate = useSelector(
selectBalanceForAccountIsAccurate(activeAccount?.index ?? 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import PriceChangeIndicator from 'screens/watchlist/components/PriceChangeIndica
import { useSearchableTokenList } from 'screens/portfolio/useSearchableTokenList'
import { useTokenPortfolioPriceChange } from 'hooks/balance/useTokenPortfolioPriceChange'
import { useNetworks } from 'hooks/networks/useNetworks'
import { selectTokenBlacklist } from 'store/portfolio/slice'

const NetworkTokensHeader = (): JSX.Element => {
const {
Expand All @@ -26,8 +27,13 @@ const NetworkTokensHeader = (): JSX.Element => {
const isLoadingBalance = useSelector(selectIsLoadingBalances)
const isRefetchingBalance = useSelector(selectIsRefetchingBalances)
const account = useSelector(selectActiveAccount)
const tokenBlacklist = useSelector(selectTokenBlacklist)
const balanceTotal = useSelector(
selectBalanceTotalInCurrencyForNetworkAndAccount(chainId, account?.index)
selectBalanceTotalInCurrencyForNetworkAndAccount(
chainId,
account?.index,
tokenBlacklist
)
)
const formattedTotalBalance = currencyFormatter(balanceTotal)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
selectIsBalanceLoadedForAddress
} from 'store/balance/slice'
import { QueryStatus } from 'store/balance/types'
import { selectTokenBlacklist } from 'store/portfolio/slice'

type Props = {
account: Account
Expand All @@ -29,8 +30,9 @@ type Props = {
const AccountItem = ({ account, onSelect, selected }: Props): JSX.Element => {
const { theme } = useApplicationContext()
const { activeNetwork } = useNetworks()
const tokenBlacklist = useSelector(selectTokenBlacklist)
const accountBalance = useSelector(
selectBalanceTotalInCurrencyForAccount(account.index)
selectBalanceTotalInCurrencyForAccount(account.index, tokenBlacklist)
)

const isBalanceLoaded = useSelector(
Expand Down
21 changes: 15 additions & 6 deletions packages/core-mobile/app/store/balance/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Balance,
Balances,
BalanceState,
LocalTokenId,
LocalTokenWithBalance,
QueryStatus
} from './types'
Expand Down Expand Up @@ -194,13 +195,15 @@ export const selectTokensWithBalanceForAccount = createSelector(
)

export const selectBalanceTotalInCurrencyForAccount =
(accountIndex: number) => (state: RootState) => {
(accountIndex: number, blacklist: LocalTokenId[]) => (state: RootState) => {
const tokens = selectTokensWithBalanceForAccount(state, accountIndex)

return tokens.reduce((total, token) => {
total += token.balanceInCurrency ?? 0
return total
}, 0)
return tokens
.filter(token => !blacklist.includes(token.localId))
.reduce((total, token) => {
total += token.balanceInCurrency ?? 0
return total
}, 0)
}
export const selectBalanceForAccountIsAccurate =
(accountIndex: number) => (state: RootState) => {
Expand All @@ -212,7 +215,12 @@ export const selectBalanceForAccountIsAccurate =
}

export const selectBalanceTotalInCurrencyForNetworkAndAccount =
(chainId: number, accountIndex: number | undefined) => (state: RootState) => {
(
chainId: number,
accountIndex: number | undefined,
blacklist: LocalTokenId[]
) =>
(state: RootState) => {
if (accountIndex === undefined) return 0

const balances = Object.values(state.balance.balances).filter(
Expand All @@ -224,6 +232,7 @@ export const selectBalanceTotalInCurrencyForNetworkAndAccount =

for (const balance of balances) {
for (const token of balance.tokens) {
if (blacklist.includes(token.localId)) continue
totalInCurrency += token.balanceInCurrency ?? 0
}
}
Expand Down

0 comments on commit 30604e4

Please sign in to comment.