@0xmonaco/core 0.4.1 → 0.5.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.
- package/README.md +23 -8
- package/dist/api/applications/api.d.ts +1 -2
- package/dist/api/applications/api.d.ts.map +1 -1
- package/dist/api/applications/api.js +1 -2
- package/dist/api/applications/api.js.map +1 -1
- package/dist/api/applications/index.d.ts +1 -2
- package/dist/api/applications/index.d.ts.map +1 -1
- package/dist/api/applications/index.js +1 -1
- package/dist/api/applications/index.js.map +1 -1
- package/dist/api/auth/api.d.ts +1 -1
- package/dist/api/auth/api.d.ts.map +1 -1
- package/dist/api/auth/api.js +2 -2
- package/dist/api/auth/api.js.map +1 -1
- package/dist/api/auth/index.d.ts +1 -2
- package/dist/api/auth/index.d.ts.map +1 -1
- package/dist/api/auth/index.js +1 -1
- package/dist/api/auth/index.js.map +1 -1
- package/dist/api/base.d.ts +26 -1
- package/dist/api/base.d.ts.map +1 -1
- package/dist/api/base.js +173 -43
- package/dist/api/base.js.map +1 -1
- package/dist/api/fees/api.d.ts +1 -1
- package/dist/api/fees/api.d.ts.map +1 -1
- package/dist/api/fees/api.js +4 -4
- package/dist/api/fees/api.js.map +1 -1
- package/dist/api/fees/index.d.ts +1 -1
- package/dist/api/fees/index.d.ts.map +1 -1
- package/dist/api/fees/index.js +1 -1
- package/dist/api/fees/index.js.map +1 -1
- package/dist/api/index.d.ts +10 -8
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +10 -8
- package/dist/api/index.js.map +1 -1
- package/dist/api/market/api.d.ts +1 -1
- package/dist/api/market/api.d.ts.map +1 -1
- package/dist/api/market/api.js +12 -3
- package/dist/api/market/api.js.map +1 -1
- package/dist/api/market/index.d.ts +1 -2
- package/dist/api/market/index.d.ts.map +1 -1
- package/dist/api/market/index.js +1 -1
- package/dist/api/market/index.js.map +1 -1
- package/dist/api/orderbook/api.d.ts +16 -0
- package/dist/api/orderbook/api.d.ts.map +1 -0
- package/dist/api/orderbook/api.js +38 -0
- package/dist/api/orderbook/api.js.map +1 -0
- package/dist/api/orderbook/index.d.ts +2 -0
- package/dist/api/orderbook/index.d.ts.map +1 -0
- package/dist/api/orderbook/index.js +2 -0
- package/dist/api/orderbook/index.js.map +1 -0
- package/dist/api/profile/api.d.ts +10 -11
- package/dist/api/profile/api.d.ts.map +1 -1
- package/dist/api/profile/api.js +17 -11
- package/dist/api/profile/api.js.map +1 -1
- package/dist/api/profile/index.d.ts +1 -1
- package/dist/api/profile/index.d.ts.map +1 -1
- package/dist/api/profile/index.js +1 -1
- package/dist/api/profile/index.js.map +1 -1
- package/dist/api/trades/api.d.ts +45 -0
- package/dist/api/trades/api.d.ts.map +1 -0
- package/dist/api/trades/api.js +43 -0
- package/dist/api/trades/api.js.map +1 -0
- package/dist/api/trades/index.d.ts +2 -0
- package/dist/api/trades/index.d.ts.map +1 -0
- package/dist/api/trades/index.js +2 -0
- package/dist/api/trades/index.js.map +1 -0
- package/dist/api/trading/api.d.ts +1 -2
- package/dist/api/trading/api.d.ts.map +1 -1
- package/dist/api/trading/api.js +10 -2
- package/dist/api/trading/api.js.map +1 -1
- package/dist/api/trading/index.d.ts +1 -2
- package/dist/api/trading/index.d.ts.map +1 -1
- package/dist/api/trading/index.js +1 -1
- package/dist/api/trading/index.js.map +1 -1
- package/dist/api/vault/api.d.ts +17 -19
- package/dist/api/vault/api.d.ts.map +1 -1
- package/dist/api/vault/api.js +58 -46
- package/dist/api/vault/api.js.map +1 -1
- package/dist/api/vault/index.d.ts +1 -2
- package/dist/api/vault/index.d.ts.map +1 -1
- package/dist/api/vault/index.js +1 -1
- package/dist/api/vault/index.js.map +1 -1
- package/dist/api/websocket/index.d.ts +3 -9
- package/dist/api/websocket/index.d.ts.map +1 -1
- package/dist/api/websocket/index.js +3 -9
- package/dist/api/websocket/index.js.map +1 -1
- package/dist/api/websocket/types.d.ts +34 -0
- package/dist/api/websocket/types.d.ts.map +1 -0
- package/dist/api/websocket/types.js +2 -0
- package/dist/api/websocket/types.js.map +1 -0
- package/dist/api/websocket/utils.d.ts +9 -0
- package/dist/api/websocket/utils.d.ts.map +1 -0
- package/dist/api/websocket/utils.js +23 -0
- package/dist/api/websocket/utils.js.map +1 -0
- package/dist/api/websocket/websocket.d.ts +6 -0
- package/dist/api/websocket/websocket.d.ts.map +1 -0
- package/dist/api/websocket/websocket.js +309 -0
- package/dist/api/websocket/websocket.js.map +1 -0
- package/dist/errors/errors.d.ts +382 -0
- package/dist/errors/errors.d.ts.map +1 -0
- package/dist/errors/errors.js +801 -0
- package/dist/errors/errors.js.map +1 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +5 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -22
- package/dist/index.js.map +1 -1
- package/dist/networks/index.d.ts +2 -0
- package/dist/networks/index.d.ts.map +1 -0
- package/dist/networks/index.js +2 -0
- package/dist/networks/index.js.map +1 -0
- package/dist/{networks.d.ts → networks/networks.d.ts} +1 -1
- package/dist/networks/networks.d.ts.map +1 -0
- package/dist/{networks.js → networks/networks.js} +4 -3
- package/dist/networks/networks.js.map +1 -0
- package/dist/sdk.d.ts +19 -27
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +77 -107
- package/dist/sdk.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -1
- package/package.json +5 -7
- package/dist/api/websocket/base-ws-client.d.ts +0 -85
- package/dist/api/websocket/base-ws-client.d.ts.map +0 -1
- package/dist/api/websocket/base-ws-client.js +0 -256
- package/dist/api/websocket/base-ws-client.js.map +0 -1
- package/dist/api/websocket/clients/ohlcv-ws-client.d.ts +0 -57
- package/dist/api/websocket/clients/ohlcv-ws-client.d.ts.map +0 -1
- package/dist/api/websocket/clients/ohlcv-ws-client.js +0 -132
- package/dist/api/websocket/clients/ohlcv-ws-client.js.map +0 -1
- package/dist/api/websocket/clients/orderbook-ws-client.d.ts +0 -70
- package/dist/api/websocket/clients/orderbook-ws-client.d.ts.map +0 -1
- package/dist/api/websocket/clients/orderbook-ws-client.js +0 -197
- package/dist/api/websocket/clients/orderbook-ws-client.js.map +0 -1
- package/dist/api/websocket/clients/orders-ws-client.d.ts +0 -61
- package/dist/api/websocket/clients/orders-ws-client.d.ts.map +0 -1
- package/dist/api/websocket/clients/orders-ws-client.js +0 -150
- package/dist/api/websocket/clients/orders-ws-client.js.map +0 -1
- package/dist/errors.d.ts +0 -103
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -108
- package/dist/errors.js.map +0 -1
- package/dist/networks.d.ts.map +0 -1
- package/dist/networks.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xmonaco/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -12,8 +12,9 @@
|
|
|
12
12
|
"viem": "^2.31.7"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@0xmonaco/contracts": "0.
|
|
16
|
-
"@0xmonaco/types": "0.
|
|
15
|
+
"@0xmonaco/contracts": "0.5.0",
|
|
16
|
+
"@0xmonaco/types": "0.5.0",
|
|
17
|
+
"http-status-codes": "^2.3.0"
|
|
17
18
|
},
|
|
18
19
|
"devDependencies": {
|
|
19
20
|
"@types/node": "^20.11.30",
|
|
@@ -38,14 +39,11 @@
|
|
|
38
39
|
"test": "vitest run --config vitest.config.ts",
|
|
39
40
|
"test:unit": "vitest run --config tests/configs/unit.config.ts",
|
|
40
41
|
"test:integration": "vitest run --config tests/configs/integration.config.ts",
|
|
41
|
-
"test:e2e": "vitest run --config tests/configs/e2e.config.ts",
|
|
42
42
|
"test:coverage": "vitest run --coverage --config vitest.config.ts",
|
|
43
43
|
"test:coverage:unit": "vitest run --coverage --config tests/configs/unit.config.ts",
|
|
44
44
|
"test:coverage:integration": "vitest run --coverage --config tests/configs/integration.config.ts",
|
|
45
45
|
"test:watch": "vitest --config vitest.config.ts --watch",
|
|
46
46
|
"test:watch:unit": "vitest --config tests/configs/unit.config.ts --watch",
|
|
47
|
-
"test:watch:integration": "vitest --config tests/configs/integration.config.ts --watch"
|
|
48
|
-
"mint:tokens": "tsx tests/scripts/mint-test-tokens.ts",
|
|
49
|
-
"check:balances": "tsx tests/debug/check-balances.ts"
|
|
47
|
+
"test:watch:integration": "vitest --config tests/configs/integration.config.ts --watch"
|
|
50
48
|
}
|
|
51
49
|
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base WebSocket Client
|
|
3
|
-
*
|
|
4
|
-
* Production-ready WebSocket client with:
|
|
5
|
-
* - Message queuing and retry logic
|
|
6
|
-
* - Heartbeat/ping-pong for connection health
|
|
7
|
-
* - Exponential backoff reconnection
|
|
8
|
-
* - Event-driven error handling
|
|
9
|
-
* - Optional JWT authentication via query parameters (only added if accessToken is provided)
|
|
10
|
-
*
|
|
11
|
-
* Supports both public channels (no authentication) and private channels (requires JWT).
|
|
12
|
-
*/
|
|
13
|
-
import { type BaseWebSocketClient, type ConnectionStatus, type RawEventMessage, type WebSocketChannel, type WebSocketConfig } from "@0xmonaco/types";
|
|
14
|
-
/**
|
|
15
|
-
* Base WebSocket client implementation for Monaco Protocol
|
|
16
|
-
*
|
|
17
|
-
* This client supports both authenticated and unauthenticated connections.
|
|
18
|
-
* If an accessToken is provided in the config, it will be included as a query parameter.
|
|
19
|
-
* If no accessToken is provided, the connection will be made without authentication (public channels).
|
|
20
|
-
*/
|
|
21
|
-
export declare class BaseWebSocketClientImpl implements BaseWebSocketClient {
|
|
22
|
-
protected static readonly CONNECTION_TIMEOUT = 10000;
|
|
23
|
-
protected static readonly HEARTBEAT_INTERVAL = 15000;
|
|
24
|
-
protected static readonly HEARTBEAT_TIMEOUT = 30000;
|
|
25
|
-
protected static readonly MAX_RECONNECT_DELAY = 30000;
|
|
26
|
-
protected ws: WebSocket | null;
|
|
27
|
-
protected config: WebSocketConfig;
|
|
28
|
-
protected connectionStatus: ConnectionStatus;
|
|
29
|
-
protected reconnectAttempts: number;
|
|
30
|
-
protected reconnectTimer: NodeJS.Timeout | null;
|
|
31
|
-
protected messageQueue: unknown[];
|
|
32
|
-
protected isAuthenticated: boolean;
|
|
33
|
-
protected heartbeatInterval: NodeJS.Timeout | null;
|
|
34
|
-
protected lastPong: number;
|
|
35
|
-
constructor(config: WebSocketConfig);
|
|
36
|
-
/**
|
|
37
|
-
* Connect to the WebSocket server.
|
|
38
|
-
* For public channels (OHLCV, Orderbook), authentication is optional.
|
|
39
|
-
* For private channels (Orders), authentication is required - call setAccessToken() first.
|
|
40
|
-
*/
|
|
41
|
-
connect(): Promise<void>;
|
|
42
|
-
/** Disconnect from the WebSocket server */
|
|
43
|
-
disconnect(): void;
|
|
44
|
-
/** Get current connection status */
|
|
45
|
-
getConnectionStatus(): ConnectionStatus;
|
|
46
|
-
/** Check if currently connected */
|
|
47
|
-
isConnected(): boolean;
|
|
48
|
-
/** Set the access token for authentication */
|
|
49
|
-
setAccessToken(token: string): void;
|
|
50
|
-
/**
|
|
51
|
-
* Send message (queues if not connected).
|
|
52
|
-
* Messages are queued if the WebSocket is not in OPEN state, regardless of authentication.
|
|
53
|
-
* This allows both authenticated and public channels to queue messages until connected.
|
|
54
|
-
*/
|
|
55
|
-
send(data: unknown): void;
|
|
56
|
-
/** Flush all queued messages */
|
|
57
|
-
protected flushMessageQueue(): void;
|
|
58
|
-
/** Start heartbeat ping/pong */
|
|
59
|
-
protected startHeartbeat(): void;
|
|
60
|
-
/** Stop heartbeat interval */
|
|
61
|
-
protected stopHeartbeat(): void;
|
|
62
|
-
/** Handle connection errors with cleanup */
|
|
63
|
-
protected handleConnectionError(error: unknown): void;
|
|
64
|
-
/** Handle incoming WebSocket messages - can be overridden by subclasses */
|
|
65
|
-
protected handleMessage(message: RawEventMessage): void;
|
|
66
|
-
/**
|
|
67
|
-
* Helper method to validate WebSocket event messages using WebSocketChannel enum
|
|
68
|
-
* @param message - The message to validate
|
|
69
|
-
* @param channelType - The expected channel type (WebSocketChannel.Orders, WebSocketChannel.Orderbook, etc.)
|
|
70
|
-
* @returns True if the message is a valid event with the specified channel type
|
|
71
|
-
*/
|
|
72
|
-
protected isEventMessage(message: unknown, channelType: WebSocketChannel): boolean;
|
|
73
|
-
/**
|
|
74
|
-
* Create WebSocket connection with event handlers.
|
|
75
|
-
*
|
|
76
|
-
* If accessToken is provided in config, it will be appended as a query parameter.
|
|
77
|
-
* If no accessToken is provided, connection will be made without authentication (public channels).
|
|
78
|
-
*/
|
|
79
|
-
protected createConnection(): Promise<void>;
|
|
80
|
-
/** Schedule reconnect with exponential backoff */
|
|
81
|
-
protected scheduleReconnect(): void;
|
|
82
|
-
/** Stop reconnection timer */
|
|
83
|
-
protected stopReconnectTimer(): void;
|
|
84
|
-
}
|
|
85
|
-
//# sourceMappingURL=base-ws-client.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base-ws-client.d.ts","sourceRoot":"","sources":["../../../src/api/websocket/base-ws-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EAErB,MAAM,iBAAiB,CAAC;AAEzB;;;;;;GAMG;AACH,qBAAa,uBAAwB,YAAW,mBAAmB;IAEjE,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,SAAS;IACrD,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,SAAS;IACrD,SAAS,CAAC,MAA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C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,gBAAgB,GAAG,OAAO;IAgBlF;;;;;OAKG;cACa,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;IA8DjD,kDAAkD;IAClD,SAAS,CAAC,iBAAiB,IAAI,IAAI;IAuBnC,8BAA8B;IAC9B,SAAS,CAAC,kBAAkB,IAAI,IAAI;CAMrC"}
|
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Base WebSocket Client
|
|
3
|
-
*
|
|
4
|
-
* Production-ready WebSocket client with:
|
|
5
|
-
* - Message queuing and retry logic
|
|
6
|
-
* - Heartbeat/ping-pong for connection health
|
|
7
|
-
* - Exponential backoff reconnection
|
|
8
|
-
* - Event-driven error handling
|
|
9
|
-
* - Optional JWT authentication via query parameters (only added if accessToken is provided)
|
|
10
|
-
*
|
|
11
|
-
* Supports both public channels (no authentication) and private channels (requires JWT).
|
|
12
|
-
*/
|
|
13
|
-
import { WebSocketStatus, } from "@0xmonaco/types";
|
|
14
|
-
/**
|
|
15
|
-
* Base WebSocket client implementation for Monaco Protocol
|
|
16
|
-
*
|
|
17
|
-
* This client supports both authenticated and unauthenticated connections.
|
|
18
|
-
* If an accessToken is provided in the config, it will be included as a query parameter.
|
|
19
|
-
* If no accessToken is provided, the connection will be made without authentication (public channels).
|
|
20
|
-
*/
|
|
21
|
-
export class BaseWebSocketClientImpl {
|
|
22
|
-
constructor(config) {
|
|
23
|
-
// Core WebSocket properties
|
|
24
|
-
this.ws = null;
|
|
25
|
-
this.connectionStatus = WebSocketStatus.Disconnected;
|
|
26
|
-
// Reconnection management
|
|
27
|
-
this.reconnectAttempts = 0;
|
|
28
|
-
this.reconnectTimer = null;
|
|
29
|
-
// Message handling
|
|
30
|
-
this.messageQueue = [];
|
|
31
|
-
this.isAuthenticated = false;
|
|
32
|
-
// Heartbeat management
|
|
33
|
-
this.heartbeatInterval = null;
|
|
34
|
-
this.lastPong = Date.now();
|
|
35
|
-
this.config = {
|
|
36
|
-
autoReconnect: true,
|
|
37
|
-
reconnectDelay: 5000,
|
|
38
|
-
maxReconnectAttempts: 5,
|
|
39
|
-
accessToken: "",
|
|
40
|
-
...config,
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Connect to the WebSocket server.
|
|
45
|
-
* For public channels (OHLCV, Orderbook), authentication is optional.
|
|
46
|
-
* For private channels (Orders), authentication is required - call setAccessToken() first.
|
|
47
|
-
*/
|
|
48
|
-
async connect() {
|
|
49
|
-
try {
|
|
50
|
-
if (this.ws) {
|
|
51
|
-
this.ws.close(1000, "Reconnecting");
|
|
52
|
-
this.ws = null;
|
|
53
|
-
}
|
|
54
|
-
if (this.connectionStatus === WebSocketStatus.Connecting || this.connectionStatus === WebSocketStatus.Connected) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
// No validation here - let each specific client decide if auth is required
|
|
58
|
-
// Public channels (OHLCV, Orderbook) work without authentication
|
|
59
|
-
// Private channels (Orders) should validate in their own implementation if needed
|
|
60
|
-
this.connectionStatus = WebSocketStatus.Connecting;
|
|
61
|
-
await this.createConnection();
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
this.handleConnectionError(error);
|
|
65
|
-
throw error;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/** Disconnect from the WebSocket server */
|
|
69
|
-
disconnect() {
|
|
70
|
-
this.config.autoReconnect = false;
|
|
71
|
-
this.stopReconnectTimer();
|
|
72
|
-
this.stopHeartbeat();
|
|
73
|
-
if (this.ws) {
|
|
74
|
-
this.ws.close();
|
|
75
|
-
this.ws = null;
|
|
76
|
-
}
|
|
77
|
-
this.connectionStatus = WebSocketStatus.Disconnected;
|
|
78
|
-
this.isAuthenticated = false;
|
|
79
|
-
}
|
|
80
|
-
/** Get current connection status */
|
|
81
|
-
getConnectionStatus() {
|
|
82
|
-
return this.connectionStatus;
|
|
83
|
-
}
|
|
84
|
-
/** Check if currently connected */
|
|
85
|
-
isConnected() {
|
|
86
|
-
return this.connectionStatus === "connected";
|
|
87
|
-
}
|
|
88
|
-
/** Set the access token for authentication */
|
|
89
|
-
setAccessToken(token) {
|
|
90
|
-
this.config.accessToken = token;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Send message (queues if not connected).
|
|
94
|
-
* Messages are queued if the WebSocket is not in OPEN state, regardless of authentication.
|
|
95
|
-
* This allows both authenticated and public channels to queue messages until connected.
|
|
96
|
-
*/
|
|
97
|
-
send(data) {
|
|
98
|
-
if (this.ws?.readyState !== WebSocket.OPEN) {
|
|
99
|
-
this.messageQueue.push(data);
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
this.ws.send(JSON.stringify(data));
|
|
103
|
-
}
|
|
104
|
-
/** Flush all queued messages */
|
|
105
|
-
flushMessageQueue() {
|
|
106
|
-
while (this.messageQueue.length > 0) {
|
|
107
|
-
const message = this.messageQueue.shift();
|
|
108
|
-
this.send(message);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
/** Start heartbeat ping/pong */
|
|
112
|
-
startHeartbeat() {
|
|
113
|
-
this.heartbeatInterval = setInterval(() => {
|
|
114
|
-
if (Date.now() - this.lastPong > BaseWebSocketClientImpl.HEARTBEAT_TIMEOUT) {
|
|
115
|
-
this.ws?.close();
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
this.ws?.send(JSON.stringify({ type: "Ping" }));
|
|
119
|
-
}, BaseWebSocketClientImpl.HEARTBEAT_INTERVAL);
|
|
120
|
-
}
|
|
121
|
-
/** Stop heartbeat interval */
|
|
122
|
-
stopHeartbeat() {
|
|
123
|
-
if (this.heartbeatInterval) {
|
|
124
|
-
clearInterval(this.heartbeatInterval);
|
|
125
|
-
this.heartbeatInterval = null;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/** Handle connection errors with cleanup */
|
|
129
|
-
handleConnectionError(error) {
|
|
130
|
-
this.connectionStatus = WebSocketStatus.Disconnected;
|
|
131
|
-
this.isAuthenticated = false;
|
|
132
|
-
this.stopHeartbeat();
|
|
133
|
-
console.warn("WebSocket connection error:", error);
|
|
134
|
-
}
|
|
135
|
-
/** Handle incoming WebSocket messages - can be overridden by subclasses */
|
|
136
|
-
handleMessage(message) {
|
|
137
|
-
if (message.type === "Pong") {
|
|
138
|
-
this.lastPong = Date.now();
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Helper method to validate WebSocket event messages using WebSocketChannel enum
|
|
144
|
-
* @param message - The message to validate
|
|
145
|
-
* @param channelType - The expected channel type (WebSocketChannel.Orders, WebSocketChannel.Orderbook, etc.)
|
|
146
|
-
* @returns True if the message is a valid event with the specified channel type
|
|
147
|
-
*/
|
|
148
|
-
isEventMessage(message, channelType) {
|
|
149
|
-
if (!message || typeof message !== "object") {
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
const msg = message;
|
|
153
|
-
return (msg.type === "Event" &&
|
|
154
|
-
typeof msg.channel === "string" &&
|
|
155
|
-
msg.channel.startsWith(`${channelType}:`) &&
|
|
156
|
-
msg.data !== null &&
|
|
157
|
-
msg.data !== undefined &&
|
|
158
|
-
typeof msg.data === "object");
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Create WebSocket connection with event handlers.
|
|
162
|
-
*
|
|
163
|
-
* If accessToken is provided in config, it will be appended as a query parameter.
|
|
164
|
-
* If no accessToken is provided, connection will be made without authentication (public channels).
|
|
165
|
-
*/
|
|
166
|
-
async createConnection() {
|
|
167
|
-
return new Promise((resolve, reject) => {
|
|
168
|
-
try {
|
|
169
|
-
const url = new URL(this.config.wsUrl);
|
|
170
|
-
// Only add authentication token if provided (optional for public channels)
|
|
171
|
-
if (this.config.accessToken) {
|
|
172
|
-
url.searchParams.set("token", this.config.accessToken);
|
|
173
|
-
}
|
|
174
|
-
this.ws = new WebSocket(url.toString());
|
|
175
|
-
const connectionTimeout = setTimeout(() => {
|
|
176
|
-
if (this.ws && this.ws.readyState === WebSocket.CONNECTING) {
|
|
177
|
-
this.ws.close();
|
|
178
|
-
reject(new Error("WebSocket connection timeout"));
|
|
179
|
-
}
|
|
180
|
-
}, BaseWebSocketClientImpl.CONNECTION_TIMEOUT);
|
|
181
|
-
this.ws.onopen = () => {
|
|
182
|
-
clearTimeout(connectionTimeout);
|
|
183
|
-
this.connectionStatus = WebSocketStatus.Connected;
|
|
184
|
-
this.reconnectAttempts = 0;
|
|
185
|
-
this.isAuthenticated = !!this.config.accessToken;
|
|
186
|
-
this.lastPong = Date.now();
|
|
187
|
-
this.startHeartbeat();
|
|
188
|
-
this.flushMessageQueue();
|
|
189
|
-
resolve();
|
|
190
|
-
};
|
|
191
|
-
this.ws.onmessage = (event) => {
|
|
192
|
-
try {
|
|
193
|
-
const message = JSON.parse(event.data);
|
|
194
|
-
this.handleMessage(message);
|
|
195
|
-
}
|
|
196
|
-
catch (error) {
|
|
197
|
-
console.warn("Failed to parse WebSocket message:", error);
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
this.ws.onclose = (event) => {
|
|
201
|
-
clearTimeout(connectionTimeout);
|
|
202
|
-
this.connectionStatus = WebSocketStatus.Disconnected;
|
|
203
|
-
this.isAuthenticated = false;
|
|
204
|
-
this.stopHeartbeat();
|
|
205
|
-
if (this.config.autoReconnect && this.reconnectAttempts < (this.config.maxReconnectAttempts || 5) && event.code !== 1000) {
|
|
206
|
-
this.scheduleReconnect();
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
this.ws.onerror = () => {
|
|
210
|
-
clearTimeout(connectionTimeout);
|
|
211
|
-
if (this.connectionStatus === "connecting") {
|
|
212
|
-
reject(new Error("Failed to connect to WebSocket"));
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
catch (error) {
|
|
217
|
-
this.connectionStatus = WebSocketStatus.Disconnected;
|
|
218
|
-
reject(error);
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
/** Schedule reconnect with exponential backoff */
|
|
223
|
-
scheduleReconnect() {
|
|
224
|
-
this.connectionStatus = WebSocketStatus.Reconnecting;
|
|
225
|
-
this.reconnectAttempts++;
|
|
226
|
-
const baseDelay = this.config.reconnectDelay || 5000;
|
|
227
|
-
const exponentialDelay = Math.min(baseDelay * 2 ** (this.reconnectAttempts - 1), BaseWebSocketClientImpl.MAX_RECONNECT_DELAY);
|
|
228
|
-
this.reconnectTimer = setTimeout(async () => {
|
|
229
|
-
try {
|
|
230
|
-
if (!this.config.accessToken?.trim()) {
|
|
231
|
-
console.error("JWT token expired or invalid, cannot reconnect");
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
await this.connect();
|
|
235
|
-
}
|
|
236
|
-
catch (_error) {
|
|
237
|
-
if (this.reconnectAttempts >= (this.config.maxReconnectAttempts || 5)) {
|
|
238
|
-
console.error("Failed to reconnect after maximum attempts");
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}, exponentialDelay);
|
|
242
|
-
}
|
|
243
|
-
/** Stop reconnection timer */
|
|
244
|
-
stopReconnectTimer() {
|
|
245
|
-
if (this.reconnectTimer) {
|
|
246
|
-
clearTimeout(this.reconnectTimer);
|
|
247
|
-
this.reconnectTimer = null;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
// Connection constants
|
|
252
|
-
BaseWebSocketClientImpl.CONNECTION_TIMEOUT = 10000; // 10 seconds
|
|
253
|
-
BaseWebSocketClientImpl.HEARTBEAT_INTERVAL = 15000; // 15 seconds
|
|
254
|
-
BaseWebSocketClientImpl.HEARTBEAT_TIMEOUT = 30000; // 30 seconds
|
|
255
|
-
BaseWebSocketClientImpl.MAX_RECONNECT_DELAY = 30000; // 30 seconds
|
|
256
|
-
//# sourceMappingURL=base-ws-client.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"base-ws-client.js","sour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I,IAAI,CAAC,iBAAiB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,oBAAoB,IAAI,CAAC,CAAC,EAAE,CAAC;oBACtE,OAAO,CAAC,KAAK,CAAC,4CAA4C,CAAC,CAAC;gBAC9D,CAAC;YACH,CAAC;QACH,CAAC,EAAE,gBAAgB,CAAC,CAAC;IACvB,CAAC;IAED,8BAA8B;IACpB,kBAAkB;QAC1B,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxB,YAAY,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;YAClC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;QAC7B,CAAC;IACH,CAAC;;AA5QD,uBAAuB;AACG,0CAAkB,GAAG,KAAK,AAAR,CAAS,CAAC,aAAa;AACzC,0CAAkB,GAAG,KAAK,AAAR,CAAS,CAAC,aAAa;AACzC,yCAAiB,GAAG,KAAK,AAAR,CAAS,CAAC,aAAa;AACxC,2CAAmB,GAAG,KAAK,AAAR,CAAS,CAAC,aAAa"}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OHLCV WebSocket Client Implementation
|
|
3
|
-
*
|
|
4
|
-
* PUBLIC WebSocket client for OHLCV (Open, High, Low, Close, Volume) candlestick data events.
|
|
5
|
-
* No authentication required - provides public market candlestick data.
|
|
6
|
-
* Extends the base WebSocket client with OHLCV-specific functionality.
|
|
7
|
-
*/
|
|
8
|
-
import { type Interval, type OHLCVEvent, type OHLCVWebSocketClient, type TradingMode } from "@0xmonaco/types";
|
|
9
|
-
import { BaseWebSocketClientImpl } from "../base-ws-client.js";
|
|
10
|
-
/**
|
|
11
|
-
* OHLCV-specific WebSocket client implementation
|
|
12
|
-
*/
|
|
13
|
-
export declare class OHLCVWebSocketClientImpl extends BaseWebSocketClientImpl implements OHLCVWebSocketClient {
|
|
14
|
-
private ohlcvHandlers;
|
|
15
|
-
/**
|
|
16
|
-
* Subscribe to OHLCV (candlestick) updates for a specific trading pair, mode, and interval.
|
|
17
|
-
* No authentication required - provides public market data.
|
|
18
|
-
*
|
|
19
|
-
* @param symbol - Trading pair symbol (e.g., "BTC/USDC")
|
|
20
|
-
* @param tradingMode - Trading mode (TradingMode.SPOT or TradingMode.MARGIN)
|
|
21
|
-
* @param interval - Candlestick interval (e.g., "1m", "5m", "1h", "1d")
|
|
22
|
-
* @param callback - Function to handle incoming OHLCV events
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```typescript
|
|
26
|
-
* // No authentication required
|
|
27
|
-
* await ohlcvWS.connect();
|
|
28
|
-
*
|
|
29
|
-
* ohlcvWS.subscribeToOHLCV("BTC/USDC", TradingMode.SPOT, "1m", (event) => {
|
|
30
|
-
* console.log("New OHLCV data:", event.candlestick.o, event.candlestick.c);
|
|
31
|
-
* });
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
subscribeToOHLCV(symbol: string, tradingMode: TradingMode, interval: Interval, callback: (event: OHLCVEvent) => void): void;
|
|
35
|
-
/**
|
|
36
|
-
* Unsubscribe from OHLCV (candlestick) updates.
|
|
37
|
-
*
|
|
38
|
-
* @param symbol - Trading pair symbol (e.g., "BTC/USDC")
|
|
39
|
-
* @param tradingMode - Trading mode (TradingMode.SPOT or TradingMode.MARGIN)
|
|
40
|
-
* @param interval - Candlestick interval (e.g., "1m", "5m", "1h", "1d")
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* ohlcvWS.unsubscribeFromOHLCV("BTC/USDC", TradingMode.SPOT, "1m");
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
unsubscribeFromOHLCV(symbol: string, tradingMode: TradingMode, interval: Interval): void;
|
|
48
|
-
/** Override message handling to process OHLCV-specific events */
|
|
49
|
-
protected handleMessage(message: unknown): void;
|
|
50
|
-
/** Check if message is an OHLCV event from the backend */
|
|
51
|
-
private isOHLCVEvent;
|
|
52
|
-
/** Handle OHLCV-specific events from the backend */
|
|
53
|
-
private handleOHLCVEvent;
|
|
54
|
-
/** Clean up resources when disconnecting */
|
|
55
|
-
protected cleanup(): void;
|
|
56
|
-
}
|
|
57
|
-
//# sourceMappingURL=ohlcv-ws-client.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ohlcv-ws-client.d.ts","sourceRoot":"","sources":["../../../../src/api/websocket/clients/ohlcv-ws-client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EACL,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,oBAAoB,EAEzB,KAAK,WAAW,EAEjB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAE/D;;GAEG;AACH,qBAAa,wBAAyB,SAAQ,uBAAwB,YAAW,oBAAoB;IAEnG,OAAO,CAAC,aAAa,CAAkD;IAEvE;;;;;;;;;;;;;;;;;;OAkBG;IACH,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,GAAG,IAAI;IAa3H;;;;;;;;;;;OAWG;IACH,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI;IAaxF,iEAAiE;IACjE,SAAS,CAAC,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAS/C,0DAA0D;IAC1D,OAAO,CAAC,YAAY;IAIpB,oDAAoD;IACpD,OAAO,CAAC,gBAAgB;IAgDxB,4CAA4C;IAC5C,SAAS,CAAC,OAAO,IAAI,IAAI;CAG1B"}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OHLCV WebSocket Client Implementation
|
|
3
|
-
*
|
|
4
|
-
* PUBLIC WebSocket client for OHLCV (Open, High, Low, Close, Volume) candlestick data events.
|
|
5
|
-
* No authentication required - provides public market candlestick data.
|
|
6
|
-
* Extends the base WebSocket client with OHLCV-specific functionality.
|
|
7
|
-
*/
|
|
8
|
-
import { WebSocketChannel, } from "@0xmonaco/types";
|
|
9
|
-
import { BaseWebSocketClientImpl } from "../base-ws-client.js";
|
|
10
|
-
/**
|
|
11
|
-
* OHLCV-specific WebSocket client implementation
|
|
12
|
-
*/
|
|
13
|
-
export class OHLCVWebSocketClientImpl extends BaseWebSocketClientImpl {
|
|
14
|
-
constructor() {
|
|
15
|
-
super(...arguments);
|
|
16
|
-
// Map to store OHLCV event handlers by subscription key
|
|
17
|
-
this.ohlcvHandlers = new Map();
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Subscribe to OHLCV (candlestick) updates for a specific trading pair, mode, and interval.
|
|
21
|
-
* No authentication required - provides public market data.
|
|
22
|
-
*
|
|
23
|
-
* @param symbol - Trading pair symbol (e.g., "BTC/USDC")
|
|
24
|
-
* @param tradingMode - Trading mode (TradingMode.SPOT or TradingMode.MARGIN)
|
|
25
|
-
* @param interval - Candlestick interval (e.g., "1m", "5m", "1h", "1d")
|
|
26
|
-
* @param callback - Function to handle incoming OHLCV events
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* // No authentication required
|
|
31
|
-
* await ohlcvWS.connect();
|
|
32
|
-
*
|
|
33
|
-
* ohlcvWS.subscribeToOHLCV("BTC/USDC", TradingMode.SPOT, "1m", (event) => {
|
|
34
|
-
* console.log("New OHLCV data:", event.candlestick.o, event.candlestick.c);
|
|
35
|
-
* });
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
subscribeToOHLCV(symbol, tradingMode, interval, callback) {
|
|
39
|
-
const channel = `ohlcv:${symbol}:${tradingMode}:${interval}`;
|
|
40
|
-
const subscriptionKey = `${symbol.toUpperCase()}-${tradingMode.toUpperCase()}-${interval.toLowerCase()}`;
|
|
41
|
-
this.ohlcvHandlers.set(subscriptionKey, callback);
|
|
42
|
-
// Send subscription message using the backend's channel format
|
|
43
|
-
this.send({
|
|
44
|
-
type: "Subscribe",
|
|
45
|
-
channels: [channel],
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Unsubscribe from OHLCV (candlestick) updates.
|
|
50
|
-
*
|
|
51
|
-
* @param symbol - Trading pair symbol (e.g., "BTC/USDC")
|
|
52
|
-
* @param tradingMode - Trading mode (TradingMode.SPOT or TradingMode.MARGIN)
|
|
53
|
-
* @param interval - Candlestick interval (e.g., "1m", "5m", "1h", "1d")
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* ohlcvWS.unsubscribeFromOHLCV("BTC/USDC", TradingMode.SPOT, "1m");
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
unsubscribeFromOHLCV(symbol, tradingMode, interval) {
|
|
61
|
-
const channel = `ohlcv:${symbol}:${tradingMode}:${interval}`;
|
|
62
|
-
const subscriptionKey = `${symbol.toUpperCase()}-${tradingMode.toUpperCase()}-${interval.toLowerCase()}`;
|
|
63
|
-
this.ohlcvHandlers.delete(subscriptionKey);
|
|
64
|
-
// Send unsubscription message
|
|
65
|
-
this.send({
|
|
66
|
-
type: "Unsubscribe",
|
|
67
|
-
channels: [channel],
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
/** Override message handling to process OHLCV-specific events */
|
|
71
|
-
handleMessage(message) {
|
|
72
|
-
super.handleMessage(message);
|
|
73
|
-
// Handle OHLCV-specific Event messages
|
|
74
|
-
if (this.isOHLCVEvent(message)) {
|
|
75
|
-
this.handleOHLCVEvent(message);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
/** Check if message is an OHLCV event from the backend */
|
|
79
|
-
isOHLCVEvent(message) {
|
|
80
|
-
return this.isEventMessage(message, WebSocketChannel.OHLCV);
|
|
81
|
-
}
|
|
82
|
-
/** Handle OHLCV-specific events from the backend */
|
|
83
|
-
handleOHLCVEvent(event) {
|
|
84
|
-
try {
|
|
85
|
-
const msg = event;
|
|
86
|
-
const backendEvent = msg.data; // Parent (has pair, interval, trading_mode)
|
|
87
|
-
const ohlcvData = backendEvent.data; // Nested (has open, high, low, close)
|
|
88
|
-
const symbol = backendEvent.pair;
|
|
89
|
-
const tradingMode = backendEvent.trading_mode;
|
|
90
|
-
const interval = backendEvent.interval;
|
|
91
|
-
if (!symbol || !tradingMode || !interval) {
|
|
92
|
-
console.warn("Missing required OHLCV event data:", backendEvent);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
const subscriptionKey = `${symbol.toUpperCase()}-${tradingMode.toUpperCase()}-${interval.toLowerCase()}`;
|
|
96
|
-
// Find the appropriate handler
|
|
97
|
-
const handler = this.ohlcvHandlers.get(subscriptionKey);
|
|
98
|
-
if (handler) {
|
|
99
|
-
// Map backend field names to the expected structure
|
|
100
|
-
const ohlcvEvent = {
|
|
101
|
-
symbol: symbol,
|
|
102
|
-
tradingMode: tradingMode,
|
|
103
|
-
interval: interval,
|
|
104
|
-
candlestick: {
|
|
105
|
-
T: ohlcvData.period_start || 0, // Candle start time (ms)
|
|
106
|
-
t: ohlcvData.period_end || 0, // Candle end time (ms)
|
|
107
|
-
o: ohlcvData.open || "0", // Open price
|
|
108
|
-
h: ohlcvData.high || "0", // High price
|
|
109
|
-
l: ohlcvData.low || "0", // Low price
|
|
110
|
-
c: ohlcvData.close || "0", // Close price
|
|
111
|
-
v: ohlcvData.volume || "0", // Volume (base token)
|
|
112
|
-
s: symbol, // Symbol from backend data
|
|
113
|
-
i: interval, // Interval from backend data
|
|
114
|
-
n: ohlcvData.trades_count || 0, // Number of trades
|
|
115
|
-
},
|
|
116
|
-
};
|
|
117
|
-
handler(ohlcvEvent);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
console.warn(`No handler found for OHLCV event: ${subscriptionKey}`, event);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
catch (error) {
|
|
124
|
-
console.error("Error handling OHLCV event:", error, event);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
/** Clean up resources when disconnecting */
|
|
128
|
-
cleanup() {
|
|
129
|
-
this.ohlcvHandlers.clear();
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
//# sourceMappingURL=ohlcv-ws-client.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ohlcv-ws-client.js","sourceRoot":"","sources":["../../../../src/api/websocket/clients/ohlcv-ws-client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAML,gBAAgB,GACjB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAC;AAE/D;;GAEG;AACH,MAAM,OAAO,wBAAyB,SAAQ,uBAAuB;IAArE;;QACE,wDAAwD;QAChD,kBAAa,GAAG,IAAI,GAAG,EAAuC,CAAC;IA+HzE,CAAC;IA7HC;;;;;;;;;;;;;;;;;;OAkBG;IACH,gBAAgB,CAAC,MAAc,EAAE,WAAwB,EAAE,QAAkB,EAAE,QAAqC;QAClH,MAAM,OAAO,GAAG,SAAS,MAAM,IAAI,WAAW,IAAI,QAAQ,EAAE,CAAC;QAC7D,MAAM,eAAe,GAAG,GAAG,MAAM,CAAC,WAAW,EAAE,IAAI,WAAW,CAAC,WAAW,EAAE,IAAI,QAAQ,CAAC,WAAW,EAAE,EAAE,CAAC;QAEzG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;QAElD,+DAA+D;QAC/D,IAAI,CAAC,IAAI,CAAC;YACR,IAAI,EAAE,WAAW;YACjB,QAAQ,EAAE,CAAC,OAAO,CAAC;SACpB,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;;;OAWG;IACH,oBAAoB,CAAC,MAAc,EAAE,WAAwB,EAAE,QAAkB;QAC/E,MAAM,OAAO,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