You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: docs/docs/guides/15_web3_upgrade_guide/providers_migration_guide.md
+58-21Lines changed: 58 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,13 @@ sidebar_label: web3.providers
6
6
7
7
For full description about the providers, their priorities and their types, you can check [web3.js Providers Guide](/guides/web3_providers_guide/).
8
8
9
-
###Provider Options Changes
9
+
## Provider Options Changes
10
10
11
-
There are differences in the objects that could be passed in the Provider constructors between version 1.x and 4.x. Below, you will find the difference for every Provider object type.
11
+
There are differences in the objects that could be passed in the Provider constructors between version 1.x and v4. Below, you will find the difference for every Provider object type.
12
12
13
-
#### HttpProvider
13
+
### HttpProvider
14
+
15
+
#### HttpProviderOptions
14
16
15
17
In 1.x, options passed in the constructor should be of type [`HttpProviderOptions`](https://github.com/web3/web3.js/blob/1.x/packages/web3-core-helpers/types/index.d.ts#L173). The `HttpProviderOptions` interface consists of:
16
18
@@ -35,7 +37,7 @@ interface HttpHeader {
35
37
}
36
38
```
37
39
38
-
In 4.x, the options is of type `HttpProviderOptions`, which is an object with a `providerOptions` key and value a `RequestInit` object.
40
+
In v4, the options is of type `HttpProviderOptions`, which is an object with a `providerOptions` key and value a `RequestInit` object.
39
41
Regarding `RequestInit` see [microsoft's github](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_dom_d_.requestinit.html).
40
42
41
43
For example:
@@ -58,7 +60,7 @@ let httpOptions = {
58
60
}
59
61
};
60
62
61
-
// in 4.x
63
+
// in v4
62
64
let httpOptions = {
63
65
providerOptions: {
64
66
body: undefined,
@@ -80,7 +82,9 @@ let httpOptions = {
80
82
};
81
83
```
82
84
83
-
#### WebSocketProvider
85
+
### WebSocketProvider
86
+
87
+
#### WebsocketProviderOptions
84
88
85
89
In 1.x, options passed in the constructor should be of type [`WebsocketProviderOptions`](https://github.com/web3/web3.js/blob/1.x/packages/web3-core-helpers/types/index.d.ts#L192). The `WebsocketProviderOptions` interface consists of:
86
90
@@ -105,9 +109,9 @@ interface ReconnectOptions {
105
109
}
106
110
```
107
111
108
-
In 4.x, the `socketOptions` parameter is of type `ClientRequestArgs` or of `ClientOptions`. See [here](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules__types_node_http_d_._http_.clientrequestargs.html) for `ClientRequestArgs` and [here](https://github.com/websockets/ws) for `ClientOptions`.
112
+
In v4, the `socketOptions` parameter is of type `ClientRequestArgs` or of `ClientOptions`. See [here](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules__types_node_http_d_._http_.clientrequestargs.html) for `ClientRequestArgs` and [here](https://github.com/websockets/ws) for `ClientOptions`.
109
113
110
-
In 4.x the `reconnectOptions` parameter can be given to control: auto-reconnecting, delay and max tries attempts. And here is its type:
114
+
In v4 the `reconnectOptions` parameter can be given to control: auto-reconnecting, delay and max tries attempts. And here is its type:
111
115
112
116
```ts
113
117
// this is the same options interface used for both WebSocketProvider and IpcProvider
@@ -118,7 +122,36 @@ type ReconnectOptions = {
118
122
};
119
123
```
120
124
121
-
##### Options examples
125
+
#### Program Behavior with WebSocket Provider
126
+
127
+
In 1.x, a program connected to a WebSocket provider would automatically terminate after executing all code, even if the WebSocket connection was still active:
128
+
129
+
```ts
130
+
const web3 =newWeb3(wsUrl);
131
+
// The program will terminate after connecting since there are no more lines of code to run.
132
+
```
133
+
134
+
In version 4.x, this behavior has changed. When connected to a WebSocket provider, the program will not automatically terminate after the code finishes running. This is to ensure the WebSocket connection remains open, allowing the program to continue listening for events.
135
+
136
+
The program will remain active until you explicitly disconnect from the WebSocket provider:
137
+
138
+
```ts
139
+
const web3 =newWeb3(wsUrl);
140
+
// The program will keep running to listen for events.
141
+
```
142
+
143
+
#### Terminating the Program
144
+
145
+
When you're ready to stop the program, you can manually disconnect from the WebSocket provider by calling the disconnect method. This will properly close the connection and terminate the program:
146
+
147
+
```ts
148
+
const web3 =newWeb3(wsUrl);
149
+
// When you are ready to terminate your program
150
+
web3.currentProvider?.disconnect();
151
+
// The program will now terminate
152
+
```
153
+
154
+
### Options examples
122
155
123
156
Below is an example for the passed options for each version:
124
157
@@ -150,7 +183,7 @@ var options = {
150
183
onTimeout: false,
151
184
},
152
185
};
153
-
// in 4.x
186
+
// in v4
154
187
let clientOptions:ClientOptions= {
155
188
// Useful for credentialed urls, e.g: ws://username:password@localhost:8546
156
189
headers: {
@@ -199,7 +232,9 @@ const provider = new WebSocketProvider(
199
232
);
200
233
```
201
234
202
-
#### Legacy Event `close` has been deprecated
235
+
### Legacy Event `close` has been deprecated
236
+
237
+
#### Disconnect and close event
203
238
204
239
Following EIP-1193, the `close` event has been deprecated and is superceded by `disconnect`.
205
240
In 1.x, we listen for a `close` event:
@@ -215,7 +250,7 @@ provider.disconnect(1012);
215
250
// would eventually log closed
216
251
```
217
252
218
-
In 4.x, we listen for a `disconnect` event:
253
+
In v4, we listen for a `disconnect` event:
219
254
220
255
```ts
221
256
const provider =newWebSocketProvider(host+port);
@@ -228,7 +263,9 @@ provider.disconnect(1012);
228
263
// would eventually log 'closed'
229
264
```
230
265
231
-
#### IpcProvider
266
+
### IpcProvider
267
+
268
+
#### Using IpcProvider
232
269
233
270
The IPC provider is used in node.js dapps when running a local node. And it provide the most secure connection.
In 4.x the third parameter is called `reconnectOptions` that is of the type `ReconnectOptions`. It can be given to control: auto-reconnecting, delay and max tries attempts. And here its type:
302
+
In v4 the third parameter is called `reconnectOptions` that is of the type `ReconnectOptions`. It can be given to control: auto-reconnecting, delay and max tries attempts. And here its type:
266
303
267
304
```ts
268
305
// this is the same options interface used for both WebSocketProvider and IpcProvider
@@ -285,7 +322,7 @@ new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net);
285
322
// on windows the path is: "\\\\.\\pipe\\geth.ipc"
286
323
// on linux the path is: "/users/myuser/.ethereum/geth.ipc"
287
324
288
-
// in 4.x
325
+
// in v4
289
326
let clientOptions:SocketConstructorOpts= {
290
327
allowHalfOpen: false;
291
328
readable: true;
@@ -333,7 +370,7 @@ const provider = new IpcProvider(
333
370
);
334
371
```
335
372
336
-
####Error message for reconnect attempts
373
+
### Error message for reconnect attempts
337
374
338
375
:::note
339
376
This section applies for both `IpcProvider` and `WebSocketProvider`.
@@ -342,11 +379,11 @@ This section applies for both `IpcProvider` and `WebSocketProvider`.
342
379
The error in, version 1.x, was an Error object that contains the message:
343
380
`'Maximum number of reconnect attempts reached!'`
344
381
345
-
And, the error in version 4.x, is the same, but will also contain the value of the variable `maxAttempts` as follows:
382
+
And, the error in version v4, is the same, but will also contain the value of the variable `maxAttempts` as follows:
346
383
347
384
`` `Maximum number of reconnect attempts reached! (${maxAttempts})` ``
348
385
349
-
And here is how to catch the error, in version 4.x, if max attempts reached when there is auto reconnecting:
386
+
And here is how to catch the error, in version v4, if max attempts reached when there is auto reconnecting:
0 commit comments