Skip to content

Commit de09691

Browse files
authored
Merge pull request #6 from cyyber/main
Fix for incorrect balance shown in some cases
2 parents 53d8c7e + 65a7a8f commit de09691

File tree

4 files changed

+81
-18
lines changed

4 files changed

+81
-18
lines changed

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "zond-web3-wallet",
33
"description": "A browser extension, for creating accounts, signing transactions and sending transactions over the zond blockchain.",
44
"private": true,
5-
"version": "0.1.0",
5+
"version": "0.1.1",
66
"type": "module",
77
"scripts": {
88
"dev": "vite",
@@ -50,6 +50,7 @@
5050
"@vitejs/plugin-react-swc": "^3.5.0",
5151
"autoprefixer": "^10.4.19",
5252
"babel-jest": "^29.7.0",
53+
"bignumber.js": "^4.0.4",
5354
"buffer": "^6.0.3",
5455
"class-variance-authority": "^0.7.1",
5556
"clsx": "^2.1.1",

src/functions/__tests__/getOptimalTokenBalance.test.ts

+45-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ describe("getOptimalTokenBalance", () => {
3030
expect(getOptimalTokenBalance(balance)).toBe(tokenBalance);
3131
});
3232

33-
it("should return the balance 1787372.5556 if the balance is 1787372.5556", () => {
34-
const balance = "1787372.5556";
35-
const tokenBalance = "1787372.5556 ZND";
33+
it("should return the balance 1,787,372.5556 if the balance is 1787372.55569", () => {
34+
const balance = "1787372.55569";
35+
const tokenBalance = "1,787,372.5556 ZND";
3636

3737
expect(getOptimalTokenBalance(balance)).toBe(tokenBalance);
3838
});
@@ -72,6 +72,27 @@ describe("getOptimalTokenBalance", () => {
7272
expect(getOptimalTokenBalance(balance)).toBe(tokenBalance);
7373
});
7474

75+
it("should return the balance 10,000.0 if the balance is 10000", () => {
76+
const balance = "10000";
77+
const tokenBalance = "10,000.0 ZOND";
78+
79+
expect(getOptimalTokenBalance(balance, "ZOND")).toBe(tokenBalance);
80+
});
81+
82+
it("should return the balance 100,000.0 if the balance is 100000", () => {
83+
const balance = "100000";
84+
const tokenBalance = "100,000.0 ZOND";
85+
86+
expect(getOptimalTokenBalance(balance, "ZOND")).toBe(tokenBalance);
87+
});
88+
89+
it("should return the balance 0.0 if the balance is a text `Zond`", () => {
90+
const balance = "Zond";
91+
const tokenBalance = "0.0 ZND";
92+
93+
expect(getOptimalTokenBalance(balance)).toBe(tokenBalance);
94+
});
95+
7596
it("should return the balance 1.9999 if the balance is 1.999999999999999999", () => {
7697
const balance = "1.999999999999999999";
7798
const tokenBalance = "1.9999 ZND";
@@ -92,4 +113,25 @@ describe("getOptimalTokenBalance", () => {
92113

93114
expect(getOptimalTokenBalance(balance, "XYZ")).toBe(tokenBalance);
94115
});
116+
117+
it("should return the balance 3,453.5678 with the passed token symbol if the balance is 3453.567889", () => {
118+
const balance = "3453.567889";
119+
const tokenBalance = "3,453.5678 ZYZ";
120+
121+
expect(getOptimalTokenBalance(balance, "ZYZ")).toBe(tokenBalance);
122+
});
123+
124+
it("should return the balance 100,000,000.0 with the passed token symbol if the balance is 100000000", () => {
125+
const balance = "100000000";
126+
const tokenBalance = "100,000,000.0 TOK";
127+
128+
expect(getOptimalTokenBalance(balance, "TOK")).toBe(tokenBalance);
129+
});
130+
131+
it("should return the balance 23,000,000,000.0 if the balance is 23000000000", () => {
132+
const balance = "23000000000";
133+
const tokenBalance = "23,000,000,000.0 ZND";
134+
135+
expect(getOptimalTokenBalance(balance)).toBe(tokenBalance);
136+
});
95137
});
+23-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
import { NATIVE_TOKEN } from "@/constants/nativeToken";
2+
import { BigNumber } from "bignumber.js";
3+
4+
BigNumber.config({
5+
DECIMAL_PLACES: 18,
6+
EXPONENTIAL_AT: 1e9,
7+
ROUNDING_MODE: BigNumber.ROUND_DOWN,
8+
FORMAT: {
9+
decimalSeparator: ".",
10+
groupSeparator: ",",
11+
groupSize: 3,
12+
},
13+
});
214

315
export const getOptimalTokenBalance = (
416
balance: string,
517
tokenSymbol?: string,
618
) => {
719
const symbol = tokenSymbol ?? NATIVE_TOKEN.symbol;
820
try {
9-
if (Number(balance) == 0) return `0.0 ${symbol}`;
10-
const precisionFloat = parseFloat(
11-
balance.slice(0, balance.indexOf(".") + 5),
12-
).toFixed(4);
13-
let deleteIndex = precisionFloat.length - 1;
14-
const postDecimalIndex = precisionFloat.indexOf(".") + 2;
21+
const bigNumber = new BigNumber(balance);
22+
if (bigNumber.isNaN() || bigNumber.isZero()) return `0.0 ${symbol}`;
23+
24+
let formatted = bigNumber
25+
.toFormat(4, BigNumber.ROUND_DOWN)
26+
.replace(/\.?0+$/, "");
1527

16-
while (
17-
deleteIndex >= postDecimalIndex &&
18-
precisionFloat[deleteIndex] === "0"
19-
) {
20-
deleteIndex--;
28+
if (!formatted.includes(".")) {
29+
formatted += ".0";
2130
}
2231

23-
return `${precisionFloat.substring(0, deleteIndex + 1)} ${symbol}`;
24-
} catch (error) {
25-
return `${balance} ${symbol}`;
32+
return `${formatted} ${symbol}`;
33+
} catch {
34+
return `0.0 ${symbol}`;
2635
}
2736
};

0 commit comments

Comments
 (0)