1 | /**
|
2 | * This is the main (or 'umbrella') class of the web3.js library.
|
3 | *
|
4 | * ```ts
|
5 | * import Web3 from 'web3';
|
6 | *
|
7 | * > Web3.utils
|
8 | * > Web3.version
|
9 | * > Web3.givenProvider
|
10 | * > Web3.providers
|
11 | * > Web3.modules
|
12 | * ```
|
13 | *
|
14 | * # Web3.modules
|
15 | *
|
16 | * ```ts
|
17 | * Web3.modules
|
18 | * ```
|
19 | *
|
20 | * Will return an object with the classes of all major sub modules, to be able to instantiate them manually.
|
21 | *
|
22 | * #### Returns
|
23 | *
|
24 | * `Object` A list of module constructors:
|
25 | *
|
26 | *
|
27 | * + `Eth` - `Constructor`: The Eth module for interacting with the Ethereum network
|
28 | *
|
29 | *
|
30 | * + `Net` - `Constructor`: The Net module for interacting with network properties.
|
31 | *
|
32 | *
|
33 | * + `Personal` - `constructor`: The Personal module for interacting with the Ethereum accounts (web3.eth.personal).
|
34 | *
|
35 | * #### Example
|
36 | *
|
37 | * ```ts
|
38 | * Web3.modules
|
39 | * > {
|
40 | * Eth: Eth(provider),
|
41 | * Net: Net(provider),
|
42 | * Personal: Personal(provider),
|
43 | * }
|
44 | * ```
|
45 | *
|
46 | * See details: {@link Web3.modules}
|
47 | *
|
48 | * # Web3 Instance
|
49 | *
|
50 | * The Web3 class is an umbrella package to house all Ethereum related modules.
|
51 | *
|
52 | * ```ts
|
53 | * import Web3 from 'web3';
|
54 | *
|
55 | * // "Web3.givenProvider" will be set if in an Ethereum supported browser.
|
56 | * const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546');
|
57 | *
|
58 | * > web3.eth
|
59 | * > web3.utils
|
60 | * > web3.version
|
61 | * ```
|
62 | *
|
63 | * ### version
|
64 | *
|
65 | * Contains the current package version of the web3.js library.
|
66 | *
|
67 | * #### Returns
|
68 | * //todo enable when functionality added
|
69 | * // @see Web3.version
|
70 | *
|
71 | * ### utils
|
72 | *
|
73 | * Static accessible property of the Web3 class and property of the instance as well.
|
74 | *
|
75 | * ```ts
|
76 | * Web3.utils
|
77 | * web3.utils
|
78 | * ```
|
79 | *
|
80 | * Utility functions are also exposed on the `Web3` class object diretly.
|
81 | *
|
82 | * //todo enable when implemented
|
83 | * //See details: {@link Web3.utils}
|
84 | *
|
85 | * ### setProvider
|
86 | *
|
87 | * ```ts
|
88 | * web3.setProvider(myProvider)
|
89 | * web3.eth.setProvider(myProvider)
|
90 | * ...
|
91 | * ```
|
92 | *
|
93 | * Will change the provider for its module.
|
94 | *
|
95 | * **_NOTE:_** When called on the umbrella package web3 it will also set the provider for all sub modules web3.eth etc.
|
96 | *
|
97 | * #### Parameters
|
98 | * `Object` - `myProvider`: a valid provider.
|
99 | *
|
100 | * #### Returns
|
101 | * `Boolean`
|
102 | *
|
103 | * See details: {@link Web3.setProvider}
|
104 | *
|
105 | * #### Example: Local Geth Node
|
106 | * ```ts
|
107 | * import Web3 from "web3";
|
108 | * let web3: Web3 = new Web3('http://localhost:8545');
|
109 | * // or
|
110 | * let web3: Web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
|
111 | *
|
112 | * // change provider
|
113 | * web3.setProvider('ws://localhost:8546');
|
114 | * // or
|
115 | * web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));
|
116 | *
|
117 | * //todo add IPC provider
|
118 | * ```
|
119 | *
|
120 | * #### Example: Remote Geth Node
|
121 | *
|
122 | * ```ts
|
123 | * // Using a remote node provider, like Alchemy (https://www.alchemyapi.io/supernode), is simple.
|
124 | * import Web3 from "web3";
|
125 | * let web3: Web3 = new Web3("https://eth-mainnet.alchemyapi.io/v2/your-api-key");
|
126 | * ```
|
127 | *
|
128 | * ### providers
|
129 | *
|
130 | * ```ts
|
131 | * web3.providers
|
132 | * web3.eth.providers
|
133 | * ```
|
134 | * Contains the current available providers.
|
135 | *
|
136 | * #### Returns
|
137 | * `Object` with the following providers:
|
138 | *
|
139 | *
|
140 | * + `Object` - `HttpProvider`: HTTP provider, does not support subscriptions.
|
141 | *
|
142 | *
|
143 | * + `Object` - `WebSocketProvider`: The WebSocket provider is the standard for usage in legacy browsers.
|
144 | *
|
145 | *
|
146 | * + `Object` - `IpcProvider`: The IPC provider is used in node.js dapps when running a local node. Gives the most secure connection.
|
147 | *
|
148 | *
|
149 | * #### Example
|
150 | * ```ts
|
151 | * import { Web3 } from 'web3';
|
152 | * // use the given Provider or instantiate a new websocket provider
|
153 | * let web3 = new Web3(Web3.givenProvider || 'ws://remotenode.com:8546');
|
154 | * // or
|
155 | * let web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://remotenode.com:8546'));
|
156 | *
|
157 | * // Using the IPC provider in node.js
|
158 | * import { Web3 } from 'web3';
|
159 | * import { IpcProvider } from 'web3-providers-ipc';
|
160 | * var web3 = new Web3(new IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc')); // mac os path
|
161 | * // on windows the path is: "\\\\.\\pipe\\geth.ipc"
|
162 | * // on linux the path is: "/users/myuser/.ethereum/geth.ipc"
|
163 | * ```
|
164 | * #### Configuration
|
165 | *
|
166 | * ```ts
|
167 | *
|
168 | * //===
|
169 | * //Http
|
170 | * //===
|
171 | *
|
172 | * import Web3HttpProvider, { HttpProviderOptions } from "web3-providers-http";
|
173 | *
|
174 | * let options: HttpProviderOptions = {
|
175 | * providerOptions: {
|
176 | * keepalive: true,
|
177 | * credentials: "omit",
|
178 | * headers: {
|
179 | * "Access-Control-Allow-Origin": "*",
|
180 | * },
|
181 | * },
|
182 | * };
|
183 | *
|
184 | *
|
185 | * var provider = new Web3HttpProvider("http://localhost:8545", options);
|
186 | * web3.setProvider(provider);
|
187 | *
|
188 | * //===
|
189 | * //WebSockets
|
190 | * //===
|
191 | * import Web3WsProvider, {
|
192 | * ClientOptions,
|
193 | * ClientRequestArgs,
|
194 | * ReconnectOptions,
|
195 | * } from "web3-providers-ws";
|
196 | *
|
197 | *
|
198 | * let clientOptions: ClientOptions = {
|
199 | * // Useful for credentialed urls, e.g: ws://username:password@localhost:8546
|
200 | * headers: {
|
201 | * authorization: "Basic username:password",
|
202 | * },
|
203 | * maxPayload: 100000000,
|
204 | * };
|
205 | *
|
206 | * // Enable auto reconnection
|
207 | * let reconnectOptions: ReconnectOptions = {
|
208 | * autoReconnect: true,
|
209 | * delay: 5000, // ms
|
210 | * maxAttempts: 5,
|
211 | * };
|
212 | *
|
213 | * //clientOptions and reconnectOptions are optional
|
214 | * //clientOptions: ClientOptions | ClientRequestArgs
|
215 | * let ws = new Web3WsProvider(
|
216 | * "ws://localhost:8546",
|
217 | * clientOptions,
|
218 | * reconnectOptions
|
219 | * );
|
220 | * web3.setProvider(ws);
|
221 | *
|
222 | * ```
|
223 | * More information for the Http and Websocket provider modules can be found here:
|
224 | *
|
225 | *
|
226 | * - {@link HttpProvider}
|
227 | *
|
228 | *
|
229 | * - {@link WebSocketProvider}
|
230 | *
|
231 | * See details: {@link Web3.providers}
|
232 | *
|
233 | *
|
234 | * ### givenProvider
|
235 | *
|
236 | * ```ts
|
237 | * web3.givenProvider
|
238 | * web3.eth.givenProvider
|
239 | * ...
|
240 | * ```
|
241 | * When using web3.js in an Ethereum compatible browser, it will set with the current native provider by that browser.
|
242 | * Will return the given provider by the (browser) environment, otherwise `undefined`.
|
243 | *
|
244 | * #### Returns
|
245 | * `Object` - The given provider set or `undefined`.
|
246 | *
|
247 | * See details: {@link Web3.givenProvider}
|
248 | *
|
249 | * ### currentProvider
|
250 | *
|
251 | * ```ts
|
252 | * web3.currentProvider
|
253 | * web3.eth.currentProvider
|
254 | * ...
|
255 | * ```
|
256 | * Will return the current provider, otherwise `undefined`.
|
257 | *
|
258 | * #### Returns
|
259 | * `Object`: The current provider, otherwise `undefined`.
|
260 | *
|
261 | * See details: {@link Web3.currentProvider}
|
262 | *
|
263 | * ### BatchRequest
|
264 | *
|
265 | * ```ts
|
266 | * new web3.BatchRequest()
|
267 | * new web3.BatchRequest()
|
268 | * ...
|
269 | * ```
|
270 | * Class to create and execute batch requests.
|
271 | *
|
272 | * #### Parameters
|
273 | * none
|
274 | *
|
275 | * #### Returns
|
276 | * `Object`: With the following methods:
|
277 | *
|
278 | * + `add(request)`: To add a request object to the batch call.
|
279 | *
|
280 | * + `execute()` : To execute the batch request.
|
281 | *
|
282 | * #### Example
|
283 | * ```ts
|
284 | * let request1: JsonRpcOptionalRequest = {
|
285 | * id: 10,
|
286 | * method: 'eth_getBalance',
|
287 | * params: ["0xdc6bad79dab7ea733098f66f6c6f9dd008da3258", 'latest'],
|
288 | * };
|
289 | * let request2: JsonRpcOptionalRequest = {
|
290 | * id: 11,
|
291 | * method: 'eth_getBalance',
|
292 | * params: ["0x962f9a9c2a6c092474d24def35eccb3d9363265e", 'latest'],
|
293 | * };
|
294 | *
|
295 | * const batch = new web3.BatchRequest();
|
296 | *
|
297 | * batch.add(request1);
|
298 | * batch.add(request2);
|
299 | * // add returns a deferred promise which can be used to run specific code after completion of each respective request.
|
300 | * //const request2Promise = batch.add(request2);
|
301 | *
|
302 | * const response = await batch.execute();
|
303 | * ```
|
304 | * See details: {@link Web3.BatchRequest}
|
305 | */
|
306 | /**
|
307 | * This comment _supports3_ [Markdown](https://marked.js.org/)
|
308 | */
|
309 | import Web3 from './web3.js';
|
310 | export * from './types.js';
|
311 | export * from './web3_eip6963.js';
|
312 | export default Web3;
|
313 | /**
|
314 | * Named exports for all objects which are the default-exported-object in their packages
|
315 | */
|
316 | export { Web3 };
|
317 | export { Web3Context, Web3PluginBase, Web3EthPluginBase, Web3PromiEvent } from 'web3-core';
|
318 | export { Web3Eth } from 'web3-eth';
|
319 | export { Contract, ContractDeploySend, ContractMethodSend } from 'web3-eth-contract';
|
320 | export { Iban } from 'web3-eth-iban';
|
321 | export { Personal } from 'web3-eth-personal';
|
322 | export { Net } from 'web3-net';
|
323 | export { HttpProvider } from 'web3-providers-http';
|
324 | export { WebSocketProvider } from 'web3-providers-ws';
|
325 | export { Web3Validator } from 'web3-validator';
|
326 | /**
|
327 | * Export all packages grouped by name spaces
|
328 | */
|
329 | export * as core from 'web3-core';
|
330 | export * as errors from 'web3-errors';
|
331 | export * as eth from './eth.exports.js';
|
332 | export * as net from 'web3-net';
|
333 | export * as providers from './providers.exports.js';
|
334 | export * as rpcMethods from 'web3-rpc-methods';
|
335 | export * as types from 'web3-types';
|
336 | export * as utils from 'web3-utils';
|
337 | export * as validator from 'web3-validator';
|
338 | /**
|
339 | * Export all types from `web3-types` without a namespace (in addition to being available at `types` namespace).
|
340 | * To enable the user to write: `function something(): Web3Api` without the need for `types.Web3Api`.
|
341 | * And the same for `web3-errors`. Because this package contains error classes and constants.
|
342 | */
|
343 | export * from 'web3-errors';
|
344 | export * from 'web3-types';
|