From 5005b816b34a1924801251da712c58e47b417c7c Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Wed, 26 Feb 2025 10:16:39 +0000 Subject: [PATCH] feat(): add all symbols allLiquidaitons ws example --- examples/ws-public-allLiquidations.ts | 92 +++++++++++++++++++++++++++ src/util/typeGuards.ts | 37 +++++++++++ 2 files changed, 129 insertions(+) create mode 100644 examples/ws-public-allLiquidations.ts diff --git a/examples/ws-public-allLiquidations.ts b/examples/ws-public-allLiquidations.ts new file mode 100644 index 0000000..98983ed --- /dev/null +++ b/examples/ws-public-allLiquidations.ts @@ -0,0 +1,92 @@ +import { RestClientV5, WebsocketClient, isWsAllLiquidationEvent } from '../src'; + +// or +// import { +// RestClientV5, +// WebsocketClient, +// isWsAllLiquidationEvent, +// } from 'bybit-api'; + +function onAllLiquidationEvent(event) { + console.log( + new Date(), + 'allLiquidationEvent', + JSON.stringify(event, null, 2), + ); +} + +const wsClient = new WebsocketClient({}); + +wsClient.on('update', (data) => { + if (isWsAllLiquidationEvent(data)) { + return onAllLiquidationEvent(data); + } + + console.log('raw unahndled message received ', JSON.stringify(data)); +}); + +wsClient.on('open', (data) => { + console.log('connection opened open:', data.wsKey); +}); +wsClient.on('response', (data) => { + console.log('log response: ', JSON.stringify(data, null, 2)); +}); +wsClient.on('reconnect', ({ wsKey }) => { + console.log('ws automatically reconnecting.... ', wsKey); +}); +wsClient.on('reconnected', (data) => { + console.log('ws has reconnected ', data?.wsKey); +}); + +wsClient.on('exception', (data) => { + console.error('ws exception: ', data); +}); + +/** + * + * If you want to receive data for all available symbols, this websocket topic + * requires you to subscribe to each symbol individually. + * + * This can be easily automated by fetching a list of symbols via the REST client, + * generating a list of topics (one per symbol), before simply passing an + * array of topics to the websocket client per product group (linear & inverse perps). + * + */ +async function start() { + const restClientV5 = new RestClientV5(); + + const allSymbolsV5ResultLinear = await restClientV5.getTickers({ + category: 'linear', + }); + const allSymbolsV5ResultInverse = await restClientV5.getTickers({ + category: 'inverse', + }); + + const allLinearSymbols = allSymbolsV5ResultLinear.result.list.map( + (ticker) => ticker.symbol, + ); + const allInverseSymbols = allSymbolsV5ResultInverse.result.list.map( + (ticker) => ticker.symbol, + ); + + console.log('all v5 linear symbols: ', JSON.stringify(allLinearSymbols)); + console.log('all v5 inverse symbols: ', JSON.stringify(allInverseSymbols)); + + const TOPIC_NAME = 'allLiquidation'; + + // Make an array of topics ready for submission + const allLinearTopics = allLinearSymbols.map( + (symbol) => `${TOPIC_NAME}.${symbol}`, + ); + const inverseTopics = allInverseSymbols.map( + (symbol) => `${TOPIC_NAME}.${symbol}`, + ); + + // subscribe to all linear symbols + wsClient.subscribeV5(allLinearTopics, 'linear'); + + // subscribe to all inverse symbols + wsClient.subscribeV5(inverseTopics, 'inverse'); +} + +start().catch((e) => console.error('exception in main logic: ', e)); diff --git a/src/util/typeGuards.ts b/src/util/typeGuards.ts index f721f99..bd9c7c4 100644 --- a/src/util/typeGuards.ts +++ b/src/util/typeGuards.ts @@ -14,6 +14,29 @@ import { WSPositionEventV5, } from '../types/websockets/ws-events'; +export interface BybitEventV5 { + topic: string; + type: string; + ts: number; + data: TData; + wsKey: string; +} + +export function isWsEventV5( + event: unknown, +): event is BybitEventV5 { + if ( + typeof event !== 'object' || + !event || + typeof event['topic'] !== 'string' || + typeof event['type'] !== 'string' + ) { + return false; + } + + return true; +} + /** * Type guard to detect a V5 orderbook event (delta & snapshots) * @@ -141,3 +164,17 @@ export function isTopicSubscriptionConfirmation( return true; } + +export function isWsAllLiquidationEvent( + event: unknown, + // eslint-disable-next-line @typescript-eslint/no-explicit-any +): event is BybitEventV5 { + if (!isWsEventV5(event)) { + return false; + } + + if (event['topic'].startsWith('allLiquidation')) { + return true; + } + return false; +}