tardis-dev
Version:
Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js
50 lines • 1.87 kB
JavaScript
import { getJSON } from "../handy.js";
import { RealTimeFeedBase } from "./realtimefeed.js";
export class BinanceDexRealTimeFeed extends RealTimeFeedBase {
wssURL = 'wss://dex.binance.org/api/ws';
httpURL = 'https://dex.binance.org/api/v1';
mapToSubscribeMessages(filters) {
return filters
.filter((f) => f.channel !== 'depthSnapshot')
.map((filter) => {
if (!filter.symbols || filter.symbols.length === 0) {
throw new Error('BinanceDexRealTimeFeed requires explicitly specified symbols when subscribing to live feed');
}
return {
method: 'subscribe',
topic: filter.channel,
symbols: filter.symbols
};
});
}
messageIsError(message) {
if (message.stream === undefined) {
return true;
}
return false;
}
async provideManualSnapshots(filters, shouldCancel) {
const depthSnapshotFilter = filters.find((f) => f.channel === 'depthSnapshot');
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}/depth?symbol=${symbol}&limit=1000`);
const snapshot = {
stream: `depthSnapshot`,
generated: true,
data: {
symbol,
...data
}
};
this.manualSnapshotsBuffer.push(snapshot);
}
this.debug('requested manual snapshots successfully for: %s ', depthSnapshotFilter.symbols);
}
}
//# sourceMappingURL=binancedex.js.map