Skip to content

Commit

Permalink
get default common tokens from ks setting
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYhun committed Jul 24, 2023
1 parent 780644f commit 1bec85e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/services/ksSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type KyberSwapConfig = {
elasticClient: ApolloClient<NormalizedCacheObject>
readProvider: ethers.providers.JsonRpcProvider | undefined
connection: Connection | undefined
commonTokens?: string[]
}

export type KyberSwapConfigResponse = {
Expand All @@ -25,6 +26,7 @@ export type KyberSwapConfigResponse = {
blockSubgraph: string
classicSubgraph: string
elasticSubgraph: string
commonTokens?: string[]
}

export type KyberswapConfigurationResponse = {
Expand Down
3 changes: 3 additions & 0 deletions src/state/application/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ function getDefaultConfig(chainId: ChainId): KyberSwapConfigResponse {
blockSubgraph: (evm ? NETWORKS_INFO[chainId] : ethereumInfo).defaultBlockSubgraph,
elasticSubgraph: (evm ? NETWORKS_INFO[chainId] : ethereumInfo).elastic.defaultSubgraph,
classicSubgraph: (evm ? NETWORKS_INFO[chainId] : ethereumInfo).classic.defaultSubgraph,
commonTokens: undefined,
}
}

Expand Down Expand Up @@ -461,11 +462,13 @@ export const useKyberSwapConfig = (customChainId?: ChainId): KyberSwapConfig =>
elasticClient,
classicClient,
connection: isSolana(chainId) ? new Connection(config.rpc, { commitment: 'confirmed' }) : undefined,
commonTokens: config.commonTokens,
}
}, [
config.rpc,
config.isEnableBlockService,
config.prochart,
config.commonTokens,
readProvider,
blockClient,
elasticClient,
Expand Down
1 change: 1 addition & 0 deletions src/state/application/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export default createReducer(initialState, builder =>
blockSubgraph,
elasticSubgraph,
classicSubgraph,
commonTokens: data.commonTokens,
},
}
}),
Expand Down
6 changes: 5 additions & 1 deletion src/state/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export const toggleTopTrendingTokens = createAction<void>('user/toggleTopTrendin
export type ToggleFavoriteTokenPayload = {
chainId: ChainId
} & ({ isNative?: false; address: string } | { isNative: true; address?: never })
export const toggleFavoriteToken = createAction<ToggleFavoriteTokenPayload>('user/toggleFavoriteToken')
export const toggleFavoriteToken = createAction<
ToggleFavoriteTokenPayload & {
defaultCommonTokens: { addresses: string[]; includeNativeToken: boolean }
}
>('user/toggleFavoriteToken')
export const updateChainId = createAction<ChainId>('user/updateChainId')
export const updateTokenAnalysisSettings = createAction<string>('user/updateTokenAnalysisSettings')
export const updateAcceptedTermVersion = createAction<number | null>('user/updateAcceptedTermVersion')
Expand Down
25 changes: 18 additions & 7 deletions src/state/user/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback, useMemo } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { useGetParticipantInfoQuery } from 'services/kyberAISubscription'

import { SUGGESTED_BASES } from 'constants/bases'
import { DEFAULT_SLIPPAGE_TESTNET, TERM_FILES_PATH } from 'constants/index'
import { SupportedLocale } from 'constants/locales'
import { PINNED_PAIRS } from 'constants/tokens'
Expand All @@ -16,6 +17,7 @@ import {
import useDebounce from 'hooks/useDebounce'
import { ParticipantInfo, ParticipantStatus } from 'pages/TrueSightV2/types'
import { AppDispatch, AppState } from 'state'
import { useKyberSwapConfig } from 'state/application/hooks'
import { useIsConnectingWallet, useSessionInfo } from 'state/authen/hooks'
import { useAppDispatch, useAppSelector } from 'state/hooks'
import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo'
Expand Down Expand Up @@ -48,7 +50,7 @@ import {
updateUserSlippageTolerance,
updateUserSlippageToleranceForLineaTestnet,
} from 'state/user/actions'
import { CROSS_CHAIN_SETTING_DEFAULT, CrossChainSetting, VIEW_MODE, getFavoriteTokenDefault } from 'state/user/reducer'
import { CROSS_CHAIN_SETTING_DEFAULT, CrossChainSetting, VIEW_MODE } from 'state/user/reducer'
import { isAddress, isChristmasTime } from 'utils'

function serializeToken(token: Token | WrappedTokenInfo): SerializedToken {
Expand Down Expand Up @@ -395,17 +397,26 @@ export function useToggleTopTrendingTokens(): () => void {
export const useUserFavoriteTokens = (chainId: ChainId) => {
const dispatch = useDispatch<AppDispatch>()
const { favoriteTokensByChainId } = useSelector((state: AppState) => state.user)
const { commonTokens } = useKyberSwapConfig(chainId)
const defaultTokens = useMemo(() => {
return { addresses: commonTokens || SUGGESTED_BASES[chainId || 1].map(e => e.address), includeNativeToken: true }
}, [commonTokens, chainId])

const favoriteTokens = useMemo(() => {
if (!chainId) return undefined
return favoriteTokensByChainId
? favoriteTokensByChainId[chainId] || getFavoriteTokenDefault(chainId)
: getFavoriteTokenDefault(chainId)
}, [chainId, favoriteTokensByChainId])
return favoriteTokensByChainId?.[chainId] || defaultTokens
}, [chainId, favoriteTokensByChainId, defaultTokens])

const toggleFavoriteToken = useCallback(
(payload: ToggleFavoriteTokenPayload) => dispatch(toggleFavoriteTokenAction(payload)),
[dispatch],
(payload: ToggleFavoriteTokenPayload) => {
dispatch(
toggleFavoriteTokenAction({
...payload,
defaultCommonTokens: defaultTokens,
}),
)
},
[dispatch, defaultTokens],
)

return { favoriteTokens, toggleFavoriteToken }
Expand Down
4 changes: 2 additions & 2 deletions src/state/user/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ export default createReducer(initialState, builder =>
.addCase(toggleKyberAIBanner, state => {
state.showKyberAIBanner = !state.showKyberAIBanner
})
.addCase(toggleFavoriteToken, (state, { payload: { chainId, isNative, address } }) => {
.addCase(toggleFavoriteToken, (state, { payload: { chainId, isNative, address, defaultCommonTokens } }) => {
if (!state.favoriteTokensByChainId) {
state.favoriteTokensByChainId = {}
}

let favoriteTokens = state.favoriteTokensByChainId[chainId]
if (!favoriteTokens) {
favoriteTokens = getFavoriteTokenDefault(chainId)
favoriteTokens = defaultCommonTokens
state.favoriteTokensByChainId[chainId] = favoriteTokens
}

Expand Down

0 comments on commit 1bec85e

Please sign in to comment.