-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We should correctly display user balances. Let us use the formatting functions and display the balance correctly by region.
- Loading branch information
1 parent
65ab5ab
commit 0b6c887
Showing
3 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./numbers" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const toLocaleString = (value: number): string => | ||
value.toLocaleString("default", { maximumFractionDigits: 2 }) | ||
|
||
// Parse token amount by moving the decimal point | ||
export function bigIntToUserAmount( | ||
amount: bigint, | ||
decimals = 18, | ||
desiredDecimals = 2, | ||
): string { | ||
const desiredDecimalsAmount = | ||
amount / 10n ** BigInt(Math.max(1, decimals - desiredDecimals)) | ||
|
||
return toLocaleString( | ||
Number(desiredDecimalsAmount) / 10 ** Math.min(desiredDecimals, decimals), | ||
) | ||
} | ||
export const formatTokenAmount = ( | ||
amount: number | string, | ||
decimals = 18, | ||
desiredDecimals = 2, | ||
) => bigIntToUserAmount(BigInt(amount), decimals, desiredDecimals) | ||
|
||
export const formatSatoshiAmount = ( | ||
amount: number | string, | ||
desiredDecimals = 2, | ||
) => formatTokenAmount(amount, 8, desiredDecimals) |