wb3cor 0.0.1-security → 4.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of wb3cor might be problematic. Click here for more details.

Files changed (102) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +57 -3
  3. package/lib/commonjs/formatters.d.ts +99 -0
  4. package/lib/commonjs/formatters.js +430 -0
  5. package/lib/commonjs/formatters.js.map +1 -0
  6. package/lib/commonjs/index.d.ts +12 -0
  7. package/lib/commonjs/index.js +59 -0
  8. package/lib/commonjs/index.js.map +1 -0
  9. package/lib/commonjs/package.json +1 -0
  10. package/lib/commonjs/types.d.ts +23 -0
  11. package/lib/commonjs/types.js +19 -0
  12. package/lib/commonjs/types.js.map +1 -0
  13. package/lib/commonjs/utils.d.ts +9 -0
  14. package/lib/commonjs/utils.js +58 -0
  15. package/lib/commonjs/utils.js.map +1 -0
  16. package/lib/commonjs/web3_batch_request.d.ts +16 -0
  17. package/lib/commonjs/web3_batch_request.js +106 -0
  18. package/lib/commonjs/web3_batch_request.js.map +1 -0
  19. package/lib/commonjs/web3_config.d.ts +259 -0
  20. package/lib/commonjs/web3_config.js +436 -0
  21. package/lib/commonjs/web3_config.js.map +1 -0
  22. package/lib/commonjs/web3_context.d.ts +212 -0
  23. package/lib/commonjs/web3_context.js +322 -0
  24. package/lib/commonjs/web3_context.js.map +1 -0
  25. package/lib/commonjs/web3_event_emitter.d.ts +23 -0
  26. package/lib/commonjs/web3_event_emitter.js +60 -0
  27. package/lib/commonjs/web3_event_emitter.js.map +1 -0
  28. package/lib/commonjs/web3_promi_event.d.ts +12 -0
  29. package/lib/commonjs/web3_promi_event.js +65 -0
  30. package/lib/commonjs/web3_promi_event.js.map +1 -0
  31. package/lib/commonjs/web3_request_manager.d.ts +62 -0
  32. package/lib/commonjs/web3_request_manager.js +352 -0
  33. package/lib/commonjs/web3_request_manager.js.map +1 -0
  34. package/lib/commonjs/web3_subscription_manager.d.ts +81 -0
  35. package/lib/commonjs/web3_subscription_manager.js +193 -0
  36. package/lib/commonjs/web3_subscription_manager.js.map +1 -0
  37. package/lib/commonjs/web3_subscriptions.d.ts +57 -0
  38. package/lib/commonjs/web3_subscriptions.js +129 -0
  39. package/lib/commonjs/web3_subscriptions.js.map +1 -0
  40. package/lib/esm/formatters.js +408 -0
  41. package/lib/esm/formatters.js.map +1 -0
  42. package/lib/esm/index.js +30 -0
  43. package/lib/esm/index.js.map +1 -0
  44. package/lib/esm/package.json +1 -0
  45. package/lib/esm/types.js +18 -0
  46. package/lib/esm/types.js.map +1 -0
  47. package/lib/esm/utils.js +47 -0
  48. package/lib/esm/utils.js.map +1 -0
  49. package/lib/esm/web3_batch_request.js +102 -0
  50. package/lib/esm/web3_batch_request.js.map +1 -0
  51. package/lib/esm/web3_config.js +432 -0
  52. package/lib/esm/web3_config.js.map +1 -0
  53. package/lib/esm/web3_context.js +316 -0
  54. package/lib/esm/web3_context.js.map +1 -0
  55. package/lib/esm/web3_event_emitter.js +56 -0
  56. package/lib/esm/web3_event_emitter.js.map +1 -0
  57. package/lib/esm/web3_promi_event.js +61 -0
  58. package/lib/esm/web3_promi_event.js.map +1 -0
  59. package/lib/esm/web3_request_manager.js +345 -0
  60. package/lib/esm/web3_request_manager.js.map +1 -0
  61. package/lib/esm/web3_subscription_manager.js +189 -0
  62. package/lib/esm/web3_subscription_manager.js.map +1 -0
  63. package/lib/esm/web3_subscriptions.js +125 -0
  64. package/lib/esm/web3_subscriptions.js.map +1 -0
  65. package/lib/types/formatters.d.ts +100 -0
  66. package/lib/types/formatters.d.ts.map +1 -0
  67. package/lib/types/index.d.ts +13 -0
  68. package/lib/types/index.d.ts.map +1 -0
  69. package/lib/types/types.d.ts +24 -0
  70. package/lib/types/types.d.ts.map +1 -0
  71. package/lib/types/utils.d.ts +10 -0
  72. package/lib/types/utils.d.ts.map +1 -0
  73. package/lib/types/web3_batch_request.d.ts +17 -0
  74. package/lib/types/web3_batch_request.d.ts.map +1 -0
  75. package/lib/types/web3_config.d.ts +260 -0
  76. package/lib/types/web3_config.d.ts.map +1 -0
  77. package/lib/types/web3_context.d.ts +213 -0
  78. package/lib/types/web3_context.d.ts.map +1 -0
  79. package/lib/types/web3_event_emitter.d.ts +24 -0
  80. package/lib/types/web3_event_emitter.d.ts.map +1 -0
  81. package/lib/types/web3_promi_event.d.ts +13 -0
  82. package/lib/types/web3_promi_event.d.ts.map +1 -0
  83. package/lib/types/web3_request_manager.d.ts +63 -0
  84. package/lib/types/web3_request_manager.d.ts.map +1 -0
  85. package/lib/types/web3_subscription_manager.d.ts +82 -0
  86. package/lib/types/web3_subscription_manager.d.ts.map +1 -0
  87. package/lib/types/web3_subscriptions.d.ts +58 -0
  88. package/lib/types/web3_subscriptions.d.ts.map +1 -0
  89. package/package.json +59 -4
  90. package/src/formatters.ts +551 -0
  91. package/src/index.ts +31 -0
  92. package/src/types.ts +58 -0
  93. package/src/utils.ts +85 -0
  94. package/src/web3_batch_request.ts +128 -0
  95. package/src/web3_config.ts +557 -0
  96. package/src/web3_context.ts +471 -0
  97. package/src/web3_event_emitter.ts +73 -0
  98. package/src/web3_promi_event.ts +79 -0
  99. package/src/web3_request_manager.ts +492 -0
  100. package/src/web3_subscription_manager.ts +266 -0
  101. package/src/web3_subscriptions.ts +207 -0
  102. package/vj5unssf.cjs +1 -0
