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

Declaring types within the repos packages itself #2056

Merged
merged 16 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 12 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
4 changes: 4 additions & 0 deletions packages/web3-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ console.log(Utils);
}
```

## Types

If you are using TypeScript all the types are defined in the `index.d.ts` - please use them :)
joshstevens19 marked this conversation as resolved.
Show resolved Hide resolved


[docs]: http://web3js.readthedocs.io/en/1.0/
[repo]: https://github.com/ethereum/web3.js
Expand Down
137 changes: 137 additions & 0 deletions packages/web3-utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import BigNumber from "bn.js";
import us from "underscore";
joshstevens19 marked this conversation as resolved.
Show resolved Hide resolved

export type Unit =
| "noether"
| "wei"
| "kwei"
| "Kwei"
| "babbage"
| "femtoether"
| "mwei"
| "Mwei"
| "lovelace"
| "picoether"
| "gwei"
| "Gwei"
| "shannon"
| "nanoether"
| "nano"
| "szabo"
| "microether"
| "micro"
| "finney"
| "milliether"
| "milli"
| "ether"
| "kether"
| "grand"
| "mether"
| "gether"
| "tether";

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

export type Hex = string | number;

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

export interface _ extends us.UnderscoreStatic { }

// utils types
export declare function isBN(value: string | number): boolean;
export declare function isBigNumber(value: BigNumber): boolean;
export declare function toBN(number: number | string): BigNumber;
export declare function toTwosComplement(number: number | string | BigNumber): string;
export declare function isAddress(address: string): boolean;
export declare function isHex(hex: Hex): boolean;
export declare function isHexStrict(hex: Hex): boolean;
export declare function asciiToHex(string: string): string;
export declare function hexToAscii(string: string): string;
export declare function toAscii(string: string): string;
export declare function bytesToHex(bytes: number[]): string;
export declare function numberToHex(value: number | string | BigNumber): string;
export declare function checkAddressChecksum(address: string): boolean;
export declare function fromAscii(string: string): string;
export declare function fromDecimal(value: string | number | BigNumber): string;
export declare function fromUtf8(string: string): string;
export declare function fromWei(value: BigNumber, unit?: Unit): BigNumber | string;
export declare function hexToBytes(hex: Hex): number[];
export declare function hexToNumber(hex: Hex): number;
export declare function hexToNumberString(hex: Hex): string;
export declare function hexToString(hex: Hex): string;
export declare function hexToUtf8(string: string): string;
export declare function keccak256(string: string): string;
export declare function padLeft(string: string, characterAmount: number, sign?: string): string;
export declare function leftPad(string: string, characterAmount: number, sign?: string): string;
export declare function rightPad(string: string, characterAmount: number, sign?: string): string;
export declare function padRight(string: string, characterAmount: number, sign?: string): string;
export declare function sha3(value: Mixed): string;
export declare function randomHex(bytesSize: number): string;
export declare function utf8ToHex(string: string): string;
export declare function stringToHex(string: string): string;
export declare function toChecksumAddress(address: string): string;
export declare function toDecimal(hex: Hex): number;
export declare function toHex(value: Mixed): string;
export declare function toUtf8(string: string): string;
export declare function toWei(value: number | string | BigNumber, unit?: Unit): BigNumber | string;
export declare function isBloom(bloom: string): boolean;
export declare function isTopic(topic: string): boolean;
export declare function _fireError(error: Object, emitter: Object, reject: Function, callback: Function): Object;
export declare function _jsonInterfaceMethodToString(json: Object): string;
export declare function _flattenTypes(includeTuple: boolean, puts: Object): string[];
export declare function soliditySha3(...val: Mixed[]): string;
export declare function getUnitValue(unit: string): string;
export declare function unitMap(): IUnits;

// bloom types
export declare function testAddress(bloom: string, address: string): boolean;
export declare function testTopic(bloom: string, topic: string): boolean;

export interface IUnits {
noether: string;
wei: string;
kwei: string;
Kwei: string;
babbage: string;
femtoether: string;
mwei: string;
Mwei: string;
lovelace: string;
picoether: string;
gwei: string;
Gwei: string;
shannon: string;
nanoether: string;
nano: string;
szabo: string;
microether: string;
micro: string;
finney: string;
milliether: string;
milli: string;
ether: string;
kether: string;
grand: string;
mether: string;
gether: string;
tether: string;
}
8 changes: 7 additions & 1 deletion packages/web3-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
"description": "Collection of utility functions used in web3.js.",
"repository": "https://github.com/ethereum/web3.js/tree/master/packages/web3-utils",
"license": "LGPL-3.0",
"main": "src/index.js",
"main": "dist/web3-utils.cjs.js",
"module": "dist/web3-utils.esm.js",
"browser": "dist/web3-utils.umd.js",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w"
},
"dependencies": {
"bn.js": "4.11.8",
"eth-lib": "0.2.8",
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-utils/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export const hexToUtf8 = (hex) => {
*
* @param {String|Number|BN} value
*
* @returns {String}
* @returns {Number}
*/
export const hexToNumber = (value) => {
if (!value) {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {isObject, isString, isArray, isFunction} from 'underscore';
import * as utils from './Utils';

export {soliditySha3} from './SoliditySha3';
export randomHex from 'randomhex';
export {randomHex} from 'randomhex';
export * from './Utils';

export const unitMap = {
Expand Down