Skip to content

Commit

Permalink
feat: leave 0.01ETH for gas on max click
Browse files Browse the repository at this point in the history
  • Loading branch information
toniocodo committed Oct 1, 2023
1 parent f8c6418 commit ebdefab
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions libs/shared/components/src/Inputs/TokenInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
Expand Down Expand Up @@ -68,10 +70,17 @@ export const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>(
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 (
Expand Down Expand Up @@ -148,7 +157,7 @@ export const TokenInput = forwardRef<HTMLInputElement, TokenInputProps>(
},
)}
</Typography>
{!hideMaxButton && (
{maxVisible && (
<Button
onClick={handleMaxClick}
disabled={maxDisabled}
Expand Down

0 comments on commit ebdefab

Please sign in to comment.