tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
115 lines • 4.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KucoinFuturesSingleConnectionRealTimeFeed = exports.KucoinFuturesRealTimeFeed = void 0;
const handy_1 = require("../handy");
const realtimefeed_1 = require("./realtimefeed");
const kucoinHttpOptions = {
timeout: 10 * 1000,
retry: {
limit: 10,
statusCodes: [418, 429, 500, 403],
maxRetryAfter: 120 * 1000
}
};
class KucoinFuturesRealTimeFeed extends realtimefeed_1.MultiConnectionRealTimeFeedBase {
constructor() {
super(...arguments);
this._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);
}
}
}
exports.KucoinFuturesRealTimeFeed = KucoinFuturesRealTimeFeed;
class KucoinFuturesSingleConnectionRealTimeFeed extends realtimefeed_1.RealTimeFeedBase {
constructor(exchange, filters, _httpURL, timeoutIntervalMS, onError) {
super(exchange, filters, timeoutIntervalMS, onError);
this._httpURL = _httpURL;
this.wssURL = '';
this.sendCustomPing = () => {
this.send({
id: new Date().valueOf().toString(),
type: 'ping'
});
};
}
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 !== '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: (0, handy_1.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 depthSnapshotResponse = (await handy_1.httpClient
.get(`${this._httpURL}/v1/level2/snapshot?symbol=${symbol}`, kucoinHttpOptions)
.json());
const snapshot = {
type: 'message',
generated: true,
topic: `/contractMarket/level2Snapshot:${symbol}`,
subject: 'level2Snapshot',
...depthSnapshotResponse
};
this.manualSnapshotsBuffer.push(snapshot);
}
this.debug('requested manual snapshots successfully for: %s ', depthSnapshotFilter.symbols);
}
messageIsError(message) {
return message.type === 'error';
}
messageIsHeartbeat(msg) {
return msg.type === 'pong';
}
}
exports.KucoinFuturesSingleConnectionRealTimeFeed = KucoinFuturesSingleConnectionRealTimeFeed;
class KucoinFuturesContractDetailsClient extends realtimefeed_1.PoolingClientBase {
constructor(exchange, _httpURL) {
super(exchange, 6);
this._httpURL = _httpURL;
}
async poolDataToStream(outputStream) {
const openInterestResponse = (await handy_1.httpClient.get(`${this._httpURL}/v1/contracts/active`, kucoinHttpOptions).json());
for (const instrument of openInterestResponse.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