forked from argentlabs/argent-starknet-recover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatTokenBalance.ts
27 lines (22 loc) · 923 Bytes
/
formatTokenBalance.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { BigNumber, utils } from "ethers";
const formatTokenBalanceToCharLength =
(length: number) =>
(balance: string = "0", decimals = "18"): string => {
const balanceBn = BigNumber.from(balance);
const balanceFullString = utils.formatUnits(balanceBn, decimals);
// show max ${length} characters or what's needed to show everything before the decimal point
const balanceString = balanceFullString.slice(
0,
Math.max(length, balanceFullString.indexOf("."))
);
// make sure seperator is not the last character, if so remove it
// remove unnecessary 0s from the end, except for ".0"
let cleanedBalanceString = balanceString
.replace(/\.$/, "")
.replace(/0+$/, "");
if (cleanedBalanceString.endsWith(".")) {
cleanedBalanceString += "0";
}
return cleanedBalanceString;
};
export const formatTokenBalance = formatTokenBalanceToCharLength(9);