Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use chains config from admin #2231

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 && (
nguyenhoaidanh marked this conversation as resolved.
Show resolved Hide resolved
<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
42 changes: 42 additions & 0 deletions src/hooks/useChainsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { useMemo } from 'react'
import { useGetChainsConfigurationQuery } from 'services/ksSetting'

import { MAINNET_NETWORKS, NETWORKS_INFO } from 'constants/networks'
import { NetworkInfo } from 'constants/networks/type'
import { useKyberswapGlobalConfig } from 'hooks/useKyberSwapConfig'

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

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

const defaultData = MAINNET_NETWORKS.map(chainId => NETWORKS_INFO[chainId])
export default function useChainsConfig() {
const { data } = useGetChainsConfigurationQuery()
const globalConfig = useKyberswapGlobalConfig()

return useMemo(() => {
const hasConfig = !!data
const chains: NetworkInfo[] = (data || defaultData).map(chain => {
const chainId = +chain.chainId as ChainId
const chainState = hasConfig ? globalConfig?.chainStates?.[chainId] : ChainState.ACTIVE
return {
...NETWORKS_INFO[chainId],
...chain, // BE config
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
17 changes: 17 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,20 @@ const ksSettingApi = createApi({
},
}),
}),
getChainsConfiguration: builder.query<{ chainId: string; name: string; icon: string }[], void>({
query: () => ({
url: '/configurations/fetch',
params: {
serviceCode: `chains`,
},
}),
transformResponse: (data: any) =>
data?.data?.config?.map((e: any) => ({
...e,
name: e.displayName,
icon: e.logoUrl,
})),
}),

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

export default ksSettingApi
3 changes: 2 additions & 1 deletion src/state/lists/updater.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import { useEffect } from 'react'
import { useDispatch } from 'react-redux'
import { useLazyGetTokenListQuery } from 'services/ksSetting'
import { useGetChainsConfigurationQuery, useLazyGetTokenListQuery } from 'services/ksSetting'

import { MAINNET_NETWORKS } from 'constants/networks'
import { TokenMap, formatAndCacheToken } from 'hooks/Tokens'
Expand Down Expand Up @@ -29,6 +29,7 @@ export default function Updater(): null {
const dispatch = useDispatch<AppDispatch>()

const [fetchTokenList] = useLazyGetTokenListQuery()
useGetChainsConfigurationQuery()

useEffect(() => {
const getTokens = async () => {
Expand Down
Loading