@atomiqlabs/chain-evm 1.0.0-dev.53 → 1.0.0-dev.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/evm/ReconnectingWebSocketProvider.d.ts +1 -0
- package/dist/evm/ReconnectingWebSocketProvider.js +14 -3
- package/dist/evm/SocketProvider.d.ts +1 -0
- package/dist/evm/SocketProvider.js +6 -0
- package/package.json +1 -1
- package/src/evm/ReconnectingWebSocketProvider.ts +12 -3
- package/src/evm/SocketProvider.ts +8 -0
|
@@ -2,6 +2,7 @@ import { JsonRpcApiProviderOptions } from "ethers";
|
|
|
2
2
|
import type { Networkish, WebSocketLike } from "ethers";
|
|
3
3
|
import { SocketProvider } from "./SocketProvider";
|
|
4
4
|
export declare class ReconnectingWebSocketProvider extends SocketProvider {
|
|
5
|
+
requestTimeoutSeconds: number;
|
|
5
6
|
reconnectSeconds: number;
|
|
6
7
|
pingIntervalSeconds: number;
|
|
7
8
|
pingInterval: any;
|
|
@@ -5,8 +5,9 @@ const SocketProvider_1 = require("./SocketProvider");
|
|
|
5
5
|
class ReconnectingWebSocketProvider extends SocketProvider_1.SocketProvider {
|
|
6
6
|
constructor(url, network, options) {
|
|
7
7
|
super(network, options);
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
8
|
+
this.requestTimeoutSeconds = 10;
|
|
9
|
+
this.reconnectSeconds = 5;
|
|
10
|
+
this.pingIntervalSeconds = 30;
|
|
10
11
|
this.url = url;
|
|
11
12
|
this.connect();
|
|
12
13
|
}
|
|
@@ -16,13 +17,21 @@ class ReconnectingWebSocketProvider extends SocketProvider_1.SocketProvider {
|
|
|
16
17
|
this._connected();
|
|
17
18
|
this._start();
|
|
18
19
|
this.pingInterval = setInterval(() => {
|
|
19
|
-
this.
|
|
20
|
+
this.getBlockNumber().catch(e => {
|
|
21
|
+
//Error
|
|
22
|
+
if (e.code === "NETWORK_ERROR") {
|
|
23
|
+
console.error("Websocket ping error: ", e);
|
|
24
|
+
this.websocket.close();
|
|
25
|
+
}
|
|
26
|
+
});
|
|
20
27
|
}, this.pingIntervalSeconds * 1000);
|
|
21
28
|
};
|
|
22
29
|
this.websocket.onerror = (err) => {
|
|
23
30
|
console.error(`Websocket connection error: `, err);
|
|
24
31
|
if (this.destroyed)
|
|
25
32
|
return;
|
|
33
|
+
if (this.websocket == null)
|
|
34
|
+
return;
|
|
26
35
|
this.websocket = null;
|
|
27
36
|
if (this.pingInterval != null)
|
|
28
37
|
clearInterval(this.pingInterval);
|
|
@@ -38,6 +47,8 @@ class ReconnectingWebSocketProvider extends SocketProvider_1.SocketProvider {
|
|
|
38
47
|
console.error(`Websocket connection closed: `, event);
|
|
39
48
|
if (this.destroyed)
|
|
40
49
|
return;
|
|
50
|
+
if (this.websocket == null)
|
|
51
|
+
return;
|
|
41
52
|
this.websocket = null;
|
|
42
53
|
if (this.pingInterval != null)
|
|
43
54
|
clearInterval(this.pingInterval);
|
|
@@ -82,6 +82,7 @@ export declare class SocketEventSubscriber extends SocketSubscriber {
|
|
|
82
82
|
*/
|
|
83
83
|
export declare class SocketProvider extends JsonRpcApiProvider {
|
|
84
84
|
#private;
|
|
85
|
+
requestTimeoutSeconds: number;
|
|
85
86
|
/**
|
|
86
87
|
* Creates a new **SocketProvider** connected to %%network%%.
|
|
87
88
|
*
|
|
@@ -188,6 +188,7 @@ class SocketProvider extends ethers_1.JsonRpcApiProvider {
|
|
|
188
188
|
options.staticNetwork = true;
|
|
189
189
|
}
|
|
190
190
|
super(network, options);
|
|
191
|
+
this.requestTimeoutSeconds = 10;
|
|
191
192
|
_SocketProvider_callbacks.set(this, void 0);
|
|
192
193
|
// Maps each filterId to its subscriber
|
|
193
194
|
_SocketProvider_subs.set(this, void 0);
|
|
@@ -249,6 +250,11 @@ class SocketProvider extends ethers_1.JsonRpcApiProvider {
|
|
|
249
250
|
// Prepare a promise to respond to
|
|
250
251
|
const promise = new Promise((resolve, reject) => {
|
|
251
252
|
__classPrivateFieldGet(this, _SocketProvider_callbacks, "f").set(payload.id, { payload, resolve, reject });
|
|
253
|
+
setTimeout(() => {
|
|
254
|
+
if (__classPrivateFieldGet(this, _SocketProvider_callbacks, "f").delete(payload.id)) {
|
|
255
|
+
reject((0, ethers_1.makeError)("Request timed out!", "NETWORK_ERROR"));
|
|
256
|
+
}
|
|
257
|
+
}, this.requestTimeoutSeconds * 1000);
|
|
252
258
|
});
|
|
253
259
|
// Wait until the socket is connected before writing to it
|
|
254
260
|
await this._waitUntilReady();
|
package/package.json
CHANGED
|
@@ -5,8 +5,9 @@ import {SocketProvider} from "./SocketProvider";
|
|
|
5
5
|
|
|
6
6
|
export class ReconnectingWebSocketProvider extends SocketProvider {
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
requestTimeoutSeconds: number = 10;
|
|
9
|
+
reconnectSeconds: number = 5;
|
|
10
|
+
pingIntervalSeconds: number = 30;
|
|
10
11
|
|
|
11
12
|
pingInterval: any;
|
|
12
13
|
reconnectTimer: any;
|
|
@@ -28,13 +29,20 @@ export class ReconnectingWebSocketProvider extends SocketProvider {
|
|
|
28
29
|
this._start();
|
|
29
30
|
|
|
30
31
|
this.pingInterval = setInterval(() => {
|
|
31
|
-
this.
|
|
32
|
+
this.getBlockNumber().catch(e => {
|
|
33
|
+
//Error
|
|
34
|
+
if(e.code==="NETWORK_ERROR") {
|
|
35
|
+
console.error("Websocket ping error: ", e);
|
|
36
|
+
this.websocket.close();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
32
39
|
}, this.pingIntervalSeconds * 1000);
|
|
33
40
|
};
|
|
34
41
|
|
|
35
42
|
this.websocket.onerror = (err) => {
|
|
36
43
|
console.error(`Websocket connection error: `, err);
|
|
37
44
|
if(this.destroyed) return;
|
|
45
|
+
if(this.websocket==null) return;
|
|
38
46
|
this.websocket = null;
|
|
39
47
|
if(this.pingInterval!=null) clearInterval(this.pingInterval);
|
|
40
48
|
|
|
@@ -52,6 +60,7 @@ export class ReconnectingWebSocketProvider extends SocketProvider {
|
|
|
52
60
|
this.websocket.onclose = (event) => {
|
|
53
61
|
console.error(`Websocket connection closed: `, event);
|
|
54
62
|
if(this.destroyed) return;
|
|
63
|
+
if(this.websocket==null) return;
|
|
55
64
|
this.websocket = null;
|
|
56
65
|
if(this.pingInterval!=null) clearInterval(this.pingInterval);
|
|
57
66
|
|
|
@@ -181,6 +181,9 @@ export class SocketEventSubscriber extends SocketSubscriber {
|
|
|
181
181
|
* its communication channel.
|
|
182
182
|
*/
|
|
183
183
|
export class SocketProvider extends JsonRpcApiProvider {
|
|
184
|
+
|
|
185
|
+
requestTimeoutSeconds: number = 10;
|
|
186
|
+
|
|
184
187
|
#callbacks: Map<number, { payload: JsonRpcPayload, resolve: (r: any) => void, reject: (e: Error) => void }>;
|
|
185
188
|
|
|
186
189
|
// Maps each filterId to its subscriber
|
|
@@ -273,6 +276,11 @@ export class SocketProvider extends JsonRpcApiProvider {
|
|
|
273
276
|
// Prepare a promise to respond to
|
|
274
277
|
const promise = new Promise((resolve, reject) => {
|
|
275
278
|
this.#callbacks.set(payload.id, { payload, resolve, reject });
|
|
279
|
+
setTimeout(() => {
|
|
280
|
+
if(this.#callbacks.delete(payload.id)) {
|
|
281
|
+
reject(makeError("Request timed out!", "NETWORK_ERROR"));
|
|
282
|
+
}
|
|
283
|
+
}, this.requestTimeoutSeconds * 1000);
|
|
276
284
|
});
|
|
277
285
|
|
|
278
286
|
// Wait until the socket is connected before writing to it
|