Skip to content

Commit 2abb91e

Browse files
committed
fix: clean up formatting in formatBalance utility and dashboard page
1 parent aefff86 commit 2abb91e

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

packages/nextjs/app/dapp/dashboard/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export default function DashboardPage() {
4747
const { timeRemainingFromBlocks, blocksRemaining, currentBlock } =
4848
useDrawInfo({ drawId: currentDrawId });
4949

50-
5150
// Get STRKP contract address from Lottery
5251
const { data: strkpContractAddress, isLoading: loadingStrkpAddress } =
5352
useScaffoldReadContract({

packages/nextjs/utils/formatBalance.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,52 @@
66
* @returns Formatted balance as number
77
*/
88
export function formatBalance(
9-
balance: bigint | number | undefined | null,
10-
decimals: number = 18,
11-
displayDecimals: number = 2
12-
): number {
13-
if (!balance) return 0;
14-
15-
try {
16-
// Convert to bigint if it's a number
17-
const balanceBigInt =
18-
typeof balance === "bigint" ? balance : BigInt(balance);
19-
20-
// Calculate divisor (10^decimals)
21-
const divisor = BigInt(10 ** decimals);
22-
23-
// Get integer and fractional parts
24-
const integerPart = balanceBigInt / divisor;
25-
const fractionalPart = balanceBigInt % divisor;
26-
27-
// Convert to decimal
28-
const formatted =
29-
Number(integerPart) + Number(fractionalPart) / Number(divisor);
30-
31-
// Round to display decimals
32-
const multiplier = Math.pow(10, displayDecimals);
33-
return Math.round(formatted * multiplier) / multiplier;
34-
} catch (error) {
35-
console.error("Error formatting balance:", error);
36-
return 0;
37-
}
9+
balance: bigint | number | undefined | null,
10+
decimals: number = 18,
11+
displayDecimals: number = 2,
12+
): number {
13+
if (!balance) return 0;
14+
15+
try {
16+
// Convert to bigint if it's a number
17+
const balanceBigInt =
18+
typeof balance === "bigint" ? balance : BigInt(balance);
19+
20+
// Calculate divisor (10^decimals)
21+
const divisor = BigInt(10 ** decimals);
22+
23+
// Get integer and fractional parts
24+
const integerPart = balanceBigInt / divisor;
25+
const fractionalPart = balanceBigInt % divisor;
26+
27+
// Convert to decimal
28+
const formatted =
29+
Number(integerPart) + Number(fractionalPart) / Number(divisor);
30+
31+
// Round to display decimals
32+
const multiplier = Math.pow(10, displayDecimals);
33+
return Math.round(formatted * multiplier) / multiplier;
34+
} catch (error) {
35+
console.error("Error formatting balance:", error);
36+
return 0;
3837
}
39-
40-
/**
41-
* Formats balance to a readable string with optional thousand separators
42-
* @param balance Balance in wei
43-
* @param decimals Number of decimals (default: 18)
44-
* @param displayDecimals Number of decimals to show (default: 2)
45-
* @returns Formatted balance string like "1,234.56"
46-
*/
47-
export function formatBalanceToString(
48-
balance: bigint | number | undefined | null,
49-
decimals: number = 18,
50-
displayDecimals: number = 2
51-
): string {
52-
const formatted = formatBalance(balance, decimals, displayDecimals);
53-
return formatted.toLocaleString("en-US", {
54-
minimumFractionDigits: displayDecimals,
55-
maximumFractionDigits: displayDecimals,
56-
});
57-
}
38+
}
39+
40+
/**
41+
* Formats balance to a readable string with optional thousand separators
42+
* @param balance Balance in wei
43+
* @param decimals Number of decimals (default: 18)
44+
* @param displayDecimals Number of decimals to show (default: 2)
45+
* @returns Formatted balance string like "1,234.56"
46+
*/
47+
export function formatBalanceToString(
48+
balance: bigint | number | undefined | null,
49+
decimals: number = 18,
50+
displayDecimals: number = 2,
51+
): string {
52+
const formatted = formatBalance(balance, decimals, displayDecimals);
53+
return formatted.toLocaleString("en-US", {
54+
minimumFractionDigits: displayDecimals,
55+
maximumFractionDigits: displayDecimals,
56+
});
57+
}

0 commit comments

Comments
 (0)