diff --git a/test/linear/ws.public.test.ts b/test/linear/ws.public.test.ts index 6281c07..a29f159 100644 --- a/test/linear/ws.public.test.ts +++ b/test/linear/ws.public.test.ts @@ -23,7 +23,6 @@ describe('Public Linear Perps Websocket Client', () => { afterAll(() => { wsClient.closeAll(); - wsClient.removeAllListeners(); }); it('should open a public ws connection', async () => { @@ -65,31 +64,4 @@ describe('Public Linear Perps Websocket Client', () => { console.error(`Wait for "${wsTopic}" event exception: `, e); } }); - - it('should fail to subscribe to private events (no keys)', async () => { - const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); - // const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); - - const wsTopic = 'wallet'; - - // No easy way to trigger a private event (other than executing trades) - // expect(wsUpdatePromise).resolves.toMatchObject({ - // topic: wsTopic, - // data: expect.any(Array), - // }); - - wsClient.subscribe(wsTopic); - - try { - expect(await wsResponsePromise).toBeFalsy(); - } catch (e) { - expect(e).toMatchObject({ - request: { - args: [wsTopic], - op: 'subscribe', - }, - success: false, - }); - } - }); }); diff --git a/test/unified-margin/ws.private.test.ts b/test/unified-margin/ws.private.test.ts new file mode 100644 index 0000000..72c16a1 --- /dev/null +++ b/test/unified-margin/ws.private.test.ts @@ -0,0 +1,77 @@ +import { + WebsocketClient, + WSClientConfigurableOptions, + WS_KEY_MAP, +} from '../../src'; +import { + logAllEvents, + getSilentLogger, + waitForSocketEvent, + WS_OPEN_EVENT_PARTIAL, + fullLogger, +} from '../ws.util'; + +describe('Private Unified Margin Websocket Client', () => { + let wsClient: WebsocketClient; + + const wsClientOptions: WSClientConfigurableOptions = { + market: 'unifiedPerp', + }; + + beforeAll(() => { + wsClient = new WebsocketClient( + wsClientOptions, + getSilentLogger('expectSuccessNoAuth') + // fullLogger + ); + // logAllEvents(wsClient); + wsClient.connectPrivate(); + }); + + afterAll(() => { + wsClient.closeAll(); + }); + + it('should open a public ws connection', async () => { + const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); + try { + expect(await wsOpenPromise).toMatchObject({ + event: WS_OPEN_EVENT_PARTIAL, + wsKey: WS_KEY_MAP.unifiedPrivate, + }); + } catch (e) { + expect(e).toBeFalsy(); + } + }); + + // Should work, but don't have unified margin activated - needs extra testing + // {"conn_id": "064443fffef10442-0000798d-0000a9f6-ba32aeee49712540-1752c4f4", "ret_msg": "3303001", "success": false, "type": "COMMAND_RESP", "wsKey": "unifiedPrivate"} + + it.skip('should subscribe to public private unified account events', async () => { + const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); + const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); + + // USDT should be detected and automatically routed through the USDT connection + const topic = 'user.position.unifiedAccount'; + wsClient.subscribe(topic); + + try { + expect(await wsResponsePromise).toMatchObject({ + op: 'subscribe', + req_id: topic, + success: true, + wsKey: WS_KEY_MAP.unifiedPrivate, + }); + } catch (e) { + // sub failed + expect(e).toBeFalsy(); + } + + try { + expect(await wsUpdatePromise).toStrictEqual(''); + } catch (e) { + // no data + expect(e).toBeFalsy(); + } + }); +}); diff --git a/test/unified-margin/ws.public.option.test.ts b/test/unified-margin/ws.public.option.test.ts index 4c179c1..cd05b9b 100644 --- a/test/unified-margin/ws.public.option.test.ts +++ b/test/unified-margin/ws.public.option.test.ts @@ -41,7 +41,7 @@ describe('Public Unified Margin Websocket Client (Options)', () => { } }); - it('should subscribe to public trade events', async () => { + it('should subscribe to public orderbook events', async () => { const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); // const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); diff --git a/test/unified-margin/ws.public.perp.usdc.test.ts b/test/unified-margin/ws.public.perp.usdc.test.ts new file mode 100644 index 0000000..1b526a3 --- /dev/null +++ b/test/unified-margin/ws.public.perp.usdc.test.ts @@ -0,0 +1,85 @@ +import { + WebsocketClient, + WSClientConfigurableOptions, + WS_KEY_MAP, +} from '../../src'; +import { + logAllEvents, + getSilentLogger, + waitForSocketEvent, + WS_OPEN_EVENT_PARTIAL, + fullLogger, +} from '../ws.util'; + +describe('Public Unified Margin Websocket Client (Perps - USDC)', () => { + let wsClient: WebsocketClient; + + const wsClientOptions: WSClientConfigurableOptions = { + market: 'unifiedPerp', + }; + + beforeAll(() => { + wsClient = new WebsocketClient( + wsClientOptions, + getSilentLogger('expectSuccessNoAuth') + // fullLogger + ); + // logAllEvents(wsClient); + wsClient.connectPublic(); + }); + + afterAll(() => { + wsClient.closeAll(); + }); + + it('should open a public ws connection', async () => { + const wsOpenPromise = waitForSocketEvent(wsClient, 'open'); + try { + expect(await wsOpenPromise).toMatchObject({ + event: WS_OPEN_EVENT_PARTIAL, + wsKey: expect.stringContaining('unifiedPerpUSD'), + }); + } catch (e) { + expect(e).toBeFalsy(); + } + }); + + // TODO: are there USDC topics? This doesn't seem to work + it.skip('should subscribe to public orderbook events through USDC connection', async () => { + const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); + const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); + + // USDT should be detected and automatically routed through the USDT connection + wsClient.subscribe('orderbook.25.BTCUSDC'); + + try { + expect(await wsResponsePromise).toMatchObject({ + op: 'subscribe', + req_id: 'orderbook.25.BTCUSDC', + success: true, + wsKey: WS_KEY_MAP.unifiedPerpUSDTPublic, + }); + } catch (e) { + // sub failed + expect(e).toBeFalsy(); + } + + try { + expect(await wsUpdatePromise).toMatchObject({ + data: { + a: expect.any(Array), + b: expect.any(Array), + s: 'BTCUSDT', + u: expect.any(Number), + }, + topic: 'orderbook.25.BTCUSDC', + ts: expect.any(Number), + type: 'snapshot', + wsKey: WS_KEY_MAP.unifiedPerpUSDTPublic, + }); + } catch (e) { + // no data + expect(e).toBeFalsy(); + } + }); +}); diff --git a/test/unified-margin/ws.public.perp.usdt.test.ts b/test/unified-margin/ws.public.perp.usdt.test.ts index 98215d4..800c961 100644 --- a/test/unified-margin/ws.public.perp.usdt.test.ts +++ b/test/unified-margin/ws.public.perp.usdt.test.ts @@ -44,16 +44,18 @@ describe('Public Unified Margin Websocket Client (Perps - USDT)', () => { } }); - it('should subscribe to public trade events through USDT topic', async () => { + it('should subscribe to public orderbook events through USDT connection', async () => { const wsResponsePromise = waitForSocketEvent(wsClient, 'response'); const wsUpdatePromise = waitForSocketEvent(wsClient, 'update'); - wsClient.subscribe('orderbook.25.BTCUSDT'); + // USDT should be detected and automatically routed through the USDT connection + const topic = 'orderbook.25.BTCUSDT'; + wsClient.subscribe(topic); try { expect(await wsResponsePromise).toMatchObject({ op: 'subscribe', - req_id: 'orderbook.25.BTCUSDT', + req_id: topic, success: true, wsKey: WS_KEY_MAP.unifiedPerpUSDTPublic, }); @@ -70,7 +72,7 @@ describe('Public Unified Margin Websocket Client (Perps - USDT)', () => { s: 'BTCUSDT', u: expect.any(Number), }, - topic: 'orderbook.25.BTCUSDT', + topic: topic, ts: expect.any(Number), type: 'snapshot', wsKey: WS_KEY_MAP.unifiedPerpUSDTPublic,