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,492 @@
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
+
18
+ import {
19
+ ContractExecutionError,
20
+ InvalidResponseError,
21
+ ProviderError,
22
+ ResponseError,
23
+ rpcErrorsMap,
24
+ RpcError,
25
+ } from 'web3-errors';
26
+ import HttpProvider from 'web3-providers-http';
27
+ import WSProvider from 'web3-providers-ws';
28
+ import {
29
+ EthExecutionAPI,
30
+ JsonRpcBatchRequest,
31
+ JsonRpcBatchResponse,
32
+ JsonRpcPayload,
33
+ JsonRpcResponse,
34
+ JsonRpcError,
35
+ JsonRpcResponseWithResult,
36
+ JsonRpcResponseWithError,
37
+ SupportedProviders,
38
+ Web3APIMethod,
39
+ Web3APIPayload,
40
+ Web3APIRequest,
41
+ Web3APIReturnType,
42
+ Web3APISpec,
43
+ Web3BaseProvider,
44
+ Web3BaseProviderConstructor,
45
+ } from 'web3-types';
46
+ import { isNullish, isPromise, jsonRpc, isResponseRpcError } from 'web3-utils';
47
+ import {
48
+ isEIP1193Provider,
49
+ isLegacyRequestProvider,
50
+ isLegacySendAsyncProvider,
51
+ isLegacySendProvider,
52
+ isWeb3Provider,
53
+ } from './utils.js';
54
+ import { Web3EventEmitter } from './web3_event_emitter.js';
55
+ import { RequestManagerMiddleware } from './types.js';
56
+
57
+ export enum Web3RequestManagerEvent {
58
+ PROVIDER_CHANGED = 'PROVIDER_CHANGED',
59
+ BEFORE_PROVIDER_CHANGE = 'BEFORE_PROVIDER_CHANGE',
60
+ }
61
+
62
+ const availableProviders: {
63
+ HttpProvider: Web3BaseProviderConstructor;
64
+ WebsocketProvider: Web3BaseProviderConstructor;
65
+ } = {
66
+ HttpProvider: HttpProvider as Web3BaseProviderConstructor,
67
+ WebsocketProvider: WSProvider as Web3BaseProviderConstructor,
68
+ };
69
+
70
+ export class Web3RequestManager<
71
+ API extends Web3APISpec = EthExecutionAPI,
72
+ > extends Web3EventEmitter<{
73
+ [key in Web3RequestManagerEvent]: SupportedProviders<API> | undefined;
74
+ }> {
75
+ private _provider?: SupportedProviders<API>;
76
+ private readonly useRpcCallSpecification?: boolean;
77
+ public middleware?: RequestManagerMiddleware<API>;
78
+
79
+ public constructor(
80
+ provider?: SupportedProviders<API> | string,
81
+ useRpcCallSpecification?: boolean,
82
+ requestManagerMiddleware?: RequestManagerMiddleware<API>,
83
+ ) {
84
+ super();
85
+
86
+ if (!isNullish(provider)) {
87
+ this.setProvider(provider);
88
+ }
89
+ this.useRpcCallSpecification = useRpcCallSpecification;
90
+
91
+ if (!isNullish(requestManagerMiddleware)) this.middleware = requestManagerMiddleware;
92
+ }
93
+
94
+ /**
95
+ * Will return all available providers
96
+ */
97
+ public static get providers() {
98
+ return availableProviders;
99
+ }
100
+
101
+ /**
102
+ * Will return the current provider.
103
+ *
104
+ * @returns Returns the current provider
105
+ */
106
+ public get provider() {
107
+ return this._provider;
108
+ }
109
+
110
+ /**
111
+ * Will return all available providers
112
+ */
113
+ // eslint-disable-next-line class-methods-use-this
114
+ public get providers() {
115
+ return availableProviders;
116
+ }
117
+
118
+ /**
119
+ * Use to set provider. Provider can be a provider instance or a string.
120
+ *
121
+ * @param provider - The provider to set
122
+ */
123
+ public setProvider(provider?: SupportedProviders<API> | string): boolean {
124
+ let newProvider: SupportedProviders<API> | undefined;
125
+
126
+ // autodetect provider
127
+ if (provider && typeof provider === 'string' && this.providers) {
128
+ // HTTP
129
+ if (/^http(s)?:\/\//i.test(provider)) {
130
+ newProvider = new this.providers.HttpProvider<API>(provider);
131
+
132
+ // WS
133
+ } else if (/^ws(s)?:\/\//i.test(provider)) {
134
+ newProvider = new this.providers.WebsocketProvider<API>(provider);
135
+ } else {
136
+ throw new ProviderError(`Can't autodetect provider for "${provider}"`);
137
+ }
138
+ } else if (isNullish(provider)) {
139
+ // In case want to unset the provider
140
+ newProvider = undefined;
141
+ } else {
142
+ newProvider = provider as SupportedProviders<API>;
143
+ }
144
+
145
+ this.emit(Web3RequestManagerEvent.BEFORE_PROVIDER_CHANGE, this._provider);
146
+ this._provider = newProvider;
147
+ this.emit(Web3RequestManagerEvent.PROVIDER_CHANGED, this._provider);
148
+ return true;
149
+ }
150
+
151
+ public setMiddleware(requestManagerMiddleware: RequestManagerMiddleware<API>) {
152
+ this.middleware = requestManagerMiddleware;
153
+ }
154
+
155
+ /**
156
+ *
157
+ * Will execute a request
158
+ *
159
+ * @param request - {@link Web3APIRequest} The request to send
160
+ *
161
+ * @returns The response of the request {@link ResponseType}. If there is error
162
+ * in the response, will throw an error
163
+ */
164
+ public async send<
165
+ Method extends Web3APIMethod<API>,
166
+ ResponseType = Web3APIReturnType<API, Method>,
167
+ >(request: Web3APIRequest<API, Method>): Promise<ResponseType> {
168
+ const requestObj = { ...request };
169
+
170
+ let response = await this._sendRequest<Method, ResponseType>(requestObj);
171
+
172
+ if (!isNullish(this.middleware)) response = await this.middleware.processResponse(response);
173
+
174
+ if (jsonRpc.isResponseWithResult(response)) {
175
+ return response.result;
176
+ }
177
+
178
+ throw new ResponseError(response);
179
+ }
180
+
181
+ /**
182
+ * Same as send, but, will execute a batch of requests
183
+ *
184
+ * @param request {@link JsonRpcBatchRequest} The batch request to send
185
+ */
186
+ public async sendBatch(request: JsonRpcBatchRequest): Promise<JsonRpcBatchResponse<unknown>> {
187
+ const response = await this._sendRequest<never, never>(request);
188
+
189
+ return response as JsonRpcBatchResponse<unknown>;
190
+ }
191
+
192
+ private async _sendRequest<
193
+ Method extends Web3APIMethod<API>,
194
+ ResponseType = Web3APIReturnType<API, Method>,
195
+ >(
196
+ request: Web3APIRequest<API, Method> | JsonRpcBatchRequest,
197
+ ): Promise<JsonRpcResponse<ResponseType>> {
198
+ const { provider } = this;
199
+
200
+ if (isNullish(provider)) {
201
+ throw new ProviderError(
202
+ 'Provider not available. Use `.setProvider` or `.provider=` to initialize the provider.',
203
+ );
204
+ }
205
+
206
+ let payload = (
207
+ jsonRpc.isBatchRequest(request)
208
+ ? jsonRpc.toBatchPayload(request)
209
+ : jsonRpc.toPayload(request)
210
+ ) as JsonRpcPayload;
211
+
212
+ if (!isNullish(this.middleware)) {
213
+ payload = await this.middleware.processRequest(payload);
214
+ }
215
+ if (isWeb3Provider(provider)) {
216
+ let response;
217
+
218
+ try {
219
+ response = await provider.request<Method, ResponseType>(
220
+ payload as Web3APIPayload<API, Method>,
221
+ );
222
+ } catch (error) {
223
+ // Check if the provider throw an error instead of reject with error
224
+ response = error as JsonRpcResponse<ResponseType>;
225
+ }
226
+ return this._processJsonRpcResponse(payload, response, { legacy: false, error: false });
227
+ }
228
+
229
+ if (isEIP1193Provider(provider)) {
230
+ return (provider as Web3BaseProvider<API>)
231
+ .request<Method, ResponseType>(payload as Web3APIPayload<API, Method>)
232
+ .then(
233
+ res =>
234
+ this._processJsonRpcResponse(payload, res, {
235
+ legacy: true,
236
+ error: false,
237
+ }) as JsonRpcResponseWithResult<ResponseType>,
238
+ )
239
+ .catch(error =>
240
+ this._processJsonRpcResponse(
241
+ payload,
242
+ error as JsonRpcResponse<ResponseType, unknown>,
243
+ { legacy: true, error: true },
244
+ ),
245
+ );
246
+ }
247
+
248
+ // TODO: This could be deprecated and removed.
249
+ if (isLegacyRequestProvider(provider)) {
250
+ return new Promise<JsonRpcResponse<ResponseType>>((resolve, reject) => {
251
+ const rejectWithError = (err: unknown) => {
252
+ reject(
253
+ this._processJsonRpcResponse(
254
+ payload,
255
+ err as JsonRpcResponse<ResponseType>,
256
+ {
257
+ legacy: true,
258
+ error: true,
259
+ },
260
+ ),
261
+ );
262
+ };
263
+
264
+ const resolveWithResponse = (response: JsonRpcResponse<ResponseType>) =>
265
+ resolve(
266
+ this._processJsonRpcResponse(payload, response, {
267
+ legacy: true,
268
+ error: false,
269
+ }),
270
+ );
271
+ const result = provider.request<ResponseType>(
272
+ payload,
273
+ // a callback that is expected to be called after getting the response:
274
+ (err, response) => {
275
+ if (err) {
276
+ return rejectWithError(err);
277
+ }
278
+
279
+ return resolveWithResponse(response);
280
+ },
281
+ );
282
+ // Some providers, that follow a previous drafted version of EIP1193, has a `request` function
283
+ // that is not defined as `async`, but it returns a promise.
284
+ // Such providers would not be picked with if(isEIP1193Provider(provider)) above
285
+ // because the `request` function was not defined with `async` and so the function definition is not `AsyncFunction`.
286
+ // Like this provider: https://github.dev/NomicFoundation/hardhat/blob/62bea2600785595ba36f2105564076cf5cdf0fd8/packages/hardhat-core/src/internal/core/providers/backwards-compatibility.ts#L19
287
+ // So check if the returned result is a Promise, and resolve with it accordingly.
288
+ // Note: in this case we expect the callback provided above to never be called.
289
+ if (isPromise(result)) {
290
+ const responsePromise = result as unknown as Promise<
291
+ JsonRpcResponse<ResponseType>
292
+ >;
293
+ responsePromise.then(resolveWithResponse).catch(error => {
294
+ try {
295
+ // Attempt to process the error response
296
+ const processedError = this._processJsonRpcResponse(
297
+ payload,
298
+ error as JsonRpcResponse<ResponseType, unknown>,
299
+ { legacy: true, error: true },
300
+ );
301
+ reject(processedError);
302
+ } catch (processingError) {
303
+ // Catch any errors that occur during the error processing
304
+ reject(processingError);
305
+ }
306
+ });
307
+ }
308
+ });
309
+ }
310
+
311
+ // TODO: This could be deprecated and removed.
312
+ if (isLegacySendProvider(provider)) {
313
+ return new Promise<JsonRpcResponse<ResponseType>>((resolve, reject): void => {
314
+ provider.send<ResponseType>(payload, (err, response) => {
315
+ if (err) {
316
+ return reject(
317
+ this._processJsonRpcResponse(
318
+ payload,
319
+ err as unknown as JsonRpcResponse<ResponseType>,
320
+ {
321
+ legacy: true,
322
+ error: true,
323
+ },
324
+ ),
325
+ );
326
+ }
327
+
328
+ if (isNullish(response)) {
329
+ throw new ResponseError(
330
+ {} as never,
331
+ 'Got a "nullish" response from provider.',
332
+ );
333
+ }
334
+
335
+ return resolve(
336
+ this._processJsonRpcResponse(payload, response, {
337
+ legacy: true,
338
+ error: false,
339
+ }),
340
+ );
341
+ });
342
+ });
343
+ }
344
+
345
+ // TODO: This could be deprecated and removed.
346
+ if (isLegacySendAsyncProvider(provider)) {
347
+ return provider
348
+ .sendAsync<ResponseType>(payload)
349
+ .then(response =>
350
+ this._processJsonRpcResponse(payload, response, { legacy: true, error: false }),
351
+ )
352
+ .catch(error =>
353
+ this._processJsonRpcResponse(payload, error as JsonRpcResponse<ResponseType>, {
354
+ legacy: true,
355
+ error: true,
356
+ }),
357
+ );
358
+ }
359
+
360
+ throw new ProviderError('Provider does not have a request or send method to use.');
361
+ }
362
+
363
+ // eslint-disable-next-line class-methods-use-this
364
+ private _processJsonRpcResponse<ResultType, ErrorType, RequestType>(
365
+ payload: JsonRpcPayload<RequestType>,
366
+ response: JsonRpcResponse<ResultType, ErrorType>,
367
+ { legacy, error }: { legacy: boolean; error: boolean },
368
+ ): JsonRpcResponse<ResultType> | never {
369
+ if (isNullish(response)) {
370
+ return this._buildResponse(
371
+ payload,
372
+ // Some providers uses "null" as valid empty response
373
+ // eslint-disable-next-line no-null/no-null
374
+ null as unknown as JsonRpcResponse<ResultType, ErrorType>,
375
+ error,
376
+ );
377
+ }
378
+
379
+ // This is the majority of the cases so check these first
380
+ // A valid JSON-RPC response with error object
381
+ if (jsonRpc.isResponseWithError<ErrorType>(response)) {
382
+ // check if its an rpc error
383
+ if (
384
+ this.useRpcCallSpecification &&
385
+ isResponseRpcError(response as JsonRpcResponseWithError)
386
+ ) {
387
+ const rpcErrorResponse = response as JsonRpcResponseWithError;
388
+ // check if rpc error flag is on and response error code match an EIP-1474 or a standard rpc error code
389
+ if (rpcErrorsMap.get(rpcErrorResponse.error.code)) {
390
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
391
+ const Err = rpcErrorsMap.get(rpcErrorResponse.error.code)!.error;
392
+ throw new Err(rpcErrorResponse);
393
+ } else {
394
+ throw new RpcError(rpcErrorResponse);
395
+ }
396
+ } else if (!Web3RequestManager._isReverted(response)) {
397
+ throw new InvalidResponseError<ErrorType, RequestType>(response, payload);
398
+ }
399
+ }
400
+
401
+ // This is the majority of the cases so check these first
402
+ // A valid JSON-RPC response with result object
403
+ if (jsonRpc.isResponseWithResult<ResultType>(response)) {
404
+ return response;
405
+ }
406
+
407
+ if ((response as unknown) instanceof Error) {
408
+ Web3RequestManager._isReverted(response);
409
+ throw response;
410
+ }
411
+
412
+ if (!legacy && jsonRpc.isBatchRequest(payload) && jsonRpc.isBatchResponse(response)) {
413
+ return response as JsonRpcBatchResponse<ResultType>;
414
+ }
415
+
416
+ if (legacy && !error && jsonRpc.isBatchRequest(payload)) {
417
+ return response as JsonRpcBatchResponse<ResultType>;
418
+ }
419
+
420
+ if (legacy && error && jsonRpc.isBatchRequest(payload)) {
421
+ // In case of error batch response we don't want to throw Invalid response
422
+ throw response;
423
+ }
424
+
425
+ if (
426
+ legacy &&
427
+ !jsonRpc.isResponseWithError(response) &&
428
+ !jsonRpc.isResponseWithResult(response)
429
+ ) {
430
+ return this._buildResponse(payload, response, error);
431
+ }
432
+ if (jsonRpc.isBatchRequest(payload) && !Array.isArray(response)) {
433
+ throw new ResponseError(response, 'Got normal response for a batch request.');
434
+ }
435
+
436
+ if (!jsonRpc.isBatchRequest(payload) && Array.isArray(response)) {
437
+ throw new ResponseError(response, 'Got batch response for a normal request.');
438
+ }
439
+
440
+ throw new ResponseError(response, 'Invalid response');
441
+ }
442
+
443
+ private static _isReverted<ResultType, ErrorType>(
444
+ response: JsonRpcResponse<ResultType, ErrorType>,
445
+ ): boolean {
446
+ let error: JsonRpcError | undefined;
447
+
448
+ if (jsonRpc.isResponseWithError<ErrorType>(response)) {
449
+ error = (response as JsonRpcResponseWithError).error;
450
+ } else if ((response as unknown) instanceof Error) {
451
+ error = response as unknown as JsonRpcError;
452
+ }
453
+
454
+ // This message means that there was an error while executing the code of the smart contract
455
+ // However, more processing will happen at a higher level to decode the error data,
456
+ // according to the Error ABI, if it was available as of EIP-838.
457
+ if (error?.message.includes('revert')) throw new ContractExecutionError(error);
458
+
459
+ return false;
460
+ }
461
+ // Need to use same types as _processJsonRpcResponse so have to declare as instance method
462
+ // eslint-disable-next-line class-methods-use-this
463
+ private _buildResponse<ResultType, ErrorType, RequestType>(
464
+ payload: JsonRpcPayload<RequestType>,
465
+ response: JsonRpcResponse<ResultType, ErrorType>,
466
+ error: boolean,
467
+ ): JsonRpcResponse<ResultType> {
468
+ const res = {
469
+ jsonrpc: '2.0',
470
+ // eslint-disable-next-line no-nested-ternary
471
+ id: jsonRpc.isBatchRequest(payload)
472
+ ? payload[0].id
473
+ : 'id' in payload
474
+ ? payload.id
475
+ : // Have to use the null here explicitly
476
+ // eslint-disable-next-line no-null/no-null
477
+ null,
478
+ };
479
+
480
+ if (error) {
481
+ return {
482
+ ...res,
483
+ error: response as unknown,
484
+ } as JsonRpcResponse<ResultType>;
485
+ }
486
+
487
+ return {
488
+ ...res,
489
+ result: response as unknown,
490
+ } as JsonRpcResponse<ResultType>;
491
+ }
492
+ }