tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
63 lines • 2.48 kB
JavaScript
import { getRandomString, getJSON, postJSON } from "../handy.js";
import { RealTimeFeedBase } from "./realtimefeed.js";
export class KucoinRealTimeFeed extends RealTimeFeedBase {
wssURL = '';
_httpURL = 'https://api.kucoin.com/api';
async getWebSocketUrl() {
const { data: body } = await postJSON(`${this._httpURL}/v1/bullet-public`, { retry: 3, timeout: 10000 });
return `${body.data.instanceServers[0].endpoint}?token=${body.data.token}&connectId=${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: 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 { data } = await getJSON(`${this._httpURL}/v1/market/orderbook/level2_100?symbol=${symbol}`, {
timeout: 10000
});
const snapshot = {
type: 'message',
generated: true,
topic: `/market/level2Snapshot:${symbol}`,
subject: 'trade.l2Snapshot',
...data
};
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';
}
}
//# sourceMappingURL=kucoin.js.map