From 6d6f31d7dae53ab1ff67ed45197b7b146ae870bf Mon Sep 17 00:00:00 2001 From: Josh DeCristi Date: Wed, 11 Oct 2023 19:36:38 -0400 Subject: [PATCH] feat: allow WETH when adding liquidity to an existsing LP position --- .../AddLiquidity/WrappedCurrencyToggle.tsx | 65 +++++++++++++++++++ src/pages/AddLiquidity/index.tsx | 26 ++++++-- 2 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 src/pages/AddLiquidity/WrappedCurrencyToggle.tsx diff --git a/src/pages/AddLiquidity/WrappedCurrencyToggle.tsx b/src/pages/AddLiquidity/WrappedCurrencyToggle.tsx new file mode 100644 index 00000000000..a29368390ce --- /dev/null +++ b/src/pages/AddLiquidity/WrappedCurrencyToggle.tsx @@ -0,0 +1,65 @@ +import { Trans } from '@lingui/macro' +import { useWeb3React } from '@web3-react/core' +import { ToggleElement, ToggleWrapper } from 'components/Toggle/MultiToggle' +import { useCurrency } from 'hooks/Tokens' + +import { WRAPPED_NATIVE_CURRENCY } from '../../constants/tokens' + +function isNative(currencyId?: string) { + return currencyId === 'ETH' +} + +function isWrappedNative(currencyId?: string, chainId?: number) { + return chainId !== undefined && currencyId === WRAPPED_NATIVE_CURRENCY[chainId]?.address +} + +type WrappedCurrencyToggleProps = { + currencyIdA?: string + currencyIdB?: string + onChangeCurrencies: (CurrencyAId?: string, currencyBId?: string) => void +} + +export function WrappedCurrencyToggle({ currencyIdA, currencyIdB, onChangeCurrencies }: WrappedCurrencyToggleProps) { + const { chainId } = useWeb3React() + + // Get native and wrapped native Currencies + const native = useCurrency('ETH', chainId) + const wrappedNative = chainId ? WRAPPED_NATIVE_CURRENCY[chainId] : undefined + + // checking to see if currencyIdA of currencyIdB is native or wrapped native + const currencyIsNative = isNative(currencyIdA) || isNative(currencyIdB) + const currencyIsWrappedNative = isWrappedNative(currencyIdA, chainId) || isWrappedNative(currencyIdB, chainId) + + if (!currencyIsNative && !currencyIsWrappedNative) return null + + const handleToggle = () => { + if (currencyIsNative) { + if (isNative(currencyIdA)) onChangeCurrencies(wrappedNative?.address, currencyIdB) + else onChangeCurrencies(currencyIdA, wrappedNative?.address) + } + + if (currencyIsWrappedNative) { + if (isWrappedNative(currencyIdA)) onChangeCurrencies('ETH', currencyIdA) + else onChangeCurrencies(currencyIdA, 'ETH') + } + } + + if (!currencyIsNative && !currencyIsWrappedNative) return null + + return ( + <> + {native && wrappedNative ? ( +
+ + + {native.symbol} + + + {wrappedNative.symbol} + + +
+ ) : null} + + ) +} diff --git a/src/pages/AddLiquidity/index.tsx b/src/pages/AddLiquidity/index.tsx index 0af60b354af..ad7fcc42b98 100644 --- a/src/pages/AddLiquidity/index.tsx +++ b/src/pages/AddLiquidity/index.tsx @@ -66,6 +66,7 @@ import { maxAmountSpend } from '../../utils/maxAmountSpend' import { Dots } from '../Pool/styled' import { Review } from './Review' import { DynamicSection, MediumOnly, ResponsiveTwoColumns, ScrollablePage, StyledInput, Wrapper } from './styled' +import { WrappedCurrencyToggle } from './WrappedCurrencyToggle' const DEFAULT_ADD_IN_RANGE_SLIPPAGE_TOLERANCE = new Percent(50, 10_000) @@ -86,8 +87,8 @@ export default function AddLiquidityWrapper() { function AddLiquidity() { const navigate = useNavigate() const { - currencyIdA, - currencyIdB, + currencyIdA: currencyIdAParam, + currencyIdB: currencyIdBParam, feeAmount: feeAmountFromUrl, tokenId, } = useParams<{ @@ -116,6 +117,8 @@ function AddLiquidity() { feeAmountFromUrl && Object.values(FeeAmount).includes(parseFloat(feeAmountFromUrl)) ? parseFloat(feeAmountFromUrl) : undefined + const [currencyIdA, setCurrencyIdA] = useState(currencyIdAParam) + const [currencyIdB, setCurrencyIdB] = useState(currencyIdBParam) const baseCurrency = useCurrency(currencyIdA) const currencyB = useCurrency(currencyIdB) @@ -564,6 +567,11 @@ function AddLiquidity() { addressesAreEquivalent(owner, account) || addressesAreEquivalent(existingPositionDetails?.operator, account) const showOwnershipWarning = Boolean(hasExistingPosition && account && !ownsNFT) + const handleChangeCurrencies = (currencyA?: string, currencyB?: string) => { + setCurrencyIdA(currencyA) + setCurrencyIdB(currencyB) + } + return ( <> @@ -863,9 +871,17 @@ function AddLiquidity() {
- - {hasExistingPosition ? Add more liquidity : Deposit amounts} - + + + {hasExistingPosition ? Add more liquidity : Deposit amounts} + + + +