Skip to content

Commit

Permalink
chore: cleanup hidden and deleted tokens code
Browse files Browse the repository at this point in the history
  • Loading branch information
josheleonard committed Dec 13, 2023
1 parent 9f8c743 commit 8a78707
Show file tree
Hide file tree
Showing 18 changed files with 508 additions and 461 deletions.
5 changes: 0 additions & 5 deletions components/brave_wallet_ui/common/actions/wallet_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ export const {
updateUserAsset,
setHidePortfolioGraph,
setHidePortfolioBalances,
setRemovedFungibleTokenIds,
setRemovedNonFungibleTokenIds,
setDeletedNonFungibleTokenIds,
setDeletedNonFungibleTokens,
setHidePortfolioNFTsTab,
setRemovedNonFungibleTokens,
setFilteredOutPortfolioNetworkKeys,
setFilteredOutPortfolioAccountAddresses,
setHidePortfolioSmallBalances,
Expand Down
203 changes: 139 additions & 64 deletions components/brave_wallet_ui/common/async/base-query-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ import getAPIProxy from './bridge'
import {
addChainIdToToken,
getAssetIdKey,
GetBlockchainTokenIdArg
GetBlockchainTokenIdArg,
getDeletedTokenIds,
getHiddenTokenIds
} from '../../utils/asset-utils'
import { addLogoToToken } from './lib'
import { makeNetworkAsset } from '../../options/asset-options'
Expand Down Expand Up @@ -77,6 +79,7 @@ export class BaseQueryCache {
private _walletInfo?: BraveWallet.WalletInfo
private _allAccountsInfo?: BraveWallet.AllAccountsInfo
private _accountsRegistry?: AccountInfoEntityState
private _knownTokensRegistry?: BlockchainTokenEntityAdaptorState
private _userTokensRegistry?: BlockchainTokenEntityAdaptorState
private _nftImageIpfsGateWayUrlRegistry: Record<string, string | null> = {}
private _extractedIPFSUrlRegistry: Record<string, string | undefined> = {}
Expand Down Expand Up @@ -256,70 +259,38 @@ export class BaseQueryCache {
this._networksRegistry = undefined
}

getKnownTokensRegistry = async () => {
if (!this._knownTokensRegistry) {
const { blockchainRegistry } = apiProxyFetcher()
const networksRegistry = await this.getNetworksRegistry()

this._knownTokensRegistry = await makeTokenRegistry({
type: 'known',
networksRegistry,
blockchainRegistry
})
}
return this._knownTokensRegistry
}

getUserTokensRegistry = async () => {
if (!this._userTokensRegistry) {
const { braveWalletService } = apiProxyFetcher()
const networksRegistry = await this.getNetworksRegistry()

const tokenIdsByChainId: Record<string, string[]> = {}
const tokenIdsByCoinType: Record<BraveWallet.CoinType, string[]> = {}
const visibleTokenIds: string[] = []
const visibleTokenIdsByChainId: Record<string, string[]> = {}
const visibleTokenIdsByCoinType: Record<BraveWallet.CoinType, string[]> =
{}

const userTokenListsForNetworks = await mapLimit(
Object.entries(networksRegistry.entities),
10,
async ([networkId, network]: [string, BraveWallet.NetworkInfo]) => {
if (!network) {
return []
}

const fullTokensListForNetwork: BraveWallet.BlockchainToken[] =
await fetchUserAssetsForNetwork(braveWalletService, network)

tokenIdsByChainId[networkId] =
fullTokensListForNetwork.map(getAssetIdKey)

tokenIdsByCoinType[network.coin] = (
tokenIdsByCoinType[network.coin] || []
).concat(tokenIdsByChainId[networkId] || [])

const visibleTokensForNetwork: BraveWallet.BlockchainToken[] =
fullTokensListForNetwork.filter((t) => t.visible)

visibleTokenIdsByChainId[networkId] =
visibleTokensForNetwork.map(getAssetIdKey)

visibleTokenIdsByCoinType[network.coin] = (
visibleTokenIdsByCoinType[network.coin] || []
).concat(visibleTokenIdsByChainId[networkId] || [])

visibleTokenIds.push(...visibleTokenIdsByChainId[networkId])

return fullTokensListForNetwork
}
)

const userTokensByChainIdRegistry = blockchainTokenEntityAdaptor.setAll(
{
...blockchainTokenEntityAdaptorInitialState,
idsByChainId: tokenIdsByChainId,
tokenIdsByChainId,
visibleTokenIds,
visibleTokenIdsByChainId,
visibleTokenIdsByCoinType,
idsByCoinType: tokenIdsByCoinType
},
userTokenListsForNetworks.flat(1)
)

this._userTokensRegistry = userTokensByChainIdRegistry
this._userTokensRegistry = await makeTokenRegistry({
type: 'user',
networksRegistry,
braveWalletService
})
}
return this._userTokensRegistry
}

clearKnownTokensRegistry = () => {
this._knownTokensRegistry = undefined
}

clearUserTokensRegistry = () => {
this._userTokensRegistry = undefined
}
Expand Down Expand Up @@ -416,15 +387,29 @@ export const resetCache = () => {
}

// internals
async function fetchUserAssetsForNetwork(
braveWalletService: BraveWallet.BraveWalletServiceRemote,
network: BraveWallet.NetworkInfo
) {
async function fetchAssetsForNetwork({
assetListType,
blockchainRegistry,
braveWalletService,
network
}:
| {
assetListType: 'user'
braveWalletService: BraveWallet.BraveWalletServiceRemote
blockchainRegistry?: never
network: BraveWallet.NetworkInfo
}
| {
assetListType: 'known'
blockchainRegistry: BraveWallet.BlockchainRegistryRemote
braveWalletService?: never
network: BraveWallet.NetworkInfo
}) {
// Get a list of user tokens for each coinType and network.
const { tokens } = await braveWalletService.getUserAssets(
network.chainId,
network.coin
)
const { tokens } =
assetListType === 'user'
? await braveWalletService.getUserAssets(network.chainId, network.coin)
: await blockchainRegistry.getAllTokens(network.chainId, network.coin)

// Adds a logo and chainId to each token object
const tokenList: BraveWallet.BlockchainToken[] = await mapLimit(
Expand All @@ -446,3 +431,93 @@ async function fetchUserAssetsForNetwork(

return tokenList
}
async function makeTokenRegistry({
braveWalletService,
blockchainRegistry,
networksRegistry,
type
}:
| {
type: 'user'
networksRegistry: NetworksRegistry
braveWalletService: BraveWallet.BraveWalletServiceRemote
blockchainRegistry?: never
}
| {
type: 'known'
networksRegistry: NetworksRegistry
blockchainRegistry: BraveWallet.BlockchainRegistryRemote
braveWalletService?: never
}) {
const tokenIdsByChainId: Record<string, string[]> = {}
const tokenIdsByCoinType: Record<BraveWallet.CoinType, string[]> = {}
const visibleTokenIds: string[] = []
const visibleTokenIdsByChainId: Record<string, string[]> = {}
const visibleTokenIdsByCoinType: Record<BraveWallet.CoinType, string[]> = {}

const deletedTokenIds: string[] = type === 'user' ? getDeletedTokenIds() : []
const hiddenTokenIds: string[] = type === 'user' ? getHiddenTokenIds() : []
const removedTokenIds = deletedTokenIds.concat(hiddenTokenIds)

const tokenListsForNetworks = await mapLimit(
Object.entries(networksRegistry.entities),
10,
async ([networkId, network]: [string, BraveWallet.NetworkInfo]) => {
if (!network) {
return []
}

const fullTokensListForNetwork: BraveWallet.BlockchainToken[] =
type === 'known'
? await fetchAssetsForNetwork({
assetListType: type,
blockchainRegistry,
network
})
: await fetchAssetsForNetwork({
assetListType: type,
braveWalletService,
network
})

tokenIdsByChainId[networkId] = fullTokensListForNetwork.map(getAssetIdKey)

tokenIdsByCoinType[network.coin] = (
tokenIdsByCoinType[network.coin] || []
).concat(tokenIdsByChainId[networkId] || [])

const visibleTokensForNetwork: BraveWallet.BlockchainToken[] =
fullTokensListForNetwork.filter((t) =>
t.visible && type === 'user'
? !removedTokenIds.includes(getAssetIdKey(t))
: true
)

visibleTokenIdsByChainId[networkId] =
visibleTokensForNetwork.map(getAssetIdKey)

visibleTokenIdsByCoinType[network.coin] = (
visibleTokenIdsByCoinType[network.coin] || []
).concat(visibleTokenIdsByChainId[networkId] || [])

visibleTokenIds.push(...visibleTokenIdsByChainId[networkId])

return fullTokensListForNetwork
}
)

return blockchainTokenEntityAdaptor.setAll(
{
...blockchainTokenEntityAdaptorInitialState,
idsByChainId: tokenIdsByChainId,
tokenIdsByChainId,
visibleTokenIds,
visibleTokenIdsByChainId,
visibleTokenIdsByCoinType,
idsByCoinType: tokenIdsByCoinType,
deletedTokenIds,
hiddenTokenIds
},
tokenListsForNetworks.flat(1)
)
}
10 changes: 10 additions & 0 deletions components/brave_wallet_ui/common/async/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { Store } from './types'
import InteractionNotifier from './interactionNotifier'
import { walletApi } from '../slices/api.slice'
import { getVisibleNetworksList } from '../../utils/api-utils'
import { getAssetIdKey, getDeletedTokenIds } from '../../utils/asset-utils'
import { LOCAL_STORAGE_KEYS } from '../constants/local-storage-keys'

const handler = new AsyncActionHandler()

Expand Down Expand Up @@ -211,6 +213,14 @@ handler.on(

const result = await braveWalletService.addUserAsset(payload)

// token may have previously been deleted
localStorage.setItem(
LOCAL_STORAGE_KEYS.USER_DELETED_TOKEN_IDS,
JSON.stringify(
getDeletedTokenIds().filter((id) => id !== getAssetIdKey(payload))
)
)

// Refresh balances here for adding ERC721 tokens if result is successful
if ((payload.isErc721 || payload.isNft) && result.success) {
refreshBalancesPricesAndHistory(store)
Expand Down
16 changes: 6 additions & 10 deletions components/brave_wallet_ui/common/async/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
import * as WalletActions from '../actions/wallet_actions'

// Utils
import { getAssetIdKey, isNativeAsset } from '../../utils/asset-utils'
import {
getAssetIdKey,
getHiddenOrDeletedTokenIdsList,
isNativeAsset
} from '../../utils/asset-utils'
import {
makeNativeAssetLogo,
makeNetworkAsset
Expand Down Expand Up @@ -210,19 +214,11 @@ export function refreshVisibleTokenInfo(
async (item: BraveWallet.NetworkInfo) => await inner(item)
)

const removedAssetIds = [
...getState().wallet.removedFungibleTokenIds,
...getState().wallet.removedNonFungibleTokenIds,
...getState().wallet.deletedNonFungibleTokenIds
]
const removedAssetIds = getHiddenOrDeletedTokenIdsList()
const userVisibleTokensInfo = visibleAssets
.flat(1)
.filter((token) => !removedAssetIds.includes(getAssetIdKey(token)))
const removedNfts = visibleAssets
.flat(1)
.filter((token) => removedAssetIds.includes(getAssetIdKey(token)))
await dispatch(WalletActions.setVisibleTokensInfo(userVisibleTokensInfo))
await dispatch(WalletActions.setRemovedNonFungibleTokens(removedNfts))
}
}

Expand Down
22 changes: 12 additions & 10 deletions components/brave_wallet_ui/common/constants/local-storage-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ export const LOCAL_STORAGE_KEYS = {
IS_IPFS_BANNER_HIDDEN: 'BRAVE_WALLET_IS_IPFS_BANNER_HIDDEN',
IS_ENABLE_NFT_AUTO_DISCOVERY_MODAL_HIDDEN:
'BRAVE_WALLET_IS_ENABLE_NFT_AUTO_DISCOVERY_MODAL_HIDDEN',
USER_REMOVED_NON_FUNGIBLE_TOKEN_IDS:
'BRAVE_WALLET_USER_REMOVED_NON_FUNGIBLE_TOKEN_IDS',
USER_DELETED_NON_FUNGIBLE_TOKEN_IDS:
'BRAVE_WALLET_USER_DELETED_NON_FUNGIBLE_TOKEN_IDS',
USER_REMOVED_FUNGIBLE_TOKEN_IDS:
'BRAVE_WALLET_USER_REMOVED_FUNGIBLE_TOKEN_IDS',
USER_HIDDEN_TOKEN_IDS: 'BRAVE_WALLET_USER_HIDDEN_TOKEN_IDS',
USER_DELETED_TOKEN_IDS: 'BRAVE_WALLET_USER_DELETED_TOKEN_IDS',
DEBUG: 'BRAVE_WALLET_DEBUG',
FILTERED_OUT_PORTFOLIO_NETWORK_KEYS:
'BRAVE_WALLET_FILTERED_OUT_PORTFOLIO_NETWORK_KEYS',
Expand All @@ -37,11 +33,17 @@ export const LOCAL_STORAGE_KEYS = {
} as const

const LOCAL_STORAGE_KEYS_DEPRECATED = {
PORTFOLIO_ACCOUNT_FILTER_OPTION: 'PORTFOLIO_ACCOUNT_FILTER_OPTION'
PORTFOLIO_ACCOUNT_FILTER_OPTION: 'PORTFOLIO_ACCOUNT_FILTER_OPTION',
USER_REMOVED_NON_FUNGIBLE_TOKEN_IDS:
'BRAVE_WALLET_USER_REMOVED_NON_FUNGIBLE_TOKEN_IDS',
USER_DELETED_NON_FUNGIBLE_TOKEN_IDS:
'BRAVE_WALLET_USER_DELETED_NON_FUNGIBLE_TOKEN_IDS',
USER_REMOVED_FUNGIBLE_TOKEN_IDS:
'BRAVE_WALLET_USER_REMOVED_FUNGIBLE_TOKEN_IDS'
}

export const removeDeprecatedLocalStorageKeys = () => {
window.localStorage.removeItem(
LOCAL_STORAGE_KEYS_DEPRECATED.PORTFOLIO_ACCOUNT_FILTER_OPTION
)
Object.keys(LOCAL_STORAGE_KEYS_DEPRECATED).forEach((key) => {
window.localStorage.removeItem(LOCAL_STORAGE_KEYS_DEPRECATED[key])
})
}
Loading

0 comments on commit 8a78707

Please sign in to comment.