Commit 78b6f5c7 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: destructure currencies for ref equality (#3498)

parent f9fb71a8
...@@ -36,66 +36,53 @@ const BAD_RECIPIENT_ADDRESSES: { [address: string]: true } = { ...@@ -36,66 +36,53 @@ const BAD_RECIPIENT_ADDRESSES: { [address: string]: true } = {
// from the current swap inputs, compute the best trade and return it. // from the current swap inputs, compute the best trade and return it.
function useComputeSwapInfo(): SwapInfo { function useComputeSwapInfo(): SwapInfo {
const { account } = useActiveWeb3React() const { account } = useActiveWeb3React()
const { type: wrapType } = useWrapCallback()
const isWrapping = wrapType === WrapType.WRAP || wrapType === WrapType.UNWRAP
const { const {
independentField, independentField,
amount, amount,
[Field.INPUT]: inputCurrency, [Field.INPUT]: inputCurrency,
[Field.OUTPUT]: outputCurrency, [Field.OUTPUT]: outputCurrency,
} = useAtomValue(swapAtom) } = useAtomValue(swapAtom)
const isExactIn = independentField === Field.INPUT
const feeOptions = useAtomValue(feeOptionsAtom) const feeOptions = useAtomValue(feeOptionsAtom)
const to = account
const relevantTokenBalances = useCurrencyBalances(
account,
useMemo(() => [inputCurrency ?? undefined, outputCurrency ?? undefined], [inputCurrency, outputCurrency])
)
const isExactIn = independentField === Field.INPUT
const parsedAmount = useMemo( const parsedAmount = useMemo(
() => tryParseCurrencyAmount(amount, (isExactIn ? inputCurrency : outputCurrency) ?? undefined), () => tryParseCurrencyAmount(amount, (isExactIn ? inputCurrency : outputCurrency) ?? undefined),
[inputCurrency, isExactIn, outputCurrency, amount] [inputCurrency, isExactIn, outputCurrency, amount]
) )
// TODO(ianlapham): this would eventually be replaced with routing api logic.
//@TODO(ianlapham): this would eventually be replaced with routing api logic.
const trade = useBestTrade( const trade = useBestTrade(
isExactIn ? TradeType.EXACT_INPUT : TradeType.EXACT_OUTPUT, isExactIn ? TradeType.EXACT_INPUT : TradeType.EXACT_OUTPUT,
parsedAmount, parsedAmount,
(isExactIn ? outputCurrency : inputCurrency) ?? undefined (isExactIn ? outputCurrency : inputCurrency) ?? undefined
) )
const currencies = useMemo(
() => ({
[Field.INPUT]: inputCurrency ?? undefined,
[Field.OUTPUT]: outputCurrency ?? undefined,
}),
[inputCurrency, outputCurrency]
)
const currencyBalances = useMemo(
() => ({
[Field.INPUT]: relevantTokenBalances[0],
[Field.OUTPUT]: relevantTokenBalances[1],
}),
[relevantTokenBalances]
)
// Use same amount for input and output if user is wrapping.
const { type: wrapType } = useWrapCallback()
const isWrapping = wrapType === WrapType.WRAP || wrapType === WrapType.UNWRAP
const tradeCurrencyAmounts = useMemo( const tradeCurrencyAmounts = useMemo(
() => ({ () => ({
// Use same amount for input and output if user is wrapping.
[Field.INPUT]: isWrapping ? parsedAmount : trade.trade?.inputAmount, [Field.INPUT]: isWrapping ? parsedAmount : trade.trade?.inputAmount,
[Field.OUTPUT]: isWrapping ? parsedAmount : trade.trade?.outputAmount, [Field.OUTPUT]: isWrapping ? parsedAmount : trade.trade?.outputAmount,
}), }),
[isWrapping, parsedAmount, trade.trade?.inputAmount, trade.trade?.outputAmount] [isWrapping, parsedAmount, trade.trade?.inputAmount, trade.trade?.outputAmount]
) )
const slippage = useSlippage(trade.trade) const slippage = useSlippage(trade.trade)
const currencies = useMemo(
() => ({ [Field.INPUT]: inputCurrency, [Field.OUTPUT]: outputCurrency }),
[inputCurrency, outputCurrency]
)
const [inputCurrencyBalance, outputCurrencyBalance] = useCurrencyBalances(
account,
useMemo(() => [inputCurrency, outputCurrency], [inputCurrency, outputCurrency])
)
const currencyBalances = useMemo(
() => ({
[Field.INPUT]: inputCurrencyBalance,
[Field.OUTPUT]: outputCurrencyBalance,
}),
[inputCurrencyBalance, outputCurrencyBalance]
)
const inputError = useMemo(() => { const inputError = useMemo(() => {
let inputError: ReactNode | undefined let inputError: ReactNode | undefined
...@@ -111,11 +98,11 @@ function useComputeSwapInfo(): SwapInfo { ...@@ -111,11 +98,11 @@ function useComputeSwapInfo(): SwapInfo {
inputError = inputError ?? <Trans>Enter an amount</Trans> inputError = inputError ?? <Trans>Enter an amount</Trans>
} }
const formattedTo = isAddress(to) const formattedAddress = isAddress(account)
if (!to || !formattedTo) { if (!account || !formattedAddress) {
inputError = inputError ?? <Trans>Enter a recipient</Trans> inputError = inputError ?? <Trans>Enter a recipient</Trans>
} else { } else {
if (BAD_RECIPIENT_ADDRESSES[formattedTo]) { if (BAD_RECIPIENT_ADDRESSES[formattedAddress]) {
inputError = inputError ?? <Trans>Invalid recipient</Trans> inputError = inputError ?? <Trans>Invalid recipient</Trans>
} }
} }
...@@ -128,7 +115,7 @@ function useComputeSwapInfo(): SwapInfo { ...@@ -128,7 +115,7 @@ function useComputeSwapInfo(): SwapInfo {
} }
return inputError return inputError
}, [account, slippage.allowed, currencies, currencyBalances, parsedAmount, to, trade.trade]) }, [account, slippage.allowed, currencies, currencyBalances, parsedAmount, trade.trade])
return useMemo( return useMemo(
() => ({ () => ({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment