diff --git a/src/util.test.ts b/src/util.test.ts index 3a9128fe44f..b0c35e827b4 100644 --- a/src/util.test.ts +++ b/src/util.test.ts @@ -109,31 +109,31 @@ describe('util', () => { describe('gweiDecToWEIBN', () => { it('should convert a whole number to WEI', () => { - expect(util.gweiDecToWEIBN(1).toNumber()).toBe(1000); - expect(util.gweiDecToWEIBN(123).toNumber()).toBe(123000); - expect(util.gweiDecToWEIBN(101).toNumber()).toBe(101000); - expect(util.gweiDecToWEIBN(1234).toNumber()).toBe(1234000); + expect(util.gweiDecToWEIBN(1).toNumber()).toBe(1000000000); + expect(util.gweiDecToWEIBN(123).toNumber()).toBe(123000000000); + expect(util.gweiDecToWEIBN(101).toNumber()).toBe(101000000000); + expect(util.gweiDecToWEIBN(1234).toNumber()).toBe(1234000000000); }); it('should convert a number with a decimal part to WEI', () => { - expect(util.gweiDecToWEIBN(1.1).toNumber()).toBe(1100); - expect(util.gweiDecToWEIBN(123.01).toNumber()).toBe(123010); - expect(util.gweiDecToWEIBN(101.001).toNumber()).toBe(101001); - expect(util.gweiDecToWEIBN(1234.567).toNumber()).toBe(1234567); + expect(util.gweiDecToWEIBN(1.1).toNumber()).toBe(1100000000); + expect(util.gweiDecToWEIBN(123.01).toNumber()).toBe(123010000000); + expect(util.gweiDecToWEIBN(101.001).toNumber()).toBe(101001000000); + expect(util.gweiDecToWEIBN(1234.567).toNumber()).toBe(1234567000000); }); it('should convert a number < 1 to WEI', () => { - expect(util.gweiDecToWEIBN(0.1).toNumber()).toBe(100); - expect(util.gweiDecToWEIBN(0.01).toNumber()).toBe(10); - expect(util.gweiDecToWEIBN(0.001).toNumber()).toBe(1); - expect(util.gweiDecToWEIBN(0.567).toNumber()).toBe(567); + expect(util.gweiDecToWEIBN(0.1).toNumber()).toBe(100000000); + expect(util.gweiDecToWEIBN(0.01).toNumber()).toBe(10000000); + expect(util.gweiDecToWEIBN(0.001).toNumber()).toBe(1000000); + expect(util.gweiDecToWEIBN(0.567).toNumber()).toBe(567000000); }); it('should round to whole WEI numbers', () => { - expect(util.gweiDecToWEIBN(0.1001).toNumber()).toBe(100); - expect(util.gweiDecToWEIBN(0.0109).toNumber()).toBe(11); - expect(util.gweiDecToWEIBN(0.0014).toNumber()).toBe(1); - expect(util.gweiDecToWEIBN(0.5676).toNumber()).toBe(568); + expect(util.gweiDecToWEIBN(0.1001).toNumber()).toBe(100100000); + expect(util.gweiDecToWEIBN(0.0109).toNumber()).toBe(10900000); + expect(util.gweiDecToWEIBN(0.0014).toNumber()).toBe(1400000); + expect(util.gweiDecToWEIBN(0.5676).toNumber()).toBe(567600000); }); it('should handle NaN', () => { diff --git a/src/util.ts b/src/util.ts index 7f7149ab03a..fd7fc3f84a5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -62,7 +62,7 @@ export function fractionBN( return targetBN.mul(numBN).div(denomBN); } -const BN_1000 = new BN(1000, 10); +const BN_1GWEI_IN_WEI = new BN(1000000000, 10); /** * Used to convert a base-10 number from GWEI to WEI. Can handle numbers with decimal parts @@ -73,11 +73,11 @@ const BN_1000 = new BN(1000, 10); export function gweiDecToWEIBN(n: number | string) { const wholePart = Math.floor(Number(n)); const decimalPartMatch = Number(n) - .toFixed(3) + .toFixed(9) .match(/\.(\d+)/u); const decimalPart = decimalPartMatch ? decimalPartMatch[1] : '0'; - const wholePartAsWEI = new BN(wholePart, 10).mul(BN_1000); + const wholePartAsWEI = new BN(wholePart, 10).mul(BN_1GWEI_IN_WEI); const decimalPartAsWEI = new BN(decimalPart, 10); return wholePartAsWEI.add(decimalPartAsWEI);