@talismn/chain-connectors 0.0.0

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.
Files changed (29) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +1 -0
  3. package/dist/declarations/src/dot/ChainConnectorDot.d.ts +79 -0
  4. package/dist/declarations/src/dot/ChainConnectorDotStub.d.ts +11 -0
  5. package/dist/declarations/src/dot/IChainConnectorDot.d.ts +10 -0
  6. package/dist/declarations/src/dot/Websocket.d.ts +111 -0
  7. package/dist/declarations/src/dot/helpers.d.ts +15 -0
  8. package/dist/declarations/src/dot/index.d.ts +3 -0
  9. package/dist/declarations/src/eth/ChainConnectorEth.d.ts +10 -0
  10. package/dist/declarations/src/eth/ChainConnectorEthStub.d.ts +10 -0
  11. package/dist/declarations/src/eth/IChainConnectorEth.d.ts +7 -0
  12. package/dist/declarations/src/eth/getChainFromEvmNetwork.d.ts +4 -0
  13. package/dist/declarations/src/eth/getEvmNetworkPublicClient.d.ts +4 -0
  14. package/dist/declarations/src/eth/getEvmNetworkWalletClient.d.ts +7 -0
  15. package/dist/declarations/src/eth/getTransportForEvmNetwork.d.ts +8 -0
  16. package/dist/declarations/src/eth/index.d.ts +3 -0
  17. package/dist/declarations/src/index.d.ts +3 -0
  18. package/dist/declarations/src/log.d.ts +2 -0
  19. package/dist/declarations/src/sol/ChainConnectorSol.d.ts +8 -0
  20. package/dist/declarations/src/sol/ChainConnectorSolStub.d.ts +8 -0
  21. package/dist/declarations/src/sol/IChainConnectorSol.d.ts +5 -0
  22. package/dist/declarations/src/sol/getSolConnection.d.ts +3 -0
  23. package/dist/declarations/src/sol/index.d.ts +3 -0
  24. package/dist/talismn-chain-connectors.cjs.d.ts +1 -0
  25. package/dist/talismn-chain-connectors.cjs.dev.js +1236 -0
  26. package/dist/talismn-chain-connectors.cjs.js +7 -0
  27. package/dist/talismn-chain-connectors.cjs.prod.js +1236 -0
  28. package/dist/talismn-chain-connectors.esm.js +1202 -0
  29. package/package.json +63 -0
