Skip to content

Commit

Permalink
fix: prevent error if user selects ETH and WETH in add liquidity
Browse files Browse the repository at this point in the history
fixes #1763
  • Loading branch information
moodysalem committed Jul 26, 2021
1 parent 80b3aa9 commit 28617d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
8 changes: 5 additions & 3 deletions src/hooks/Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ export function useCurrency(currencyId: string | undefined): Currency | null | u
const { chainId } = useActiveWeb3React()
const isETH = currencyId?.toUpperCase() === 'ETH'
const token = useToken(isETH ? undefined : currencyId)
const extendedEther = useMemo(() => (chainId ? ExtendedEther.onChain(chainId) : undefined), [chainId])
const weth = chainId ? WETH9_EXTENDED[chainId] : undefined
if (!chainId) return undefined

const ether = ExtendedEther.onChain(chainId)
const weth = WETH9_EXTENDED[chainId]
if (weth?.address?.toLowerCase() === currencyId?.toLowerCase()) return weth
return isETH ? extendedEther : token
return isETH ? ether : token
}
37 changes: 16 additions & 21 deletions src/pages/AddLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,11 @@ export default function AddLiquidity({
? parseFloat(feeAmountFromUrl)
: undefined

const currencyA = useCurrency(currencyIdA)
const baseCurrency = useCurrency(currencyIdA)
const currencyB = useCurrency(currencyIdB)

// keep track for UI display purposes of user selected base currency
const baseCurrency = currencyA
const quoteCurrency = useMemo(
() =>
currencyA && currencyB && baseCurrency ? (baseCurrency.equals(currencyA) ? currencyB : currencyA) : undefined,
[currencyA, currencyB, baseCurrency]
)
// prevent an error if they input ETH/WETH
const quoteCurrency =
baseCurrency && currencyB && baseCurrency.wrapped.equals(currencyB.wrapped) ? undefined : currencyB

// mint state
const { independentField, typedValue, startPriceTypedValue } = useV3MintState()
Expand All @@ -135,8 +130,8 @@ export default function AddLiquidity({
invertPrice,
ticksAtLimit,
} = useV3DerivedMintInfo(
currencyA ?? undefined,
currencyB ?? undefined,
baseCurrency ?? undefined,
quoteCurrency ?? undefined,
feeAmount,
baseCurrency ?? undefined,
existingPosition
Expand All @@ -154,7 +149,7 @@ export default function AddLiquidity({
// capital efficiency warning
const [showCapitalEfficiencyWarning, setShowCapitalEfficiencyWarning] = useState(false)

useEffect(() => setShowCapitalEfficiencyWarning(false), [currencyA, currencyB, feeAmount])
useEffect(() => setShowCapitalEfficiencyWarning(false), [baseCurrency, quoteCurrency, feeAmount])

// txn values
const deadline = useTransactionDeadline() // custom from users settings
Expand Down Expand Up @@ -213,7 +208,7 @@ export default function AddLiquidity({
async function onCreate() {
if (!chainId || !library) return

if (!positionManager || !currencyA || !currencyB) {
if (!positionManager || !baseCurrency || !quoteCurrency) {
return
}

Expand Down Expand Up @@ -242,7 +237,7 @@ export default function AddLiquidity({
.then((response: TransactionResponse) => {
setAttemptingTxn(false)
addTransaction(response, {
summary: t`Create ${currencyA?.symbol}/${currencyB?.symbol} V3 pool`,
summary: t`Create ${baseCurrency?.symbol}/${quoteCurrency?.symbol} V3 pool`,
})
// dont set txn hash as we dont want submitted txn screen for create
ReactGA.event({
Expand All @@ -268,12 +263,12 @@ export default function AddLiquidity({
async function onAdd() {
if (!chainId || !library || !account) return

if (!positionManager || !currencyA || !currencyB) {
if (!positionManager || !baseCurrency || !quoteCurrency) {
return
}

if (position && account && deadline) {
const useNative = currencyA.isNative ? currencyA : currencyB.isNative ? currencyB : undefined
const useNative = baseCurrency.isNative ? baseCurrency : quoteCurrency.isNative ? quoteCurrency : undefined
const { calldata, value } =
hasExistingPosition && tokenId
? NonfungiblePositionManager.addCallParameters(position, {
Expand Down Expand Up @@ -338,8 +333,8 @@ export default function AddLiquidity({
setAttemptingTxn(false)
addTransaction(response, {
summary: noLiquidity
? t`Create pool and add ${currencyA?.symbol}/${currencyB?.symbol} V3 liquidity`
: t`Add ${currencyA?.symbol}/${currencyB?.symbol} V3 liquidity`,
? t`Create pool and add ${baseCurrency?.symbol}/${quoteCurrency?.symbol} V3 liquidity`
: t`Add ${baseCurrency?.symbol}/${quoteCurrency?.symbol} V3 liquidity`,
})
setTxHash(response.hash)
ReactGA.event({
Expand Down Expand Up @@ -663,11 +658,11 @@ export default function AddLiquidity({
</RowBetween>

<FeeSelector
disabled={!currencyB || !currencyA}
disabled={!quoteCurrency || !baseCurrency}
feeAmount={feeAmount}
handleFeePoolSelect={handleFeePoolSelect}
currencyA={currencyA ?? undefined}
currencyB={currencyB ?? undefined}
currencyA={baseCurrency ?? undefined}
currencyB={quoteCurrency ?? undefined}
/>
</AutoColumn>{' '}
</>
Expand Down

0 comments on commit 28617d9

Please sign in to comment.