Skip to content

Commit

Permalink
feat: move token amount formatting to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed May 24, 2022
1 parent acd71c0 commit ec55aa1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { InputAdornment, Skeleton } from '@mui/material';
import { useMemo } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useTokenBalance } from '../../hooks';
import {
SwapFormKeyHelper,
SwapFormTypeProps,
} from '../../providers/SwapFormProvider';
import { formatTokenAmount, formatTokenPrice } from '../../utils/format';
import { formatTokenPrice } from '../../utils/format';
import { PriceTypography } from '../PriceTypography';
import { SwapMaxAmountTypography } from './SwapInputAdornment.style';

Expand All @@ -27,13 +26,8 @@ export const SwapInputAdornment: React.FC<SwapFormTypeProps> = ({
tokenAddress,
);

const amount = useMemo(
() => (token?.amount ? formatTokenAmount(token.amount) : null),
[token],
);

const handleMax = () => {
setValue(SwapFormKeyHelper.getAmountKey(formType), amount);
setValue(SwapFormKeyHelper.getAmountKey(formType), token?.amount ?? '');
};

return (
Expand All @@ -56,7 +50,7 @@ export const SwapInputAdornment: React.FC<SwapFormTypeProps> = ({
data-amount={token?.amount}
>
{t(`swap.maxAmount`, {
amount,
amount: token?.amount,
})}
</SwapMaxAmountTypography>
) : null
Expand Down
6 changes: 5 additions & 1 deletion packages/widget/src/hooks/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback } from 'react';
import { useQuery, useQueryClient } from 'react-query';
import { LiFi } from '../lifi';
import { useWallet } from '../providers/WalletProvider';
import { formatTokenAmount } from '../utils/format';
import { useToken } from './useToken';

export const useTokenBalance = (chainId: number, tokenAddress: string) => {
Expand All @@ -22,7 +23,10 @@ export const useTokenBalance = (chainId: number, tokenAddress: string) => {
return null;
}
const tokenBalance = await LiFi.getTokenBalance(address as string, token);
return tokenBalance;
return {
...tokenBalance,
amount: formatTokenAmount(tokenBalance?.amount),
};
},
{
enabled: Boolean(account.address) && Boolean(token),
Expand Down

0 comments on commit ec55aa1

Please sign in to comment.