Skip to content

Commit

Permalink
update to handle lowercase address
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYhun committed Jul 27, 2023
1 parent a8fd82a commit 4681e6b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .env.adpr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ VITE_TYPE_AND_SWAP_URL=https://type-swap.dev.kyberengineering.io/api
VITE_TRANSAK_URL=https://staging-global.transak.com
VITE_TRANSAK_API_KEY=327b8b63-626b-4376-baf2-70a304c48488

VITE_KS_SETTING_API=https://ks-setting.stg.kyberengineering.io/api
VITE_KS_SETTING_API=https://ks-setting.dev.kyberengineering.io/api

VITE_GTM_ID=

Expand Down
10 changes: 8 additions & 2 deletions src/components/SearchModal/CurrencySearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,14 @@ export function CurrencySearch({
const addressesToFetch: string[] = []

favoriteTokens?.forEach(address => {
if (defaultTokens[address]) {
result.push(defaultTokens[address])
let token
Object.entries(defaultTokens).forEach(([add, t]) => {
if (add.toLowerCase() === address.toLowerCase()) {
token = t
}
})
if (token) {
result.push(token)
return
}
addressesToFetch.push(address)
Expand Down
2 changes: 1 addition & 1 deletion src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if ('user' in preloadedState) {
(acc, [chainId, obj]) => {
acc[chainId] = {}
obj.addresses.forEach((address: string) => {
acc[chainId][address] = true
acc[chainId][address.toLowerCase()] = true
})
return acc
},
Expand Down
1 change: 1 addition & 0 deletions src/state/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const toggleTopTrendingTokens = createAction<void>('user/toggleTopTrendin
export type ToggleFavoriteTokenPayload = {
chainId: ChainId
address: string
newValue?: boolean
}
export const toggleFavoriteToken = createAction<ToggleFavoriteTokenPayload>('user/toggleFavoriteToken')
export const updateChainId = createAction<ChainId>('user/updateChainId')
Expand Down
19 changes: 10 additions & 9 deletions src/state/user/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -405,26 +405,27 @@ export const useUserFavoriteTokens = (chainId: ChainId) => {
}, [commonTokens, chainId])

const favoriteTokens = useMemo(() => {
if (!chainId || !defaultTokens) return undefined
if (!chainId) return undefined
const favoritedTokens = favoriteTokensByChainId?.[chainId] || {}
const favoritedTokenAddresses = defaultTokens
.filter(address => favoritedTokens[address] !== false)
.filter(address => favoritedTokens[address.toLowerCase()] !== false)
.concat(Object.keys(favoritedTokens).filter(address => favoritedTokens[address]))

return [...new Set(favoritedTokenAddresses)]
return [...new Set(favoritedTokenAddresses.map(a => a.toLowerCase()))]
}, [chainId, favoriteTokensByChainId, defaultTokens])

const toggleFavoriteToken = useCallback(
(payload: ToggleFavoriteTokenPayload) => {
if (!favoriteTokens) return
const address = payload.address.toLowerCase()
// Is adding favorite and reached max limit
if (
favoriteTokens &&
favoriteTokens?.indexOf(payload.address) < 0 &&
favoriteTokens.length >= MAX_FAVORITE_LIMIT
) {
if (favoriteTokens.indexOf(address) < 0 && favoriteTokens.length >= MAX_FAVORITE_LIMIT) {
return
}
dispatch(toggleFavoriteTokenAction(payload))
const newValue = favoriteTokens.indexOf(address) < 0
console.log('🚀 ~ file: hooks.tsx:425 ~ useUserFavoriteTokens ~ newValue:', newValue, address, favoriteTokens)

dispatch(toggleFavoriteTokenAction({ ...payload, newValue }))
},
[dispatch, favoriteTokens],
)
Expand Down
14 changes: 8 additions & 6 deletions src/state/user/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,20 @@ export default createReducer(initialState, builder =>
.addCase(toggleKyberAIBanner, state => {
state.showKyberAIBanner = !state.showKyberAIBanner
})
.addCase(toggleFavoriteToken, (state, { payload: { chainId, address } }) => {
.addCase(toggleFavoriteToken, (state, { payload: { chainId, address, newValue } }) => {
if (!state.favoriteTokensByChainIdv2) {
state.favoriteTokensByChainIdv2 = {}
}

let favoriteTokens = state.favoriteTokensByChainIdv2[chainId]

if (!favoriteTokens) {
favoriteTokens = {}
if (!state.favoriteTokensByChainIdv2[chainId]) {
state.favoriteTokensByChainIdv2[chainId] = {}
}

favoriteTokens[address] = !favoriteTokens[address]
const favoriteTokens = state.favoriteTokensByChainIdv2[chainId]
const lowercaseAddress = address.toLowerCase()
if (favoriteTokens) {
favoriteTokens[lowercaseAddress] = newValue !== undefined ? newValue : !favoriteTokens[lowercaseAddress]
}
})
.addCase(updateChainId, (state, { payload: chainId }) => {
state.chainId = chainId
Expand Down

0 comments on commit 4681e6b

Please sign in to comment.