tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
67 lines • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KucoinRealTimeFeed = void 0;
const handy_1 = require("../handy");
const realtimefeed_1 = require("./realtimefeed");
class KucoinRealTimeFeed extends realtimefeed_1.RealTimeFeedBase {
wssURL = '';
_httpURL = 'https://api.kucoin.com/api';
async getWebSocketUrl() {
const response = (await handy_1.httpClient.post(`${this._httpURL}/v1/bullet-public`, { retry: 3, timeout: 10000 }).json());
return `${response.data.instanceServers[0].endpoint}?token=${response.data.token}&connectId=${(0, handy_1.getRandomString)()}`;
}
mapToSubscribeMessages(filters) {
return filters
.filter((f) => f.channel !== 'market/level2Snapshot')
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error('KucoinRealTimeFeed requires explicitly specified symbols when subscribing to live feed');
}
return {
id: (0, handy_1.getRandomString)(),
type: 'subscribe',
topic: `/${filter.channel}:${filter.symbols.join(',')}`,
privateChannel: false,
response: true
};
});
}
async provideManualSnapshots(filters, shouldCancel) {
const depthSnapshotFilter = filters.find((f) => f.channel === 'market/level2Snapshot');
if (!depthSnapshotFilter) {
return;
}
this.debug('requesting manual snapshots for: %s', depthSnapshotFilter.symbols);
for (let symbol of depthSnapshotFilter.symbols) {
if (shouldCancel()) {
return;
}
const depthSnapshotResponse = (await handy_1.httpClient
.get(`${this._httpURL}/v1/market/orderbook/level2_100?symbol=${symbol}`, { timeout: 10000 })
.json());
const snapshot = {
type: 'message',
generated: true,
topic: `/market/level2Snapshot:${symbol}`,
subject: 'trade.l2Snapshot',
...depthSnapshotResponse
};
this.manualSnapshotsBuffer.push(snapshot);
}
this.debug('requested manual snapshots successfully for: %s ', depthSnapshotFilter.symbols);
}
messageIsError(message) {
return message.type === 'error';
}
sendCustomPing = () => {
this.send({
id: new Date().valueOf().toString(),
type: 'ping'
});
};
messageIsHeartbeat(msg) {
return msg.type === 'pong';
}
}
exports.KucoinRealTimeFeed = KucoinRealTimeFeed;
//# sourceMappingURL=kucoin.js.map