tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
107 lines • 4.31 kB
JavaScript
import { getJSON, getRandomString, postJSON } from "../handy.js";
import { MultiConnectionRealTimeFeedBase, PoolingClientBase, RealTimeFeedBase } from "./realtimefeed.js";
const kucoinHttpOptions = {
timeout: 10 * 1000,
retry: {
limit: 10,
statusCodes: [418, 429, 500, 403],
maxRetryAfter: 120 * 1000
}
};
export class KucoinFuturesRealTimeFeed extends MultiConnectionRealTimeFeedBase {
_httpURL = 'https://api-futures.kucoin.com/api';
*_getRealTimeFeeds(exchange, filters, timeoutIntervalMS, onError) {
const wsFilters = filters.filter((f) => f.channel !== 'contract/details');
if (wsFilters.length > 0) {
yield new KucoinFuturesSingleConnectionRealTimeFeed(exchange, wsFilters, this._httpURL, timeoutIntervalMS, onError);
}
const contractDetailsFilters = filters.filter((f) => f.channel === 'contract/details');
if (contractDetailsFilters.length > 0) {
yield new KucoinFuturesContractDetailsClient(exchange, this._httpURL);
}
}
}
export class KucoinFuturesSingleConnectionRealTimeFeed extends RealTimeFeedBase {
_httpURL;
constructor(exchange, filters, _httpURL, timeoutIntervalMS, onError) {
super(exchange, filters, timeoutIntervalMS, onError);
this._httpURL = _httpURL;
}
wssURL = '';
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 !== 'contractMarket/level2Snapshot')
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error('KucoinFuturesRealTimeFeed requires explicitly specified symbols when subscribing to live feed');
}
return {
id: getRandomString(),
type: 'subscribe',
topic: `/${filter.channel}:${filter.symbols.join(',')}`,
response: true
};
});
}
async provideManualSnapshots(filters, shouldCancel) {
const depthSnapshotFilter = filters.find((f) => f.channel === 'contractMarket/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/level2/snapshot?symbol=${symbol}`, kucoinHttpOptions);
const snapshot = {
type: 'message',
generated: true,
topic: `/contractMarket/level2Snapshot:${symbol}`,
subject: 'level2Snapshot',
...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';
}
}
class KucoinFuturesContractDetailsClient extends PoolingClientBase {
_httpURL;
constructor(exchange, _httpURL) {
super(exchange, 6);
this._httpURL = _httpURL;
}
async poolDataToStream(outputStream) {
const { data: body } = await getJSON(`${this._httpURL}/v1/contracts/active`, kucoinHttpOptions);
for (const instrument of body.data) {
const openInterestMessage = {
topic: `/contract/details:${instrument.symbol}`,
type: 'message',
subject: 'contractDetails',
generated: true,
data: instrument
};
if (outputStream.writable) {
outputStream.write(openInterestMessage);
}
}
}
}
//# sourceMappingURL=kucoinfutures.js.map