@drift-labs/common
Version:
Common functions for Drift
224 lines • 8.09 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIMarket = void 0;
const sdk_1 = require("@drift-labs/sdk");
const MarketId_1 = require("./MarketId");
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
const pools_1 = require("../constants/pools");
const markets_1 = require("../constants/markets");
const utils_1 = require("../utils");
const Config_1 = require("../Config");
const useAsyncMarketConfigs = process.env.NEXT_PUBLIC_USE_ASYNC_MARKET_CONFIGS === 'true';
class UIMarket {
get uiSymbols() {
return this._uiSymbols;
}
set uiSymbols(uiSymbols) {
this._uiSymbols = uiSymbols;
}
constructor(marketIndex, marketType) {
this.marketIndex = marketIndex;
this.marketType = marketType;
const marketId = new MarketId_1.MarketId(marketIndex, marketType);
const perpMarkets = useAsyncMarketConfigs
? UIMarket.perpMarkets
: Config_1.Config.perpMarketsLookup;
const spotMarkets = useAsyncMarketConfigs
? UIMarket.spotMarkets
: Config_1.Config.spotMarketsLookup;
const markets = marketId.isPerp ? perpMarkets : spotMarkets;
//@ts-ignore
const market = markets.find((m) => m.marketIndex === marketIndex);
// TODO: should we purposely throw an error here? Or construct a default market?
(0, tiny_invariant_1.default)(market, `Market not found for type: ${marketId.marketTypeStr}, market index: ${marketIndex}`);
this.marketId = marketId;
this.market = market;
this.setUiSymbols();
}
static setPerpMarkets(perpMarkets) {
this.perpMarkets = perpMarkets;
}
static setSpotMarkets(spotMarkets) {
this.spotMarkets = spotMarkets;
}
static getOrCreate(marketIndex, marketType) {
const key = MarketId_1.MarketId.key(marketIndex, marketType);
if (UIMarket.cache.has(key)) {
return UIMarket.cache.get(key);
}
const market = new UIMarket(marketIndex, marketType);
UIMarket.cache.set(key, market);
return market;
}
static createSpotMarket(marketIndex) {
return UIMarket.getOrCreate(marketIndex, sdk_1.MarketType.SPOT);
}
static createPerpMarket(marketIndex) {
return UIMarket.getOrCreate(marketIndex, sdk_1.MarketType.PERP);
}
static fromMarketId(marketId) {
return UIMarket.getOrCreate(marketId.marketIndex, marketId.marketType);
}
static checkIsPredictionMarket(marketConfig) {
if (!marketConfig.category) {
return false;
}
return marketConfig.category.includes('Prediction');
}
get isSpot() {
return this.marketId.isSpot;
}
get isPerp() {
return this.marketId.isPerp;
}
get marketTypeStr() {
return this.marketId.marketTypeStr;
}
get key() {
return this.marketId.key;
}
// @deprecated : Use uiSymbols.marketDisplaySymbol instead
get marketName() {
return `${this.market.symbol}${this.isSpot ? '/USDC' : ''}`;
}
// @deprecated : Use uiSymbols.marketSymbol instead
get symbol() {
return this.market.symbol;
}
get isUsdcMarket() {
return this.isSpot && this.marketIndex === markets_1.USDC_SPOT_MARKET_INDEX;
}
get isStableCoinMarket() {
return (this.isSpot &&
utils_1.ENUM_UTILS.match(this.market.oracleSource, sdk_1.OracleSource.PYTH_STABLE_COIN));
}
get isPredictionMarket() {
return (this.isPerp &&
UIMarket.checkIsPredictionMarket(this.market));
}
get precision() {
if (this.marketId.isPerp) {
return sdk_1.BASE_PRECISION;
}
else {
return this.market.precision;
}
}
get precisionExp() {
if (this.marketId.isPerp) {
return sdk_1.BASE_PRECISION_EXP;
}
else {
return this.market.precisionExp;
}
}
equals(other) {
return this.marketId.equals(other.marketId);
}
// @deprecated : Use uiSymbols.baseAssetSymbol instead
baseAssetSymbol(removePrefix = false) {
let baseAssetSymbol = this.isPerp
? this.market.baseAssetSymbol
: this.market.symbol;
if (removePrefix) {
baseAssetSymbol = baseAssetSymbol.replace('1K', '').replace('1M', '');
}
return baseAssetSymbol;
}
setUiSymbols() {
(0, tiny_invariant_1.default)(this.marketId, 'MarketId not set');
const marketSymbol = this.getMarketSymbol();
const marketDisplaySymbol = this.getMarketDisplaySymbol();
const baseAssetSymbol = this.getBaseAssetSymbol();
const baseAssetDisplaySymbol = this.getBaseAssetDisplaySymbol();
this.uiSymbols = {
marketSymbol,
marketDisplaySymbol,
baseAssetSymbol,
baseAssetDisplaySymbol,
};
}
getMarketSymbol() {
if (this.marketId.isPerp) {
return this.market.symbol;
}
else {
return this.market.symbol;
}
}
getMarketDisplaySymbol() {
if (this.marketId.isPerp) {
return this.market.symbol;
}
else {
const marketConfig = this.market;
switch (marketConfig.poolId) {
case pools_1.MAIN_POOL_ID:
return marketConfig.symbol;
case pools_1.JLP_POOL_ID:
return `${marketConfig.symbol.split('-')[0]}`;
case pools_1.LST_POOL_ID:
return `${marketConfig.symbol.split('-')[0]}`;
case pools_1.EXPONENT_POOL_ID: {
/*
Example market symbol conversions:
PT-fragSOL-15JUN25-3 => PT-fragSOL-15JUN25
PT-kySOL-10JUL25-3 => PT-kySOL-10JUL25
JitoSOL-3 => JitoSOL
JTO-3 => JTO
*/
return (marketConfig.symbol.startsWith('PT-')
? marketConfig.symbol.slice(0, marketConfig.symbol.lastIndexOf('-'))
: marketConfig.symbol.split('-')[0]);
}
case pools_1.SACRED_POOL_ID:
return `${marketConfig.symbol.split('-')[0]}`;
default:
return marketConfig.symbol;
}
}
}
getBaseAssetSymbol() {
if (this.marketId.isPerp) {
return this.market.baseAssetSymbol
.replace('1K', '')
.replace('1M', '');
}
else {
return this.getMarketDisplaySymbol(); // Currently no cases where SPOT baseAssetSymbol is different from marketDisplaySymbol
}
}
getBaseAssetDisplaySymbol() {
if (this.marketId.isPerp) {
return this.market
.baseAssetSymbol;
}
else {
const marketConfig = this.market;
switch (marketConfig.poolId) {
case pools_1.EXPONENT_POOL_ID: {
/*
Example market symbol conversions:
PT-fragSOL-15JUN25-3 => fragSOL
PT-kySOL-10JUL25-3 => kySOL
JitoSOL-3 => JitoSOL
JTO-3 => JTO
*/
return (marketConfig.symbol.startsWith('PT-')
? marketConfig.symbol.split('-')[1]
: marketConfig.symbol.split('-')[0]);
}
default:
return marketConfig.symbol;
}
}
}
}
exports.UIMarket = UIMarket;
UIMarket.perpMarkets = sdk_1.PerpMarkets['mainnet-beta'];
UIMarket.spotMarkets = sdk_1.SpotMarkets['mainnet-beta'];
UIMarket.cache = new Map();
//# sourceMappingURL=UIMarket.js.map