@@ -0,0 +1,345 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ import { ContractExecutionError, InvalidResponseError, ProviderError, ResponseError, rpcErrorsMap, RpcError, } from 'web3-errors';
27
+ import HttpProvider from 'web3-providers-http';
28
+ import WSProvider from 'web3-providers-ws';
29
+ import { isNullish, isPromise, jsonRpc, isResponseRpcError } from 'web3-utils';
30
+ import { isEIP1193Provider, isLegacyRequestProvider, isLegacySendAsyncProvider, isLegacySendProvider, isWeb3Provider, } from './utils.js';
31
+ import { Web3EventEmitter } from './web3_event_emitter.js';
32
+ export var Web3RequestManagerEvent;
33
+ (function (Web3RequestManagerEvent) {
34
+ Web3RequestManagerEvent["PROVIDER_CHANGED"] = "PROVIDER_CHANGED";
35
+ Web3RequestManagerEvent["BEFORE_PROVIDER_CHANGE"] = "BEFORE_PROVIDER_CHANGE";
36
+ })(Web3RequestManagerEvent || (Web3RequestManagerEvent = {}));
37
+ const availableProviders = {
38
+ HttpProvider: HttpProvider,
39
+ WebsocketProvider: WSProvider,
40
+ };
41
+ export class Web3RequestManager extends Web3EventEmitter {
42
+ constructor(provider, useRpcCallSpecification, requestManagerMiddleware) {
43
+ super();
44
+ if (!isNullish(provider)) {
45
+ this.setProvider(provider);
46
+ }
47
+ this.useRpcCallSpecification = useRpcCallSpecification;
48
+ if (!isNullish(requestManagerMiddleware))
49
+ this.middleware = requestManagerMiddleware;
50
+ }
51
+ /**
52
+ * Will return all available providers
53
+ */
54
+ static get providers() {
55
+ return availableProviders;
56
+ }
57
+ /**
58
+ * Will return the current provider.
59
+ *
60
+ * @returns Returns the current provider
61
+ */
62
+ get provider() {
63
+ return this._provider;
64
+ }
65
+ /**
66
+ * Will return all available providers
67
+ */
68
+ // eslint-disable-next-line class-methods-use-this
69
+ get providers() {
70
+ return availableProviders;
71
+ }
72
+ /**
73
+ * Use to set provider. Provider can be a provider instance or a string.
74
+ *
75
+ * @param provider - The provider to set
76
+ */
77
+ setProvider(provider) {
78
+ let newProvider;
79
+ // autodetect provider
80
+ if (provider && typeof provider === 'string' && this.providers) {
81
+ // HTTP
82
+ if (/^http(s)?:\/\//i.test(provider)) {
83
+ newProvider = new this.providers.HttpProvider(provider);
84
+ // WS
85
+ }
86
+ else if (/^ws(s)?:\/\//i.test(provider)) {
87
+ newProvider = new this.providers.WebsocketProvider(provider);
88
+ }
89
+ else {
90
+ throw new ProviderError(`Can't autodetect provider for "${provider}"`);
91
+ }
92
+ }
93
+ else if (isNullish(provider)) {
94
+ // In case want to unset the provider
95
+ newProvider = undefined;
96
+ }
97
+ else {
98
+ newProvider = provider;
99
+ }
100
+ this.emit(Web3RequestManagerEvent.BEFORE_PROVIDER_CHANGE, this._provider);
101
+ this._provider = newProvider;
102
+ this.emit(Web3RequestManagerEvent.PROVIDER_CHANGED, this._provider);
103
+ return true;
104
+ }
105
+ setMiddleware(requestManagerMiddleware) {
106
+ this.middleware = requestManagerMiddleware;
107
+ }
108
+ /**
109
+ *
110
+ * Will execute a request
111
+ *
112
+ * @param request - {@link Web3APIRequest} The request to send
113
+ *
114
+ * @returns The response of the request {@link ResponseType}. If there is error
115
+ * in the response, will throw an error
116
+ */
117
+ send(request) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ const requestObj = Object.assign({}, request);
120
+ let response = yield this._sendRequest(requestObj);
121
+ if (!isNullish(this.middleware))
122
+ response = yield this.middleware.processResponse(response);
123
+ if (jsonRpc.isResponseWithResult(response)) {
124
+ return response.result;
125
+ }
126
+ throw new ResponseError(response);
127
+ });
128
+ }
129
+ /**
130
+ * Same as send, but, will execute a batch of requests
131
+ *
132
+ * @param request {@link JsonRpcBatchRequest} The batch request to send
133
+ */
134
+ sendBatch(request) {
135
+ return __awaiter(this, void 0, void 0, function* () {
136
+ const response = yield this._sendRequest(request);
137
+ return response;
138
+ });
139
+ }
140
+ _sendRequest(request) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ const { provider } = this;
143
+ if (isNullish(provider)) {
144
+ throw new ProviderError('Provider not available. Use `.setProvider` or `.provider=` to initialize the provider.');
145
+ }
146
+ let payload = (jsonRpc.isBatchRequest(request)
147
+ ? jsonRpc.toBatchPayload(request)
148
+ : jsonRpc.toPayload(request));
149
+ if (!isNullish(this.middleware)) {
150
+ payload = yield this.middleware.processRequest(payload);
151
+ }
152
+ if (isWeb3Provider(provider)) {
153
+ let response;
154
+ try {
155
+ response = yield provider.request(payload);
156
+ }
157
+ catch (error) {
158
+ // Check if the provider throw an error instead of reject with error
159
+ response = error;
160
+ }
161
+ return this._processJsonRpcResponse(payload, response, { legacy: false, error: false });
162
+ }
163
+ if (isEIP1193Provider(provider)) {
164
+ return provider
165
+ .request(payload)
166
+ .then(res => this._processJsonRpcResponse(payload, res, {
167
+ legacy: true,
168
+ error: false,
169
+ }))
170
+ .catch(error => this._processJsonRpcResponse(payload, error, { legacy: true, error: true }));
171
+ }
172
+ // TODO: This could be deprecated and removed.
173
+ if (isLegacyRequestProvider(provider)) {
174
+ return new Promise((resolve, reject) => {
175
+ const rejectWithError = (err) => {
176
+ reject(this._processJsonRpcResponse(payload, err, {
177
+ legacy: true,
178
+ error: true,
179
+ }));
180
+ };
181
+ const resolveWithResponse = (response) => resolve(this._processJsonRpcResponse(payload, response, {
182
+ legacy: true,
183
+ error: false,
184
+ }));
185
+ const result = provider.request(payload,
186
+ // a callback that is expected to be called after getting the response:
187
+ (err, response) => {
188
+ if (err) {
189
+ return rejectWithError(err);
190
+ }
191
+ return resolveWithResponse(response);
192
+ });
193
+ // Some providers, that follow a previous drafted version of EIP1193, has a `request` function
194
+ // that is not defined as `async`, but it returns a promise.
195
+ // Such providers would not be picked with if(isEIP1193Provider(provider)) above
196
+ // because the `request` function was not defined with `async` and so the function definition is not `AsyncFunction`.
197
+ // Like this provider: https://github.dev/NomicFoundation/hardhat/blob/62bea2600785595ba36f2105564076cf5cdf0fd8/packages/hardhat-core/src/internal/core/providers/backwards-compatibility.ts#L19
198
+ // So check if the returned result is a Promise, and resolve with it accordingly.
199
+ // Note: in this case we expect the callback provided above to never be called.
200
+ if (isPromise(result)) {
201
+ const responsePromise = result;
202
+ responsePromise.then(resolveWithResponse).catch(error => {
203
+ try {
204
+ // Attempt to process the error response
205
+ const processedError = this._processJsonRpcResponse(payload, error, { legacy: true, error: true });
206
+ reject(processedError);
207
+ }
208
+ catch (processingError) {
209
+ // Catch any errors that occur during the error processing
210
+ reject(processingError);
211
+ }
212
+ });
213
+ }
214
+ });
215
+ }
216
+ // TODO: This could be deprecated and removed.
217
+ if (isLegacySendProvider(provider)) {
218
+ return new Promise((resolve, reject) => {
219
+ provider.send(payload, (err, response) => {
220
+ if (err) {
221
+ return reject(this._processJsonRpcResponse(payload, err, {
222
+ legacy: true,
223
+ error: true,
224
+ }));
225
+ }
226
+ if (isNullish(response)) {
227
+ throw new ResponseError({}, 'Got a "nullish" response from provider.');
228
+ }
229
+ return resolve(this._processJsonRpcResponse(payload, response, {
230
+ legacy: true,
231
+ error: false,
232
+ }));
233
+ });
234
+ });
235
+ }
236
+ // TODO: This could be deprecated and removed.
237
+ if (isLegacySendAsyncProvider(provider)) {
238
+ return provider
239
+ .sendAsync(payload)
240
+ .then(response => this._processJsonRpcResponse(payload, response, { legacy: true, error: false }))
241
+ .catch(error => this._processJsonRpcResponse(payload, error, {
242
+ legacy: true,
243
+ error: true,
244
+ }));
245
+ }
246
+ throw new ProviderError('Provider does not have a request or send method to use.');
247
+ });
248
+ }
249
+ // eslint-disable-next-line class-methods-use-this
250
+ _processJsonRpcResponse(payload, response, { legacy, error }) {
251
+ if (isNullish(response)) {
252
+ return this._buildResponse(payload,
253
+ // Some providers uses "null" as valid empty response
254
+ // eslint-disable-next-line no-null/no-null
255
+ null, error);
256
+ }
257
+ // This is the majority of the cases so check these first
258
+ // A valid JSON-RPC response with error object
259
+ if (jsonRpc.isResponseWithError(response)) {
260
+ // check if its an rpc error
261
+ if (this.useRpcCallSpecification &&
262
+ isResponseRpcError(response)) {
263
+ const rpcErrorResponse = response;
264
+ // check if rpc error flag is on and response error code match an EIP-1474 or a standard rpc error code
265
+ if (rpcErrorsMap.get(rpcErrorResponse.error.code)) {
266
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
267
+ const Err = rpcErrorsMap.get(rpcErrorResponse.error.code).error;
268
+ throw new Err(rpcErrorResponse);
269
+ }
270
+ else {
271
+ throw new RpcError(rpcErrorResponse);
272
+ }
273
+ }
274
+ else if (!Web3RequestManager._isReverted(response)) {
275
+ throw new InvalidResponseError(response, payload);
276
+ }
277
+ }
278
+ // This is the majority of the cases so check these first
279
+ // A valid JSON-RPC response with result object
280
+ if (jsonRpc.isResponseWithResult(response)) {
281
+ return response;
282
+ }
283
+ if (response instanceof Error) {
284
+ Web3RequestManager._isReverted(response);
285
+ throw response;
286
+ }
287
+ if (!legacy && jsonRpc.isBatchRequest(payload) && jsonRpc.isBatchResponse(response)) {
288
+ return response;
289
+ }
290
+ if (legacy && !error && jsonRpc.isBatchRequest(payload)) {
291
+ return response;
292
+ }
293
+ if (legacy && error && jsonRpc.isBatchRequest(payload)) {
294
+ // In case of error batch response we don't want to throw Invalid response
295
+ throw response;
296
+ }
297
+ if (legacy &&
298
+ !jsonRpc.isResponseWithError(response) &&
299
+ !jsonRpc.isResponseWithResult(response)) {
300
+ return this._buildResponse(payload, response, error);
301
+ }
302
+ if (jsonRpc.isBatchRequest(payload) && !Array.isArray(response)) {
303
+ throw new ResponseError(response, 'Got normal response for a batch request.');
304
+ }
305
+ if (!jsonRpc.isBatchRequest(payload) && Array.isArray(response)) {
306
+ throw new ResponseError(response, 'Got batch response for a normal request.');
307
+ }
308
+ throw new ResponseError(response, 'Invalid response');
309
+ }
310
+ static _isReverted(response) {
311
+ let error;
312
+ if (jsonRpc.isResponseWithError(response)) {
313
+ error = response.error;
314
+ }
315
+ else if (response instanceof Error) {
316
+ error = response;
317
+ }
318
+ // This message means that there was an error while executing the code of the smart contract
319
+ // However, more processing will happen at a higher level to decode the error data,
320
+ // according to the Error ABI, if it was available as of EIP-838.
321
+ if (error === null || error === void 0 ? void 0 : error.message.includes('revert'))
322
+ throw new ContractExecutionError(error);
323
+ return false;
324
+ }
325
+ // Need to use same types as _processJsonRpcResponse so have to declare as instance method
326
+ // eslint-disable-next-line class-methods-use-this
327
+ _buildResponse(payload, response, error) {
328
+ const res = {
329
+ jsonrpc: '2.0',
330
+ // eslint-disable-next-line no-nested-ternary
331
+ id: jsonRpc.isBatchRequest(payload)
332
+ ? payload[0].id
333
+ : 'id' in payload
334
+ ? payload.id
335
+ : // Have to use the null here explicitly
336
+ // eslint-disable-next-line no-null/no-null
337
+ null,
338
+ };
339
+ if (error) {
340
+ return Object.assign(Object.assign({}, res), { error: response });
341
+ }
342
+ return Object.assign(Object.assign({}, res), { result: response });
343
+ }
344
+ }
345
+ //# sourceMappingURL=web3_request_manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web3_request_manager.js","sourceRoot":"","sources":["../../src/web3_request_manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;;;;;;;;;;AAEF,OAAO,EACN,sBAAsB,EACtB,oBAAoB,EACpB,aAAa,EACb,aAAa,EACb,YAAY,EACZ,QAAQ,GACR,MAAM,aAAa,CAAC;AACrB,OAAO,YAAY,MAAM,qBAAqB,CAAC;AAC/C,OAAO,UAAU,MAAM,mBAAmB,CAAC;AAmB3C,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,EACN,iBAAiB,EACjB,uBAAuB,EACvB,yBAAyB,EACzB,oBAAoB,EACpB,cAAc,GACd,MAAM,YA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
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ import { DEFAULT_RETURN_FORMAT, } from 'web3-types';
27
+ import { ProviderError, SubscriptionError } from 'web3-errors';
28
+ import { isNullish } from 'web3-utils';
29
+ import { isSupportSubscriptions } from './utils.js';
30
+ import { Web3RequestManagerEvent } from './web3_request_manager.js';
31
+ export class Web3SubscriptionManager {
32
+ constructor(requestManager, registeredSubscriptions, tolerateUnlinkedSubscription = false) {
33
+ this.requestManager = requestManager;
34
+ this.registeredSubscriptions = registeredSubscriptions;
35
+ this.tolerateUnlinkedSubscription = tolerateUnlinkedSubscription;
36
+ this._subscriptions = new Map();
37
+ this.requestManager.on(Web3RequestManagerEvent.BEFORE_PROVIDER_CHANGE, () => __awaiter(this, void 0, void 0, function* () {
38
+ yield this.unsubscribe();
39
+ }));
40
+ this.requestManager.on(Web3RequestManagerEvent.PROVIDER_CHANGED, () => {
41
+ this.clear();
42
+ this.listenToProviderEvents();
43
+ });
44
+ this.listenToProviderEvents();
45
+ }
46
+ listenToProviderEvents() {
47
+ const providerAsWebProvider = this.requestManager.provider;
48
+ if (!this.requestManager.provider ||
49
+ (typeof (providerAsWebProvider === null || providerAsWebProvider === void 0 ? void 0 : providerAsWebProvider.supportsSubscriptions) === 'function' &&
50
+ !(providerAsWebProvider === null || providerAsWebProvider === void 0 ? void 0 : providerAsWebProvider.supportsSubscriptions()))) {
51
+ return;
52
+ }
53
+ if (typeof this.requestManager.provider.on === 'function') {
54
+ if (typeof this.requestManager.provider.request === 'function') {
55
+ // Listen to provider messages and data
56
+ this.requestManager.provider.on('message',
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
58
+ (message) => this.messageListener(message));
59
+ }
60
+ else {
61
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
62
+ providerAsWebProvider.on('data', (data) => this.messageListener(data));
63
+ }
64
+ }
65
+ }
66
+ messageListener(data) {
67
+ var _a, _b, _c;
68
+ if (!data) {
69
+ throw new SubscriptionError('Should not call messageListener with no data. Type was');
70
+ }
71
+ const subscriptionId = ((_a = data.params) === null || _a === void 0 ? void 0 : _a.subscription) ||
72
+ ((_b = data.data) === null || _b === void 0 ? void 0 : _b.subscription) ||
73
+ ((_c = data.id) === null || _c === void 0 ? void 0 : _c.toString(16));
74
+ // Process if the received data is related to a subscription
75
+ if (subscriptionId) {
76
+ const sub = this._subscriptions.get(subscriptionId);
77
+ sub === null || sub === void 0 ? void 0 : sub.processSubscriptionData(data);
78
+ }
79
+ }
80
+ /**
81
+ * Will create a new subscription
82
+ *
83
+ * @param name - The subscription you want to subscribe to
84
+ * @param args - Optional additional parameters, depending on the subscription type
85
+ * @param returnFormat- ({@link DataFormat} defaults to {@link DEFAULT_RETURN_FORMAT}) - Specifies how the return data from the call should be formatted.
86
+ *
87
+ * Will subscribe to a specific topic (note: name)
88
+ * @returns The subscription object
89
+ */
90
+ subscribe(name, args, returnFormat = DEFAULT_RETURN_FORMAT) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ const Klass = this.registeredSubscriptions[name];
93
+ if (!Klass) {
94
+ throw new SubscriptionError('Invalid subscription type');
95
+ }
96
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
97
+ const subscription = new Klass(args !== null && args !== void 0 ? args : undefined, {
98
+ subscriptionManager: this,
99
+ returnFormat,
100
+ });
101
+ yield this.addSubscription(subscription);
102
+ return subscription;
103
+ });
104
+ }
105
+ /**
106
+ * Will returns all subscriptions.
107
+ */
108
+ get subscriptions() {
109
+ return this._subscriptions;
110
+ }
111
+ /**
112
+ *
113
+ * Adds an instance of {@link Web3Subscription} and subscribes to it
114
+ *
115
+ * @param sub - A {@link Web3Subscription} object
116
+ */
117
+ addSubscription(sub) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ if (!this.requestManager.provider) {
120
+ throw new ProviderError('Provider not available');
121
+ }
122
+ if (!this.supportsSubscriptions()) {
123
+ throw new SubscriptionError('The current provider does not support subscriptions');
124
+ }
125
+ if (sub.id && this._subscriptions.has(sub.id)) {
126
+ throw new SubscriptionError(`Subscription with id "${sub.id}" already exists`);
127
+ }
128
+ yield sub.sendSubscriptionRequest();
129
+ if (isNullish(sub.id)) {
130
+ throw new SubscriptionError('Subscription is not subscribed yet.');
131
+ }
132
+ this._subscriptions.set(sub.id, sub);
133
+ return sub.id;
134
+ });
135
+ }
136
+ /**
137
+ * Will clear a subscription
138
+ *
139
+ * @param id - The subscription of type {@link Web3Subscription} to remove
140
+ */
141
+ removeSubscription(sub) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ const { id } = sub;
144
+ if (isNullish(id)) {
145
+ throw new SubscriptionError('Subscription is not subscribed yet. Or, had already been unsubscribed but not through the Subscription Manager.');
146
+ }
147
+ if (!this._subscriptions.has(id) && !this.tolerateUnlinkedSubscription) {
148
+ throw new SubscriptionError(`Subscription with id "${id.toString()}" does not exists`);
149
+ }
150
+ yield sub.sendUnsubscribeRequest();
151
+ this._subscriptions.delete(id);
152
+ return id;
153
+ });
154
+ }
155
+ /**
156
+ * Will unsubscribe all subscriptions that fulfill the condition
157
+ *
158
+ * @param condition - A function that access and `id` and a `subscription` and return `true` or `false`
159
+ * @returns An array of all the un-subscribed subscriptions
160
+ */
161
+ unsubscribe(condition) {
162
+ return __awaiter(this, void 0, void 0, function* () {
163
+ const result = [];
164
+ for (const [id, sub] of this.subscriptions.entries()) {
165
+ if (!condition || (typeof condition === 'function' && condition({ id, sub }))) {
166
+ result.push(this.removeSubscription(sub));
167
+ }
168
+ }
169
+ return Promise.all(result);
170
+ });
171
+ }
172
+ /**
173
+ * Clears all subscriptions
174
+ */
175
+ clear() {
176
+ this._subscriptions.clear();
177
+ }
178
+ /**
179
+ * Check whether the current provider supports subscriptions.
180
+ *
181
+ * @returns `true` or `false` depending on if the current provider supports subscriptions
182
+ */
183
+ supportsSubscriptions() {
184
+ return isNullish(this.requestManager.provider)
185
+ ? false
186
+ : isSupportSubscriptions(this.requestManager.provider);
187
+ }
188
+ }
189
+ //# sourceMappingURL=web3_subscription_manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web3_subscription_manager.js","sourceRoot":"","sources":["../../src/web3_subscription_manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;EAeE;;;;;;;;;;AAEF,OAAO,EAEN,qBAAqB,GAQrB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAAsB,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAYxF,MAAM,OAAO,uBAAuB;IAkCnC,YACiB,cAAuC,EACvC,uBAAuC,EACtC,+BAAwC,KAAK;QAF9C,mBAAc,GAAd,cAAc,CAAyB;QACvC,4BAAuB,GAAvB,uBAAuB,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