tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
101 lines • 4.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BybitOptionsRealTimeDataFeed = exports.BybitSpotRealTimeDataFeed = exports.BybitRealTimeDataFeed = void 0;
const handy_1 = require("../handy");
const realtimefeed_1 = require("./realtimefeed");
class BybitRealTimeDataFeed extends realtimefeed_1.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;
};
}
}
exports.BybitRealTimeDataFeed = BybitRealTimeDataFeed;
class BybitSingleConnectionRealTimeDataFeed extends realtimefeed_1.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 [...(0, handy_1.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';
}
class BybitSpotRealTimeDataFeed extends BybitSingleConnectionRealTimeDataFeed {
wssURL = 'wss://stream.bybit.com/v5/public/spot';
}
exports.BybitSpotRealTimeDataFeed = BybitSpotRealTimeDataFeed;
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 [...(0, handy_1.batch)(args, 10)].map((argBatch) => {
return {
op: 'subscribe',
args: argBatch
};
});
}
}
exports.BybitOptionsRealTimeDataFeed = BybitOptionsRealTimeDataFeed;
//# sourceMappingURL=bybit.js.map