Skip to content

Commit

Permalink
ensure useMax sets correct input value and recalculates output after …
Browse files Browse the repository at this point in the history
…typing in output field
  • Loading branch information
gabrieltemtsen committed Oct 3, 2024
1 parent 0a75cbb commit 611d739
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/features/swap/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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')
}
Expand Down

0 comments on commit 611d739

Please sign in to comment.