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 5 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
6 changes: 6 additions & 0 deletions .changeset/warn-precision-validation
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/math": patch
---

Added warning when precision is less than minPrecision in BN.format method.
This maintains backward compatibility while alerting users to potential issues.
arboleya marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions packages/math/src/bn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,27 @@ describe('Math - BN', () => {
it('should return significant figures even if it exceeds the precision', () => {
expect(bn('4000000').format({ precision: 1 })).toEqual('0.004');
});

it('should warn when precision is less than minPrecision', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();

// Test case where precision < 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.'
);

// Test case where precision = minPrecision (should not warn)
consoleWarnSpy.mockClear();
bn(123).format({ precision: 4, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();
VolodymyrBg marked this conversation as resolved.
Show resolved Hide resolved

// Test case where precision > minPrecision (should not warn)
bn(123).format({ precision: 6, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();
VolodymyrBg marked this conversation as resolved.
Show resolved Hide resolved

consoleWarnSpy.mockRestore();
});
});
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 >= minPrecision.'
VolodymyrBg marked this conversation as resolved.
Show resolved Hide resolved
);
}

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