UNPKG

@drift-labs/common

Version:

Common functions for Drift

137 lines 5.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DLOB_SERVER_WEBSOCKET_UTILS = void 0; const UIMarket_1 = require("../../types/UIMarket"); const getMarketSymbolFromId = (marketId) => { var _a, _b; const isPerp = marketId.isPerp; if (isPerp) { const perpMarket = UIMarket_1.UIMarket.perpMarkets.find((m) => marketId.marketIndex === m.marketIndex); return (_a = perpMarket === null || perpMarket === void 0 ? void 0 : perpMarket.symbol) !== null && _a !== void 0 ? _a : ''; } else { const spotMarket = UIMarket_1.UIMarket.spotMarkets.find((m) => marketId.marketIndex === m.marketIndex); return (_b = spotMarket === null || spotMarket === void 0 ? void 0 : spotMarket.symbol) !== null && _b !== void 0 ? _b : ''; } }; const getSubscriptionProps = (props) => { const type = props.type; switch (type) { case 'orderbook': { return { type: 'subscribe', channel: 'orderbook', market: getMarketSymbolFromId(props.market), marketType: props.market.isPerp ? 'perp' : 'spot', ...(props.grouping && { grouping: props.grouping }), }; } case 'orderbook_indicative': { return { type: 'subscribe', channel: 'orderbook_indicative', market: getMarketSymbolFromId(props.market), marketType: props.market.isPerp ? 'perp' : 'spot', ...(props.grouping && { grouping: props.grouping }), }; } case 'trades': { return { type: 'subscribe', channel: 'trades', market: getMarketSymbolFromId(props.market), marketType: props.market.isPerp ? 'perp' : 'spot', }; } default: { const exhaustiveCheck = type; throw new Error(`Unhandled case: ${exhaustiveCheck}`); } } }; const getUnsubscriptionProps = (props) => { const type = props.type; switch (type) { case 'orderbook': { return { type: 'unsubscribe', channel: 'orderbook', market: getMarketSymbolFromId(props.market), marketType: props.market.isPerp ? 'perp' : 'spot', ...(props.grouping && { grouping: props.grouping }), }; } case 'orderbook_indicative': { return { type: 'unsubscribe', channel: 'orderbook_indicative', market: getMarketSymbolFromId(props.market), marketType: props.market.isPerp ? 'perp' : 'spot', ...(props.grouping && { grouping: props.grouping }), }; } case 'trades': { return { type: 'unsubscribe', channel: 'trades', market: getMarketSymbolFromId(props.market), marketType: props.market.isPerp ? 'perp' : 'spot', }; } default: { const exhaustiveCheck = type; throw new Error(`Unhandled case: ${exhaustiveCheck}`); } } }; /** * This function needs to take the subscription probs and map it into matching channel key that is returned by the websocket for the subscription * @param props * @returns */ const getChannelKey = (props) => { const type = props.type; switch (type) { case 'orderbook': { return `orderbook_${props.market.isPerp ? 'perp' : 'spot'}_${props.market.marketIndex}${props.grouping ? `_grouped_${props.grouping}` : ''}`; } case 'orderbook_indicative': { return `orderbook_${props.market.isPerp ? 'perp' : 'spot'}_${props.market.marketIndex}${props.grouping ? `_grouped_${props.grouping}` : ''}_indicative`; } case 'trades': { return `trades_${props.market.isPerp ? 'perp' : 'spot'}_${props.market.marketIndex}`; } default: { const exhaustiveCheck = type; throw new Error(`Unhandled case: ${exhaustiveCheck}`); } } }; const getMessageFilter = (props) => { const type = props.type; switch (type) { case 'orderbook': case 'orderbook_indicative': { return (message) => { return (message.channel === getChannelKey(props) || // This is a special extra message which comes through once, immediately after subscribing, to let the subscriber know the initial state of the orderbook message.channel === `last_update_orderbook_${props.market.marketTypeStr}_${props.market.marketIndex}`); }; } case 'trades': { return (message) => message.channel === getChannelKey(props) || message.channel === 'heartbeat'; } default: { const exhaustiveCheck = type; throw new Error(`Unhandled case: ${exhaustiveCheck}`); } } }; exports.DLOB_SERVER_WEBSOCKET_UTILS = { getSubscriptionProps, getUnsubscriptionProps, getMessageFilter, }; //# sourceMappingURL=DlobServerWebsocketUtils.js.map