import { Writable } from 'stream' import { getJSON } from '../handy.ts' import { Filter } from '../types.ts' import { MultiConnectionRealTimeFeedBase, PoolingClientBase, RealTimeFeedBase, Sequence } from './realtimefeed.ts' export class WhiteBitRealTimeFeed extends MultiConnectionRealTimeFeedBase { protected readonly httpURL: string = 'https://whitebit.com/api/v4/public' protected *_getRealTimeFeeds( exchange: string, filters: Filter[], timeoutIntervalMS?: number, onError?: (error: Error) => void, validateSequence?: boolean ) { filters = filters.flatMap((filter) => { const symbols = filter.symbols?.map((symbol) => symbol.toUpperCase()) if (symbols?.length && (filter.channel === 'premiumIndex_update' || filter.channel === 'futures_update')) { // Perpetual-only channels: WhiteBIT rejects a premiumIndex subscription that lists any spot market. const perpetuals = symbols.filter((symbol) => symbol.endsWith('_PERP')) return perpetuals.length > 0 ? [{ ...filter, symbols: perpetuals }] : [] } return [{ ...filter, symbols }] }) const webSocketFilters = filters.filter((filter) => filter.channel !== 'futures_update') for (const filter of webSocketFilters) { if (filter.symbols === undefined || filter.symbols.length === 0) { throw new Error('WhiteBitRealTimeFeed requires explicitly specified symbols when subscribing to live feed') } } if (webSocketFilters.length > 0) { yield new WhiteBitWebSocketRealTimeFeed(exchange, webSocketFilters, timeoutIntervalMS, onError, validateSequence) } const futuresFilters = filters.filter((filter) => filter.channel === 'futures_update') if (futuresFilters.length > 0) { const allFutures = futuresFilters.some((filter) => !filter.symbols?.length) yield new WhiteBitFuturesClient( exchange, allFutures ? [] : futuresFilters.flatMap((filter) => filter.symbols!), this.httpURL, onError ) } } } export class WhiteBitWebSocketRealTimeFeed extends RealTimeFeedBase { protected readonly wssURL = 'wss://wss.whitebit.com/ws' protected mapToSubscribeMessages(filters: Filter[]) { let id = 0 return filters.flatMap(({ channel, symbols }) => { const method = channel.replace('_update', '_subscribe') if (channel === 'depth_update') { return symbols!.map((symbol) => ({ id: ++id, method, params: [symbol, 100, '0', true] })) } if (channel === 'bookTicker_update') { return symbols!.map((symbol) => ({ id: ++id, method, params: [symbol] })) } return [ { id: ++id, method, params: symbols! } ] }) } protected messageIsError(message: { error?: unknown }) { return message.error !== undefined && message.error !== null } protected getSequence(message: any): Sequence | undefined { const { method, params } = message if (method === 'depth_update') { const [isSnapshot, book, symbol] = params return { key: symbol, sequence: book.update_id, previousSequence: isSnapshot ? undefined : book.past_update_id } } return undefined } } class WhiteBitFuturesClient extends PoolingClientBase { constructor( exchange: string, private readonly _symbols: string[], private readonly _httpURL: string, onError?: (error: Error) => void ) { super(exchange, 6, onError) } protected async poolDataToStream(outputStream: Writable) { const { data: response } = await getJSON<{ success: boolean; result: { ticker_id: string }[] }>(`${this._httpURL}/futures`, { timeout: 5000 }) if (!response.success) { throw new Error('WhiteBIT futures request was unsuccessful') } for (const market of response.result) { if (this._symbols.length > 0 && !this._symbols.includes(market.ticker_id)) { continue } if (outputStream.writable) { outputStream.write({ method: 'futures_update', params: [market.ticker_id, market], id: null, generated: true }) } } } }