tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
85 lines • 3.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeribitRealTimeDataFeed = void 0;
const handy_1 = require("../handy");
const realtimefeed_1 = require("./realtimefeed");
class DeribitRealTimeDataFeed extends realtimefeed_1.RealTimeFeedBase {
get wssURL() {
if (this._hasCredentials) {
return 'wss://www.deribit.com/ws/api/v2';
}
else {
return 'wss://streams.deribit.com/ws/api/v2';
}
}
_hasCredentials = process.env.DERIBIT_API_CLIENT_ID !== undefined && process.env.DERIBIT_API_CLIENT_SECRET !== undefined;
channelsWithIntervals = ['book', 'perpetual', 'trades', 'ticker'];
mapToSubscribeMessages(filters) {
const hasCredentials = this._hasCredentials;
const channels = filters
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error('DeribitRealTimeDataFeed requires explicitly specified symbols when subscribing to live feed');
}
return filter.symbols.map((symbol) => {
const suffix = this.channelsWithIntervals.includes(filter.channel) ? (hasCredentials ? '.raw' : '.100ms') : '';
return `${filter.channel}.${symbol}${suffix}`;
});
})
.flatMap((f) => f);
return [
{
jsonrpc: '2.0',
id: 3,
method: hasCredentials ? 'private/subscribe' : 'public/subscribe',
params: {
channels
}
}
];
}
messageIsError(message) {
return message.error !== undefined;
}
async onConnected() {
// set heartbeat so deribit won't close connection prematurely
// https://docs.deribit.com/v2/#public-set_heartbeat
this.send({
jsonrpc: '2.0',
method: 'public/set_heartbeat',
id: 0,
params: {
interval: 10
}
});
if (this._hasCredentials) {
this.send({
jsonrpc: '2.0',
method: 'public/auth',
id: 1,
params: {
grant_type: 'client_credentials',
client_id: process.env.DERIBIT_API_CLIENT_ID,
client_secret: process.env.DERIBIT_API_CLIENT_SECRET
}
});
await (0, handy_1.wait)(10);
}
}
messageIsHeartbeat(msg) {
return msg.method === 'heartbeat';
}
onMessage(msg) {
// respond with public/test message to keep connection alive
if (msg.params !== undefined && msg.params.type === 'test_request') {
this.send({
jsonrpc: '2.0',
method: 'public/test',
id: 0,
params: {}
});
}
}
}
exports.DeribitRealTimeDataFeed = DeribitRealTimeDataFeed;
//# sourceMappingURL=deribit.js.map