Skip to content

Commit

Permalink
fix: rounded values may be wrong (funds deposit/withdraw) (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
levalleux-ludo authored Apr 4, 2024
1 parent dd0e882 commit 3a4d4cb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 48 deletions.
66 changes: 33 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"dependencies": {
"@apollo/client": "^3.8.1",
"@bosonprotocol/chat-sdk": "^1.3.1-alpha.9",
"@bosonprotocol/react-kit": "^0.29.0-alpha.18",
"@bosonprotocol/react-kit": "^0.29.0-alpha.20",
"@davatar/react": "^1.10.4",
"@ethersproject/address": "^5.6.1",
"@ethersproject/units": "^5.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ export default function FinanceWithdraw({
const value = e.target.valueAsNumber || 0;
setIsWithdrawInvalid(false);

const availableFundsBig = getNumberWithDecimals(
availableAmount,
tokenDecimals
);
try {
const availableFundsBig = getNumberWithDecimals(
availableAmount,
tokenDecimals
);

if (value < tokenStep || value > availableFundsBig || !value) {
if (value < tokenStep || value > availableFundsBig || !value) {
setIsWithdrawInvalid(true);
}
} catch (e) {
setIsWithdrawInvalid(true);
}

Expand Down
12 changes: 3 additions & 9 deletions src/lib/utils/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber } from "ethers";
import { utils } from "ethers";

/**
* Given a small number, it returns its string representation without the scientific 'e' notation.
Expand Down Expand Up @@ -37,18 +37,12 @@ export const getNumberWithoutDecimals = (
value: string,
decimals: string
): string => {
const valueAsNumber = Number(value);
if (!Number.isInteger(valueAsNumber)) {
return Math.floor(valueAsNumber * 10 ** Number(decimals)).toString();
}
return BigNumber.from(valueAsNumber + "")
.mul(BigNumber.from(10).pow(BigNumber.from(decimals)))
.toString();
return utils.parseUnits(value, decimals).toString();
};

export const getNumberWithDecimals = (
value: string,
decimals: string
): number => {
return Number(value) * 10 ** -Number(decimals);
return Number(utils.formatUnits(value, decimals));
};

0 comments on commit 3a4d4cb

Please sign in to comment.