Skip to content

Commit

Permalink
fix: List cache with different keys returns same result
Browse files Browse the repository at this point in the history
  • Loading branch information
memoyil committed Nov 13, 2024
1 parent 36f1ccf commit 69e99eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 18 additions & 10 deletions apps/web/src/state/lists/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChainId } from '@pancakeswap/chains'
import { TokenAddressMap as TTokenAddressMap, TokenInfo, TokenList, WrappedTokenInfo } from '@pancakeswap/token-lists'
import { ListsState } from '@pancakeswap/token-lists/react'
import { EMPTY_LIST } from '@pancakeswap/tokens'
import { createEmptyList, EMPTY_LIST } from '@pancakeswap/tokens'
import { enumValues } from '@pancakeswap/utils/enumValues'
import {
DEFAULT_LIST_OF_LISTS,
Expand Down Expand Up @@ -70,7 +70,7 @@ export const combinedCurrenciesMapFromActiveUrlsAtom = atom(() => {
})

const combineTokenMaps = (lists: ListsState['byUrl'], urls: string[]): any => {
if (!urls) return EMPTY_LIST
if (urls?.length) return EMPTY_LIST
return (
[...urls]
// sort by priority so top priority goes last
Expand All @@ -79,13 +79,16 @@ const combineTokenMaps = (lists: ListsState['byUrl'], urls: string[]): any => {
const current = lists[currentUrl]?.current
if (!current) return allTokens
try {
const newTokens = Object.assign(listToTokenMap(current, 'address'))
return combineMaps(allTokens, newTokens)
const newTokens = listToTokenMap(current, 'address')
for (const chainId of enumValues(ChainId)) {
Object.assign(allTokens[chainId], newTokens[chainId])
}
return allTokens
} catch (error) {
console.error('Could not show token list due to error', error)
return allTokens
}
}, EMPTY_LIST)
}, createEmptyList())
)
}

Expand Down Expand Up @@ -149,11 +152,11 @@ export const combinedTokenMapFromWarningUrlsAtom = atom((get) => {

return combineMaps(localUnsupportedListMap, loadedUnsupportedListMap)
})
const listCache: WeakMap<TokenList, TokenAddressMap> | null =
typeof WeakMap !== 'undefined' ? new WeakMap<TokenList, TokenAddressMap>() : null
const listCache: WeakMap<TokenList, Map<string, TokenAddressMap>> | null =
typeof WeakMap !== 'undefined' ? new WeakMap<TokenList, Map<string, TokenAddressMap>>() : null

export function listToTokenMap(list: TokenList, key?: string): TokenAddressMap {
const result = listCache?.get(list)
export function listToTokenMap(list: TokenList, key: string): TokenAddressMap {
const result = listCache?.get(list)?.get(key)
if (result) return result

const tokenMap: WrappedTokenInfo[] = uniqBy(
Expand Down Expand Up @@ -184,7 +187,12 @@ export function listToTokenMap(list: TokenList, key?: string): TokenAddressMap {
}
})

listCache?.set(list, tokenAddressMap)
let keyCache = listCache?.get(list)
if (!keyCache) {
keyCache = new Map<string, TokenAddressMap>()
listCache?.set(list, keyCache)
}
keyCache.set(key, tokenAddressMap)
return tokenAddressMap
}

Expand Down
8 changes: 4 additions & 4 deletions packages/tokens/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { TokenAddressMap } from '@pancakeswap/token-lists'

export * from './getTokensByChain'

const createEmptyList = () => {
const list = {} as Record<ChainId, TokenAddressMap<ChainId>[ChainId]>
export const createEmptyList = () => {
const list = {} as Record<ChainId, Readonly<TokenAddressMap<ChainId>[ChainId]>>
for (const chainId of enumValues(ChainId)) {
list[chainId] = {}
}
return list as TokenAddressMap<ChainId>
return list as Readonly<TokenAddressMap<ChainId>>
}

/**
* An empty result, useful as a default.
*/
export const EMPTY_LIST: TokenAddressMap<ChainId> = createEmptyList()
export const EMPTY_LIST: Readonly<TokenAddressMap<ChainId>> = createEmptyList()

export function serializeTokens(unserializedTokens: any) {
const serializedTokens = Object.keys(unserializedTokens).reduce((accum, key) => {
Expand Down

0 comments on commit 69e99eb

Please sign in to comment.