import { Filter } from '../types' ; import { RealTimeFeedBase , MultiConnectionRealTimeFeedBase } from './realtimefeed' ; export class BybitRealTimeDataFeed extends MultiConnectionRealTimeFeedBase { protected * _getRealTimeFeeds ( exchange , filters , timeoutIntervalMS ? , onError ? ) { const linearContractsFilters = filters . reduce ( this . _only ( ( s ) => s . endsWith ( 'USDT' ) ) , [ ] as Filter < string > [ ] ) ; const inverseContractsFilters = filters . reduce ( this . _only ( ( s ) => s . endsWith ( 'USDT' ) === false ) , [ ] as Filter < string > [ ] ) ; if ( linearContractsFilters . length > 0 ) { yield new BybitSingleConnectionRealTimeDataFeed ( 'realtime_public' , exchange , linearContractsFilters , timeoutIntervalMS , onError ) ; } if ( inverseContractsFilters . length > 0 ) { yield new BybitSingleConnectionRealTimeDataFeed ( 'realtime' , exchange , inverseContractsFilters , timeoutIntervalMS , onError ) ; } } private _only ( filter ) { return ( prev , current ) => { if ( ! current . symbols || current . symbols . length === 0 ) { throw new Error ( 'BybitRealTimeDataFeed requires explicitly specified symbols when subscribing to live feed' ) ; } const symbols = current . symbols . filter ( filter ) ; if ( symbols . length > 0 ) { prev . push ( { channel : current . channel , symbols } ) ; } return prev ; } ; } } class BybitSingleConnectionRealTimeDataFeed extends RealTimeFeedBase { protected readonly wssURL : string ; constructor ( wsURLSuffix , exchange , filters , timeoutIntervalMS ? , onError ? ) { super ( exchange , filters , timeoutIntervalMS , onError ) ; this . wssURL = `wss://stream.bybit.com/${ wsURLSuffix }` ; } protected mapToSubscribeMessages ( filters ) { const args = filters . map ( ( filter ) => { return filter . symbols ! . map ( ( symbol ) => { const suffix = filter . channel === 'instrument_info' || filter . channel === 'orderBook_200' ? '.100ms' : '' ; return `${ filter . channel }${ suffix }.${ symbol }` ; } ) ; } ) . flatMap ( ( f ) => f ) ; return [ { op : 'subscribe' , args } ] ; } protected messageIsError ( message ) { return message . success === false ; } }