From ebdefab00faed767438bb1eda3a877c9e2601552 Mon Sep 17 00:00:00 2001 From: toniocodo Date: Sun, 1 Oct 2023 10:55:00 +0200 Subject: [PATCH] feat: leave 0.01ETH for gas on max click --- libs/shared/components/src/Inputs/TokenInput.tsx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libs/shared/components/src/Inputs/TokenInput.tsx b/libs/shared/components/src/Inputs/TokenInput.tsx index 178340083..f3ebe587a 100644 --- a/libs/shared/components/src/Inputs/TokenInput.tsx +++ b/libs/shared/components/src/Inputs/TokenInput.tsx @@ -7,7 +7,7 @@ import { isNilOrEmpty, } from '@origin/shared/utils'; import { useIntl } from 'react-intl'; -import { formatUnits } from 'viem'; +import { formatUnits, parseEther } from 'viem'; import { BigIntInput } from './BigIntInput'; @@ -16,6 +16,8 @@ import type { Token } from '@origin/shared/contracts'; import type { BigintInputProps } from './BigIntInput'; +const MIN_ETH_FOR_GAS = '0.01'; + export type TokenInputProps = { amount: bigint; decimals?: number; @@ -68,10 +70,17 @@ export const TokenInput = forwardRef( const intl = useIntl(); const handleMaxClick = () => { - onAmountChange(balance); + const max = + token.symbol === 'ETH' + ? balance - parseEther(MIN_ETH_FOR_GAS) + : balance; + onAmountChange(max); }; const amountUsd = +formatUnits(amount, decimals) * tokenPriceUsd; + const maxVisible = + !hideMaxButton && + (token.symbol === 'ETH' ? balance > parseEther(MIN_ETH_FOR_GAS) : true); const maxDisabled = disableMaxButton || !isConnected || isBalanceLoading; return ( @@ -148,7 +157,7 @@ export const TokenInput = forwardRef( }, )} - {!hideMaxButton && ( + {maxVisible && (