Skip to content

Commit 44888a3

Browse files
committed
improvement: handle bybit spot v5 native WS subscribe format
1 parent a5ef833 commit 44888a3

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/ws/replay.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ class WebsocketConnection {
212212
try {
213213
const messageDeserialized = JSON.parse(message)
214214

215-
if (this._subscriptionsMapper.canHandle(messageDeserialized)) {
215+
if (this._subscriptionsMapper.canHandle(messageDeserialized, new Date(this.replayOptions.from))) {
216216
// if there is a subscribe message let's map it to filters and add those to replay options
217-
const filters = this._subscriptionsMapper.map(messageDeserialized)
217+
const filters = this._subscriptionsMapper.map(messageDeserialized, new Date(this.replayOptions.from))
218218
debug('Received subscribe websocket message: %s, mapped filters: %o', message, filters)
219219
this.replayOptions.filters.push(...filters)
220220
this.subscriptionsCount++

src/ws/subscriptionsmappers.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ const huobiMapper: SubscriptionMapper = {
262262
}
263263

264264
// https://github.com/bybit-exchange/bybit-official-api-docs/blob/master/en/websocket.md
265+
const BYBIT_V5_API_SWITCH_DATE = new Date('2023-04-05T00:00:00.000Z')
266+
265267
const bybitMapper: SubscriptionMapper = {
266268
canHandle: (message: any) => {
267269
return message.op === 'subscribe'
@@ -280,11 +282,24 @@ const bybitMapper: SubscriptionMapper = {
280282
}
281283

282284
const bybitSpotMapper: SubscriptionMapper = {
283-
canHandle: (message: any) => {
285+
canHandle: (message: any, date: Date) => {
286+
if (date.valueOf() > BYBIT_V5_API_SWITCH_DATE.valueOf()) {
287+
return message.op === 'subscribe'
288+
}
284289
return message.event === 'sub'
285290
},
286291

287-
map: (message: any) => {
292+
map: (message: any, date: Date) => {
293+
if (date.valueOf() > BYBIT_V5_API_SWITCH_DATE.valueOf()) {
294+
return (message.args as string[]).map((arg) => {
295+
const pieces = arg.split('.')
296+
return {
297+
channel: pieces[0],
298+
symbols: [pieces[pieces.length - 1]]
299+
}
300+
})
301+
}
302+
288303
return [
289304
{
290305
channel: message.topic,
@@ -713,6 +728,6 @@ export const subscriptionsMappers: { [key in Exchange]: SubscriptionMapper } = {
713728
}
714729

715730
export type SubscriptionMapper = {
716-
canHandle: (message: object) => boolean
717-
map: (message: object) => Filter<string>[]
731+
canHandle: (message: object, date: Date) => boolean
732+
map: (message: object, date: Date) => Filter<string>[]
718733
}

0 commit comments

Comments
 (0)