Skip to content
Merged
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
32 changes: 16 additions & 16 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down