Skip to content

Commit

Permalink
Merge pull request #2218 from joshstevens19/fix/types/web3-utils
Browse files Browse the repository at this point in the history
fix: `fromWei` and `toWei` typings
  • Loading branch information
nivida authored Jan 27, 2019
2 parents 599c52e + 2b01bf5 commit ec96234
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
8 changes: 4 additions & 4 deletions packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ export const getUnitValue = (unit) => {
*
* @method fromWei
*
* @param {Number|String} number can be a number, number string or a HEX of a decimal
* @param {String|BN} number can be a BigNumber, number string or a HEX of a decimal
* @param {String} unit the unit to convert to, default ether
*
* @returns {String|Object} When given a BN object it returns one as well, otherwise a number
* @returns {String} Returns a string
*/
export const fromWei = (number, unit) => {
unit = getUnitValue(unit);
Expand Down Expand Up @@ -218,10 +218,10 @@ export const fromWei = (number, unit) => {
*
* @method toWei
*
* @param {Number|String|BN} number can be a number, number string or a HEX of a decimal
* @param {String|BN} number can be a number, number string or a HEX of a decimal
* @param {String} unit the unit to convert from, default ether
*
* @returns {String|Object} When given a BN object it returns one as well, otherwise a number
* @returns {String|BN} When given a BN object it returns one as well, otherwise a string
*/
export const toWei = (number, unit) => {
unit = getUnitValue(unit);
Expand Down
39 changes: 16 additions & 23 deletions packages/web3-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import BigNumber from 'bn.js';

export type Unit =
'noether'
| 'noether'
| 'wei'
| 'kwei'
| 'Kwei'
Expand Down Expand Up @@ -52,32 +52,24 @@ export type Unit =
| 'tether';

export type Mixed =
string
| string
| number
| BN
| {
type: string;
value: string;
}
type: string;
value: string;
}
| {
t: string;
v: string | BN | number;
}
t: string;
v: string | BN | number;
}
| boolean;

export type Hex = string | number;

export class BN extends BigNumber {
constructor(
number: number | string | number[] | Buffer | BN,
base?: number | 'hex',
endian?: 'le' | 'be'
)
super(
number: number | string | number[] | Buffer | BN,
base?: number | 'hex',
endian?: 'le' | 'be'
): BigNumber;
constructor(number: number | string | number[] | Buffer | BN, base?: number | 'hex', endian?: 'le' | 'be');
super(number: number | string | number[] | Buffer | BN, base?: number | 'hex', endian?: 'le' | 'be'): BigNumber;
}

// utils types
Expand All @@ -97,7 +89,7 @@ export function checkAddressChecksum(address: string): boolean;
export function fromAscii(string: string): string;
export function fromDecimal(value: string | number): string;
export function fromUtf8(string: string): string;
export function fromWei(value: BN, unit?: Unit): BN | string;
export function fromWei(value: string | BN, unit?: Unit): string;
export function hexToBytes(hex: Hex): number[];
export function hexToNumber(hex: Hex): number;
export function hexToNumberString(hex: Hex): string;
Expand All @@ -116,7 +108,8 @@ export function toChecksumAddress(address: string): string;
export function toDecimal(hex: Hex): number;
export function toHex(value: number | string | BN): string;
export function toUtf8(string: string): string;
export function toWei(value: number | string | BN, unit?: Unit): string | BN;
export function toWei(val: BN, unit?: Unit): BN;
export function toWei(val: string, unit?: Unit): string;
export function isBloom(bloom: string): boolean;
export function isTopic(topic: string): boolean;
export function jsonInterfaceMethodToString(abiItem: AbiItem): string;
Expand All @@ -128,7 +121,6 @@ export function testTopic(bloom: string, topic: string): boolean;
export function getSignatureParameters(signature: string): object;

// interfaces

export interface Utils {
isBN(value: string | number): boolean;
isBigNumber(value: BN): boolean;
Expand All @@ -146,7 +138,7 @@ export interface Utils {
fromAscii(string: string): string;
fromDecimal(value: string | number): string;
fromUtf8(string: string): string;
fromWei(value: BN, unit?: Unit): BN | string;
fromWei(value: string | BN, unit?: Unit): string;
hexToBytes(hex: Hex): number[];
hexToNumber(hex: Hex): number;
hexToNumberString(hex: Hex): string;
Expand All @@ -165,7 +157,8 @@ export interface Utils {
toDecimal(hex: Hex): number;
toHex(value: number | string | BN): string;
toUtf8(string: string): string;
toWei(value: number | string | BN, unit?: Unit): string | BN;
toWei(val: BN, unit?: Unit): BN;
toWei(val: string, unit?: Unit): string;
isBloom(bloom: string): boolean;
isTopic(topic: string): boolean;
jsonInterfaceMethodToString(abiItem: AbiItem): string;
Expand Down
8 changes: 6 additions & 2 deletions packages/web3-utils/types/tests/from-wei-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ import {BN, fromWei} from 'web3-utils';

const bigNumber = new BN(3);

// $ExpectType string | BN
// $ExpectType string
fromWei(bigNumber);
// $ExpectType string | BN
// $ExpectType string
fromWei('1');
// $ExpectType string
fromWei(bigNumber, 'ether');
// $ExpectType string
fromWei('1', 'ether');

// $ExpectError
fromWei(232);
Expand Down
14 changes: 5 additions & 9 deletions packages/web3-utils/types/tests/to-wei-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ import {BN, toWei} from 'web3-utils';

const bigNumber = new BN(3);

// $ExpectType string | BN
// $ExpectType string
toWei('1');
// $ExpectType string | BN
toWei(1);
// $ExpectType string | BN
// $ExpectType BN
toWei(bigNumber);
// $ExpectType string | BN
// $ExpectType string
toWei('1', 'finney');
// $ExpectType string | BN
toWei(1, 'finney');
// $ExpectType string | BN
// $ExpectType BN
toWei(bigNumber, 'finney');

// $ExpectError
Expand All @@ -50,4 +46,4 @@ toWei(null);
// $ExpectError
toWei(undefined);
// $ExpectError
toWei(1 , 'blah');
toWei(1, 'blah');

0 comments on commit ec96234

Please sign in to comment.