Skip to content

Commit

Permalink
Fixed BigNumber.js scoping (#1312)
Browse files Browse the repository at this point in the history
* Fixed BigNumber.js scoping

* Updated CHANGELOG.md
  • Loading branch information
MiroMargineanu authored Nov 25, 2024
1 parent 4cadbc5 commit b1333a9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Fixed BigNumber configuration, scoped to a function level in "stringIsFloat"](https://github.com/multiversx/mx-sdk-dapp/pull/1312)

## [[v3.0.12](https://github.com/multiversx/mx-sdk-dapp/pull/1310)] - 2024-11-20

- [Fixed "stringIsFloat" issue for huge decimal numbers](https://github.com/multiversx/mx-sdk-dapp/pull/1309)
Expand Down
12 changes: 9 additions & 3 deletions src/utils/validation/stringIsFloat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export const stringIsFloat = (amount: string) => {

// eslint-disable-next-line
let [wholes, decimals] = amount.split('.');
const LocalBigNumber = BigNumber.clone();

if (decimals) {
const areAllNumbers = decimals
.split('')
.every((digit) => !isNaN(parseInt(digit)));

BigNumber.set({
LocalBigNumber.set({
DECIMAL_PLACES: areAllNumbers
? decimals.length
: BigNumber.config().DECIMAL_PLACES
Expand All @@ -30,6 +32,10 @@ export const stringIsFloat = (amount: string) => {
}
}
const number = decimals ? [wholes, decimals].join('.') : wholes;
const bNparsed = new BigNumber(number);
return bNparsed.toString(10) === number && bNparsed.comparedTo(0) >= 0;
const bNparsed = LocalBigNumber(number);

const output =
bNparsed.toString(10) === number && bNparsed.comparedTo(0) >= 0;

return output;
};

0 comments on commit b1333a9

Please sign in to comment.