Skip to content

Commit b3bb765

Browse files
authored
fix: improve BN unsafe numbers error handling (#3636)
1 parent b4f5279 commit b3bb765

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

.changeset/sweet-walls-wonder.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@fuel-ts/errors": patch
3+
"@fuel-ts/math": patch
4+
---
5+
6+
fix: improve BN unsafe numbers error handling

packages/account/src/account.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,4 +999,19 @@ describe('Account', () => {
999999
expect([baseAssetId, ASSET_A, ASSET_B].includes(balance.assetId)).toBeTruthy();
10001000
});
10011001
});
1002+
1003+
test('can ensure using unsafe numbers throws proper error', async () => {
1004+
using launched = await setupTestProviderAndWallets();
1005+
const {
1006+
wallets: [wallet],
1007+
provider,
1008+
} = launched;
1009+
1010+
const baseAssetId = await provider.getBaseAssetId();
1011+
1012+
await expectToThrowFuelError(
1013+
() => wallet.transfer(wallet.address, Number.MAX_SAFE_INTEGER + 1, baseAssetId),
1014+
{ code: ErrorCode.NUMBER_TOO_BIG }
1015+
);
1016+
});
10021017
});

packages/errors/src/error-codes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ export enum ErrorCode {
103103
ACCOUNT_REQUIRED = 'account-required',
104104
UNLOCKED_WALLET_REQUIRED = 'unlocked-wallet-required',
105105

106+
// bn
107+
NUMBER_TOO_BIG = 'number-too-big',
108+
106109
// chain
107110
ERROR_BUILDING_BLOCK_EXPLORER_URL = 'error-building-block-explorer-url',
108111

packages/math/src/bn.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { FuelError, ErrorCode } from '@fuel-ts/errors';
2+
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
3+
14
import type { BN } from './bn';
25
import { bn } from './bn';
36
import type { BigNumberish } from './types';
@@ -111,6 +114,18 @@ describe('Math - BN', () => {
111114
expect(bn(numberToConvert).toHex()).toEqual(hexToConvert);
112115
});
113116

117+
it('should convert safe numbers', () => {
118+
let number = Number.MAX_SAFE_INTEGER;
119+
let expectedHex = '0x1fffffffffffff';
120+
expect(bn(number).toNumber()).toEqual(number);
121+
expect(bn(number).toHex()).toEqual(expectedHex);
122+
123+
number = Number.MAX_SAFE_INTEGER - 0.5;
124+
expectedHex = '0x1ffffffffffffe';
125+
expect(bn(number).toNumber()).toEqual(number);
126+
expect(bn(number).toHex()).toEqual(expectedHex);
127+
});
128+
114129
it('should toHex accept bytePadding config', () => {
115130
let numberToConvert: number;
116131
let bytesToConvert: Uint8Array;
@@ -541,4 +556,18 @@ describe('Math - BN', () => {
541556
it('should return significant figures even if it exceeds the precision', () => {
542557
expect(bn('4000000').format({ precision: 1 })).toEqual('0.004');
543558
});
559+
560+
it('should throws expected error when using unsafe numbers', async () => {
561+
const unsafeNumbers = [Number.MAX_SAFE_INTEGER + 1, Number.MAX_SAFE_INTEGER + 1.1];
562+
563+
for (const unsafeNumber of unsafeNumbers) {
564+
await expectToThrowFuelError(
565+
() => bn(unsafeNumber),
566+
new FuelError(
567+
ErrorCode.NUMBER_TOO_BIG,
568+
`Value ${unsafeNumber} is too large to be represented as a number, use string instead.`
569+
)
570+
);
571+
}
572+
});
544573
});

packages/math/src/bn.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export class BN extends BnJs implements BNInputOverrides, BNHiddenTypes, BNHelpe
5757
bnBase = base || 'hex';
5858
}
5959

60+
if (typeof bnValue === 'number' && bnValue > Number.MAX_SAFE_INTEGER) {
61+
throw new FuelError(
62+
ErrorCode.NUMBER_TOO_BIG,
63+
`Value ${bnValue} is too large to be represented as a number, use string instead.`
64+
);
65+
}
66+
6067
super(bnValue == null ? 0 : bnValue, bnBase, endian);
6168
}
6269

0 commit comments

Comments
 (0)