@chainflip/rpc 1.9.0-assethub.1 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/WsClient.cjs CHANGED
@@ -1,113 +1,124 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;// src/WsClient.ts
2
2
  var _async = require('@chainflip/utils/async');
3
+ var _zod = require('zod');
3
4
  var _Clientcjs = require('./Client.cjs'); var _Clientcjs2 = _interopRequireDefault(_Clientcjs);
4
5
  var _commoncjs = require('./common.cjs');
6
+ var CONNECTING = "CONNECTING";
5
7
  var READY = "READY";
6
8
  var DISCONNECT = "DISCONNECT";
7
9
  var WsClient = (_class = class extends _Clientcjs2.default {
8
- constructor(url, WebSocket = globalThis.WebSocket) {
9
- super(url);_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);;
10
- this.WebSocket = WebSocket;
11
- }
12
10
 
13
11
  __init() {this.reconnectAttempts = 0}
14
12
  __init2() {this.emitter = new EventTarget()}
15
- __init3() {this.requestMap = /* @__PURE__ */ new Map()}
16
- async close() {
17
- await this.handleClose();
13
+ __init3() {this.inFlightRequestMap = /* @__PURE__ */ new Map()}
14
+
15
+ __init4() {this.shouldConnect = true}
16
+ constructor(url, { timeout = 3e4 } = {}) {
17
+ super(url);_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);_class.prototype.__init4.call(this);_class.prototype.__init5.call(this);_class.prototype.__init6.call(this);;
18
+ this.timeout = timeout;
18
19
  }
19
- async handleClose() {
20
- if (!this.ws)
21
- return;
22
- this.ws.removeEventListener("close", this.handleDisconnect);
20
+ async close() {
21
+ this.shouldConnect = false;
22
+ if (!this.ws) return;
23
+ await this.handleDisconnect();
24
+ const waitForClose = this.ws.readyState === WebSocket.OPEN;
23
25
  this.ws.close();
24
- if (this.ws.readyState !== this.WebSocket.CLOSED) {
25
- await _async.once.call(void 0, this.ws, "close");
26
- }
26
+ if (waitForClose) await _async.once.call(void 0, this.ws, "close");
27
+ this.ws = void 0;
27
28
  }
28
29
  async connectionReady() {
29
30
  if (!this.ws) {
30
31
  return this.connect();
31
32
  }
32
- if (this.ws.readyState !== this.WebSocket.OPEN) {
33
+ if (this.ws.readyState !== WebSocket.OPEN) {
33
34
  await _async.once.call(void 0, this.emitter, READY, { timeout: 3e4 });
34
35
  }
35
36
  return this.ws;
36
37
  }
37
- __init4() {this.handleDisconnect = async () => {
38
+ __init5() {this.handleDisconnect = async () => {
38
39
  this.emitter.dispatchEvent(new Event(DISCONNECT));
39
- this.requestMap.forEach((request) => {
40
+ this.inFlightRequestMap.forEach((request) => {
40
41
  request.reject(new Error("disconnected"));
41
42
  });
42
- this.requestMap.clear();
43
+ this.inFlightRequestMap.clear();
44
+ if (!this.shouldConnect) return;
43
45
  const backoff = 250 * 2 ** this.reconnectAttempts;
44
46
  await _async.sleep.call(void 0, backoff);
45
47
  await this.connect().catch(() => {
46
48
  this.reconnectAttempts = Math.min(this.reconnectAttempts + 1, 6);
47
49
  });
48
50
  }}
49
- __init5() {this.handleMessage = (data) => {
51
+ __init6() {this.handleMessage = (data) => {
50
52
  const parsedData = JSON.parse(data.data);
51
- const response = _commoncjs.rpcResponse.safeParse(parsedData);
52
- if (!response.success)
53
- return;
54
- const { id } = response.data;
55
- _optionalChain([this, 'access', _6 => _6.requestMap, 'access', _7 => _7.get, 'call', _8 => _8(id), 'optionalAccess', _9 => _9.resolve, 'call', _10 => _10(response.data)]);
53
+ const responses = _zod.z.array(_commoncjs.rpcResponse).safeParse(parsedData);
54
+ if (!responses.success) return;
55
+ for (const response of responses.data) {
56
+ const { id } = response;
57
+ _optionalChain([this, 'access', _6 => _6.inFlightRequestMap, 'access', _7 => _7.get, 'call', _8 => _8(id), 'optionalAccess', _9 => _9.resolve, 'call', _10 => _10(response)]);
58
+ }
56
59
  }}
57
60
  async connect() {
58
- const socket = new this.WebSocket(this.url);
61
+ this.shouldConnect = true;
62
+ this.emitter.dispatchEvent(new Event(CONNECTING));
63
+ const socket = new WebSocket(this.url);
59
64
  this.ws = socket;
60
65
  this.ws.addEventListener("message", this.handleMessage);
66
+ let connected = false;
67
+ const handleConnectionError = () => {
68
+ if (!connected) {
69
+ void this.handleDisconnect();
70
+ }
71
+ };
61
72
  this.ws.addEventListener("close", this.handleDisconnect, { once: true });
62
- await _async.once.call(void 0, this.ws, "open", { timeout: 3e4 });
73
+ this.ws.addEventListener("error", handleConnectionError, { once: true });
74
+ await _async.once.call(void 0, this.ws, "open", { timeout: this.timeout });
75
+ connected = true;
63
76
  this.ws.addEventListener("error", () => {
64
77
  socket.close();
65
78
  });
66
79
  this.emitter.dispatchEvent(new Event(READY));
80
+ this.ws.removeEventListener("error", handleConnectionError);
67
81
  this.reconnectAttempts = 0;
68
82
  return socket;
69
83
  }
70
- async send(requests) {
71
- const responses = [];
72
- for (const data of requests) {
73
- const MAX_RETRIES = 5;
74
- for (let i = 0; i < MAX_RETRIES; i += 1) {
75
- let socket;
76
- try {
77
- socket = await this.connectionReady();
78
- } catch (e) {
79
- continue;
80
- }
81
- socket.send(JSON.stringify(data));
82
- const request = _async.deferredPromise.call(void 0, );
83
- this.requestMap.set(data.id, request);
84
- const controller = new AbortController();
85
- const result = await Promise.race([
86
- _async.sleep.call(void 0, 3e4, { signal: controller.signal }).then(
87
- () => ({ success: false, retry: false, error: new Error("timeout") })
84
+ async send(requests, requestMap) {
85
+ const MAX_RETRIES = 5;
86
+ let socket;
87
+ for (let i = 0; i < MAX_RETRIES; i += 1) {
88
+ try {
89
+ socket = await this.connectionReady();
90
+ } catch (e) {
91
+ continue;
92
+ }
93
+ }
94
+ if (!socket) {
95
+ this.handleErrorResponse(new Error("failed to connect"), requestMap);
96
+ return;
97
+ }
98
+ socket.send(JSON.stringify(requests));
99
+ const promises = [];
100
+ for (const [id] of requestMap) {
101
+ const result = _async.deferredPromise.call(void 0, );
102
+ const controller = new AbortController();
103
+ this.inFlightRequestMap.set(id, result);
104
+ promises.push(
105
+ Promise.race([
106
+ _async.sleep.call(void 0, this.timeout, { signal: controller.signal }).then(
107
+ () => ({ id, success: false, error: new Error("timeout") })
88
108
  ),
89
- request.promise.then(
90
- (result2) => ({ success: true, result: result2 }),
91
- (error) => ({ success: false, error, retry: true })
109
+ result.promise.then(
110
+ (r) => ({ id, success: true, result: r }),
111
+ (error) => ({ id, success: false, error })
92
112
  )
93
- ]).finally(() => {
94
- this.requestMap.delete(data.id);
113
+ ]).then((response) => {
114
+ this.handleResponse(response, requestMap);
115
+ }).finally(() => {
116
+ this.inFlightRequestMap.delete(id);
95
117
  controller.abort();
96
- });
97
- if (result.success || !result.retry) {
98
- responses.push({ ...result, id: data.id });
99
- break;
100
- }
101
- if (i === MAX_RETRIES - 1) {
102
- responses.push({
103
- success: false,
104
- error: new Error("max retries exceeded"),
105
- id: data.id
106
- });
107
- }
108
- }
118
+ })
119
+ );
109
120
  }
110
- return responses;
121
+ await Promise.all(promises);
111
122
  }
112
123
  }, _class);
113
124
 
@@ -1,23 +1,26 @@
1
- import Client, { Response } from './Client.cjs';
2
- import { RpcMethod, JsonRpcRequest } from './common.cjs';
1
+ import Client, { RequestMap } from './Client.cjs';
2
+ import { JsonRpcRequest, RpcMethod } from './common.cjs';
3
+ import '@chainflip/utils/async';
3
4
  import '@chainflip/utils/types';
4
5
  import 'zod';
5
6
  import './parsers.cjs';
6
7
 
7
8
  declare class WsClient extends Client {
8
- private readonly WebSocket;
9
9
  private ws?;
10
10
  private reconnectAttempts;
11
11
  private emitter;
12
- private requestMap;
13
- constructor(url: string, WebSocket?: typeof globalThis.WebSocket);
12
+ private inFlightRequestMap;
13
+ private readonly timeout;
14
+ private shouldConnect;
15
+ constructor(url: string, { timeout }?: {
16
+ timeout?: number;
17
+ });
14
18
  close(): Promise<void>;
15
- private handleClose;
16
19
  private connectionReady;
17
20
  private handleDisconnect;
18
21
  private handleMessage;
19
22
  private connect;
20
- protected send<const T extends RpcMethod>(requests: JsonRpcRequest<T>[]): Promise<Response[]>;
23
+ protected send(requests: JsonRpcRequest<RpcMethod>[], requestMap: RequestMap): Promise<void>;
21
24
  }
22
25
 
23
26
  export { WsClient as default };
@@ -1,23 +1,26 @@
1
- import Client, { Response } from './Client.js';
2
- import { RpcMethod, JsonRpcRequest } from './common.js';
1
+ import Client, { RequestMap } from './Client.js';
2
+ import { JsonRpcRequest, RpcMethod } from './common.js';
3
+ import '@chainflip/utils/async';
3
4
  import '@chainflip/utils/types';
4
5
  import 'zod';
5
6
  import './parsers.js';
6
7
 
7
8
  declare class WsClient extends Client {
8
- private readonly WebSocket;
9
9
  private ws?;
10
10
  private reconnectAttempts;
11
11
  private emitter;
12
- private requestMap;
13
- constructor(url: string, WebSocket?: typeof globalThis.WebSocket);
12
+ private inFlightRequestMap;
13
+ private readonly timeout;
14
+ private shouldConnect;
15
+ constructor(url: string, { timeout }?: {
16
+ timeout?: number;
17
+ });
14
18
  close(): Promise<void>;
15
- private handleClose;
16
19
  private connectionReady;
17
20
  private handleDisconnect;
18
21
  private handleMessage;
19
22
  private connect;
20
- protected send<const T extends RpcMethod>(requests: JsonRpcRequest<T>[]): Promise<Response[]>;
23
+ protected send(requests: JsonRpcRequest<RpcMethod>[], requestMap: RequestMap): Promise<void>;
21
24
  }
22
25
 
23
26
  export { WsClient as default };
package/dist/WsClient.mjs CHANGED
@@ -1,45 +1,47 @@
1
1
  // src/WsClient.ts
2
2
  import { deferredPromise, once, sleep } from "@chainflip/utils/async";
3
+ import { z } from "zod";
3
4
  import Client from "./Client.mjs";
4
5
  import { rpcResponse } from "./common.mjs";
6
+ var CONNECTING = "CONNECTING";
5
7
  var READY = "READY";
6
8
  var DISCONNECT = "DISCONNECT";
7
9
  var WsClient = class extends Client {
8
- constructor(url, WebSocket = globalThis.WebSocket) {
9
- super(url);
10
- this.WebSocket = WebSocket;
11
- }
12
10
  ws;
13
11
  reconnectAttempts = 0;
14
12
  emitter = new EventTarget();
15
- requestMap = /* @__PURE__ */ new Map();
16
- async close() {
17
- await this.handleClose();
13
+ inFlightRequestMap = /* @__PURE__ */ new Map();
14
+ timeout;
15
+ shouldConnect = true;
16
+ constructor(url, { timeout = 3e4 } = {}) {
17
+ super(url);
18
+ this.timeout = timeout;
18
19
  }
19
- async handleClose() {
20
- if (!this.ws)
21
- return;
22
- this.ws.removeEventListener("close", this.handleDisconnect);
20
+ async close() {
21
+ this.shouldConnect = false;
22
+ if (!this.ws) return;
23
+ await this.handleDisconnect();
24
+ const waitForClose = this.ws.readyState === WebSocket.OPEN;
23
25
  this.ws.close();
24
- if (this.ws.readyState !== this.WebSocket.CLOSED) {
25
- await once(this.ws, "close");
26
- }
26
+ if (waitForClose) await once(this.ws, "close");
27
+ this.ws = void 0;
27
28
  }
28
29
  async connectionReady() {
29
30
  if (!this.ws) {
30
31
  return this.connect();
31
32
  }
32
- if (this.ws.readyState !== this.WebSocket.OPEN) {
33
+ if (this.ws.readyState !== WebSocket.OPEN) {
33
34
  await once(this.emitter, READY, { timeout: 3e4 });
34
35
  }
35
36
  return this.ws;
36
37
  }
37
38
  handleDisconnect = async () => {
38
39
  this.emitter.dispatchEvent(new Event(DISCONNECT));
39
- this.requestMap.forEach((request) => {
40
+ this.inFlightRequestMap.forEach((request) => {
40
41
  request.reject(new Error("disconnected"));
41
42
  });
42
- this.requestMap.clear();
43
+ this.inFlightRequestMap.clear();
44
+ if (!this.shouldConnect) return;
43
45
  const backoff = 250 * 2 ** this.reconnectAttempts;
44
46
  await sleep(backoff);
45
47
  await this.connect().catch(() => {
@@ -48,66 +50,75 @@ var WsClient = class extends Client {
48
50
  };
49
51
  handleMessage = (data) => {
50
52
  const parsedData = JSON.parse(data.data);
51
- const response = rpcResponse.safeParse(parsedData);
52
- if (!response.success)
53
- return;
54
- const { id } = response.data;
55
- this.requestMap.get(id)?.resolve(response.data);
53
+ const responses = z.array(rpcResponse).safeParse(parsedData);
54
+ if (!responses.success) return;
55
+ for (const response of responses.data) {
56
+ const { id } = response;
57
+ this.inFlightRequestMap.get(id)?.resolve(response);
58
+ }
56
59
  };
57
60
  async connect() {
58
- const socket = new this.WebSocket(this.url);
61
+ this.shouldConnect = true;
62
+ this.emitter.dispatchEvent(new Event(CONNECTING));
63
+ const socket = new WebSocket(this.url);
59
64
  this.ws = socket;
60
65
  this.ws.addEventListener("message", this.handleMessage);
66
+ let connected = false;
67
+ const handleConnectionError = () => {
68
+ if (!connected) {
69
+ void this.handleDisconnect();
70
+ }
71
+ };
61
72
  this.ws.addEventListener("close", this.handleDisconnect, { once: true });
62
- await once(this.ws, "open", { timeout: 3e4 });
73
+ this.ws.addEventListener("error", handleConnectionError, { once: true });
74
+ await once(this.ws, "open", { timeout: this.timeout });
75
+ connected = true;
63
76
  this.ws.addEventListener("error", () => {
64
77
  socket.close();
65
78
  });
66
79
  this.emitter.dispatchEvent(new Event(READY));
80
+ this.ws.removeEventListener("error", handleConnectionError);
67
81
  this.reconnectAttempts = 0;
68
82
  return socket;
69
83
  }
70
- async send(requests) {
71
- const responses = [];
72
- for (const data of requests) {
73
- const MAX_RETRIES = 5;
74
- for (let i = 0; i < MAX_RETRIES; i += 1) {
75
- let socket;
76
- try {
77
- socket = await this.connectionReady();
78
- } catch {
79
- continue;
80
- }
81
- socket.send(JSON.stringify(data));
82
- const request = deferredPromise();
83
- this.requestMap.set(data.id, request);
84
- const controller = new AbortController();
85
- const result = await Promise.race([
86
- sleep(3e4, { signal: controller.signal }).then(
87
- () => ({ success: false, retry: false, error: new Error("timeout") })
84
+ async send(requests, requestMap) {
85
+ const MAX_RETRIES = 5;
86
+ let socket;
87
+ for (let i = 0; i < MAX_RETRIES; i += 1) {
88
+ try {
89
+ socket = await this.connectionReady();
90
+ } catch {
91
+ continue;
92
+ }
93
+ }
94
+ if (!socket) {
95
+ this.handleErrorResponse(new Error("failed to connect"), requestMap);
96
+ return;
97
+ }
98
+ socket.send(JSON.stringify(requests));
99
+ const promises = [];
100
+ for (const [id] of requestMap) {
101
+ const result = deferredPromise();
102
+ const controller = new AbortController();
103
+ this.inFlightRequestMap.set(id, result);
104
+ promises.push(
105
+ Promise.race([
106
+ sleep(this.timeout, { signal: controller.signal }).then(
107
+ () => ({ id, success: false, error: new Error("timeout") })
88
108
  ),
89
- request.promise.then(
90
- (result2) => ({ success: true, result: result2 }),
91
- (error) => ({ success: false, error, retry: true })
109
+ result.promise.then(
110
+ (r) => ({ id, success: true, result: r }),
111
+ (error) => ({ id, success: false, error })
92
112
  )
93
- ]).finally(() => {
94
- this.requestMap.delete(data.id);
113
+ ]).then((response) => {
114
+ this.handleResponse(response, requestMap);
115
+ }).finally(() => {
116
+ this.inFlightRequestMap.delete(id);
95
117
  controller.abort();
96
- });
97
- if (result.success || !result.retry) {
98
- responses.push({ ...result, id: data.id });
99
- break;
100
- }
101
- if (i === MAX_RETRIES - 1) {
102
- responses.push({
103
- success: false,
104
- error: new Error("max retries exceeded"),
105
- id: data.id
106
- });
107
- }
108
- }
118
+ })
119
+ );
109
120
  }
110
- return responses;
121
+ await Promise.all(promises);
111
122
  }
112
123
  };
113
124
  export {
package/dist/common.cjs CHANGED
@@ -1,4 +1,14 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/common.ts
2
+ var _zod = require('zod');
3
+
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
2
12
 
3
13
 
4
14
 
@@ -23,7 +33,9 @@
23
33
  var _parserscjs = require('./parsers.cjs');
24
34
 
25
35
  var rpcResult = {
26
- broker_requestSwapDepositAddress: _parserscjs.brokerRequestSwapDepositAddress,
36
+ broker_request_swap_deposit_address: _parserscjs.brokerRequestSwapDepositAddress,
37
+ broker_request_swap_parameter_encoding: _parserscjs.requestSwapParameterEncoding,
38
+ cf_request_swap_parameter_encoding: _parserscjs.requestSwapParameterEncoding,
27
39
  cf_accounts: _parserscjs.cfAccounts,
28
40
  cf_account_info: _parserscjs.cfAccountInfo,
29
41
  cf_pool_depth: _parserscjs.cfPoolDepth,
@@ -37,12 +49,23 @@ var rpcResult = {
37
49
  cf_supported_assets: _parserscjs.cfSupportedAssets,
38
50
  cf_swap_rate: _parserscjs.cfSwapRate,
39
51
  cf_swap_rate_v2: _parserscjs.cfSwapRateV2,
52
+ cf_swap_rate_v3: _parserscjs.cfSwapRateV3,
40
53
  cf_swapping_environment: _parserscjs.cfSwappingEnvironment,
41
54
  chain_getBlockHash: _parserscjs.chainGetBlockHash,
42
55
  cf_boost_pool_details: _parserscjs.cfBoostPoolDetails,
43
56
  cf_boost_pool_pending_fees: _parserscjs.cfBoostPoolPendingFees,
44
57
  state_getMetadata: _parserscjs.stateGetMetadata,
45
- state_getRuntimeVersion: _parserscjs.stateGetRuntimeVersion
58
+ state_getRuntimeVersion: _parserscjs.stateGetRuntimeVersion,
59
+ lp_total_balances: _parserscjs.lpTotalBalances,
60
+ cf_failed_call_ethereum: _parserscjs.cfFailedCallEvm.nullable(),
61
+ cf_failed_call_arbitrum: _parserscjs.cfFailedCallEvm.nullable(),
62
+ cf_authority_emission_per_block: _parserscjs.numberOrHex,
63
+ cf_epoch_duration: _zod.z.number(),
64
+ cf_auction_state: _parserscjs.cfAuctionState,
65
+ cf_flip_supply: _parserscjs.cfFlipSuppy,
66
+ cf_eth_state_chain_gateway_address: _parserscjs.ethereumAddress.nullable(),
67
+ cf_eth_key_manager_address: _parserscjs.ethereumAddress.nullable(),
68
+ cf_pool_orderbook: _parserscjs.cfPoolOrderbook
46
69
  };
47
70
 
48
71