tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
95 lines • 3.97 kB
JavaScript
import { batch } from "../handy.js";
import { RealTimeFeedBase, MultiConnectionRealTimeFeedBase } from "./realtimefeed.js";
export class BybitRealTimeDataFeed extends MultiConnectionRealTimeFeedBase {
*_getRealTimeFeeds(exchange, filters, timeoutIntervalMS, onError) {
const linearContractsFilters = filters.reduce(this._only((s) => s.endsWith('USDT') || s.includes('-') || s.endsWith('PERP')), []);
const inverseContractsFilters = filters.reduce(this._only((s) => s.endsWith('USDT') === false && s.includes('-') === false && s.endsWith('PERP') === false), []);
if (linearContractsFilters.length > 0) {
yield new BybitLinearRealTimeDataFeed(exchange, linearContractsFilters, timeoutIntervalMS, onError);
}
if (inverseContractsFilters.length > 0) {
yield new BybitInverseRealTimeDataFeed(exchange, inverseContractsFilters, timeoutIntervalMS, onError);
}
}
_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 {
mapToSubscribeMessages(filters) {
const args = filters
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error('BybitRealTimeDataFeed requires explicitly specified symbols when subscribing to live feed');
}
return filter.symbols.map((symbol) => {
return `${filter.channel}.${symbol}`;
});
})
.flatMap((f) => f);
return [...batch(args, 10)].map((argBatch) => {
return {
op: 'subscribe',
args: argBatch
};
});
}
messageIsError(message) {
return message.success === false;
}
sendCustomPing = () => {
this.send({ op: 'ping' });
};
messageIsHeartbeat(msg) {
return msg.ret_msg === 'pong' || msg.op == 'pong';
}
}
class BybitLinearRealTimeDataFeed extends BybitSingleConnectionRealTimeDataFeed {
wssURL = 'wss://stream.bybit.com/v5/public/linear';
}
class BybitInverseRealTimeDataFeed extends BybitSingleConnectionRealTimeDataFeed {
wssURL = 'wss://stream.bybit.com/v5/public/inverse';
}
export class BybitSpotRealTimeDataFeed extends BybitSingleConnectionRealTimeDataFeed {
wssURL = 'wss://stream.bybit.com/v5/public/spot';
}
export class BybitOptionsRealTimeDataFeed extends BybitSingleConnectionRealTimeDataFeed {
wssURL = 'wss://stream.bybit.com/v5/public/option';
mapToSubscribeMessages(filters) {
const args = filters
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error('BybitRealTimeDataFeed requires explicitly specified symbols when subscribing to live feed');
}
if (filter.channel === 'publicTrade') {
const baseCoins = [...new Set(filter.symbols.map((s) => s.split('-')[0]))];
return baseCoins.map((symbol) => {
return `${filter.channel}.${symbol}`;
});
}
return filter.symbols.map((symbol) => {
return `${filter.channel}.${symbol}`;
});
})
.flatMap((f) => f);
return [...batch(args, 10)].map((argBatch) => {
return {
op: 'subscribe',
args: argBatch
};
});
}
}
//# sourceMappingURL=bybit.js.map