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

use ethjs-unit for unit conversions #506

Merged
merged 1 commit into from
Jul 1, 2021
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"eth-sig-util": "^3.0.0",
"ethereumjs-util": "^7.0.10",
"ethereumjs-wallet": "^1.0.1",
"ethjs-unit": "^0.1.6",
"ethjs-util": "^0.1.6",
"human-standard-collectible-abi": "^1.0.2",
"human-standard-token-abi": "^2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/dependencies.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ declare module 'ethjs-provider-http';

declare module 'ethjs-util';

declare module 'ethjs-unit';

declare module 'human-standard-collectible-abi';

declare module 'human-standard-token-abi';
Expand Down
29 changes: 29 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ describe('util', () => {
});
});

describe('hexWei', () => {
it('should convert a whole number to WEI', () => {
const numbersInGwei = [1, 123, 101, 1234];
numbersInGwei.forEach((gweiDec) => {
expect(
util.weiHexToGweiDec(util.gweiDecToWEIBN(gweiDec).toString(16)),
).toBe(gweiDec.toString());
});
});

it('should convert a number with a decimal part to WEI', () => {
const numbersInGwei = [1.1, 123.01, 101.001, 1234.567];
numbersInGwei.forEach((gweiDec) => {
expect(
util.weiHexToGweiDec(util.gweiDecToWEIBN(gweiDec).toString(16)),
).toBe(gweiDec.toString());
});
});

it('should convert a number < 1 to WEI', () => {
const numbersInGwei = [0.1, 0.01, 0.001, 0.567];
numbersInGwei.forEach((gweiDec) => {
expect(
util.weiHexToGweiDec(util.gweiDecToWEIBN(gweiDec).toString(16)),
).toBe(gweiDec.toString());
});
});
});

describe('safelyExecute', () => {
it('should swallow errors', async () => {
expect(
Expand Down
25 changes: 14 additions & 11 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
toChecksumAddress,
} from 'ethereumjs-util';
import { stripHexPrefix } from 'ethjs-util';
import { fromWei, toWei } from 'ethjs-unit';
import { ethErrors } from 'eth-rpc-errors';
import ensNamehash from 'eth-ens-namehash';
import { TYPED_MESSAGE_SCHEMA, typedSignatureHash } from 'eth-sig-util';
Expand Down Expand Up @@ -62,25 +63,27 @@ export function fractionBN(
return targetBN.mul(numBN).div(denomBN);
}

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
*
* @param n - The base 10 number to convert to WEI
* @returns - The number in WEI, as a BN
*/
export function gweiDecToWEIBN(n: number | string) {
const wholePart = Math.floor(Number(n));
const decimalPartMatch = Number(n)
.toFixed(9)
.match(/\.(\d+)/u);
const decimalPart = decimalPartMatch ? decimalPartMatch[1] : '0';

const wholePartAsWEI = new BN(wholePart, 10).mul(BN_1GWEI_IN_WEI);
const decimalPartAsWEI = new BN(decimalPart, 10);
if (Number.isNaN(n)) {
return new BN(0);
}
return toWei(n.toString(), 'gwei');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this now throws an error if you input a number with too many decimal places. Beforehand it would ignore the extra decimals instead.

This seems safer. I just wanted to note the change in behaviour.

}

return wholePartAsWEI.add(decimalPartAsWEI);
/**
* Used to convert values from wei hex format to dec gwei format
* @param hex - value in hex wei
* @returns - value in dec gwei as string
*/
export function weiHexToGweiDec(hex: string) {
const hexWei = new BN(hex, 16);
return fromWei(hexWei, 'gwei').toString(10);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3036,7 +3036,7 @@ ethjs-schema@0.2.1:
resolved "https://registry.yarnpkg.com/ethjs-schema/-/ethjs-schema-0.2.1.tgz#47e138920421453617069034684642e26bb310f4"
integrity sha512-DXd8lwNrhT9sjsh/Vd2Z+4pfyGxhc0POVnLBUfwk5udtdoBzADyq+sK39dcb48+ZU+2VgtwHxtGWnLnCfmfW5g==

ethjs-unit@0.1.6:
ethjs-unit@0.1.6, ethjs-unit@^0.1.6:
version "0.1.6"
resolved "https://registry.yarnpkg.com/ethjs-unit/-/ethjs-unit-0.1.6.tgz#c665921e476e87bce2a9d588a6fe0405b2c41699"
integrity sha1-xmWSHkduh7ziqdWIpv4EBbLEFpk=
Expand Down