UNPKG

@drift-labs/common

Version:

Common functions for Drift

118 lines 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataApiWsClient = void 0; const EnvironmentConstants_1 = require("../EnvironmentConstants"); const rxjs_1 = require("rxjs"); const MultiplexWebSocket_1 = require("../utils/MultiplexWebSocket"); const getBaseDataApiUrl = (env) => { const constantEnv = env.isStaging ? 'staging' : env.isDevnet ? 'dev' : 'mainnet'; const dataApiUrl = EnvironmentConstants_1.EnvironmentConstants.dataServerUrl[constantEnv]; return dataApiUrl.replace('https://', ''); }; const getWsSubscriptionPath = (config) => { const baseDataApiUrl = getBaseDataApiUrl(config.env); return `wss://${baseDataApiUrl}/ws`; }; const getWsSubscriptionMessage = (config) => { return JSON.stringify({ type: 'subscribe', symbol: config.marketSymbol, resolution: `${config.resolution}`, }); }; class DataApiWsClient { constructor(config) { this.multiplexSubscription = null; this.handleWsMessage = (message) => { const parsedMessage = message; switch (parsedMessage.type) { case 'update': { const candle = parsedMessage.candle; const trades = parsedMessage.trades; if (candle) this.candleSubject.next(candle); if (trades) this.tradesSubject.next(trades); } break; default: break; } }; this.handleError = () => { console.error('candlesv2:: dataApiWsClient error occurred'); }; this.handleClose = () => { console.log('candlesv2:: dataApiWsClient connection closed'); }; this.subscribe = async () => { const subscriptionId = `${this.config.marketSymbol}-${this.config.resolution}`; this.multiplexSubscription = MultiplexWebSocket_1.MultiplexWebSocket.createWebSocketSubscription({ wsUrl: getWsSubscriptionPath(this.config), subscriptionId, subscribeMessage: getWsSubscriptionMessage(this.config), unsubscribeMessage: JSON.stringify({ type: 'unsubscribe', symbol: this.config.marketSymbol, resolution: `${this.config.resolution}`, }), onError: this.handleError, onMessage: this.handleWsMessage, onClose: this.handleClose, messageFilter: (message) => { var _a, _b; try { // Only accept `update` messages with a matching resolution and symbol for this subscription if ((message === null || message === void 0 ? void 0 : message.type) === 'update' && ((_a = message.candle) === null || _a === void 0 ? void 0 : _a.resolution) === this.config.resolution && ((_b = message.candle) === null || _b === void 0 ? void 0 : _b.symbol) === this.config.marketSymbol) { return true; } return false; } catch (error) { console.error('candlesv2:: dataApiWsClient messageFilter error', { error, message, }); return false; } }, errorMessageFilter: (_message) => { return false; // No known error messages }, }); return Promise.resolve(); }; this.unsubscribe = () => { if (this.multiplexSubscription) { this.multiplexSubscription.unsubscribe(); this.multiplexSubscription = null; } this.candleSubject.complete(); this.tradesSubject.complete(); }; this.config = config; this.candleSubject = new rxjs_1.Subject(); this.tradesSubject = new rxjs_1.Subject(); this._candleObservable = this.candleSubject.asObservable(); this._tradesObservable = this.tradesSubject.asObservable(); } /** * Get the candle updates stream * @returns An observable that emits candle updates */ get candlesObservable() { return this._candleObservable; } /** * Get the trade updates stream * @returns An observable that emits trade updates */ get tradesObservable() { return this._tradesObservable; } } exports.DataApiWsClient = DataApiWsClient; //# sourceMappingURL=dataApiWsClient.js.map