From 611d7391450a035922feabca99c38a204d55b316 Mon Sep 17 00:00:00 2001 From: Gabriel Temtsen Date: Thu, 3 Oct 2024 22:13:44 +0100 Subject: [PATCH] ensure useMax sets correct input value and recalculates output after typing in output field --- src/features/swap/SwapForm.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/features/swap/SwapForm.tsx b/src/features/swap/SwapForm.tsx index 05a42ce..81435c6 100644 --- a/src/features/swap/SwapForm.tsx +++ b/src/features/swap/SwapForm.tsx @@ -107,8 +107,10 @@ function SwapFormInputs({ balances }: { balances: AccountBalances }) { const { isLoading, quote, rate } = useSwapQuote(amount, direction, fromTokenId, toTokenId) useEffect(() => { - setFieldValue('quote', quote) - }, [quote, setFieldValue]) + if (values.direction === 'in' && quote) { + setFieldValue('quote', quote); + } + }, [quote, setFieldValue, values.direction]); useEffect(() => { if (chain && isConnected && !isSwappable(values.fromTokenId, values.toTokenId, chain?.id)) { @@ -122,7 +124,10 @@ function SwapFormInputs({ balances }: { balances: AccountBalances }) { const roundedBalance = fromWeiRounded(balances[fromTokenId], Tokens[fromTokenId].decimals) const isRoundedBalanceGreaterThanZero = Boolean(Number.parseInt(roundedBalance) > 0) const onClickUseMax = () => { - setFieldValue('amount', fromWei(balances[fromTokenId], Tokens[fromTokenId].decimals)) + const maxAmount = fromWei(balances[fromTokenId], Tokens[fromTokenId].decimals); + setFieldValue('amount', maxAmount); + + setFieldValue('direction', 'in'); if (fromTokenId === TokenId.CELO) { toast.warn('Consider keeping some CELO for transaction fees') }