Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add warning for invalid precision in BN format method #3586

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warn-precision-validation
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/math": patch
---

feat: add warning for invalid precision in `BN` format method
31 changes: 31 additions & 0 deletions packages/math/src/bn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,35 @@ describe('Math - BN', () => {
it('should return significant figures even if it exceeds the precision', () => {
expect(bn('4000000').format({ precision: 1 })).toEqual('0.004');
});

describe('precision validation', () => {
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
});

afterEach(() => {
consoleWarnSpy.mockRestore();
});

it('should warn when precision is less than minPrecision', () => {
bn(123).format({ precision: 2, minPrecision: 4 });

expect(consoleWarnSpy).toHaveBeenCalledWith(
'Warning: precision (2) is less than minPrecision (4). ' +
'This may lead to unexpected behavior. Consider setting precision >= minPrecision.'
);
});

it('should not warn when precision equals minPrecision', () => {
bn(123).format({ precision: 4, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();
});

it('should not warn when precision is greater than minPrecision', () => {
bn(123).format({ precision: 6, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();
});
});
});
11 changes: 9 additions & 2 deletions packages/math/src/bn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,15 @@ export class BN extends BnJs implements BNInputOverrides, BNHiddenTypes, BNHelpe
return this.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

// Adjust precision and minPrecision
// TODO: This really should throw an error because you can't have a precision less than the minPrecision but this would be a breaking change
// Warn if precision is less than minPrecision
if (initialPrecision < initialMinPrecision) {
console.warn(
`Warning: precision (${initialPrecision}) is less than minPrecision (${initialMinPrecision}). ` +
'This may lead to unexpected behavior. Consider setting precision greater than or equal to minPrecision.'
);
}

// Adjust precision and minPrecision to maintain backward compatibility
const minPrecision =
initialMinPrecision > initialPrecision ? initialPrecision : initialMinPrecision;
const precision =
Expand Down