Skip to content

Commit

Permalink
🍡 Display balance with less precision digits (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartlomiej Tarczynski <[email protected]>
  • Loading branch information
mrushkova and b-tarczynski authored Apr 30, 2024
1 parent b4657f9 commit ff866b6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/frontend/src/components/form/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useAccount, useBalance } from 'wagmi'
import { formatInputAmount } from './formatInputAmount'
import { CloseCircleIcon, EtherIcon } from '../icons'
import { Colors } from '@/styles/colors'
import { formatEther } from 'viem'
import { formatBalance } from '@/utils/formatters/formatBalance'

interface InputProps {
initialAmount: string
Expand Down Expand Up @@ -63,7 +63,7 @@ export const Input = ({ initialAmount, setAmount, notEnoughBalance, bidTooLow }:

return (
<InputWrapper $userHasBid={!!userBid}>
<InputLabel>Balance: {userBalance !== undefined ? formatEther(userBalance) : '-'} ETH</InputLabel>
<InputLabel>Balance: {formatBalance(userBalance)} ETH</InputLabel>
<StyledInputWrapper $isBadAmount={notEnoughBalance || bidTooLow}>
<TokenIconWrapper>
<EtherIcon />
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend/src/components/form/ReviewForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Form, FormRow } from '.'
import { formatEther } from 'viem'
import { Button } from '../buttons'
import { heading } from '../auction/AuctionTransaction'
import { formatBalance } from '@/utils/formatters/formatBalance'

const amountLabel = {
[Transactions.Place]: 'Your Bid',
Expand Down Expand Up @@ -44,7 +45,7 @@ export const ReviewForm = ({ action: { status, ...action }, amount, impact, view
)}
<FormRow>
<span>Wallet Balance</span>
<span>{!!etherBalance && formatEther(etherBalance)} ETH</span>
<span>{formatBalance(etherBalance)} ETH</span>
</FormRow>
<Button view="primary" isLoading={isPending} onClick={sendTransaction} wide>
{heading[action.type]}
Expand Down
14 changes: 14 additions & 0 deletions packages/frontend/src/utils/formatters/formatBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { formatEther } from 'viem'

const decimalPlaces = 4
const factor = 10 ** decimalPlaces

export function formatBalance(balance: bigint | undefined) {
if (!balance) {
return '-'
}

const balanceStr = formatEther(balance)
const formattedValue = Math.round(Number(balanceStr) * factor) / factor
return formattedValue.toString()
}

0 comments on commit ff866b6

Please sign in to comment.