Skip to content

Commit

Permalink
format balances
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Dec 19, 2024
1 parent 9109bfc commit 2d4e83f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@hookform/resolvers": "3.9.0",
"@polkadot/api": "14.3.1",
"@polkadot/react-identicon": "3.11.3",
"@polkadot/util": "13.2.3",
"@tanstack/react-query": "5.61.5",
"@web3modal/wagmi": "5.1.3",
"graphql": "16.9.0",
Expand Down
3 changes: 3 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions frontend/src/components/formatted-balance/fortmatted-balance.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { formatBalance } from '@polkadot/util';
import { formatUnits } from 'viem';

import { cx } from '@/utils';
Expand All @@ -6,16 +7,6 @@ import { Tooltip } from '../tooltip';

import styles from './fortmatted-balance.module.scss';

// simplest solution without rounding for now
const withPrecision = (value: string) => {
const DIGITS_COUNT = 2;
const decimalIndex = value.indexOf('.');

if (decimalIndex === -1) return value;

return value.slice(0, decimalIndex + DIGITS_COUNT + 1);
};

type Props = {
value: bigint;
decimals: number;
Expand All @@ -25,11 +16,12 @@ type Props = {

function FormattedBalance({ value, decimals, symbol, className }: Props) {
const formattedValue = formatUnits(value, decimals);
const compactBalance = formatBalance(value, { decimals, withUnit: symbol, withZero: false });

return (
<span className={cx(styles.balance, className)}>
{withPrecision(formattedValue)} {symbol}
<Tooltip text={formattedValue} />
{compactBalance}
<Tooltip text={`${formattedValue} ${symbol}`} />
</span>
);
}
Expand Down

0 comments on commit 2d4e83f

Please sign in to comment.