Skip to content

Commit

Permalink
use chains from config
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoaidanh committed Sep 11, 2023
1 parent 47b6d12 commit 584be2b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 15 deletions.
45 changes: 40 additions & 5 deletions src/components/Header/web3/NetworkModal/Networks.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChainId, getChainType } from '@kyberswap/ks-sdk-core'
import { Trans, t } from '@lingui/macro'
import { darken, rgba } from 'polished'
import { stringify } from 'querystring'
import React from 'react'
Expand All @@ -8,15 +9,30 @@ import styled, { css } from 'styled-components'

import { ButtonEmpty } from 'components/Button'
import { MouseoverTooltip } from 'components/Tooltip'
import { MAINNET_NETWORKS, NETWORKS_INFO } from 'constants/networks'
import { NetworkInfo } from 'constants/networks/type'
import { Z_INDEXS } from 'constants/styles'
import { SUPPORTED_WALLETS } from 'constants/wallets'
import { useActiveWeb3React } from 'hooks'
import useChainsConfig, { ChainState } from 'hooks/useChainsConfig'
import useParsedQueryString from 'hooks/useParsedQueryString'
import useTheme from 'hooks/useTheme'
import { useChangeNetwork } from 'hooks/web3/useChangeNetwork'
import { useIsDarkMode } from 'state/user/hooks'

const NewLabel = styled.span`
font-size: 12px;
color: ${({ theme }) => theme.red};
margin-left: 2px;
margin-top: -10px;
`

const MaintainLabel = styled.span`
font-size: 8px;
color: ${({ theme }) => theme.red};
margin-left: 2px;
margin-top: -10px;
`

