From f96862f5d31264aabed232b265a0f49f6f1afba1 Mon Sep 17 00:00:00 2001 From: viet-nv Date: Tue, 14 Nov 2023 11:38:24 +0700 Subject: [PATCH] fix: sync app chainId and swapform chainId when not connected --- src/pages/PartnerSwap/index.tsx | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/pages/PartnerSwap/index.tsx b/src/pages/PartnerSwap/index.tsx index 993b1208cd..3c81ae1f27 100644 --- a/src/pages/PartnerSwap/index.tsx +++ b/src/pages/PartnerSwap/index.tsx @@ -31,8 +31,10 @@ import { import { TRANSACTION_STATE_DEFAULT } from 'constants/index' import { SUPPORTED_NETWORKS } from 'constants/networks' import { DEFAULT_OUTPUT_TOKEN_BY_CHAIN, NativeCurrencies } from 'constants/tokens' +import { useActiveWeb3React } from 'hooks' import { useAllTokens, useCurrencyV2 } from 'hooks/Tokens' import useTheme from 'hooks/useTheme' +import { useChangeNetwork } from 'hooks/web3/useChangeNetwork' import { BodyWrapper } from 'pages/AppBody' import CrossChain from 'pages/CrossChain' import CrossChainLink from 'pages/CrossChain/CrossChainLink' @@ -86,6 +88,8 @@ export const RoutingIconWrapper = styled(RoutingIcon)` ` export default function Swap() { + const { account, chainId: walletChainId } = useActiveWeb3React() + const { changeNetwork } = useChangeNetwork() const [searchParams, setSearchParams] = useSearchParams() const chainIdFromParam = searchParams.get('chainId') @@ -94,6 +98,13 @@ export default function Swap() { const expectedChainId = chainIdFromParam && SUPPORTED_NETWORKS.includes(+chainIdFromParam) ? +chainIdFromParam : ChainId.MAINNET + // sync form chainId and wallet chainId when disconnected + useEffect(() => { + if (!account && walletChainId !== expectedChainId) { + changeNetwork(expectedChainId) + } + }, [account, walletChainId, expectedChainId, changeNetwork]) + const currencyIn = useCurrencyV2(inputTokenFromParam || undefined, expectedChainId) || NativeCurrencies[expectedChainId as ChainId] const currencyOut = @@ -135,20 +146,8 @@ export default function Swap() { const theme = useTheme() - // dismiss warning if all imported tokens are in active lists const allTokens = useAllTokens() - // const handleConfirmTokenWarning = useCallback( - // (tokens: Currency[]) => { - // // handleDismissTokenWarning() - // if (isLimitPage) { - // onSelectPairLimit(tokens[0], tokens[1]) - // setIsSelectCurrencyManually(true) - // } - // }, - // [isLimitPage, onSelectPairLimit], - // ) - const onBackToSwapTab = () => setActiveTab(previousTab || TAB.SWAP) const tradeRouteComposition = useMemo(() => {