UNPKG

@drift-labs/common

Version:

Common functions for Drift

126 lines 4.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MARKET_UTILS = void 0; const sdk_1 = require("@drift-labs/sdk"); const utils_1 = require("../utils"); const markets_1 = require("../constants/markets"); const getBaseAssetSymbol = (marketName, removePrefix = false) => { let baseAssetSymbol = marketName.replace('-PERP', '').replace('/USDC', ''); if (removePrefix) { baseAssetSymbol = baseAssetSymbol.replace('1K', '').replace('1M', ''); } return baseAssetSymbol; }; const PerpOperationsMap = { UPDATE_FUNDING: 'Funding', AMM_FILL: 'AMM Fills', FILL: 'Fills', SETTLE_PNL: 'Settle P&L', SETTLE_PNL_WITH_POSITION: 'Settle P&L With Open Position', }; const SpotOperationsMap = { UPDATE_CUMULATIVE_INTEREST: 'Update Cumulative Interest', FILL: 'Fills', WITHDRAW: 'Withdrawals', }; const InsuranceFundOperationsMap = { INIT: 'Initialize IF', ADD: 'Deposit To IF', REQUEST_REMOVE: 'Request Withdrawal From IF', REMOVE: 'Withdraw From IF', }; const getPausedOperations = (marketAccount) => { if (!marketAccount) return []; const pausedOperations = []; //@ts-ignore const isPerp = !!marketAccount.amm; // check perp operations if (isPerp) { Object.keys(sdk_1.PerpOperation) .filter((operation) => (0, sdk_1.isOperationPaused)(marketAccount.pausedOperations, sdk_1.PerpOperation[operation])) .forEach((pausedOperation) => { pausedOperations.push(PerpOperationsMap[pausedOperation]); }); } else { // check spot operations Object.keys(sdk_1.SpotOperation) .filter((operation) => (0, sdk_1.isOperationPaused)(marketAccount.pausedOperations, sdk_1.SpotOperation[operation])) .forEach((pausedOperation) => { pausedOperations.push(SpotOperationsMap[pausedOperation]); }); // check IF operations Object.keys(sdk_1.InsuranceFundOperation) .filter((operation) => (0, sdk_1.isOperationPaused)( //@ts-ignore marketAccount.ifPausedOperations, sdk_1.InsuranceFundOperation[operation])) .forEach((pausedOperation) => { pausedOperations.push(InsuranceFundOperationsMap[pausedOperation]); }); } return pausedOperations; }; function getMarketConfig(driftEnv, marketType, marketIndex) { const isPerp = utils_1.ENUM_UTILS.match(marketType, sdk_1.MarketType.PERP); if (isPerp) { return sdk_1.PerpMarkets[driftEnv][marketIndex]; } else { return sdk_1.SpotMarkets[driftEnv][marketIndex]; } } const getMaxLeverageForMarket = (marketType, marketIndex, driftClient) => { try { if ((0, sdk_1.isVariant)(marketType, 'perp')) { const marketAccount = driftClient.getPerpMarketAccount(marketIndex); const maxLeverage = parseFloat((1 / (((marketAccount === null || marketAccount === void 0 ? void 0 : marketAccount.marginRatioInitial) ? marketAccount.marginRatioInitial : 10000 / markets_1.DEFAULT_MAX_MARKET_LEVERAGE) / 10000)).toFixed(2)); const marketHasHighLeverageMode = !!(marketAccount === null || marketAccount === void 0 ? void 0 : marketAccount.highLeverageMarginRatioInitial); const highLeverageMaxLeverage = marketHasHighLeverageMode ? parseFloat((1 / (((marketAccount === null || marketAccount === void 0 ? void 0 : marketAccount.highLeverageMarginRatioInitial) ? marketAccount === null || marketAccount === void 0 ? void 0 : marketAccount.highLeverageMarginRatioInitial : 10000 / markets_1.DEFAULT_MAX_MARKET_LEVERAGE) / 10000)).toFixed(1)) : 0; return { maxLeverage, highLeverageMaxLeverage, hasHighLeverage: marketHasHighLeverageMode, }; } else { const marketAccount = driftClient.getSpotMarketAccount(marketIndex); const liabilityWeight = marketAccount ? marketAccount.initialLiabilityWeight / 10000 : 0; return { maxLeverage: parseFloat((1 / (liabilityWeight - 1)).toFixed(2)), highLeverageMaxLeverage: 0, hasHighLeverage: false, }; } } catch (e) { console.error(e); return { maxLeverage: 0, highLeverageMaxLeverage: 0, hasHighLeverage: false, }; } }; exports.MARKET_UTILS = { getBaseAssetSymbol, getPausedOperations, PerpOperationsMap, SpotOperationsMap, InsuranceFundOperationsMap, getMarketConfig, getMaxLeverageForMarket, }; //# sourceMappingURL=market.js.map