tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
54 lines • 1.99 kB
JavaScript
import { getJSON } from "../handy.js";
import { RealTimeFeedBase } from "./realtimefeed.js";
export class BitstampRealTimeFeed extends RealTimeFeedBase {
wssURL = 'wss://ws.bitstamp.net';
httpURL = 'https://www.bitstamp.net/api/v2';
mapToSubscribeMessages(filters) {
return filters
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error('BitstampRealTimeFeed requires explicitly specified symbols when subscribing to live feed');
}
return filter.symbols.map((symbol) => {
return {
event: 'bts:subscribe',
data: {
channel: `${filter.channel}_${symbol}`
}
};
});
})
.flatMap((c) => c);
}
messageIsError(message) {
if (message.channel === undefined) {
return true;
}
if (message.event === 'bts:request_reconnect') {
return true;
}
return false;
}
async provideManualSnapshots(filters, shouldCancel) {
const orderBookFilter = filters.find((f) => f.channel === 'diff_order_book');
if (!orderBookFilter) {
return;
}
this.debug('requesting manual snapshots for: %s', orderBookFilter.symbols);
for (let symbol of orderBookFilter.symbols) {
if (shouldCancel()) {
return;
}
const { data } = await getJSON(`${this.httpURL}/order_book/${symbol}?group=1`);
const snapshot = {
data,
event: 'snapshot',
channel: `diff_order_book_${symbol}`,
generated: true
};
this.manualSnapshotsBuffer.push(snapshot);
}
this.debug('requested manual snapshots successfully for: %s ', orderBookFilter.symbols);
}
}
//# sourceMappingURL=bitstamp.js.map