diff --git a/.changeset/tidy-scissors-sip.md b/.changeset/tidy-scissors-sip.md new file mode 100644 index 0000000000..5dc64e0a33 --- /dev/null +++ b/.changeset/tidy-scissors-sip.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/math": patch +--- + +fix: `parseUnits` bug with 0 units diff --git a/packages/math/src/bn.test.ts b/packages/math/src/bn.test.ts index 02750d6cfc..721fd762ec 100644 --- a/packages/math/src/bn.test.ts +++ b/packages/math/src/bn.test.ts @@ -475,6 +475,10 @@ describe('Math - BN', () => { expect(bn.parseUnits('100,100,100.00002', 5).toHex()).toEqual(bn('10010010000002').toHex()); expect(bn.parseUnits('.').toHex()).toEqual(bn('0').toHex()); expect(bn.parseUnits('.', 5).toHex()).toEqual(bn('0').toHex()); + expect(bn.parseUnits('1', 0).toHex()).toEqual(bn('1').toHex()); + expect(bn.parseUnits('0.000000001', 0).toHex()).toEqual(bn('0').toHex()); + expect(bn.parseUnits('100.00002', 0).toHex()).toEqual(bn('100').toHex()); + expect(bn.parseUnits('100,100.00002', 0).toHex()).toEqual(bn('100100').toHex()); expect(() => { bn.parseUnits('100,100.000002', 5); diff --git a/packages/math/src/bn.ts b/packages/math/src/bn.ts index c849acf750..3696df1684 100644 --- a/packages/math/src/bn.ts +++ b/packages/math/src/bn.ts @@ -322,6 +322,11 @@ bn.parseUnits = (value: string, units: number = DEFAULT_DECIMAL_UNITS): BN => { const [valueUnits = '0', valueDecimals = '0'] = valueToParse.split('.'); const length = valueDecimals.length; + if (units === 0) { + const valueWithoutDecimals = valueToParse.replace(',', '').split('.')[0]; + return bn(valueWithoutDecimals); + } + if (length > units) { throw new FuelError( ErrorCode.CONVERTING_FAILED,