diff --git a/src/components/CurrencyInputPanel/index.tsx b/src/components/CurrencyInputPanel/index.tsx index 5fe958ce7c..2e865df91c 100644 --- a/src/components/CurrencyInputPanel/index.tsx +++ b/src/components/CurrencyInputPanel/index.tsx @@ -248,14 +248,14 @@ export default function CurrencyInputPanel({ }: CurrencyInputPanelProps) { const tight = Boolean(tightProp && !currency) const [modalOpen, setModalOpen] = useState(false) - const { chainId, account } = useActiveWeb3React() + const { account } = useActiveWeb3React() - const selectedCurrencyBalance = useCurrencyBalance(currency ?? undefined) + const selectedCurrencyBalance = useCurrencyBalance(currency ?? undefined, customChainId) const balanceRef = useRef(selectedCurrencyBalance?.toSignificant(10)) useEffect(() => { balanceRef.current = undefined - }, [chainId]) + }, [customChainId]) // Keep previous value of balance if rpc node was down useEffect(() => { diff --git a/src/components/Header/web3/NetworkModal/DraggableNetworkButton.tsx b/src/components/Header/web3/NetworkModal/DraggableNetworkButton.tsx index 27785191d8..2cb3e219ea 100644 --- a/src/components/Header/web3/NetworkModal/DraggableNetworkButton.tsx +++ b/src/components/Header/web3/NetworkModal/DraggableNetworkButton.tsx @@ -162,10 +162,10 @@ export default function DraggableNetworkButton({ customOnSelectNetwork(chainId) } else if (getChainType(chainId) === getChainType(chainId)) { changeNetwork(chainId, () => { - const { inputCurrency, outputCurrency, ...rest } = qs + // const { inputCurrency, outputCurrency, ...rest } = qs navigate( { - search: stringify(rest), + search: stringify(qs), }, { replace: true }, ) diff --git a/src/components/SwapForm/InputCurrencyPanel.tsx b/src/components/SwapForm/InputCurrencyPanel.tsx index f0bc5ee572..7498cf7ee2 100644 --- a/src/components/SwapForm/InputCurrencyPanel.tsx +++ b/src/components/SwapForm/InputCurrencyPanel.tsx @@ -1,4 +1,4 @@ -import { Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' +import { ChainId, Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' import { useEffect } from 'react' import CurrencyInputPanel from 'components/CurrencyInputPanel' @@ -16,6 +16,7 @@ type Props = { balanceIn: CurrencyAmount | undefined onChangeCurrencyIn: (c: Currency) => void setTypedValue: (v: string) => void + customChainId?: ChainId } const InputCurrencyPanel: React.FC = ({ wrapType, @@ -25,6 +26,7 @@ const InputCurrencyPanel: React.FC = ({ currencyOut, balanceIn, onChangeCurrencyIn, + customChainId, }) => { const { isSolana } = useActiveWeb3React() @@ -64,6 +66,7 @@ const InputCurrencyPanel: React.FC = ({ dataTestId="swap-currency-input" showCommonBases={true} estimatedUsd={trade?.amountInUsd ? `${formattedNum(trade.amountInUsd.toString(), true)}` : undefined} + customChainId={customChainId} /> ) } diff --git a/src/components/SwapForm/OutputCurrencyPanel.tsx b/src/components/SwapForm/OutputCurrencyPanel.tsx index 1d59af0b81..7216a1affe 100644 --- a/src/components/SwapForm/OutputCurrencyPanel.tsx +++ b/src/components/SwapForm/OutputCurrencyPanel.tsx @@ -1,4 +1,4 @@ -import { Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' +import { ChainId, Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' import { Trans } from '@lingui/macro' import React from 'react' import { Text } from 'rebass' @@ -27,6 +27,7 @@ type Props = { amountOutUsd: string | undefined onChangeCurrencyOut: (c: Currency) => void + customChainId?: ChainId } const OutputCurrencyPanel: React.FC = ({ @@ -37,8 +38,10 @@ const OutputCurrencyPanel: React.FC = ({ currencyOut, amountOutUsd, onChangeCurrencyOut, + customChainId, }) => { - const { chainId } = useActiveWeb3React() + const { chainId: walletChainId } = useActiveWeb3React() + const chainId = customChainId || walletChainId // showWrap = true if this swap is either WRAP or UNWRAP const showWrap: boolean = wrapType !== WrapType.NOT_APPLICABLE @@ -101,6 +104,7 @@ const OutputCurrencyPanel: React.FC = ({ } positionLabel="in" + customChainId={customChainId} /> ) } diff --git a/src/components/SwapForm/SwapActionButton/index.tsx b/src/components/SwapForm/SwapActionButton/index.tsx index a6557f8303..65073d0139 100644 --- a/src/components/SwapForm/SwapActionButton/index.tsx +++ b/src/components/SwapForm/SwapActionButton/index.tsx @@ -1,4 +1,4 @@ -import { Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' +import { ChainId, Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' import { Trans } from '@lingui/macro' import { useEffect, useState } from 'react' import styled from 'styled-components' @@ -14,10 +14,12 @@ import { BuildRouteResult } from 'components/SwapForm/hooks/useBuildRoute' import { SwapCallbackError } from 'components/swapv2/styleds' import { useActiveWeb3React } from 'hooks' import { ApprovalState, useApproveCallback } from 'hooks/useApproveCallback' +import { NETWORKS_INFO } from 'hooks/useChainsConfig' import useMixpanel, { MIXPANEL_TYPE } from 'hooks/useMixpanel' import { PermitState, usePermit } from 'hooks/usePermit' import useTheme from 'hooks/useTheme' import { WrapType } from 'hooks/useWrapCallback' +import { useChangeNetwork } from 'hooks/web3/useChangeNetwork' import ApprovalModal from 'pages/SwapV3/ApprovalModal' import { ApplicationModal } from 'state/application/actions' import { useToggleModal, useWalletModalToggle } from 'state/application/hooks' @@ -57,6 +59,7 @@ type Props = { setProcessingSwap: React.Dispatch> onWrap: (() => Promise) | undefined buildRoute: () => Promise + customChainId?: ChainId } const SwapActionButton: React.FC = ({ @@ -80,9 +83,11 @@ const SwapActionButton: React.FC = ({ setProcessingSwap, onWrap, buildRoute, + customChainId, }) => { const theme = useTheme() - const { account, walletKey } = useActiveWeb3React() + const { changeNetwork } = useChangeNetwork() + const { account, walletKey, chainId } = useActiveWeb3React() const { mixpanelHandler } = useMixpanel() const [errorWhileSwap, setErrorWhileSwap] = useState('') const noRouteFound = routeSummary && !routeSummary.route @@ -199,6 +204,14 @@ const SwapActionButton: React.FC = ({ ) } + if (customChainId && customChainId !== chainId) { + return ( + changeNetwork(customChainId)}> + Swich to {NETWORKS_INFO[customChainId].name} + + ) + } + if (wrapInputError) { return {wrapInputError} } diff --git a/src/components/SwapForm/index.tsx b/src/components/SwapForm/index.tsx index 8f3b5e1fe7..1bae6d7326 100644 --- a/src/components/SwapForm/index.tsx +++ b/src/components/SwapForm/index.tsx @@ -1,15 +1,17 @@ -import { Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' +import { ChainId, Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core' import { Trans } from '@lingui/macro' import { rgba } from 'polished' import { stringify } from 'querystring' import { useCallback, useEffect, useMemo, useState } from 'react' -import { useLocation, useNavigate } from 'react-router-dom' +import { useLocation, useNavigate, useSearchParams } from 'react-router-dom' import { useMedia } from 'react-use' import { Box, Flex, Text } from 'rebass' import { parseGetRouteResponse } from 'services/route/utils' import styled from 'styled-components' +import { ReactComponent as DropdownSVG } from 'assets/svg/down.svg' import AddressInputPanel from 'components/AddressInputPanel' +import NetworkModal from 'components/Header/web3/NetworkModal' import { Clock } from 'components/Icons' import { AutoRow } from 'components/Row' import SlippageWarningNote from 'components/SlippageWarningNote' @@ -28,6 +30,7 @@ import TradePrice from 'components/swapv2/TradePrice' import { Wrapper } from 'components/swapv2/styleds' import { APP_PATHS } from 'constants/index' import { useActiveWeb3React } from 'hooks' +import { NETWORKS_INFO } from 'hooks/useChainsConfig' import useTheme from 'hooks/useTheme' import useWrapCallback, { WrapType } from 'hooks/useWrapCallback' import { PROFILE_MANAGE_ROUTES } from 'pages/NotificationCenter/const' @@ -59,6 +62,21 @@ const PriceAlertButton = styled.div` align-items: center; height: fit-content; ` + +const SelectNetwork = styled.div` + border: 999px; + font-size: 14px; + font-weight: 500; + padding: 6px 12px; + display: flex; + align-items: center; + gap: 8px; + color: ${({ theme }) => theme.subText}; + background: ${({ theme }) => theme.buttonBlack}; + border-radius: 999px; + cursor: pointer; +` + export type SwapFormProps = { hidden: boolean @@ -79,6 +97,8 @@ export type SwapFormProps = { onChangeCurrencyIn: (c: Currency) => void onChangeCurrencyOut: (c: Currency) => void goToSettingsView: () => void + customChainId?: ChainId + omniView?: boolean } const SwapForm: React.FC = props => { @@ -97,9 +117,12 @@ const SwapForm: React.FC = props => { permit, onChangeCurrencyIn, onChangeCurrencyOut, + customChainId, + omniView, } = props - const { isEVM, isSolana, chainId } = useActiveWeb3React() + const { isEVM, isSolana, chainId: walletChainId } = useActiveWeb3React() + const chainId = customChainId || walletChainId const navigate = useNavigate() const [isProcessingSwap, setProcessingSwap] = useState(false) const { typedValue } = useSwapState() @@ -120,7 +143,11 @@ const SwapForm: React.FC = props => { }, [onUserInput]) const parsedAmount = useParsedAmount(currencyIn, typedValue) - const { wrapType, inputError: wrapInputError, execute: onWrap } = useWrapCallback(currencyIn, currencyOut, typedValue) + const { + wrapType, + inputError: wrapInputError, + execute: onWrap, + } = useWrapCallback(currencyIn, currencyOut, typedValue, false, customChainId) const isWrapOrUnwrap = wrapType !== WrapType.NOT_APPLICABLE const isStablePairSwap = useCheckStablePairSwap(currencyIn, currencyOut) @@ -131,6 +158,7 @@ const SwapForm: React.FC = props => { isSaveGas, parsedAmount, isProcessingSwap, + customChain: chainId, }) const { data: getRouteRawResponse, isFetching: isGettingRoute, error: getRouteError } = result @@ -161,14 +189,6 @@ const SwapForm: React.FC = props => { parsedAmountFromTypedValue: parsedAmount, }) - const handleChangeCurrencyIn = (c: Currency) => { - onChangeCurrencyIn(c) - } - - const handleChangeCurrencyOut = (c: Currency) => { - onChangeCurrencyOut(c) - } - const isSolanaUnwrap = isSolana && wrapType === WrapType.UNWRAP useEffect(() => { // reset value for unwrapping WSOL @@ -181,6 +201,9 @@ const SwapForm: React.FC = props => { setRouteSummary(routeSummary) }, [routeSummary, setRouteSummary]) + const [isOpenNetworkModal, setIsOpenNetworkModal] = useState(false) + const [searchParams, setSearchParams] = useSearchParams() + return ( = props => { isStablePairSwap={isStablePairSwap} isAdvancedMode={isDegenMode} > + { + searchParams.set('chainId', chain.toString()) + setSearchParams(searchParams) + }} + isOpen={isOpenNetworkModal} + customToggleModal={() => setIsOpenNetworkModal(prev => !prev)} + /> +