Skip to content

Commit

Permalink
fix(add liquidity): fix the mint hooks to return a price as well as r…
Browse files Browse the repository at this point in the history
…eturn the dependent amount in the input currency (#1011)
  • Loading branch information
moodysalem authored Jul 28, 2020
1 parent 9bb50d6 commit 1b07e95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/pages/AddLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export default function AddLiquidity({
}}
attemptingTxn={attemptingTxn}
hash={txHash}
topContent={() => modalHeader()}
topContent={modalHeader}
bottomContent={modalBottom}
pendingText={pendingText}
title={noLiquidity ? 'You are creating a pool' : 'You will receive'}
Expand Down
30 changes: 18 additions & 12 deletions src/state/mint/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Currency, CurrencyAmount, JSBI, Pair, Percent, Price, TokenAmount } from '@uniswap/sdk'
import { Currency, CurrencyAmount, ETHER, JSBI, Pair, Percent, Price, TokenAmount } from '@uniswap/sdk'
import { useCallback, useMemo } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { PairState, usePair } from '../../data/Reserves'
Expand Down Expand Up @@ -65,17 +65,24 @@ export function useDerivedMintInfo(
}

// amounts
const independentAmount = tryParseAmount(typedValue, currencies[independentField])
const dependentAmount = useMemo(() => {
if (noLiquidity && otherTypedValue && currencies[dependentField]) {
return tryParseAmount(otherTypedValue, currencies[dependentField])
const independentAmount: CurrencyAmount | undefined = tryParseAmount(typedValue, currencies[independentField])
const dependentAmount: CurrencyAmount | undefined = useMemo(() => {
if (noLiquidity) {
if (otherTypedValue && currencies[dependentField]) {
return tryParseAmount(otherTypedValue, currencies[dependentField])
}
return
} else if (independentAmount) {
// we wrap the currencies just to get the price in terms of the other token
const wrappedIndependentAmount = wrappedCurrencyAmount(independentAmount, chainId)
const [tokenA, tokenB] = [wrappedCurrency(currencyA, chainId), wrappedCurrency(currencyB, chainId)]
if (tokenA && tokenB && wrappedIndependentAmount && pair) {
return dependentField === Field.CURRENCY_B
? pair.priceOf(tokenA).quote(wrappedIndependentAmount)
: pair.priceOf(tokenB).quote(wrappedIndependentAmount)
const dependentCurrency = dependentField === Field.CURRENCY_B ? currencyB : currencyA
const dependentTokenAmount =
dependentField === Field.CURRENCY_B
? pair.priceOf(tokenA).quote(wrappedIndependentAmount)
: pair.priceOf(tokenB).quote(wrappedIndependentAmount)
return dependentCurrency === ETHER ? CurrencyAmount.ether(dependentTokenAmount.raw) : dependentTokenAmount
}
return
} else {
Expand All @@ -89,12 +96,11 @@ export function useDerivedMintInfo(

const price = useMemo(() => {
const { [Field.CURRENCY_A]: currencyAAmount, [Field.CURRENCY_B]: currencyBAmount } = parsedAmounts
if (noLiquidity && currencyAAmount && currencyBAmount) {
if (currencyAAmount && currencyBAmount) {
return new Price(currencyAAmount.currency, currencyBAmount.currency, currencyAAmount.raw, currencyBAmount.raw)
} else {
return
}
}, [noLiquidity, parsedAmounts])
return
}, [parsedAmounts])

// liquidity minted
const totalSupply = useTotalSupply(pair?.liquidityToken)
Expand Down

1 comment on commit 1b07e95

@vercel
Copy link

@vercel vercel bot commented on 1b07e95 Jul 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.