UNPKG

9.54 kBTypeScriptView Raw
1declare module 'web3' {
2 import * as BigNumber from 'bignumber.js';
3 import {
4 AbiDefinition,
5 BlockWithTransactionData,
6 BlockWithoutTransactionData,
7 BlockParam,
8 CallData,
9 Provider,
10 Unit,
11 TxData,
12 Transaction,
13 ContractAbi,
14 TransactionReceipt,
15 FilterObject,
16 LogEntryEvent,
17 JSONRPCRequestPayload,
18 JSONRPCResponsePayload,
19 } from 'ethereum-types';
20
21 type MixedData = string | number | object | any[] | BigNumber.BigNumber;
22
23 class Web3 {
24 public static providers: typeof providers;
25 public currentProvider: Provider;
26
27 public eth: Web3.EthApi;
28 public personal: Web3.PersonalApi | undefined;
29 public version: Web3.VersionApi;
30 public net: Web3.NetApi;
31
32 public constructor(provider?: Provider);
33
34 public isConnected(): boolean;
35 public setProvider(provider: Provider): void;
36 public reset(keepIsSyncing: boolean): void;
37 public toHex(data: MixedData): string;
38 public toAscii(hex: string): string;
39 public fromAscii(ascii: string, padding?: number): string;
40 public toDecimal(hex: string): number;
41 public fromDecimal(value: number | string): string;
42 public fromWei(value: number | string, unit: Unit): string;
43 public fromWei(value: BigNumber.BigNumber, unit: Unit): BigNumber.BigNumber;
44 public toWei(amount: number | string, unit: Unit): string;
45 public toWei(amount: BigNumber.BigNumber, unit: Unit): BigNumber.BigNumber;
46 public toBigNumber(value: number | string): BigNumber.BigNumber;
47 public isAddress(address: string): boolean;
48 public isChecksumAddress(address: string): boolean;
49 public sha3(value: string, options?: Web3.Sha3Options): string;
50 }
51
52 namespace providers {
53 class HttpProvider implements Provider {
54 constructor(url?: string, timeout?: number, username?: string, password?: string);
55 public sendAsync(
56 payload: JSONRPCRequestPayload,
57 callback: (err: Error, result: JSONRPCResponsePayload) => void,
58 ): void;
59 }
60 }
61
62 namespace Web3 {
63 interface ContractInstance {
64 address: string;
65 abi: ContractAbi;
66 [name: string]: any;
67 }
68
69 interface Contract<A extends ContractInstance> {
70 at(address: string): A;
71 getData(...args: any[]): string;
72 'new'(...args: any[]): A;
73 }
74
75 interface FilterResult {
76 get(callback: () => void): void;
77 watch(callback: (err: Error, result: LogEntryEvent) => void): void;
78 stopWatching(callback?: () => void): void;
79 }
80
81 interface Sha3Options {
82 encoding: 'hex';
83 }
84
85 interface EthApi {
86 coinbase: string;
87 mining: boolean;
88 hashrate: number;
89 gasPrice: BigNumber.BigNumber;
90 accounts: string[];
91 blockNumber: number;
92 defaultAccount?: string;
93 defaultBlock: BlockParam;
94 syncing: Web3.SyncingResult;
95 compile: {
96 solidity(sourceString: string, cb?: (err: Error, result: any) => void): object;
97 };
98 getMining(cd: (err: Error, mining: boolean) => void): void;
99 getHashrate(cd: (err: Error, hashrate: number) => void): void;
100 getGasPrice(cd: (err: Error, gasPrice: BigNumber.BigNumber) => void): void;
101 getAccounts(cd: (err: Error, accounts: string[]) => void): void;
102 getBlockNumber(callback: (err: Error, blockNumber: number) => void): void;
103 getSyncing(cd: (err: Error, syncing: Web3.SyncingResult) => void): void;
104 isSyncing(cb: (err: Error, isSyncing: boolean, syncingState: Web3.SyncingState) => void): Web3.IsSyncing;
105
106 getBlock(hashStringOrBlockNumber: string | BlockParam): BlockWithoutTransactionData;
107 getBlock(
108 hashStringOrBlockNumber: string | BlockParam,
109 callback: (err: Error, blockObj: BlockWithoutTransactionData) => void,
110 ): void;
111 getBlock(
112 hashStringOrBlockNumber: string | BlockParam,
113 returnTransactionObjects: true,
114 ): BlockWithTransactionData;
115 getBlock(
116 hashStringOrBlockNumber: string | BlockParam,
117 returnTransactionObjects: true,
118 callback: (err: Error, blockObj: BlockWithTransactionData) => void,
119 ): void;
120
121 getBlockTransactionCount(hashStringOrBlockNumber: string | BlockParam): number;
122 getBlockTransactionCount(
123 hashStringOrBlockNumber: string | BlockParam,
124 callback: (err: Error, blockTransactionCount: number) => void,
125 ): void;
126
127 // TODO returnTransactionObjects
128 getUncle(hashStringOrBlockNumber: string | BlockParam, uncleNumber: number): BlockWithoutTransactionData;
129 getUncle(
130 hashStringOrBlockNumber: string | BlockParam,
131 uncleNumber: number,
132 callback: (err: Error, uncle: BlockWithoutTransactionData) => void,
133 ): void;
134
135 getTransaction(transactionHash: string): Transaction;
136 getTransaction(transactionHash: string, callback: (err: Error, transaction: Transaction) => void): void;
137
138 getTransactionFromBlock(hashStringOrBlockNumber: string | BlockParam, indexNumber: number): Transaction;
139 getTransactionFromBlock(
140 hashStringOrBlockNumber: string | BlockParam,
141 indexNumber: number,
142 callback: (err: Error, transaction: Transaction) => void,
143 ): void;
144
145 contract(abi: AbiDefinition[]): Web3.Contract<any>;
146
147 // TODO block param
148 getBalance(addressHexString: string): BigNumber.BigNumber;
149 getBalance(addressHexString: string, callback: (err: Error, result: BigNumber.BigNumber) => void): void;
150
151 // TODO block param
152 getStorageAt(address: string, position: number): string;
153 getStorageAt(address: string, position: number, callback: (err: Error, storage: string) => void): void;
154
155 // TODO block param
156 getCode(addressHexString: string): string;
157 getCode(addressHexString: string, callback: (err: Error, code: string) => void): void;
158
159 filter(value: string | FilterObject): Web3.FilterResult;
160
161 sendTransaction(txData: TxData): string;
162 sendTransaction(txData: TxData, callback: (err: Error, value: string) => void): void;
163
164 sendRawTransaction(rawTxData: string): string;
165 sendRawTransaction(rawTxData: string, callback: (err: Error, value: string) => void): void;
166
167 sign(address: string, data: string): string;
168 sign(address: string, data: string, callback: (err: Error, signature: string) => void): void;
169
170 getTransactionReceipt(txHash: string): TransactionReceipt | null;
171 getTransactionReceipt(
172 txHash: string,
173 callback: (err: Error, receipt: TransactionReceipt | null) => void,
174 ): void;
175
176 // TODO block param
177 call(callData: CallData): string;
178 call(callData: CallData, callback: (err: Error, result: string) => void): void;
179
180 estimateGas(callData: CallData): number;
181 estimateGas(callData: CallData, callback: (err: Error, gas: number) => void): void;
182
183 // TODO defaultBlock
184 getTransactionCount(address: string): number;
185 getTransactionCount(address: string, callback: (err: Error, count: number) => void): void;
186 }
187
188 interface VersionApi {
189 api: string;
190 network: string;
191 node: string;
192 ethereum: string;
193 whisper: string;
194 getNetwork(cd: (err: Error, networkId: string) => void): void;
195 getNode(cd: (err: Error, nodeVersion: string) => void): void;
196 getEthereum(cd: (err: Error, ethereum: string) => void): void;
197 getWhisper(cd: (err: Error, whisper: string) => void): void;
198 }
199
200 interface PersonalApi {
201 listAccounts: string[] | undefined;
202 newAccount(password?: string): string;
203 unlockAccount(address: string, password?: string, duration?: number): boolean;
204 lockAccount(address: string): boolean;
205 sign(message: string, account: string, password: string): string;
206 sign(hexMessage: string, account: string, callback: (error: Error, signature: string) => void): void;
207 }
208
209 interface NetApi {
210 listening: boolean;
211 peerCount: number;
212 getListening(cd: (err: Error, listening: boolean) => void): void;
213 getPeerCount(cd: (err: Error, peerCount: number) => void): void;
214 }
215
216 interface SyncingState {
217 startingBlock: number;
218 currentBlock: number;
219 highestBlock: number;
220 }
221 type SyncingResult = false | SyncingState;
222
223 interface IsSyncing {
224 addCallback(cb: (err: Error, isSyncing: boolean, syncingState: SyncingState) => void): void;
225 stopWatching(): void;
226 }
227 }
228 /* tslint:disable */
229 export = Web3;
230 /* tslint:enable */
231}