Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Final web3 types PR #2133

Merged
merged 15 commits into from
Dec 18, 2018
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
2 changes: 2 additions & 0 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Fixes #(issue)
- [ ] I have commented my code, particularly in hard-to-understand areas.
- [ ] I have made corresponding changes to the documentation.
- [ ] My changes generate no warnings.
- [ ] I have updated or added types for all modules I've changed
- [ ] Any dependent changes have been merged and published in downstream modules.
- [ ] I ran ```npm run test``` with success and extended the tests if necessary.
- [ ] I ran ```npm run build``` and tested it in the browser and with node.
- [ ] I ran ```npm run-script dtslint``` and tested that all my types are correct
- [ ] I have tested my code on the live network.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,15 @@ web3.eth.getAccounts()

### Usage with TypeScript

Type definitions are maintained at [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) by others. You can install them with
We do support types within the repo itself. Please open an issue here if you find any wrong types.

```bash
npm install --dev @types/web3.js
```

You might need to install type definitions for `bignumber.js` and `lodash` too.

And then use `web3.js` as follows:
You can then use `web3.js` as follows:

```typescript
import Web3 = require("web3"); // Note the special syntax! Copy this line when in doubt!
import Web3 from 'web3';
const web3 = new Web3("ws://localhost:8546");
```

**Please note:** We do not support TypeScript ourselves. If you have any issue with TypeScript and `web3.js` do not create an issue here. Go over to DefinitelyTyped and do it there.

## Documentation

