Skip to content

Commit 1b07e95

Browse files
authored
fix(add liquidity): fix the mint hooks to return a price as well as return the dependent amount in the input currency (#1011)
1 parent 9bb50d6 commit 1b07e95

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/pages/AddLiquidity/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export default function AddLiquidity({
311311
}}
312312
attemptingTxn={attemptingTxn}
313313
hash={txHash}
314-
topContent={() => modalHeader()}
314+
topContent={modalHeader}
315315
bottomContent={modalBottom}
316316
pendingText={pendingText}
317317
title={noLiquidity ? 'You are creating a pool' : 'You will receive'}

src/state/mint/hooks.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Currency, CurrencyAmount, JSBI, Pair, Percent, Price, TokenAmount } from '@uniswap/sdk'
1+
import { Currency, CurrencyAmount, ETHER, JSBI, Pair, Percent, Price, TokenAmount } from '@uniswap/sdk'
22
import { useCallback, useMemo } from 'react'
33
import { useDispatch, useSelector } from 'react-redux'
44
import { PairState, usePair } from '../../data/Reserves'
@@ -65,17 +65,24 @@ export function useDerivedMintInfo(
6565
}
6666

6767
// amounts
68-
const independentAmount = tryParseAmount(typedValue, currencies[independentField])
69-
const dependentAmount = useMemo(() => {
70-
if (noLiquidity && otherTypedValue && currencies[dependentField]) {
71-
return tryParseAmount(otherTypedValue, currencies[dependentField])
68+
const independentAmount: CurrencyAmount | undefined = tryParseAmount(typedValue, currencies[independentField])
69+
const dependentAmount: CurrencyAmount | undefined = useMemo(() => {
70+
if (noLiquidity) {
71+
if (otherTypedValue && currencies[dependentField]) {
72+
return tryParseAmount(otherTypedValue, currencies[dependentField])
73+
}
74+
return
7275
} else if (independentAmount) {
76+
// we wrap the currencies just to get the price in terms of the other token
7377
const wrappedIndependentAmount = wrappedCurrencyAmount(independentAmount, chainId)
7478
const [tokenA, tokenB] = [wrappedCurrency(currencyA, chainId), wrappedCurrency(currencyB, chainId)]
7579
if (tokenA && tokenB && wrappedIndependentAmount && pair) {
76-
return dependentField === Field.CURRENCY_B
77-
? pair.priceOf(tokenA).quote(wrappedIndependentAmount)
78-
: pair.priceOf(tokenB).quote(wrappedIndependentAmount)
80+
const dependentCurrency = dependentField === Field.CURRENCY_B ? currencyB : currencyA
81+
const dependentTokenAmount =
82+
dependentField === Field.CURRENCY_B
83+
? pair.priceOf(tokenA).quote(wrappedIndependentAmount)
84+
: pair.priceOf(tokenB).quote(wrappedIndependentAmount)
85+
return dependentCurrency === ETHER ? CurrencyAmount.ether(dependentTokenAmount.raw) : dependentTokenAmount
7986
}
8087
return
8188
} else {
@@ -89,12 +96,11 @@ export function useDerivedMintInfo(
8996

9097
const price = useMemo(() => {
9198
const { [Field.CURRENCY_A]: currencyAAmount, [Field.CURRENCY_B]: currencyBAmount } = parsedAmounts
92-
if (noLiquidity && currencyAAmount && currencyBAmount) {
99+
if (currencyAAmount && currencyBAmount) {
93100
return new Price(currencyAAmount.currency, currencyBAmount.currency, currencyAAmount.raw, currencyBAmount.raw)
94-
} else {
95-
return
96101
}
97-
}, [noLiquidity, parsedAmounts])
102+
return
103+
}, [parsedAmounts])
98104

99105
// liquidity minted
100106
const totalSupply = useTotalSupply(pair?.liquidityToken)

0 commit comments

Comments
 (0)