Skip to content

Commit

Permalink
feat: show wallet balance in header
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Oct 20, 2023
1 parent d81ee92 commit 59ad714
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/common/ConnectWallet/AccountCenter.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
33 changes: 26 additions & 7 deletions src/components/common/WalletInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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 (
<Box className={css.container}>
<Box className={css.imageContainer}>
<Suspense>
<WalletIcon provider={wallet.label} icon={wallet.icon} />
</Suspense>
</Suspense>{' '}
</Box>

<Box className={css.walletDetails}>
<Typography variant="caption" component="div" className={css.walletName}>
{wallet.label} @ {walletChain?.chainName || UNKNOWN_CHAIN_NAME}
</Typography>

<Typography variant="caption" fontWeight="bold" component="div">
{wallet.ens ? (
<div>{wallet.ens}</div>
) : (
<EthHashInfo prefix={prefix || ''} address={wallet.address} showName={false} showAvatar avatarSize={12} />
<EthHashInfo
showPrefix={false}
prefix={prefix || ''}
address={wallet.address}
showName={false}
showAvatar={false}
/>
)}
</Typography>

<Box display="flex" flexDirection="row" alignItems="center" gap={1}>
<Typography variant="caption" component="div" className={css.balance}>
{balance !== undefined ? (
<>
{' '}
{formatVisualAmount(balance.toString(10), walletChain?.nativeCurrency.decimals ?? 18)}{' '}
{walletChain?.nativeCurrency.symbol ?? 'ETH'}
</>
) : (
<Skeleton />
)}
</Typography>
</Box>
</Box>
</Box>
)
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/WalletInfo/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
height: auto;
}

.walletName {
.balance {
display: none;
}
}
19 changes: 19 additions & 0 deletions src/hooks/wallets/useWalletBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import useAsync from '../useAsync'
import useWallet from './useWallet'

const useWalletBalance = () => {
const wallet = useWallet()

return useAsync<BigInt | undefined>(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

0 comments on commit 59ad714

Please sign in to comment.