@@ -0,0 +1,1202 @@
1
+ import { throwAfter, Deferred, sleep, isTruthy } from '@talismn/util';
2
+ import anylogger from 'anylogger';
3
+ import { RpcCoder } from '@polkadot/rpc-provider/coder';
4
+ import { getWSErrorString } from '@polkadot/rpc-provider/ws/errors';
5
+ import { isChildClass, isUndefined, isNull, objectSpread } from '@polkadot/util';
6
+ import { xglobal } from '@polkadot/x-global';
7
+ import { WebSocket } from '@polkadot/x-ws';
8
+ import EventEmitter from 'eventemitter3';
9
+ import { WsProvider } from '@polkadot/rpc-provider';
10
+ import { fallback, http, createPublicClient, createWalletClient } from 'viem';
11
+ import { fromPairs, toPairs, camelCase } from 'lodash-es';
12
+ import * as chains from 'viem/chains';
13
+ import { Connection } from '@solana/web3.js';
14
+
15
+ var packageJson = {
16
+ name: "@talismn/chain-connectors"};
17
+
18
+ var log = anylogger(packageJson.name);
19
+
20
+ const twoSecondsMs = 2 * 1000;
21
+ const twoMinutesMs = 2 * 60 * 1000;
22
+ class ExponentialBackoff {
23
+ #minInterval;
24
+ #maxInterval;
25
+ #nextInterval = 0;
26
+ #active = true;
27
+ constructor(maxIntervalMs = twoMinutesMs, minIntervalMs = twoSecondsMs) {
28
+ this.#minInterval = minIntervalMs;
29
+ this.#maxInterval = maxIntervalMs;
30
+ this.reset();
31
+ }
32
+ enable() {
33
+ this.#active = true;
34
+ }
35
+ disable() {
36
+ this.#active = false;
37
+ }
38
+ increase() {
39
+ if (this.#nextInterval === 0) this.#nextInterval = 1;
40
+ this.#nextInterval = this.#capMax(this.#capMin(this.#nextInterval * 2));
41
+ }
42
+ decrease() {
43
+ this.#nextInterval = this.#capMax(this.#capMin(this.#nextInterval / 2));
44
+ }
45
+ reset() {
46
+ this.#nextInterval = this.#minInterval;
47
+ }
48
+ resetTo(nextInterval) {
49
+ this.#nextInterval = this.#capMax(this.#capMin(nextInterval));
50
+ }
51
+ resetToMax() {
52
+ this.#nextInterval = this.#maxInterval;
53
+ }
54
+ get isActive() {
55
+ return this.#active;
56
+ }
57
+ get next() {
58
+ return this.#nextInterval;
59
+ }
60
+ get isMin() {
61
+ return this.#nextInterval === this.#minInterval;
62
+ }
63
+ get isMax() {
64
+ return this.#nextInterval === this.#maxInterval;
65
+ }
66
+ #capMin(value) {
67
+ return Math.max(this.#minInterval, value);
68
+ }
69
+ #capMax(value) {
70
+ return Math.min(this.#maxInterval, value);
71
+ }
72
+ }
73
+
74
+ // to account for new requirement for generic arg in this type https://github.com/polkadot-js/api/commit/f4c2b150d3d69d43c56699613666b96dd0a763f4#diff-f87c17bc7fae027ec6d43bac5fc089614d9fa097f466aa2be333b44cee81f0fd
75
+ // TODO incrementally replace 'unknown' with proper types where possible
76
+
77
+ const ALIASES = {
78
+ chain_finalisedHead: "chain_finalizedHead",
79
+ chain_subscribeFinalisedHeads: "chain_subscribeFinalizedHeads",
80
+ chain_unsubscribeFinalisedHeads: "chain_unsubscribeFinalizedHeads"
81
+ };
82
+ const DEFAULT_TIMEOUT_MS = 60 * 1000;
83
+ const TIMEOUT_INTERVAL = 5_000;
84
+ function eraseRecord(record, cb) {
85
+ Object.keys(record).forEach(key => {
86
+ if (cb) {
87
+ cb(record[key]);
88
+ }
89
+ delete record[key];
90
+ });
91
+ }
92
+
93
+ /**
94
+ * # @talismn/chain-connector/Websocket
95
+ *
96
+ * @name Websocket
97
+ *
98
+ * @description The WebSocket Provider allows sending requests using WebSocket to a WebSocket RPC server TCP port. Unlike the [[HttpProvider]], it does support subscriptions and allows listening to events such as new blocks or balance changes.
99
+ *
100
+ * @example
101
+ * <BR>
102
+ *
103
+ * ```javascript
104
+ * import { Websocket } from '@talismn/chain-connector';
105
+ *
106
+ * const provider = new Websocket('ws://127.0.0.1:9944');
107
+ * ```
108
+ *
109
+ * @see [[HttpProvider]]
110
+ */
111
+ class Websocket {
112
+ #coder;
113
+ #endpoints;
114
+ #headers;
115
+ #eventemitter;
116
+ #handlers = {};
117
+ #isReadyPromise;
118
+ #waitingForId = {};
119
+ #autoConnectBackoff;
120
+ #endpointIndex;
121
+ #endpointsTriedSinceLastConnection = 0;
122
+ #isConnected = false;
123
+ #subscriptions = {};
124
+ #timeoutId = null;
125
+ #websocket;
126
+ #timeout;
127
+
128
+ /**
129
+ * @param {string | string[]} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`, may provide an array of endpoint strings.
130
+ * @param {Record<string, string>} headers The headers provided to the underlying WebSocket
131
+ * @param {number} [timeout] Custom timeout value used per request . Defaults to `DEFAULT_TIMEOUT_MS`
132
+ */
133
+ constructor(endpoint, headers = {}, timeout, nextBackoffInterval) {
134
+ const endpoints = Array.isArray(endpoint) ? endpoint : [endpoint];
135
+ if (endpoints.length === 0) {
136
+ throw new Error("Websocket requires at least one Endpoint");
137
+ }
138
+ endpoints.forEach(endpoint => {
139
+ if (!/^(wss|ws):\/\//.test(endpoint)) {
140
+ throw new Error(`Endpoint should start with 'ws://', received '${endpoint}'`);
141
+ }
142
+ });
143
+ this.#eventemitter = new EventEmitter();
144
+ this.#autoConnectBackoff = new ExponentialBackoff();
145
+ if (nextBackoffInterval) this.#autoConnectBackoff.resetTo(nextBackoffInterval);
146
+ this.#coder = new RpcCoder();
147
+ this.#endpointIndex = -1;
148
+ this.#endpoints = endpoints;
149
+ this.#headers = headers;
150
+ this.#websocket = null;
151
+ this.#timeout = timeout || DEFAULT_TIMEOUT_MS;
152
+ if (this.#autoConnectBackoff.isActive) {
153
+ this.connectWithRetry().catch(() => {
154
+ // does not throw
155
+ });
156
+ }
157
+ this.#isReadyPromise = new Promise(resolve => {
158
+ this.#eventemitter.once("connected", () => {
159
+ resolve(this);
160
+ });
161
+ });
162
+ }
163
+
164
+ /**
165
+ * @summary `true` when this provider supports subscriptions
166
+ */
167
+ get hasSubscriptions() {
168
+ return true;
169
+ }
170
+
171
+ /**
172
+ * @summary `true` when this provider supports clone()
173
+ */
174
+ get isClonable() {
175
+ return true;
176
+ }
177
+
178
+ /**
179
+ * @summary Whether the node is connected or not.
180
+ * @return {boolean} true if connected
181
+ */
182
+ get isConnected() {
183
+ return this.#isConnected;
184
+ }
185
+
186
+ /**
187
+ * @description Promise that resolves the first time we are connected and loaded
188
+ */
189
+ get isReady() {
190
+ return this.#isReadyPromise;
191
+ }
192
+ get endpoint() {
193
+ return this.#endpoints[this.#endpointIndex];
194
+ }
195
+
196
+ /**
197
+ * @description Returns a clone of the object
198
+ */
199
+ clone() {
200
+ return new Websocket(this.#endpoints);
201
+ }
202
+ selectEndpointIndex(endpoints) {
203
+ this.#endpointsTriedSinceLastConnection += 1;
204
+ return (this.#endpointIndex + 1) % endpoints.length;
205
+ }
206
+
207
+ /**
208
+ * @summary Manually connect
209
+ * @description The [[Websocket]] connects automatically by default, however if you decided otherwise, you may
210
+ * connect manually using this method.
211
+ */
212
+ // eslint-disable-next-line @typescript-eslint/require-await
213
+ async connect() {
214
+ if (this.#websocket) {
215
+ throw new Error("WebSocket is already connected");
216
+ }
217
+ try {
218
+ this.#endpointIndex = this.selectEndpointIndex(this.#endpoints);
219
+
220
+ // the as typeof WebSocket here is Deno-specific - not available on the globalThis
221
+ this.#websocket = typeof xglobal.WebSocket !== "undefined" && isChildClass(xglobal.WebSocket, WebSocket) ? new WebSocket(this.endpoint) :
222
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
223
+ // @ts-ignore - WS may be an instance of ws, which supports options
224
+ new WebSocket(this.endpoint, undefined, {
225
+ headers: this.#headers
226
+ });
227
+ if (this.#websocket) {
228
+ this.#websocket.onclose = this.#onSocketClose;
229
+ this.#websocket.onerror = this.#onSocketError;
230
+ this.#websocket.onmessage = this.#onSocketMessage;
231
+ this.#websocket.onopen = this.#onSocketOpen;
232
+ }
233
+
234
+ // timeout any handlers that have not had a response
235
+ this.#timeoutId = setInterval(() => this.#timeoutHandlers(), TIMEOUT_INTERVAL);
236
+ } catch (error) {
237
+ log.error(error);
238
+ this.#emit("error", error);
239
+ throw error;
240
+ }
241
+ }
242
+
243
+ /**
244
+ * @description Connect, never throwing an error, but rather forcing a retry
245
+ */
246
+ async connectWithRetry() {
247
+ if (!this.#autoConnectBackoff.isActive) return;
248
+ try {
249
+ await this.connect();
250
+ } catch (error) {
251
+ this.scheduleNextRetry();
252
+ }
253
+ }
254
+ scheduleNextRetry() {
255
+ if (!this.#autoConnectBackoff.isActive) return;
256
+ const haveTriedAllEndpoints = this.#endpointsTriedSinceLastConnection > 0 && this.#endpointsTriedSinceLastConnection % this.#endpoints.length === 0;
257
+ setTimeout(() => {
258
+ this.connectWithRetry().catch(() => {
259
+ // does not throw
260
+ });
261
+ }, haveTriedAllEndpoints ? this.#autoConnectBackoff.next : 0);
262
+
263
+ // Increase backoff when we've tried all endpoints
264
+ if (haveTriedAllEndpoints) this.#autoConnectBackoff.increase();
265
+
266
+ // Fire a stale-rpcs event when we've tried all endpoints in the list
267
+ // but haven't successfully connected to any of them
268
+ if (haveTriedAllEndpoints) this.#emit("stale-rpcs", {
269
+ nextBackoffInterval: this.#autoConnectBackoff.next
270
+ });
271
+ }
272
+
273
+ /**
274
+ * @description Manually disconnect from the connection, clearing auto-connect logic
275
+ */
276
+ // eslint-disable-next-line @typescript-eslint/require-await
277
+ async disconnect() {
278
+ // switch off autoConnect, we are in manual mode now
279
+ this.#autoConnectBackoff.disable();
280
+ try {
281
+ if (this.#websocket) {
282
+ // 1000 - Normal closure; the connection successfully completed
283
+ this.#websocket.close(1000);
284
+ }
285
+ } catch (error) {
286
+ log.error(error);
287
+ this.#emit("error", error);
288
+ throw error;
289
+ }
290
+ }
291
+
292
+ /**
293
+ * @summary Listens on events after having subscribed using the [[subscribe]] function.
294
+ * @param {ProviderInterfaceEmitted} type Event
295
+ * @param {ProviderInterfaceEmitCb} sub Callback
296
+ * @return unsubscribe function
297
+ */
298
+ on(type, sub) {
299
+ this.#eventemitter.on(type, sub);
300
+ return () => {
301
+ this.#eventemitter.removeListener(type, sub);
302
+ };
303
+ }
304
+
305
+ /**
306
+ * @summary Send JSON data using WebSockets to configured HTTP Endpoint or queue.
307
+ * @param method The RPC methods to execute
308
+ * @param params Encoded parameters as applicable for the method
309
+ * @param subscription Subscription details (internally used)
310
+ */
311
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
312
+ send(method, params, /** @deprecated \@talismn/chain-connector doesn't implement a cache */
313
+ isCacheable, subscription) {
314
+ const [id, body] = this.#coder.encodeJson(method, params);
315
+ const resultPromise = this.#send(id, body, method, params, subscription);
316
+ return resultPromise;
317
+ }
318
+ async #send(id, body, method, params, subscription) {
319
+ return new Promise((resolve, reject) => {
320
+ try {
321
+ if (!this.isConnected || this.#websocket === null) {
322
+ throw new Error("WebSocket is not connected");
323
+ }
324
+ const callback = (error, result) => {
325
+ error ? reject(error) : resolve(result);
326
+ };
327
+
328
+ // log.debug(() => ["calling", method, body])
329
+
330
+ this.#handlers[id] = {
331
+ callback,
332
+ method,
333
+ params,
334
+ start: Date.now(),
335
+ subscription
336
+ };
337
+ this.#websocket.send(body);
338
+ } catch (error) {
339
+ reject(error);
340
+ }
341
+ });
342
+ }
343
+
344
+ /**
345
+ * @name subscribe
346
+ * @summary Allows subscribing to a specific event.
347
+ *
348
+ * @example
349
+ * <BR>
350
+ *
351
+ * ```javascript
352
+ * const provider = new Websocket('ws://127.0.0.1:9944');
353
+ * const rpc = new Rpc(provider);
354
+ *
355
+ * rpc.state.subscribeStorage([[storage.system.account, <Address>]], (_, values) => {
356
+ * console.log(values)
357
+ * }).then((subscriptionId) => {
358
+ * console.log('balance changes subscription id: ', subscriptionId)
359
+ * })
360
+ * ```
361
+ */
362
+ subscribe(type, method, params, callback) {
363
+ return this.send(method, params, false, {
364
+ callback,
365
+ type
366
+ });
367
+ }
368
+
369
+ /**
370
+ * @summary Allows unsubscribing to subscriptions made with [[subscribe]].
371
+ */
372
+ async unsubscribe(type, method, id) {
373
+ const subscription = `${type}::${id}`;
374
+
375
+ // FIXME This now could happen with re-subscriptions. The issue is that with a re-sub
376
+ // the assigned id now does not match what the API user originally received. It has
377
+ // a slight complication in solving - since we cannot rely on the send id, but rather
378
+ // need to find the actual subscription id to map it
379
+ if (isUndefined(this.#subscriptions[subscription])) {
380
+ // log.debug(() => `Unable to find active subscription=${subscription}`)
381
+
382
+ return false;
383
+ }
384
+ delete this.#subscriptions[subscription];
385
+ try {
386
+ return this.isConnected && !isNull(this.#websocket) ? this.send(method, [id]) : true;
387
+ } catch (error) {
388
+ return false;
389
+ }
390
+ }
391
+ #emit = (type, ...args) => {
392
+ this.#eventemitter.emit(type, ...args);
393
+ };
394
+ #onSocketClose = event => {
395
+ const error = new Error(`disconnected from ${this.endpoint}: ${event.code}:: ${event.reason || getWSErrorString(event.code)}`);
396
+ if (this.#autoConnectBackoff.isActive) {
397
+ // 1000 is a normal closure and should not be logged as an error
398
+ if (event.code !== 1000) log.error(error.message);
399
+ }
400
+ this.#isConnected = false;
401
+ if (this.#websocket) {
402
+ this.#websocket.onclose = null;
403
+ this.#websocket.onerror = null;
404
+ this.#websocket.onmessage = null;
405
+ this.#websocket.onopen = null;
406
+ this.#websocket = null;
407
+ }
408
+ if (this.#timeoutId) {
409
+ clearInterval(this.#timeoutId);
410
+ this.#timeoutId = null;
411
+ }
412
+
413
+ // reject all hanging requests
414
+ eraseRecord(this.#handlers, h => {
415
+ try {
416
+ h.callback(error, undefined);
417
+ } catch (err) {
418
+ // does not throw
419
+ log.error(err);
420
+ }
421
+ });
422
+ eraseRecord(this.#waitingForId);
423
+ this.#emit("disconnected");
424
+ this.scheduleNextRetry();
425
+ };
426
+ #onSocketError = error => {
427
+ // log.debug(() => ["socket error", error])
428
+ this.#emit("error", error);
429
+ };
430
+ #onSocketMessage = message => {
431
+ // log.debug(() => ["received", message.data])
432
+ try {
433
+ const response = JSON.parse(message.data);
434
+ return isUndefined(response.method) ? this.#onSocketMessageResult(response) : this.#onSocketMessageSubscribe(response);
435
+ } catch (e) {
436
+ this.#emit("error", new Error("Invalid websocket message received", {
437
+ cause: e
438
+ }));
439
+ }
440
+ };
441
+ #onSocketMessageResult = response => {
442
+ const handler = this.#handlers[response.id];
443
+ if (!handler) {
444
+ // log.debug(() => `Unable to find handler for id=${response.id}`)
445
+
446
+ return;
447
+ }
448
+ try {
449
+ const {
450
+ method,
451
+ params,
452
+ subscription
453
+ } = handler;
454
+ const result = this.#coder.decodeResponse(response);
455
+
456
+ // first send the result - in case of subs, we may have an update
457
+ // immediately if we have some queued results already
458
+ handler.callback(null, result);
459
+ if (subscription) {
460
+ const subId = `${subscription.type}::${result}`;
461
+ this.#subscriptions[subId] = objectSpread({}, subscription, {
462
+ method,
463
+ params
464
+ });
465
+
466
+ // if we have a result waiting for this subscription already
467
+ if (this.#waitingForId[subId]) {
468
+ this.#onSocketMessageSubscribe(this.#waitingForId[subId]);
469
+ }
470
+ }
471
+ } catch (error) {
472
+ handler.callback(error, undefined);
473
+ }
474
+ delete this.#handlers[response.id];
475
+ };
476
+ #onSocketMessageSubscribe = response => {
477
+ const method = ALIASES[response.method] || response.method || "invalid";
478
+ const subId = `${method}::${response.params.subscription}`;
479
+ const handler = this.#subscriptions[subId];
480
+ if (!handler) {
481
+ // store the JSON, we could have out-of-order subid coming in
482
+ this.#waitingForId[subId] = response;
483
+
484
+ // log.debug(() => `Unable to find handler for subscription=${subId}`)
485
+
486
+ return;
487
+ }
488
+
489
+ // housekeeping
490
+ delete this.#waitingForId[subId];
491
+ try {
492
+ const result = this.#coder.decodeResponse(response);
493
+ handler.callback(null, result);
494
+ } catch (error) {
495
+ handler.callback(error, undefined);
496
+ }
497
+ };
498
+ #onSocketOpen = () => {
499
+ if (this.#websocket === null) {
500
+ throw new Error("WebSocket cannot be null in onOpen");
501
+ }
502
+
503
+ // log.debug(() => ["connected to", this.endpoint])
504
+
505
+ this.#isConnected = true;
506
+ this.#endpointsTriedSinceLastConnection = 0;
507
+ this.#autoConnectBackoff.reset();
508
+ this.#resubscribe();
509
+ this.#emit("connected");
510
+ return true;
511
+ };
512
+ #resubscribe = () => {
513
+ const subscriptions = this.#subscriptions;
514
+ this.#subscriptions = {};
515
+ Promise.all(Object.keys(subscriptions).map(async id => {
516
+ const {
517
+ callback,
518
+ method,
519
+ params,
520
+ type
521
+ } = subscriptions[id];
522
+
523
+ // only re-create subscriptions which are not in author (only area where
524
+ // transactions are created, i.e. submissions such as 'author_submitAndWatchExtrinsic'
525
+ // are not included (and will not be re-broadcast)
526
+ if (type.startsWith("author_")) {
527
+ return;
528
+ }
529
+ try {
530
+ await this.subscribe(type, method, params, callback);
531
+ } catch (error) {
532
+ log.error(error);
533
+ }
534
+ })).catch(log.error);
535
+ };
536
+ #timeoutHandlers = () => {
537
+ const now = Date.now();
538
+ const ids = Object.keys(this.#handlers);
539
+ for (let i = 0; i < ids.length; i++) {
540
+ const handler = this.#handlers[ids[i]];
541
+ if (now - handler.start > this.#timeout) {
542
+ try {
543
+ handler.callback(new Error(`No response received from RPC endpoint in ${this.#timeout / 1000}s`), undefined);
544
+ } catch {
545
+ // ignore
546
+ }
547
+ delete this.#handlers[ids[i]];
548
+ }
549
+ }
550
+ };
551
+ }
552
+
553
+ // errors that require an rpc fallback
554
+ // https://docs.blastapi.io/blast-documentation/things-you-need-to-know/error-reference
555
+ const BAD_RPC_ERRORS = {
556
+ "-32097": "Rate limit exceeded",
557
+ "-32098": "Capacity exceeded"
558
+ };
559
+ class ChainConnectionError extends Error {
560
+ constructor(chainId, options) {
561
+ super(`Unable to connect to chain ${chainId}`, options);
562
+ this.type = "CHAIN_CONNECTION_ERROR";
563
+ this.chainId = chainId;
564
+ }
565
+ }
566
+ class StaleRpcError extends Error {
567
+ constructor(chainId, options) {
568
+ super(`RPCs are stale/unavailable for chain ${chainId}`, options);
569
+ this.type = "STALE_RPC_ERROR";
570
+ this.chainId = chainId;
571
+ }
572
+ }
573
+ class WebsocketAllocationExhaustedError extends Error {
574
+ constructor(chainId, options) {
575
+ super(`No websockets are available from the browser pool to connect to chain ${chainId}`, options);
576
+ this.type = "WEBSOCKET_ALLOCATION_EXHAUSTED_ERROR";
577
+ this.chainId = chainId;
578
+ }
579
+ }
580
+ class CallerUnsubscribedError extends Error {
581
+ constructor(chainId, unsubscribeMethod, options) {
582
+ super(`Caller unsubscribed from ${chainId}`, options);
583
+ this.type = "CALLER_UNSUBSCRIBED_ERROR";
584
+ this.chainId = chainId;
585
+ this.unsubscribeMethod = unsubscribeMethod;
586
+ }
587
+ }
588
+ /**
589
+ * ChainConnector provides an interface similar to WsProvider, but with three points of difference:
590
+ *
591
+ * 1. ChainConnector methods all accept a `chainId` instead of an array of RPCs. RPCs are then fetched internally from chaindata.
592
+ * 2. ChainConnector creates only one `WsProvider` per chain and ensures that all downstream requests to a chain share the one socket connection.
593
+ * 3. Subscriptions return a callable `unsubscribe` method instead of an id.
594
+ *
595
+ * Additionally, when run on the clientside of a dapp where `window.talismanSub` is available, instead of spinning up new websocket
596
+ * connections this class will forward all requests through to the wallet backend - where another instance of this class will
597
+ * handle the websocket connections.
598
+ */
599
+ class ChainConnectorDot {
600
+ #chaindataChainProvider;
601
+ #connectionMetaDb;
602
+ #socketConnections = {};
603
+ #socketKeepAliveIntervals = {};
604
+ #socketUsers = {};
605
+ constructor(chaindataChainProvider, connectionMetaDb) {
606
+ this.#chaindataChainProvider = chaindataChainProvider;
607
+ this.#connectionMetaDb = connectionMetaDb;
608
+ if (this.#connectionMetaDb) {
609
+ this.#chaindataChainProvider.getNetworkIds("polkadot").then(chainIds => {
610
+ // tidy up connectionMeta for chains which no longer exist
611
+ this.#connectionMetaDb?.chainPriorityRpcs.where("id").noneOf(chainIds).delete();
612
+ this.#connectionMetaDb?.chainBackoffInterval.where("id").noneOf(chainIds).delete();
613
+ });
614
+ }
615
+ }
616
+
617
+ /**
618
+ * Creates a facade over this ChainConnector which conforms to the PJS ProviderInterface
619
+ * @example // Using a chainConnector as a Provider for an ApiPromise
620
+ * const provider = chainConnector.asProvider('polkadot')
621
+ * const api = new ApiPromise({ provider })
622
+ */
623
+ asProvider(chainId) {
624
+ const unsubHandler = new Map();
625
+ const providerFacade = {
626
+ hasSubscriptions: true,
627
+ isClonable: false,
628
+ isConnected: true,
629
+ clone: () => providerFacade,
630
+ connect: () => Promise.resolve(),
631
+ disconnect: () => Promise.resolve(),
632
+ on: () => () => {},
633
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
634
+ send: async (method, params, isCacheable) => await this.send(chainId, method, params, isCacheable),
635
+ subscribe: async (type, method, params, cb) => {
636
+ const unsubscribe = await this.subscribe(chainId, method, type, params, cb);
637
+ const subscriptionId = this.getExclusiveRandomId([...unsubHandler.keys()].map(Number)).toString();
638
+ unsubHandler.set(subscriptionId, unsubscribe);
639
+ return subscriptionId;
640
+ },
641
+ unsubscribe: async (_type, unsubscribeMethod, subscriptionId) => {
642
+ unsubHandler.get(subscriptionId)?.(unsubscribeMethod);
643
+ unsubHandler.delete(subscriptionId);
644
+ return true;
645
+ }
646
+ };
647
+ return providerFacade;
648
+ }
649
+
650
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
651
+ async send(chainId, method, params, isCacheable, extraOptions) {
652
+ const talismanSub = this.getTalismanSub();
653
+ if (talismanSub !== undefined) {
654
+ try {
655
+ const chain = await this.#chaindataChainProvider.getNetworkById(chainId, "polkadot");
656
+ if (!chain) throw new Error(`Chain ${chainId} not found in store`);
657
+ const {
658
+ genesisHash
659
+ } = chain;
660
+ if (typeof genesisHash !== "string") throw new Error(`Chain ${chainId} has no genesisHash in store`);
661
+ return await talismanSub.send(genesisHash, method, params);
662
+ } catch (error) {
663
+ log.warn(`Failed to make wallet-proxied send request for chain ${chainId}. Falling back to plain websocket`, error);
664
+ }
665
+ }
666
+ try {
667
+ // eslint-disable-next-line no-var
668
+ var [socketUserId, ws] = await this.connectChainSocket(chainId);
669
+ } catch (error) {
670
+ throw new StaleRpcError(chainId, {
671
+ cause: error
672
+ });
673
+ }
674
+ try {
675
+ // wait for ws to be ready, but don't wait forever
676
+ const timeout = 15_000; // 15 seconds in milliseconds
677
+ await this.waitForWs(ws, timeout);
678
+ } catch (error) {
679
+ await this.disconnectChainSocket(chainId, socketUserId);
680
+ throw new ChainConnectionError(chainId, {
681
+ cause: error
682
+ });
683
+ }
684
+ try {
685
+ const timeout = 30_000; // throw after 30 seconds if no response
686
+ // eslint-disable-next-line no-var
687
+ var response = await Promise.race([ws.send(method, params, isCacheable), throwAfter(timeout, "TIMEOUT")]);
688
+ } catch (err) {
689
+ const error = err;
690
+ if (error?.message === "TIMEOUT") {
691
+ log.error(`ChainConnector timeout`, {
692
+ chainId,
693
+ endpoint: ws.endpoint,
694
+ error
695
+ });
696
+ await this.updateRpcPriority(chainId, ws.endpoint, "last");
697
+ await this.reset(chainId);
698
+ throw new Error("Timeout");
699
+ }
700
+ const badRpcError = BAD_RPC_ERRORS[error?.code?.toString() ?? ""];
701
+ if (badRpcError) {
702
+ log.error(`ChainConnector ${badRpcError}`, {
703
+ error,
704
+ chainId,
705
+ endpoint: ws.endpoint
706
+ });
707
+ await this.updateRpcPriority(chainId, ws.endpoint, "last");
708
+ await this.reset(chainId);
709
+ throw new Error(badRpcError);
710
+ }
711
+ if (!extraOptions?.expectErrors) log.error(`Failed to send ${method} on chain ${chainId}\nparams: ${JSON.stringify(params)}`, {
712
+ error,
713
+ endpoint: ws.endpoint
714
+ });
715
+ await this.disconnectChainSocket(chainId, socketUserId);
716
+ throw error;
717
+ }
718
+ await this.disconnectChainSocket(chainId, socketUserId);
719
+ return response;
720
+ }
721
+ async subscribe(chainId, subscribeMethod, responseMethod, params, callback, timeout = 30_000 // 30 seconds in milliseconds
722
+ ) {
723
+ const talismanSub = this.getTalismanSub();
724
+ if (talismanSub !== undefined) {
725
+ try {
726
+ const chain = await this.#chaindataChainProvider.getNetworkById(chainId, "polkadot");
727
+ if (!chain) throw new Error(`Chain ${chainId} not found in store`);
728
+ const {
729
+ genesisHash
730
+ } = chain;
731
+ if (typeof genesisHash !== "string") throw new Error(`Chain ${chainId} has no genesisHash in store`);
732
+ const subscriptionId = await talismanSub.subscribe(genesisHash, subscribeMethod, responseMethod, params, callback, timeout);
733
+ return unsubscribeMethod => talismanSub.unsubscribe(subscriptionId, unsubscribeMethod);
734
+ } catch (error) {
735
+ log.warn(`Failed to create wallet-proxied subscription for chain ${chainId}. Falling back to plain websocket`, error);
736
+ }
737
+ }
738
+ try {
739
+ // eslint-disable-next-line no-var
740
+ var [socketUserId, ws] = await this.connectChainSocket(chainId);
741
+ } catch (error) {
742
+ throw new StaleRpcError(chainId, {
743
+ cause: error
744
+ });
745
+ }
746
+
747
+ // by using this `Deferred` promise
748
+ // (a promise which can be resolved or rejected by code outside of the scope of the promise's constructor)
749
+ // we can queue up our async cleanup on the promise and then immediately return an unsubscribe method to the caller
750
+ const unsubDeferred = Deferred();
751
+ // we return this to the caller so that they can let us know when they're no longer interested in this subscription
752
+ const unsubscribe = unsubscribeMethod => unsubDeferred.reject(new CallerUnsubscribedError(chainId, unsubscribeMethod));
753
+ // we queue up our work to clean up our subscription when this promise rejects
754
+ const callerUnsubscribed = unsubDeferred.promise;
755
+
756
+ // used to detect when there are no more websockets available from the browser websocket pool
757
+ // in this scenario, we'll be waiting for ws.isReady until some existing sockets are closed
758
+ //
759
+ // while we're waiting, we'll send an error back to the caller so that they can show some useful
760
+ // info to the user
761
+ let noMoreSocketsTimeout = undefined
762
+
763
+ // create subscription asynchronously so that the caller can unsubscribe without waiting for
764
+ // the subscription to be created (which can take some time if e.g. the connection can't be established)
765
+ ;
766
+ (async () => {
767
+ // wait for ws to be ready, but don't wait forever
768
+ // if timeout is number, cancel when timeout is reached (or caller unsubscribes)
769
+ // if timeout is false, only cancel when the caller unsubscribes
770
+ let unsubRpcStatus = null;
771
+ try {
772
+ const unsubStale = ws.on("stale-rpcs", ({
773
+ nextBackoffInterval
774
+ } = {}) => {
775
+ callback(new StaleRpcError(chainId), null);
776
+ if (this.#connectionMetaDb && nextBackoffInterval) {
777
+ const id = chainId;
778
+ this.#connectionMetaDb.chainBackoffInterval.put({
779
+ id,
780
+ interval: nextBackoffInterval
781
+ }, id);
782
+ }
783
+ });
784
+ const unsubConnected = ws.on("connected", () => {
785
+ if (this.#connectionMetaDb) this.#connectionMetaDb.chainBackoffInterval.delete(chainId);
786
+ });
787
+ unsubRpcStatus = () => {
788
+ unsubStale();
789
+ unsubConnected();
790
+ };
791
+ noMoreSocketsTimeout = setTimeout(() => callback(new WebsocketAllocationExhaustedError(chainId), null), 30_000 // 30 seconds in ms
792
+ );
793
+ if (timeout) await Promise.race([this.waitForWs(ws, timeout), callerUnsubscribed]);else await Promise.race([ws.isReady, callerUnsubscribed]);
794
+ clearTimeout(noMoreSocketsTimeout);
795
+ } catch (error) {
796
+ clearTimeout(noMoreSocketsTimeout);
797
+ unsubRpcStatus && unsubRpcStatus();
798
+ await this.disconnectChainSocket(chainId, socketUserId);
799
+ return;
800
+ }
801
+
802
+ // create subscription on ws
803
+ // handle the scenarios where the caller unsubscribes before the subscription has been created and:
804
+ // - the subscriptionId is already set
805
+ // - the subscriptionId is not set yet, but will be
806
+ let subscriptionId = null;
807
+ let disconnected = false;
808
+ let unsubscribeMethod = undefined;
809
+ try {
810
+ await Promise.race([ws.subscribe(responseMethod, subscribeMethod, params, callback).then(id => {
811
+ if (disconnected) {
812
+ unsubscribeMethod && ws.unsubscribe(responseMethod, unsubscribeMethod, id);
813
+ } else subscriptionId = id;
814
+ }), callerUnsubscribed]);
815
+ } catch (error) {
816
+ if (error instanceof CallerUnsubscribedError) unsubscribeMethod = error.unsubscribeMethod;
817
+ unsubRpcStatus && unsubRpcStatus();
818
+ disconnected = true;
819
+ if (subscriptionId !== null && unsubscribeMethod) await ws.unsubscribe(responseMethod, unsubscribeMethod, subscriptionId);
820
+ await this.disconnectChainSocket(chainId, socketUserId);
821
+ return;
822
+ }
823
+
824
+ // unsubscribe from ws subscription when the caller has unsubscribed
825
+ callerUnsubscribed.catch(async error => {
826
+ let unsubscribeMethod = undefined;
827
+ if (error instanceof CallerUnsubscribedError) unsubscribeMethod = error.unsubscribeMethod;
828
+ unsubRpcStatus && unsubRpcStatus();
829
+ if (subscriptionId !== null && unsubscribeMethod) await ws.unsubscribe(responseMethod, unsubscribeMethod, subscriptionId);
830
+ await this.disconnectChainSocket(chainId, socketUserId);
831
+ }).catch(error => log.warn(error));
832
+ })();
833
+ return unsubscribe;
834
+ }
835
+
836
+ /**
837
+ * Kills current websocket if any
838
+ * Useful after changing rpc order to make sure it's applied for futher requests
839
+ */
840
+ async reset(chainId) {
841
+ log.info("ChainConnector reset", chainId);
842
+ const ws = this.#socketConnections[chainId];
843
+ if (!ws) return;
844
+ try {
845
+ clearTimeout(this.#socketKeepAliveIntervals[chainId]);
846
+ delete this.#socketConnections[chainId];
847
+ delete this.#socketUsers[chainId];
848
+ await ws.disconnect();
849
+ } catch (error) {
850
+ log.warn(`Error occurred reseting socket ${chainId}`, error);
851
+ }
852
+ }
853
+
854
+ /**
855
+ * Wait for websocket to be ready, but don't wait forever
856
+ */
857
+ async waitForWs(ws, timeout = 30_000 // 30 seconds in milliseconds
858
+ ) {
859
+ const timer = timeout ? sleep(timeout).then(() => {
860
+ throw new Error(`RPC connect timeout reached: ${ws.endpoint}`);
861
+ }) : false;
862
+ await Promise.race([ws.isReady, timer].filter(isTruthy));
863
+ }
864
+
865
+ /**
866
+ * Connect to an RPC via chainId
867
+ *
868
+ * The caller must call disconnectChainSocket with the returned SocketUserId once they are finished with it
869
+ */
870
+ async connectChainSocket(chainId) {
871
+ const rpcs = await this.getEndpoints(chainId);
872
+ const socketUserId = this.addSocketUser(chainId);
873
+
874
+ // retrieve next rpc backoff interval from connection meta db (if one exists)
875
+ let nextBackoffInterval = undefined;
876
+ if (this.#connectionMetaDb) nextBackoffInterval = (await this.#connectionMetaDb.chainBackoffInterval.get(chainId))?.interval;
877
+
878
+ // NOTE: Make sure there are no calls to `await` between this check and the
879
+ // next step where we assign a `new Websocket` to `this.#socketConnections[chainId]`
880
+ //
881
+ // If there is an `await` between these two steps then there will be a race condition introduced.
882
+ // The result of this race condition will be the unnecessary creation of multiple instances of
883
+ // `Websocket` per chain, rather than the intended behaviour where every call to send/subscribe
884
+ // shares a single `Websocket` per chain.
885
+ if (this.#socketConnections[chainId]) return [socketUserId, this.#socketConnections[chainId]];
886
+ if (rpcs.length) this.#socketConnections[chainId] = new Websocket(rpcs, undefined, undefined, nextBackoffInterval);else {
887
+ throw new Error(`No healthy RPCs available for chain ${chainId}`);
888
+ }
889
+
890
+ // on ws connected event, store current rpc as most recently connected rpc
891
+ if (this.#connectionMetaDb) {
892
+ this.#socketConnections[chainId].on("connected", () => {
893
+ if (!this.#connectionMetaDb) return;
894
+ const id = chainId;
895
+ const url = this.#socketConnections[chainId]?.endpoint;
896
+ if (!url) return;
897
+ this.updateRpcPriority(id, url, "first").catch(err => log.warn(`updateRpcPriority failed`, err));
898
+ });
899
+ }
900
+ (async () => {
901
+ if (!this.#socketConnections[chainId]) return log.warn(`ignoring ${chainId} rpc ws healthcheck initialization: ws is not defined`);
902
+ await this.#socketConnections[chainId].isReady;
903
+ if (this.#socketKeepAliveIntervals[chainId]) clearInterval(this.#socketKeepAliveIntervals[chainId]);
904
+ const intervalMs = 10_000; // 10,000ms = 10s
905
+ this.#socketKeepAliveIntervals[chainId] = setInterval(() => {
906
+ if (!this.#socketConnections[chainId]) return log.warn(`skipping ${chainId} rpc ws healthcheck: ws is not defined`);
907
+ if (!this.#socketConnections[chainId].isConnected) return log.warn(`skipping ${chainId} rpc ws healthcheck: ws is not connected`);
908
+ this.#socketConnections[chainId].send("system_health", []).catch(error => log.warn(`Failed keep-alive for socket ${chainId}`, error));
909
+ }, intervalMs);
910
+ })();
911
+ return [socketUserId, this.#socketConnections[chainId]];
912
+ }
913
+ async disconnectChainSocket(chainId, socketUserId) {
914
+ this.removeSocketUser(chainId, socketUserId);
915
+ if (this.#socketUsers[chainId].length > 0) return;
916
+ if (!this.#socketConnections[chainId]) return log.warn(`Failed to disconnect socket: socket ${chainId} not found`);
917
+ try {
918
+ this.#socketConnections[chainId].disconnect();
919
+ } catch (error) {
920
+ log.warn(`Error occurred disconnecting socket ${chainId}`, error);
921
+ }
922
+ delete this.#socketConnections[chainId];
923
+ clearInterval(this.#socketKeepAliveIntervals[chainId]);
924
+ delete this.#socketKeepAliveIntervals[chainId];
925
+ }
926
+ addSocketUser(chainId) {
927
+ if (!Array.isArray(this.#socketUsers[chainId])) this.#socketUsers[chainId] = [];
928
+ const socketUserId = this.getExclusiveRandomId(this.#socketUsers[chainId]);
929
+ this.#socketUsers[chainId].push(socketUserId);
930
+ return socketUserId;
931
+ }
932
+ removeSocketUser(chainId, socketUserId) {
933
+ const userIndex = this.#socketUsers[chainId].indexOf(socketUserId);
934
+ if (userIndex === -1) throw new Error(`Can't remove user ${socketUserId} from socket ${chainId}: user not in list ${this.#socketUsers[chainId].join(", ")}`);
935
+ this.#socketUsers[chainId].splice(userIndex, 1);
936
+ }
937
+
938
+ /** continues to generate a random number until it finds one which is not present in the exclude list */
939
+ getExclusiveRandomId(exclude = []) {
940
+ let id = this.getRandomId();
941
+ while (exclude.includes(id)) {
942
+ id = this.getRandomId();
943
+ }
944
+ return id;
945
+ }
946
+ /** generates a random number */
947
+ getRandomId() {
948
+ return Math.trunc(Math.random() * Math.pow(10, 8));
949
+ }
950
+ getTalismanSub() {
951
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
952
+ const talismanSub = typeof window !== "undefined" && window.talismanSub;
953
+
954
+ /* eslint-disable @typescript-eslint/no-unsafe-function-type */
955
+ const rpcByGenesisHashSend = talismanSub?.rpcByGenesisHashSend;
956
+ const rpcByGenesisHashSubscribe = talismanSub?.rpcByGenesisHashSubscribe;
957
+ const rpcByGenesisHashUnsubscribe = talismanSub?.rpcByGenesisHashUnsubscribe;
958
+ if (typeof rpcByGenesisHashSend !== "function") return;
959
+ if (typeof rpcByGenesisHashSubscribe !== "function") return;
960
+ if (typeof rpcByGenesisHashUnsubscribe !== "function") return;
961
+ return {
962
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
963
+ send: (genesisHash, method, params) => rpcByGenesisHashSend(genesisHash, method, params),
964
+ subscribe: (genesisHash, subscribeMethod, responseMethod, params, callback, timeout) => rpcByGenesisHashSubscribe(genesisHash, subscribeMethod, responseMethod, params, callback, timeout),
965
+ unsubscribe: (subscriptionId, unsubscribeMethod) => rpcByGenesisHashUnsubscribe(subscriptionId, unsubscribeMethod)
966
+ };
967
+ }
968
+ async updateRpcPriority(chainId, rpc, priority) {
969
+ if (!this.#connectionMetaDb) return;
970
+ const rpcs = await this.getEndpoints(chainId);
971
+ if (!rpcs.includes(rpc)) throw new Error(`Unknown rpc for chain ${chainId} : ${rpc}`);
972
+ const urls = rpcs.filter(r => r !== rpc);
973
+ if (priority === "first") urls.unshift(rpc);
974
+ if (priority === "last") urls.push(rpc);
975
+ if (!isEqual(urls, rpcs)) {
976
+ // order may not change, especially if there is only one
977
+ await this.#connectionMetaDb.chainPriorityRpcs.put({
978
+ id: chainId,
979
+ urls
980
+ }, chainId);
981
+ }
982
+ }
983
+ async getEndpoints(chainId) {
984
+ const chain = await this.#chaindataChainProvider.getNetworkById(chainId, "polkadot");
985
+ if (!chain) throw new Error(`Chain ${chainId} not found in store`);
986
+ let rpcs = chain.rpcs.concat(); // clone to avoid mutating the original array
987
+ const priorityRpcs = this.#connectionMetaDb ? await this.#connectionMetaDb.chainPriorityRpcs.get(chainId) : undefined;
988
+ if (priorityRpcs) {
989
+ // use existing priority list of rpcs that still exist, and include missing ones
990
+ rpcs = [...priorityRpcs.urls.filter(rpc => rpcs.includes(rpc)), ...rpcs.filter(rpc => !priorityRpcs.urls.includes(rpc))];
991
+ }
992
+ return rpcs;
993
+ }
994
+ }
995
+ const isEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
996
+
997
+ const AUTO_CONNECT_TIMEOUT = 3_000;
998
+ const TIMEOUT = 10_000;
999
+ class ChainConnectorDotStub {
1000
+ #network;
1001
+ #provider;
1002
+ constructor(network) {
1003
+ this.#network = network;
1004
+ this.#provider = new WsProvider(network.rpcs, AUTO_CONNECT_TIMEOUT, undefined, TIMEOUT);
1005
+ }
1006
+ asProvider() {
1007
+ return this.#provider;
1008
+ }
1009
+ async send(chainId, method, params, isCacheable) {
1010
+ await this.#provider.isReady;
1011
+ return this.#provider.send(method, params, isCacheable);
1012
+ }
1013
+ async subscribe(chainId, subscribeMethod, responseMethod, params, callback, timeout) {
1014
+ await this.#provider.isReady;
1015
+ const subId = await Promise.race([throwAfter(timeout || TIMEOUT, `Subscription timed out after ${timeout}ms`), this.#provider.subscribe(responseMethod, subscribeMethod, params, callback)]);
1016
+ return unsubscribeMethod => {
1017
+ this.#provider.unsubscribe(responseMethod, unsubscribeMethod, subId);
1018
+ };
1019
+ }
1020
+ reset() {
1021
+ throw new Error("ChainConnectorDotStub does not implement reset");
1022
+ }
1023
+ }
1024
+
1025
+ // viem chains benefit from multicall config & other viem goodies
1026
+ const VIEM_CHAINS = Object.keys(chains).reduce((acc, curr) => {
1027
+ const chain = chains[curr];
1028
+ acc[chain.id] = chain;
1029
+ return acc;
1030
+ }, {});
1031
+ const chainsCache = new Map();
1032
+ const clearChainsCache = networkId => {
1033
+ if (networkId) chainsCache.delete(networkId);else chainsCache.clear();
1034
+ };
1035
+ const getChainFromEvmNetwork = network => {
1036
+ const {
1037
+ symbol,
1038
+ decimals
1039
+ } = network.nativeCurrency;
1040
+ if (!chainsCache.has(network.id)) {
1041
+ const chainRpcs = network.rpcs ?? [];
1042
+ const viemChain = VIEM_CHAINS[Number(network.id)] ?? {};
1043
+ const chain = {
1044
+ ...viemChain,
1045
+ id: Number(network.id),
1046
+ name: network.name ?? `Ethereum Chain ${network.id}`,
1047
+ rpcUrls: {
1048
+ public: {
1049
+ http: chainRpcs
1050
+ },
1051
+ default: {
1052
+ http: chainRpcs
1053
+ }
1054
+ },
1055
+ nativeCurrency: {
1056
+ symbol,
1057
+ decimals,
1058
+ name: symbol
1059
+ },
1060
+ contracts: {
1061
+ ...viemChain.contracts,
1062
+ ...(network.contracts ? fromPairs(toPairs(network.contracts).map(([name, address]) => [camelCase(name), {
1063
+ address
1064
+ }])) : {})
1065
+ }
1066
+ };
1067
+ chainsCache.set(network.id, chain);
1068
+ }
1069
+ return chainsCache.get(network.id);
1070
+ };
1071
+
1072
+ const getTransportForEvmNetwork = (evmNetwork, options = {}) => {
1073
+ if (!evmNetwork.rpcs?.length) throw new Error("No RPCs found for EVM network");
1074
+ const {
1075
+ batch
1076
+ } = options;
1077
+ return fallback(evmNetwork.rpcs.map(url => http(url, {
1078
+ batch,
1079
+ retryCount: 0
1080
+ })), {
1081
+ retryCount: 0
1082
+ });
1083
+ };
1084
+
1085
+ const MUTLICALL_BATCH_WAIT = 25;
1086
+ const MUTLICALL_BATCH_SIZE = 100;
1087
+ const HTTP_BATCH_WAIT = 25;
1088
+ const HTTP_BATCH_SIZE_WITH_MULTICALL = 10;
1089
+ const HTTP_BATCH_SIZE_WITHOUT_MULTICALL = 30;
1090
+
1091
+ // cache to reuse previously created public clients
1092
+ const publicClientCache = new Map();
1093
+ const clearPublicClientCache = evmNetworkId => {
1094
+ clearChainsCache(evmNetworkId);
1095
+ if (evmNetworkId) publicClientCache.delete(evmNetworkId);else publicClientCache.clear();
1096
+ };
1097
+ const getEvmNetworkPublicClient = network => {
1098
+ const chain = getChainFromEvmNetwork(network);
1099
+ if (!publicClientCache.has(network.id)) {
1100
+ if (!network.rpcs.length) throw new Error("No RPCs found for Ethereum network");
1101
+ const batch = chain.contracts?.multicall3 ? {
1102
+ multicall: {
1103
+ wait: MUTLICALL_BATCH_WAIT,
1104
+ batchSize: MUTLICALL_BATCH_SIZE
1105
+ }
1106
+ } : undefined;
1107
+ const transportOptions = {
1108
+ batch: {
1109
+ batchSize: chain.contracts?.multicall3 ? HTTP_BATCH_SIZE_WITH_MULTICALL : HTTP_BATCH_SIZE_WITHOUT_MULTICALL,
1110
+ wait: HTTP_BATCH_WAIT
1111
+ }
1112
+ };
1113
+ const transport = getTransportForEvmNetwork(network, transportOptions);
1114
+ publicClientCache.set(network.id, createPublicClient({
1115
+ chain,
1116
+ transport,
1117
+ batch
1118
+ }));
1119
+ }
1120
+ return publicClientCache.get(network.id);
1121
+ };
1122
+
1123
+ const getEvmNetworkWalletClient = (network, options = {}) => {
1124
+ const chain = getChainFromEvmNetwork(network);
1125
+ const transport = getTransportForEvmNetwork(network);
1126
+ return createWalletClient({
1127
+ chain,
1128
+ transport,
1129
+ account: options.account
1130
+ });
1131
+ };
1132
+
1133
+ class ChainConnectorEth {
1134
+ #chaindataProvider;
1135
+ constructor(chaindataProvider) {
1136
+ this.#chaindataProvider = chaindataProvider;
1137
+ }
1138
+ async getPublicClientForEvmNetwork(evmNetworkId) {
1139
+ const network = await this.#chaindataProvider.getNetworkById(evmNetworkId, "ethereum");
1140
+ if (!network) return null;
1141
+ return getEvmNetworkPublicClient(network);
1142
+ }
1143
+ async getWalletClientForEvmNetwork(evmNetworkId, account) {
1144
+ const network = await this.#chaindataProvider.getNetworkById(evmNetworkId, "ethereum");
1145
+ if (!network) return null;
1146
+ return getEvmNetworkWalletClient(network, {
1147
+ account
1148
+ });
1149
+ }
1150
+ clearRpcProvidersCache(evmNetworkId) {
1151
+ clearPublicClientCache(evmNetworkId);
1152
+ }
1153
+ }
1154
+
1155
+ class ChainConnectorEthStub {
1156
+ #network;
1157
+ constructor(network) {
1158
+ this.#network = network;
1159
+ }
1160
+ async getPublicClientForEvmNetwork() {
1161
+ return getEvmNetworkPublicClient(this.#network);
1162
+ }
1163
+ async getWalletClientForEvmNetwork(networkId, account) {
1164
+ return getEvmNetworkWalletClient(this.#network, {
1165
+ account
1166
+ });
1167
+ }
1168
+ clearRpcProvidersCache() {
1169
+ // No-op for stub
1170
+ }
1171
+ }
1172
+
1173
+ // TODO
1174
+ const getSolConnection = (_networkId, _rpcs) => {
1175
+ return new Connection("https://solana-mainnet.g.alchemy.com/v2/FlflUnY6iZ98J9likA0ZdLSMfa6SqMya", {
1176
+ commitment: "confirmed"
1177
+ });
1178
+ };
1179
+
1180
+ class ChainConnectorSol {
1181
+ #chaindataProvider;
1182
+ constructor(chaindataProvider) {
1183
+ this.#chaindataProvider = chaindataProvider;
1184
+ }
1185
+ async getConnection(networkId) {
1186
+ const network = await this.#chaindataProvider.getNetworkById(networkId, "solana");
1187
+ if (!network) throw new Error(`Network not found: ${networkId}`);
1188
+ return getSolConnection(networkId, network.rpcs);
1189
+ }
1190
+ }
1191
+
1192
+ class ChainConnectorSolStub {
1193
+ #connection;
1194
+ constructor(network) {
1195
+ this.#connection = getSolConnection(network.id, network.rpcs);
1196
+ }
1197
+ async getConnection() {
1198
+ return this.#connection;
1199
+ }
1200
+ }
1201
+
1202
+ export { ChainConnectionError, ChainConnectorDot, ChainConnectorDotStub, ChainConnectorEth, ChainConnectorEthStub, ChainConnectorSol, ChainConnectorSolStub, StaleRpcError, WebsocketAllocationExhaustedError };