From 59ad71442ac9b3997cc8b837d3be0f35c0665dac Mon Sep 17 00:00:00 2001 From: schmanu Date: Fri, 20 Oct 2023 15:44:59 +0200 Subject: [PATCH] feat: show wallet balance in header --- .../common/ConnectWallet/AccountCenter.tsx | 2 +- src/components/common/WalletInfo/index.tsx | 33 +++++++++++++++---- .../common/WalletInfo/styles.module.css | 2 +- src/hooks/wallets/useWalletBalance.ts | 19 +++++++++++ 4 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 src/hooks/wallets/useWalletBalance.ts diff --git a/src/components/common/ConnectWallet/AccountCenter.tsx b/src/components/common/ConnectWallet/AccountCenter.tsx index 03a0aab974..d9d13a4e07 100644 --- a/src/components/common/ConnectWallet/AccountCenter.tsx +++ b/src/components/common/ConnectWallet/AccountCenter.tsx @@ -1,6 +1,6 @@ import type { MouseEvent } from 'react' import { useState } from 'react' -import { Box, Button, ButtonBase, Paper, Popover, Typography } from '@mui/material' +import { Box, Button, ButtonBase, Paper, Popover, Skeleton, Typography } from '@mui/material' import css from '@/components/common/ConnectWallet/styles.module.css' import EthHashInfo from '@/components/common/EthHashInfo' import ExpandLessIcon from '@mui/icons-material/ExpandLess' diff --git a/src/components/common/WalletInfo/index.tsx b/src/components/common/WalletInfo/index.tsx index 8cbd0c8f2a..9d8da8c74e 100644 --- a/src/components/common/WalletInfo/index.tsx +++ b/src/components/common/WalletInfo/index.tsx @@ -1,4 +1,4 @@ -import { Box, Typography } from '@mui/material' +import { Box, Skeleton, Typography } from '@mui/material' import { Suspense } from 'react' import type { ReactElement } from 'react' @@ -9,33 +9,52 @@ import { useAppSelector } from '@/store' import { selectChainById } from '@/store/chainsSlice' import css from './styles.module.css' +import useWalletBalance from '@/hooks/wallets/useWalletBalance' +import { formatVisualAmount } from '@/utils/formatters' export const UNKNOWN_CHAIN_NAME = 'Unknown' const WalletInfo = ({ wallet }: { wallet: ConnectedWallet }): ReactElement => { const walletChain = useAppSelector((state) => selectChainById(state, wallet.chainId)) const prefix = walletChain?.shortName + const [balance, balanceError, balanceLoading] = useWalletBalance() return ( - + {' '} - - {wallet.label} @ {walletChain?.chainName || UNKNOWN_CHAIN_NAME} - - {wallet.ens ? (
{wallet.ens}
) : ( - + )}
+ + + + {balance !== undefined ? ( + <> + {' '} + {formatVisualAmount(balance.toString(10), walletChain?.nativeCurrency.decimals ?? 18)}{' '} + {walletChain?.nativeCurrency.symbol ?? 'ETH'} + + ) : ( + + )} + +
) diff --git a/src/components/common/WalletInfo/styles.module.css b/src/components/common/WalletInfo/styles.module.css index 8b7242711c..62a9412e2d 100644 --- a/src/components/common/WalletInfo/styles.module.css +++ b/src/components/common/WalletInfo/styles.module.css @@ -28,7 +28,7 @@ height: auto; } - .walletName { + .balance { display: none; } } diff --git a/src/hooks/wallets/useWalletBalance.ts b/src/hooks/wallets/useWalletBalance.ts new file mode 100644 index 0000000000..e2117a0985 --- /dev/null +++ b/src/hooks/wallets/useWalletBalance.ts @@ -0,0 +1,19 @@ +import useAsync from '../useAsync' +import useWallet from './useWallet' + +const useWalletBalance = () => { + const wallet = useWallet() + + return useAsync(async () => { + if (!wallet) { + return undefined + } + const balanceString = await wallet.provider.request({ + method: 'eth_getBalance', + params: [wallet.address, 'latest'], + }) + return BigInt(balanceString) + }, [wallet]) +} + +export default useWalletBalance