Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍡 Display balance with less precision digits #51

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}