Documentation can be found at [read the docs][docs]
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@
"rollup-plugin-cleanup": "^3.0.0",
"rollup-plugin-json": "^3.1.0",
"@types/bn.js": "^4.11.3",
"typescript": "^3.2.2"
},
"dependencies": {
"typescript": "^3.2.2",
"@babel/polyfill": "^7.0.0",
"@types/node": "^10.12.12"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-bzz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "jest",
"dtslint": "dtslint types"
"dtslint": "dtslint types --onlyTestTsNext"
},
"dependencies": {
"swarm-js": "^0.1.39",
Expand Down
72 changes: 72 additions & 0 deletions packages/web3-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,75 @@ export interface Providers {
WebsocketProvider: WebsocketProvider;
IpcProvider: IpcProvider;
}

export interface PromiEvent<T> extends Promise<T> {
once(type: 'transactionHash', handler: (receipt: string) => void): PromiEvent<T>
once(type: 'receipt', handler: (receipt: TransactionReceipt) => void): PromiEvent<T>
once(type: 'confirmation', handler: (confNumber: number, receipt: TransactionReceipt) => void): PromiEvent<T>
once(type: 'error', handler: (error: Error) => void): PromiEvent<T>
once(type: 'error' | 'confirmation' | 'receipt' | 'transactionHash', handler: (error: Error | TransactionReceipt | string) => void): PromiEvent<T>
on(type: 'transactionHash', handler: (receipt: string) => void): PromiEvent<T>
on(type: 'receipt', handler: (receipt: TransactionReceipt) => void): PromiEvent<T>
on(type: 'confirmation', handler: (confNumber: number, receipt: TransactionReceipt) => void): PromiEvent<T>
on(type: 'error', handler: (error: Error) => void): PromiEvent<T>
on(type: 'error' | 'confirmation' | 'receipt' | 'transactionHash', handler: (error: Error | TransactionReceipt | string) => void): PromiEvent<T>
}

export interface Transaction {
from?: string | number;
to?: string;
gasPrice?: string;
gas?: number | string;
value?: number | string;
chainId?: number;
data?: string;
nonce?: number;
v?: string;
r?: string;
s?: string;
hash?: string;
}

export interface RLPEncodedTransaction {
raw: string,
tx: Transaction
}

export interface TransactionReceipt {
transactionHash: string
transactionIndex: number
blockHash: string
blockNumber: number
from: string
to: string
contractAddress: string
cumulativeGasUsed: number
gasUsed: number
logs?: Log[]
events?: {
[eventName: string]: EventLog
}
}

export interface EventLog {
event: string
address: string
returnValues: object
logIndex: number
transactionIndex: number
transactionHash: string
blockHash: string
blockNumber: number
raw?: { data: string, topics: any[] }
}

export interface Log {
address: string;
data: string;
topics: Array<string | string[]>;
logIndex: number;
transactionIndex: number;
transactionHash: string;
blockHash: string;
blockNumber: number;
}
2 changes: 1 addition & 1 deletion packages/web3-eth-abi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "jest",
"dtslint": "dtslint types"
"dtslint": "dtslint types --onlyTestTsNext"
},
"types": "types",
"dependencies": {
Expand Down
24 changes: 2 additions & 22 deletions packages/web3-eth-abi/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @date 2018
*/

import {AbiItem, AbiInput} from 'web3-utils';

export class AbiCoder {
encodeFunctionSignature(functionName: string | AbiItem): string;
encodeEventSignature(functionName: string | AbiItem): string;
Expand All @@ -27,25 +29,3 @@ export class AbiCoder {
decodeParameters(types: Array<string | {}>, hex: string): { [key: string] : any; };
decodeLog(inputs: AbiInput[], hex: string, topics: string[]): { [key: string] : string; };
}

/******** These types need moving into a generic codebase to be shared to avoid repeating ***************/
export type AbiType = "function" | "constructor" | "event" | "fallback";

export interface AbiItem {
constant?: boolean;
inputs?: AbiInput[];
name?: string;
outputs?: AbiOuput[];
payable?: boolean;
stateMutability?: string; // could be a enum once we move this to generic place
type: AbiType;
}
export interface AbiInput {
name: string;
type: string;
indexed?: boolean;
}
export interface AbiOuput {
name: string;
type: string;
}
2 changes: 1 addition & 1 deletion packages/web3-eth-abi/types/tests/abi-coder-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @date 2018
*/

import {AbiCoder} from "web3-eth-abi";
import {AbiCoder} from 'web3-eth-abi';

const abiCoder = new AbiCoder();

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "jest",
"dtslint": "dtslint types"
"dtslint": "dtslint types --onlyTestTsNext"
},
"dependencies": {
"crypto-browserify": "3.12.0",
Expand Down
19 changes: 3 additions & 16 deletions packages/web3-eth-accounts/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @date 2018
*/

import {AbstractWeb3Module, Web3ModuleOptions} from 'web3-core';
import {AbstractWeb3Module, Web3ModuleOptions, Transaction} from 'web3-core';
import {AbstractProviderAdapter, provider} from 'web3-providers';

export class Accounts extends AbstractWeb3Module {
Expand Down Expand Up @@ -83,20 +83,7 @@ export interface EncryptedKeystoreV3Json {
}
}

/** MOVE ALL BELOW TO WEB3-CORE ONCE TYPES COMPLETE FOR CLEAR UP !!! */
export interface Transaction {
from?: string | number;
to?: string;
gasPrice?: string;
gas?: number | string;
value?: number | string;
chainId?: number;
data?: string;
nonce?: number;
v?: string;
r?: string;
s?: string;
}
/** TODO - MOVE ALL BELOW TO WEB3-CORE ONCE FIXED CONFUSING WITH RETURN TYPES !!! */

export interface SignedTransaction {
messageHash?: string;
Expand All @@ -110,4 +97,4 @@ export interface Sign extends SignedTransaction {
message: string;
}

/** END !!! */
/** END !!! */
2 changes: 1 addition & 1 deletion packages/web3-eth-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "jest",
"dtslint": "dtslint types"
"dtslint": "dtslint types --onlyTestTsNext"
},
"dependencies": {
"underscore-es": "^1.9.8",
Expand Down
25 changes: 20 additions & 5 deletions packages/web3-eth-contract/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
*/

import {AbstractProviderAdapter, provider} from 'web3-providers';
import {AbiItem, BN} from 'web3-utils';

export class Contract {
constructor(
provider: AbstractProviderAdapter | provider,
jsonInterface: {},
jsonInterface: AbiItem[] | AbiItem,
address?: string,
options?: ContractOptions
)
Expand All @@ -41,7 +42,7 @@ export class Contract {

export interface Options {
address: string;
jsonInterface: any[]; // strongly type this
jsonInterface: AbiItem[];
data: string;
from: string;
gasPrice: string;
Expand All @@ -56,18 +57,32 @@ export interface DeployOptions {
export interface DeployTransactionResponse {
array: any[];
send(options: SendOptions): () => Promise<Contract>;
estimateGas(_function: (err: Error, gas: number) => void): void;
estimateGas(options: EstimateGasOptions, callback?: (err: Error, gas: number) => void): void;
estimateGas(callback: (err: Error, gas: number) => void): void;
estimateGas(options: EstimateGasOptions, callback: (err: Error, gas: number) => void): void;
estimateGas(options: EstimateGasOptions): void;
encodeABI(): string;
}

export interface SendOptions {
from: string;
gasPrice?: string;
gas: number;
value?: any;
value?: number | string | BN;
}

export interface ContractOptions extends SendOptions {}
export interface EstimateGasOptions {
from?: string;
gas: number;
value?: number | string | BN;
}

export interface ContractOptions {
from: string;
gasPrice: string;
gas: number;
data: string;
}

export interface EventOptions {
filter: {};
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-eth-contract/types/tests/contract-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* @date 2018
*/

import { Contract } from 'web3-eth-contract';
import {Contract} from 'web3-eth-contract';

const contract = new Contract('http://localhost:500', {});
const contract = new Contract('http://localhost:500', {type: 'constructor'});

// $ExpectType Contract
contract.clone();
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-iban/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "jest",
"dtslint": "dtslint types"
"dtslint": "dtslint types --onlyTestTsNext"
},
"types": "types",
"dependencies": {
Expand Down
24 changes: 1 addition & 23 deletions packages/web3-eth-personal/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import * as net from 'net';
import {AbstractProviderAdapter, provider, BatchRequest} from 'web3-providers';
import {AbstractWeb3Module, Web3ModuleOptions, Providers} from 'web3-core';
import {AbstractWeb3Module, Web3ModuleOptions, Providers, Transaction, RLPEncodedTransaction} from 'web3-core';
export class Personal extends AbstractWeb3Module {
constructor(
provider: AbstractProviderAdapter | provider,
options?: Web3ModuleOptions
);
setProvider(provider: AbstractProviderAdapter | provider, net?: net.Server): boolean;
givenProvider: AbstractProviderAdapter | null;
BatchRequest: new() => BatchRequest;
providers: Providers;
newAccount(password: string, callback?: (error: Error, address: string) => void): Promise<string>;
Expand All @@ -35,24 +34,3 @@ export class Personal extends AbstractWeb3Module {
signTransaction(transation: Transaction, password: string, callback?: (error: Error, RLPEncodedTransaction: RLPEncodedTransaction) => void): Promise<RLPEncodedTransaction>;
unlockAccount(address: string, password: string, unlockDuration: number, callback?: (error: Error) => void): Promise<boolean>;
}

// Josh to move to web3-core
export interface Transaction {
from?: string | number;
to?: string;
gasPrice?: string;
gas?: number | string;
value?: number | string;
chainId?: number;
data?: string;
nonce?: number;
v?: string;
r?: string;
s?: string;
hash?: string;
}

export interface RLPEncodedTransaction {
raw: string,
tx: Transaction
}
6 changes: 2 additions & 4 deletions packages/web3-eth-personal/types/tests/personal-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* @date 2018
*/

import {Personal, RLPEncodedTransaction} from 'web3-eth-personal';
import {RLPEncodedTransaction} from 'web3-core';
import {Personal} from 'web3-eth-personal';
import {HttpProvider} from 'web3-providers';

const options = {
Expand All @@ -41,9 +42,6 @@ personal.setProvider(httpProvider)
// $ExpectType Providers
personal.providers;

// $ExpectType AbstractProviderAdapter | null
personal.givenProvider;

// $ExpectType AbstractProviderAdapter
personal.currentProvider;

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"test": "jest",
"dtslint": "dtslint types"
"dtslint": "dtslint types --onlyTestTsNext"
},
"types": "types",
"dependencies": {
Expand Down
Loading