Skip to content

Commit

Permalink
fix: show significant digit
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtiti committed Sep 2, 2024
1 parent 2d61ae7 commit 684db05
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
19 changes: 12 additions & 7 deletions src/__tests__/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { expect } from '@jest/globals';

import { truncateAddress, formatTimestampToDate, formatDataNumber, calculateUSDGas } from '~/utils/format';
import {
truncateAddress,
formatTimestampToDate,
formatDataNumber,
calculateUSDGas,
formatSmallNumber,
} from '~/utils/format';

describe('truncateAddress', () => {
it('should truncate the address correctly', () => {
const address = '0x1234567890abcdef1234567890abcdef12345678';
expect(truncateAddress(address)).toBe('0x1234...5678');
});
});

describe('formatTimestampToDate', () => {
it('should format the timestamp to date correctly', () => {
const timestamp = 1627580800; // July 29, 2021
expect(formatTimestampToDate(timestamp)).toBe('7/29/2021'); // The format may vary based on locale
});
});

describe('formatDataNumber', () => {
it('should format a number correctly', () => {
expect(formatDataNumber(1234.567)).toBe('1,234.567');
expect(formatDataNumber(0.0001234, 6)).toBe('0.000123');
Expand All @@ -28,9 +30,12 @@ describe('formatDataNumber', () => {
expect(formatDataNumber('not a number')).toBe('0');
expect(formatDataNumber(0, 2, true)).toBe('$0');
});
});

describe('calculateUSDGas', () => {
it('should return significant digit from small number', () => {
expect(formatSmallNumber(0.00002123333, true)).toBe('$0.0000212');
expect(formatDataNumber(0, 2, true)).toBe('$0');
});

it('should calculate the USD value of gas correctly', () => {
const txGas = BigInt(21000);
const gasPriceInWei = BigInt(1000000000); // 1 Gwei
Expand Down
4 changes: 2 additions & 2 deletions src/components/TVLTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const TVLTable = () => {
const { t } = useTranslation();
const { chainData } = useData();
const tvl = chainData?.tvl || [];

console.log(chainData);
return (
<article>
<STitle>{t('CHAIN.TVL.title')}</STitle>
Expand Down Expand Up @@ -68,7 +68,7 @@ export const TVLTable = () => {
</STableCell>

<STableCell>
{token.price && <Typography>{formatDataNumber(token.price, 0, true)}</Typography>}
{token.price && <Typography>{formatDataNumber(Number(token.price), 0, true)}</Typography>}

{!token.price && <NotAvailable>{t('CHAIN.CHAININFORMATION.notAvailable')}</NotAvailable>}
</STableCell>
Expand Down
11 changes: 6 additions & 5 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function formatDataNumber(input: string | number, formatDecimal = 3, curr

if (res === 0 || isNaN(res)) return `${currency ? '$0' : '0'}`;

if (res < 0.01) return formatSmallNumber(res);
if (res < 10) return formatSmallNumber(res, currency);

const userNotation = compact ? 'compact' : 'standard';
const notation = res > 1e12 ? 'scientific' : userNotation;
Expand All @@ -24,9 +24,9 @@ export function formatDataNumber(input: string | number, formatDecimal = 3, curr
}).format(res);
}

export const formatSmallNumber = (value: number) => {
export const formatSmallNumber = (value: number, currency?: boolean) => {
if (value === 0) {
return '0';
return currency ? '$0' : '0';
}

const formattedValue = value.toString();
Expand All @@ -45,8 +45,9 @@ export const formatSmallNumber = (value: number) => {
// Return the number with 3 digits after the last leading zero
const result = formattedValue.slice(0, numLeadingZeros + 3);

// Trim any trailing zeros from the result
return result.replace(/\.?0+$/, '');
const trimmedResult = result.replace(/\.?0+$/, '');

return currency ? `$${trimmedResult}` : trimmedResult;
};

export const calculateUSDGas = (txGas: bigint, gasPriceInWei: bigint, etherPrice: number): number => {
Expand Down

0 comments on commit 684db05

Please sign in to comment.