const ListItem = styled.div<{ selected?: boolean }>`
width: 100%;
display: flex;
Expand Down Expand Up @@ -157,11 +173,14 @@ const Networks = ({
}
}

const { supportedChains } = useChainsConfig()

return (
<NetworkList mt={mt} mb={mb}>
{MAINNET_NETWORKS.map((itemChainId: ChainId, i: number) => {
const { iconDark, icon, name } = NETWORKS_INFO[itemChainId]
const disabled = !isAcceptedTerm || (activeChainIds ? !activeChainIds?.includes(itemChainId) : false)
{supportedChains.map(({ chainId: itemChainId, iconDark, icon, name, state }: NetworkInfo, i: number) => {
const isMaintenance = state === ChainState.MAINTENANCE
const disabled =
!isAcceptedTerm || (activeChainIds ? !activeChainIds?.includes(itemChainId) : false) || isMaintenance
const selected = selectedId === itemChainId && !isWrongNetwork

const imgSrc = (isDarkMode ? iconDark : icon) || icon
Expand All @@ -175,7 +194,13 @@ const Networks = ({
<MouseoverTooltip
style={{ zIndex: Z_INDEXS.MODAL + 1 }}
key={itemChainId}
text={disabled ? disabledMsg : ''}
text={
disabled
? isMaintenance
? t`Chain under maintenance. We will be back as soon as possible`
: disabledMsg
: ''
}
width="fit-content"
>
<SelectNetworkButton
Expand All @@ -197,6 +222,16 @@ const Networks = ({
{name}
</Text>
</Flex>
{state === ChainState.NEW && (
<NewLabel>
<Trans>New</Trans>
</NewLabel>
)}
{isMaintenance && (
<MaintainLabel>
<Trans>Maintainance</Trans>
</MaintainLabel>
)}
{selected && !walletKey && <CircleGreen />}
{walletKey && (
<WalletWrapper>
Expand Down
2 changes: 2 additions & 0 deletions src/constants/networks/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChainId } from '@kyberswap/ks-sdk-core'
import { PublicKey } from '@solana/web3.js'

import { EnvKeys } from 'constants/env'
import { ChainState } from 'hooks/useChainsConfig'

export interface NetworkInfo {
readonly chainId: ChainId
Expand Down Expand Up @@ -37,6 +38,7 @@ export interface NetworkInfo {
// USDT: Token
// }
readonly geckoTermialId: string | null
readonly state?: ChainState
}

export interface EVMNetworkInfo extends NetworkInfo {
Expand Down
39 changes: 39 additions & 0 deletions src/hooks/useChainsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { useMemo } from 'react'
import { useGetChainsConfigurationQuery } from 'services/ksSetting'

import { NETWORKS_INFO } from 'constants/networks'
import { useKyberswapGlobalConfig } from 'hooks/useKyberSwapConfig'

export enum ChainState {
NEW = 'new',
ACTIVE = 'active',
INACTIVE = 'inactive',
MAINTENANCE = 'maintainain',
}

export type ChainStateMap = { [chain in ChainId]: ChainState }

export default function useChainsConfig() {
const { data } = useGetChainsConfigurationQuery()
const globalConfig = useKyberswapGlobalConfig()

return useMemo(() => {
const chains = (data || []).map(chain => {
const chainId = +chain.chainId as ChainId
const chainState = globalConfig?.chainStates?.[chainId]
return {
...NETWORKS_INFO[chainId], // FE config
...chain, // BE config from admin
chainId,
state: chainState,
}
})
return {
activeChains: chains.filter(e => [ChainState.ACTIVE, ChainState.NEW].includes(e.state)),
supportedChains: chains.filter(e =>
[ChainState.ACTIVE, ChainState.NEW, ChainState.MAINTENANCE].includes(e.state),
),
}
}, [data, globalConfig])
}
3 changes: 3 additions & 0 deletions src/hooks/useKyberSwapConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { NETWORKS_INFO, SUPPORTED_NETWORKS, isEVM, isSolana } from 'constants/ne
import ethereumInfo from 'constants/networks/ethereum'
import solanaInfo from 'constants/networks/solana'
import { AppJsonRpcProvider } from 'constants/providers'
import { ChainStateMap } from 'hooks/useChainsConfig'
import { AppState } from 'state'
import { createClient } from 'utils/client'

Expand Down Expand Up @@ -58,6 +59,7 @@ type KyberswapGlobalConfig = {
aggregatorDomain: string
aggregatorAPI: string
isEnableAuthenAggregator: boolean
chainStates: ChainStateMap
}

const parseGlobalResponse = (
Expand All @@ -68,6 +70,7 @@ const parseGlobalResponse = (
const aggregatorDomain = data?.aggregator ?? AGGREGATOR_API
const isEnableAuthenAggregator = !!data?.isEnableAuthenAggregator
return {
chainStates: data?.chainStates || ({} as ChainStateMap),
aggregatorDomain,
aggregatorAPI: `${aggregatorDomain}/${NETWORKS_INFO[chainId].aggregatorRoute}/route/encode`,
isEnableAuthenAggregator,
Expand Down
18 changes: 9 additions & 9 deletions src/pages/About/AboutKyberSwap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ import AntiSnippingAttack from 'components/Icons/AntiSnippingAttack'
import ZkSyncFull from 'components/Icons/ZkSyncFull'
import Loader from 'components/Loader'
import { APP_PATHS } from 'constants/index'
import { MAINNET_NETWORKS, NETWORKS_INFO } from 'constants/networks'
import { NETWORKS_INFO } from 'constants/networks'
import { VERSION } from 'constants/v2'
import { useActiveWeb3React } from 'hooks'
import useChainsConfig from 'hooks/useChainsConfig'
import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel'
import useTheme from 'hooks/useTheme'
import { useGlobalData } from 'state/about/hooks'
Expand Down Expand Up @@ -121,6 +122,7 @@ const ForTraderInfoCell = styled.div`
export const KSStatistic = () => {
const theme = useTheme()
const upToLarge = useMedia(`(max-width: ${MEDIA_WIDTHS.upToLarge}px)`)
const { supportedChains } = useChainsConfig()

return (
<Box sx={{ position: 'relative', marginTop: '20px' }}>
Expand Down Expand Up @@ -153,7 +155,7 @@ export const KSStatistic = () => {
<ForTraderInfoRow>
<ForTraderInfoCell>
<Text fontWeight="600" fontSize="24px">
{MAINNET_NETWORKS.length}+
{supportedChains.length}+
</Text>
<Text color={theme.subText} marginTop="4px" fontSize="14px">
<Trans>Chains</Trans>
Expand Down Expand Up @@ -475,6 +477,8 @@ function AboutKyberSwap() {
)
}

const { supportedChains } = useChainsConfig()

return (
<div style={{ position: 'relative', background: isDarkMode ? theme.buttonBlack : theme.white, width: '100%' }}>
<AboutPage>
Expand All @@ -495,14 +499,10 @@ function AboutKyberSwap() {
</Text>

<SupportedChain>
{MAINNET_NETWORKS.map(chain => (
{supportedChains.map(({ chainId: chain, iconDark, icon, name }) => (
<img
src={
isDarkMode && NETWORKS_INFO[chain].iconDark
? NETWORKS_INFO[chain].iconDark || NETWORKS_INFO[chain].icon
: NETWORKS_INFO[chain].icon
}
alt={NETWORKS_INFO[chain].name}
src={isDarkMode && iconDark ? iconDark || icon : icon}
alt={name}
key={chain}
width="36px"
height="36px"
Expand Down
6 changes: 5 additions & 1 deletion src/pages/MyEarnings/MultipleChainSelect/PopoverBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SUPPORTED_NETWORKS_FOR_MY_EARNINGS,
} from 'constants/networks'
import { VERSION } from 'constants/v2'
import useChainsConfig from 'hooks/useChainsConfig'
import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel'
import useTheme from 'hooks/useTheme'
import { AppState } from 'state'
Expand Down Expand Up @@ -107,6 +108,7 @@ const PopoverBody: React.FC<Props> = ({ onClose }) => {

const isLegacy = useAppSelector(state => state.myEarnings.activeTab === VERSION.ELASTIC_LEGACY)
const isClassic = useAppSelector(state => state.myEarnings.activeTab === VERSION.CLASSIC)
const { activeChains } = useChainsConfig()

const comingSoonList = isLegacy
? COMING_SOON_NETWORKS_FOR_MY_EARNINGS_LEGACY
Expand All @@ -121,7 +123,9 @@ const PopoverBody: React.FC<Props> = ({ onClose }) => {

const [localSelectedChains, setLocalSelectedChains] = useState(() => selectedChains)

const networkList = SUPPORTED_NETWORKS_FOR_MY_EARNINGS.filter(item => !comingSoonList.includes(item))
const networkList = SUPPORTED_NETWORKS_FOR_MY_EARNINGS.filter(
item => !comingSoonList.includes(item) && activeChains.some(e => e.chainId === item),
)

const isAllSelected = localSelectedChains.length === networkList.length
const handleChangeChains = (chains: ChainId[]) => {
Expand Down
12 changes: 12 additions & 0 deletions src/services/ksSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Connection } from '@solana/web3.js'

import { KS_SETTING_API } from 'constants/env'
import { AppJsonRpcProvider } from 'constants/providers'
import { ChainStateMap } from 'hooks/useChainsConfig'
import { TokenInfo } from 'state/lists/wrappedTokenInfo'
import { TopToken } from 'state/topTokens/type'

Expand Down Expand Up @@ -41,6 +42,7 @@ export type KyberswapGlobalConfigurationResponse = {
config: {
aggregator: string
isEnableAuthenAggregator: boolean
chainStates: ChainStateMap
}
}
}
Expand Down Expand Up @@ -77,6 +79,15 @@ const ksSettingApi = createApi({
},
}),
}),
getChainsConfiguration: builder.query<{ chainId: string; displayName: string; logoUrl: string }[], void>({
query: () => ({
url: '/configurations/fetch',
params: {
serviceCode: `chains`,
},
}),
transformResponse: (data: any) => data?.data?.config,
}),

getTokenList: builder.query<
TokenListResponse,
Expand Down Expand Up @@ -111,6 +122,7 @@ export const {
useGetTokenListQuery,
useImportTokenMutation,
useLazyGetTopTokensQuery,
useGetChainsConfigurationQuery,
} = ksSettingApi

export default ksSettingApi

0 comments on commit 584be2b

Please sign in to comment.