Skip to content

Commit

Permalink
fix: Add info to tx creation
Browse files Browse the repository at this point in the history
  • Loading branch information
usame-algan committed Oct 20, 2023
1 parent b2e06df commit 6dfa491
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/components/common/ConnectWallet/AccountCenter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const AccountCenter = ({ wallet }: { wallet: ConnectedWallet }) => {
<>
<ButtonBase onClick={handleClick} aria-describedby={id} disableRipple sx={{ alignSelf: 'stretch' }}>
<Box className={css.buttonContainer}>
<WalletInfo wallet={wallet} balance={balance} />
<WalletInfo wallet={wallet} balance={balance} showBalance />

<Box display="flex" alignItems="center" justifyContent="flex-end" marginLeft="auto">
{open ? <ExpandLessIcon color="border" /> : <ExpandMoreIcon color="border" />}
Expand Down Expand Up @@ -109,7 +109,7 @@ const AccountCenter = ({ wallet }: { wallet: ConnectedWallet }) => {
<Box className={css.row}>
<Typography variant="caption">Balance</Typography>
<Typography variant="body2">
<WalletBalance chainInfo={chainInfo} balance={balance} />
<WalletBalance balance={balance} />
</Typography>
</Box>
</Box>
Expand Down
11 changes: 7 additions & 4 deletions src/components/common/WalletBalance/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { type ChainInfo } from '@safe-global/safe-gateway-typescript-sdk'
import { formatVisualAmount } from '@/utils/formatters'
import { Skeleton } from '@mui/material'
import { type BigNumber } from 'ethers'
import { useCurrentChain } from '@/hooks/useChains'

const WalletBalance = ({ balance }: { balance: BigNumber | undefined }) => {
const currentChain = useCurrentChain()

const WalletBalance = ({ chainInfo, balance }: { chainInfo: ChainInfo | undefined; balance: BigInt | undefined }) => {
if (!balance) {
return <Skeleton width={30} />
}

return (
<>
{formatVisualAmount(balance.toString(10), chainInfo?.nativeCurrency.decimals ?? 18)}{' '}
{chainInfo?.nativeCurrency.symbol ?? 'ETH'}
{formatVisualAmount(balance, currentChain?.nativeCurrency.decimals ?? 18)}{' '}
{currentChain?.nativeCurrency.symbol ?? 'ETH'}
</>
)
}
Expand Down
23 changes: 17 additions & 6 deletions src/components/common/WalletInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ import css from './styles.module.css'
import Identicon from '@/components/common/Identicon'
import classnames from 'classnames'
import WalletBalance from '@/components/common/WalletBalance'
import { type BigNumber } from 'ethers'

export const UNKNOWN_CHAIN_NAME = 'Unknown'

const WalletInfo = ({ wallet, balance }: { wallet: ConnectedWallet; balance: BigInt | undefined }): ReactElement => {
const WalletInfo = ({
wallet,
balance,
showBalance = false,
}: {
wallet: ConnectedWallet
balance?: BigNumber | undefined
showBalance?: boolean
}): ReactElement => {
const walletChain = useAppSelector((state) => selectChainById(state, wallet.chainId))
const prefix = walletChain?.shortName

Expand Down Expand Up @@ -47,11 +56,13 @@ const WalletInfo = ({ wallet, balance }: { wallet: ConnectedWallet; balance: Big
)}
</Typography>

<Box display="flex" flexDirection="row" alignItems="center" gap={1}>
<Typography variant="caption" component="div" fontWeight="bold" className={css.balance}>
<WalletBalance chainInfo={walletChain} balance={balance} />
</Typography>
</Box>
{showBalance && (
<Box display="flex" flexDirection="row" alignItems="center" gap={1}>
<Typography variant="caption" component="div" fontWeight="bold" className={css.balance}>
<WalletBalance balance={balance} />
</Typography>
</Box>
)}
</Box>
</Box>
)
Expand Down
28 changes: 28 additions & 0 deletions src/components/tx/BalanceInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Box, Stack, SvgIcon, Typography } from '@mui/material'
import GasStationIcon from '@/public/images/common/gas-station.svg'
import css from './styles.module.css'
import useWalletBalance from '@/hooks/wallets/useWalletBalance'
import WalletBalance from '@/components/common/WalletBalance'

const BalanceInfo = () => {
const [balance] = useWalletBalance()

return (
<Box className={css.sponsoredBy}>
<SvgIcon component={GasStationIcon} inheritViewBox className={css.icon} />
<div>
<Stack direction="row" spacing={0.5} alignItems="center">
<Typography variant="body2" fontWeight={700} letterSpacing="0.1px">
Wallet balance
</Typography>
</Stack>

<Typography variant="body2" color="primary.light">
<WalletBalance balance={balance} />
</Typography>
</div>
</Box>
)
}

export default BalanceInfo
20 changes: 20 additions & 0 deletions src/components/tx/BalanceInfo/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.sponsoredBy {
padding: 8px 12px;
background-color: var(--color-background-main);
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
border-top: 1px solid var(--color-border-light);
display: flex;
}

.icon {
margin-right: 8px;
margin-top: -1px;
color: var(--color-text-secondary);
width: 24px;
}

.logo {
width: 16px;
height: 16px;
}
3 changes: 2 additions & 1 deletion src/components/tx/ExecutionMethodSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SponsoredBy from '../SponsoredBy'
import type { RelayResponse } from '@/services/tx/relaying'

import css from './styles.module.css'
import BalanceInfo from '@/components/tx/BalanceInfo'

export const enum ExecutionMethod {
RELAY = 'RELAY',
Expand Down Expand Up @@ -71,7 +72,7 @@ export const ExecutionMethodSelector = ({
</FormControl>
</div>

{shouldRelay && relays ? <SponsoredBy relays={relays} tooltip={tooltip} /> : null}
{shouldRelay && relays ? <SponsoredBy relays={relays} tooltip={tooltip} /> : wallet ? <BalanceInfo /> : null}
</Box>
)
}
16 changes: 8 additions & 8 deletions src/hooks/wallets/useWalletBalance.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import useAsync from '../useAsync'
import useWallet from './useWallet'
import { useWeb3ReadOnly } from '@/hooks/wallets/web3'
import { type BigNumber } from 'ethers'

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

return useAsync<BigInt | undefined>(async () => {
if (!wallet) {
return useAsync<BigNumber | undefined>(() => {
if (!wallet || !web3ReadOnly) {
return undefined
}
const balanceString = await wallet.provider.request({
method: 'eth_getBalance',
params: [wallet.address, 'latest'],
})
return BigInt(balanceString)
}, [wallet])

return web3ReadOnly.getBalance(wallet.address, 'latest')
}, [wallet, web3ReadOnly])
}

export default useWalletBalance

0 comments on commit 6dfa491

Please sign in to comment.