UNPKG

tardis-dev

Version:

Convenient access to tick-level historical and real-time cryptocurrency market data via Node.js

53 lines 1.84 kB
import crypto from 'crypto'; import { wait } from "../handy.js"; import { RealTimeFeedBase } from "./realtimefeed.js"; export class OkexSpreadsRealTimeFeed extends RealTimeFeedBase { wssURL = 'wss://ws.okx.com:8443/ws/v5/business'; _hasCredentials = process.env.OKX_API_KEY !== undefined; secondsSinceEpoch() { return Math.floor(Date.now() / 1000); } async onConnected() { if (this._hasCredentials) { const timestamp = this.secondsSinceEpoch().toString(); const sign = crypto.createHmac('sha256', process.env.OKX_API_SECRET_KEY).update(`${timestamp}GET/users/self/verify`).digest('base64'); this.send({ op: 'login', args: [ { apiKey: process.env.OKX_API_KEY, passphrase: process.env.OKX_API_PASSPHRASE, timestamp, sign } ] }); await wait(50); } } mapToSubscribeMessages(filters) { const args = filters .map((filter) => { if (!filter.symbols || filter.symbols.length === 0) { throw new Error(`${this._exchange} RealTimeFeed requires explicitly specified symbols when subscribing to live feed`); } return filter.symbols.map((symbol) => { return { channel: filter.channel, sprdId: symbol }; }); }) .flatMap((s) => s); return [ { op: 'subscribe', args: [...new Set(args)] } ]; } messageIsError(message) { return message.event === 'error'; } } //# sourceMappingURL=okexspreads.js.map