@drift-labs/common 1.0.51 → 1.0.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,10 +9,9 @@ const getOpenPositionData = (driftClient, userPositions, user, perpMarketLookup,
9
9
  const oracleGuardRails = driftClient.getStateAccount().oracleGuardRails;
10
10
  const newResult = userPositions
11
11
  .filter((position) => !position.baseAssetAmount.eq(sdk_1.ZERO) ||
12
- !position.quoteAssetAmount.eq(sdk_1.ZERO) ||
13
- !position.lpShares.eq(sdk_1.ZERO))
12
+ !position.quoteAssetAmount.eq(sdk_1.ZERO))
14
13
  .map((position) => {
15
- var _a, _b, _c;
14
+ var _a, _b;
16
15
  const perpMarketConfig = perpMarketLookup[position.marketIndex];
17
16
  const perpMarket = driftClient.getPerpMarketAccount(position.marketIndex);
18
17
  const usdcSpotMarket = driftClient.getSpotMarketAccount(sdk_1.QUOTE_SPOT_MARKET_INDEX);
@@ -102,9 +101,6 @@ const getOpenPositionData = (driftClient, userPositions, user, perpMarketLookup,
102
101
  costBasis: (0, sdk_1.calculateCostBasis)(position),
103
102
  realizedPnl: position.settledPnl,
104
103
  pnlIsClaimable: (0, sdk_1.isOracleValid)(perpMarket, oraclePriceData, oracleGuardRails, (_b = perpMarket.amm.lastUpdateSlot) === null || _b === void 0 ? void 0 : _b.toNumber()),
105
- lpShares: position.lpShares,
106
- remainderBaseAmount: (_c = position.remainderBaseAssetAmount) !== null && _c !== void 0 ? _c : 0,
107
- lpDeriskPrice: user.liquidationPrice(position.marketIndex, undefined, undefined, 'Initial', true),
108
104
  maxMarginRatio: position.maxMarginRatio,
109
105
  };
110
106
  });
@@ -1 +1 @@
1
- {"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/common-ui-utils/user.ts"],"names":[],"mappings":";;;AAAA,yCA0ByB;AACzB,oCAAkD;AAClD,uCAA0C;AAC1C,oCAAsC;AAEtC,MAAM,mBAAmB,GAAG,CAC3B,WAAwB,EACxB,aAA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oB,CAAC;AAC7B,CAAC,CAAC;AAEW,QAAA,UAAU,GAAG;IACzB,mBAAmB;IACnB,wBAAwB;IACxB,2BAA2B;CAC3B,CAAC","sourcesContent":["import {\n\tBASE_PRECISION_EXP,\n\tBN,\n\tBigNum,\n\tDriftClient,\n\tMarketStatus,\n\tONE,\n\tPRICE_PRECISION,\n\tPRICE_PRECISION_EXP,\n\tPerpMarketConfig,\n\tPerpPosition,\n\tPositionDirection,\n\tPublicKey,\n\tQUOTE_PRECISION_EXP,\n\tQUOTE_SPOT_MARKET_INDEX,\n\tUser,\n\tZERO,\n\tcalculateClaimablePnl,\n\tcalculateCostBasis,\n\tcalculateEntryPrice,\n\tcalculateFeesAndFundingPnl,\n\tcalculatePositionPNL,\n\tgetUserAccountPublicKeySync,\n\tcalculateUnsettledFundingPnl,\n\tisOracleValid,\n\tAMM_RESERVE_PRECISION,\n} from '@drift-labs/sdk';\nimport { OpenPosition, UIMarket } from '../types';\nimport { TRADING_UTILS } from './trading';\nimport { ENUM_UTILS } from '../utils';\n\nconst getOpenPositionData = (\n\tdriftClient: DriftClient,\n\tuserPositions: PerpPosition[],\n\tuser: User,\n\tperpMarketLookup: PerpMarketConfig[],\n\tmarkPriceCallback?: (marketIndex: number) => BN\n): OpenPosition[] => {\n\tconst oracleGuardRails = driftClient.getStateAccount().oracleGuardRails;\n\n\tconst newResult: OpenPosition[] = userPositions\n\t\t.filter(\n\t\t\t(position) =>\n\t\t\t\t!position.baseAssetAmount.eq(ZERO) ||\n\t\t\t\t!position.quoteAssetAmount.eq(ZERO) ||\n\t\t\t\t!position.lpShares.eq(ZERO)\n\t\t)\n\t\t.map((position) => {\n\t\t\tconst perpMarketConfig = perpMarketLookup[position.marketIndex];\n\t\t\tconst perpMarket = driftClient.getPerpMarketAccount(position.marketIndex);\n\n\t\t\tconst usdcSpotMarket = driftClient.getSpotMarketAccount(\n\t\t\t\tQUOTE_SPOT_MARKET_INDEX\n\t\t\t);\n\n\t\t\tconst oraclePriceData = driftClient.getOracleDataForPerpMarket(\n\t\t\t\tposition.marketIndex\n\t\t\t);\n\n\t\t\tlet oraclePrice = oraclePriceData.price;\n\n\t\t\t// mark price fetched with a callback so we don't need extra dlob server calls. fallback to oracle\n\t\t\tlet markPrice = markPriceCallback\n\t\t\t\t? markPriceCallback(position.marketIndex) ?? oraclePriceData.price\n\t\t\t\t: oraclePriceData.price;\n\n\t\t\tlet estExitPrice = user.getPositionEstimatedExitPriceAndPnl(\n\t\t\t\tposition,\n\t\t\t\tposition.baseAssetAmount\n\t\t\t)[0];\n\n\t\t\tconst entryPrice = calculateEntryPrice(position);\n\n\t\t\tconst isShort = position.baseAssetAmount.isNeg();\n\n\t\t\tif (UIMarket.checkIsPredictionMarket(perpMarketConfig)) {\n\t\t\t\tconst isResolved =\n\t\t\t\t\tENUM_UTILS.match(perpMarket?.status, MarketStatus.SETTLEMENT) ||\n\t\t\t\t\tENUM_UTILS.match(perpMarket?.status, MarketStatus.DELISTED);\n\n\t\t\t\tif (isResolved) {\n\t\t\t\t\tconst resolvedToNo = perpMarket.expiryPrice.lte(\n\t\t\t\t\t\tZERO.add(perpMarket.amm.orderTickSize)\n\t\t\t\t\t);\n\n\t\t\t\t\tconst price = resolvedToNo\n\t\t\t\t\t\t? ZERO.mul(PRICE_PRECISION)\n\t\t\t\t\t\t: ONE.mul(PRICE_PRECISION);\n\n\t\t\t\t\testExitPrice = price;\n\t\t\t\t\tmarkPrice = price;\n\t\t\t\t\toraclePrice = price;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// if for any reason oracle or mark price blips to 0, fallback to the other one so we don't show a crazy pnl\n\t\t\tif (markPrice.lte(ZERO) && oraclePrice.gt(ZERO)) {\n\t\t\t\tmarkPrice = oraclePrice;\n\t\t\t}\n\n\t\t\tif (oraclePrice.lte(ZERO) && markPrice.gt(ZERO)) {\n\t\t\t\toraclePrice = markPrice;\n\t\t\t}\n\n\t\t\tconst pnlVsMark = TRADING_UTILS.calculatePotentialProfit({\n\t\t\t\tcurrentPositionSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\tcurrentPositionDirection: isShort\n\t\t\t\t\t? PositionDirection.SHORT\n\t\t\t\t\t: PositionDirection.LONG,\n\t\t\t\tcurrentPositionEntryPrice: BigNum.from(entryPrice, PRICE_PRECISION_EXP),\n\t\t\t\ttradeDirection: isShort\n\t\t\t\t\t? PositionDirection.LONG\n\t\t\t\t\t: PositionDirection.SHORT,\n\t\t\t\texitBaseSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\texitPrice: BigNum.from(markPrice, PRICE_PRECISION_EXP),\n\t\t\t\ttakerFeeBps: 0,\n\t\t\t}).estimatedProfit.shiftTo(QUOTE_PRECISION_EXP).val;\n\n\t\t\tconst pnlVsOracle = TRADING_UTILS.calculatePotentialProfit({\n\t\t\t\tcurrentPositionSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\tcurrentPositionDirection: isShort\n\t\t\t\t\t? PositionDirection.SHORT\n\t\t\t\t\t: PositionDirection.LONG,\n\t\t\t\tcurrentPositionEntryPrice: BigNum.from(entryPrice, PRICE_PRECISION_EXP),\n\t\t\t\ttradeDirection: isShort\n\t\t\t\t\t? PositionDirection.LONG\n\t\t\t\t\t: PositionDirection.SHORT,\n\t\t\t\texitBaseSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\texitPrice: BigNum.from(oraclePrice, PRICE_PRECISION_EXP),\n\t\t\t\ttakerFeeBps: 0,\n\t\t\t}).estimatedProfit.shiftTo(QUOTE_PRECISION_EXP).val;\n\n\t\t\treturn {\n\t\t\t\tmarketIndex: position.marketIndex,\n\t\t\t\tmarketSymbol: perpMarketConfig.symbol,\n\t\t\t\tdirection: isShort ? 'short' : 'long',\n\t\t\t\tnotional: position.baseAssetAmount\n\t\t\t\t\t.abs()\n\t\t\t\t\t.mul(markPrice)\n\t\t\t\t\t.div(AMM_RESERVE_PRECISION),\n\t\t\t\tbaseSize: position.baseAssetAmount,\n\t\t\t\tmarkPrice,\n\t\t\t\tentryPrice,\n\t\t\t\texitPrice: estExitPrice,\n\t\t\t\tliqPrice: user.isPerpPositionIsolated(position)\n\t\t\t\t\t? user.liquidationPrice(\n\t\t\t\t\t\t\tposition.marketIndex,\n\t\t\t\t\t\t\tZERO,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\t'Isolated'\n\t\t\t\t\t )\n\t\t\t\t\t: user.liquidationPrice(position.marketIndex, ZERO),\n\t\t\t\tquoteAssetNotionalAmount: position.quoteAssetAmount,\n\t\t\t\tquoteEntryAmount: position.quoteEntryAmount,\n\t\t\t\tquoteBreakEvenAmount: position.quoteBreakEvenAmount,\n\t\t\t\tpnlVsMark,\n\t\t\t\tpnlVsOracle,\n\t\t\t\tunsettledPnl: calculateClaimablePnl(\n\t\t\t\t\tperpMarket,\n\t\t\t\t\tusdcSpotMarket,\n\t\t\t\t\tposition,\n\t\t\t\t\toraclePriceData\n\t\t\t\t),\n\t\t\t\tunsettledFundingPnl: calculateUnsettledFundingPnl(perpMarket, position),\n\t\t\t\t// Includes both settled and unsettled funding as well as fees\n\t\t\t\tfeesAndFundingPnl: calculateFeesAndFundingPnl(perpMarket, position),\n\t\t\t\ttotalUnrealizedPnl: calculatePositionPNL(\n\t\t\t\t\tperpMarket,\n\t\t\t\t\tposition,\n\t\t\t\t\ttrue,\n\t\t\t\t\toraclePriceData\n\t\t\t\t),\n\t\t\t\tunrealizedFundingPnl: user.getUnrealizedFundingPNL(\n\t\t\t\t\tposition.marketIndex\n\t\t\t\t),\n\t\t\t\tlastCumulativeFundingRate: position.lastCumulativeFundingRate,\n\t\t\t\topenOrders: position.openOrders,\n\t\t\t\tcostBasis: calculateCostBasis(position),\n\t\t\t\trealizedPnl: position.settledPnl,\n\t\t\t\tpnlIsClaimable: isOracleValid(\n\t\t\t\t\tperpMarket,\n\t\t\t\t\toraclePriceData,\n\t\t\t\t\toracleGuardRails,\n\t\t\t\t\tperpMarket.amm.lastUpdateSlot?.toNumber()\n\t\t\t\t),\n\t\t\t\tlpShares: position.lpShares,\n\t\t\t\tremainderBaseAmount: position.remainderBaseAssetAmount ?? 0,\n\t\t\t\tlpDeriskPrice: user.liquidationPrice(\n\t\t\t\t\tposition.marketIndex,\n\t\t\t\t\tundefined,\n\t\t\t\t\tundefined,\n\t\t\t\t\t'Initial',\n\t\t\t\t\ttrue\n\t\t\t\t),\n\t\t\t\tmaxMarginRatio: position.maxMarginRatio,\n\t\t\t};\n\t\t});\n\n\treturn newResult;\n};\n\nconst checkIfUserAccountExists = async (\n\tdriftClient: DriftClient,\n\tconfig:\n\t\t| {\n\t\t\t\ttype: 'userPubKey';\n\t\t\t\tuserPubKey: PublicKey;\n\t\t }\n\t\t| {\n\t\t\t\ttype: 'subAccountId';\n\t\t\t\tsubAccountId: number;\n\t\t\t\tauthority: PublicKey;\n\t\t }\n) => {\n\tlet userPubKey: PublicKey;\n\n\tif (config.type === 'userPubKey') {\n\t\tuserPubKey = config.userPubKey;\n\t} else {\n\t\tuserPubKey = getUserAccountPublicKeySync(\n\t\t\tdriftClient.program.programId,\n\t\t\tconfig.authority,\n\t\t\tconfig.subAccountId\n\t\t);\n\t}\n\n\tconst accountInfo = await driftClient.connection.getAccountInfo(userPubKey);\n\n\treturn accountInfo !== null;\n};\n\n/**\n * A user's max leverage for a market is stored on-chain in the `PerpPosition` struct of the `UserAccount`.\n * There are a few scenarios for how a market's max leverage is defined:\n *\n * 1. When the user does not have a position (\"empty\" or not) in the market in their `UserAccount` data,\n * and creates an order for the market, an \"empty\" `PerpPosition` will be upsert to the `UserAccount` data,\n * and will contain the max margin ratio set by the user. Note that the `UserAccount` data can store up\n * to 8 `PerpPosition` structs, and most of the time the majority of the `PerpPosition` structs will be\n * \"empty\" if the user does not have the max 8 perp positions open. The max leverage is then derived from\n * the max margin ratio set in the `PerpPosition` struct.\n *\n * 2. If the user has a position (\"empty\" or not), but no open orders and is provided with a saved max leverage,\n * the saved max leverage is used.\n *\n * 3. When the user does not have a position (\"empty\" or not), it is expected of the UI to store and persist\n * the max leverage in the UI client.\n *\n * 4. In cases where the user has a position before the market max leverage feature was shipped, the\n * position is not expected to have a max margin ratio set, and the UI should display the regular max\n * leverage for the market, unless the user is already in High Leverage Mode, in which case the UI should\n * display the high leverage max leverage for the market (if any).\n */\nconst getUserMaxLeverageForMarket = (\n\tuser: User | undefined,\n\tmarketIndex: number,\n\tmarketLeverageDetails: {\n\t\tregularMaxLeverage: number;\n\t\thighLeverageMaxLeverage: number;\n\t\thasHighLeverage: boolean;\n\t},\n\tuiSavedMaxLeverage?: number\n) => {\n\t// if no saved max leverage is provided, return the regular max leverage for the market\n\tconst DEFAULT_MAX_LEVERAGE =\n\t\tuiSavedMaxLeverage ?? marketLeverageDetails.regularMaxLeverage;\n\n\tif (!user) {\n\t\treturn DEFAULT_MAX_LEVERAGE;\n\t}\n\n\tconst openOrClosedPosition = user.getPerpPosition(marketIndex); // this position does not have to be open, it can be a closed position (a.k.a \"empty\") but has max margin ratio set.\n\n\tif (!openOrClosedPosition) {\n\t\treturn DEFAULT_MAX_LEVERAGE;\n\t}\n\n\tconst positionHasMaxMarginRatioSet = !!openOrClosedPosition.maxMarginRatio;\n\tconst isPositionOpen = !openOrClosedPosition.baseAssetAmount.eq(ZERO);\n\tconst hasNoOpenOrders = openOrClosedPosition.openOrders === 0;\n\n\tif (positionHasMaxMarginRatioSet) {\n\t\t// Special case: open position with no orders - use UI saved value if available\n\t\tif (isPositionOpen && hasNoOpenOrders && uiSavedMaxLeverage) {\n\t\t\treturn uiSavedMaxLeverage;\n\t\t}\n\n\t\treturn parseFloat(\n\t\t\t((1 / openOrClosedPosition.maxMarginRatio) * 10000).toFixed(2)\n\t\t);\n\t}\n\n\tif (isPositionOpen) {\n\t\t// user has an existing position from before PML ship (this means no max margin ratio set onchain yet)\n\t\t// display max leverage for the leverage mode their account is in\n\t\tconst isUserInHighLeverageMode = user.isHighLeverageMode('Initial');\n\t\tconst grandfatheredMaxLev = isUserInHighLeverageMode\n\t\t\t? marketLeverageDetails.hasHighLeverage\n\t\t\t\t? marketLeverageDetails.highLeverageMaxLeverage\n\t\t\t\t: marketLeverageDetails.regularMaxLeverage\n\t\t\t: marketLeverageDetails.regularMaxLeverage;\n\t\treturn grandfatheredMaxLev;\n\t}\n\n\t// user has closed position with no margin ratio set, return default value\n\treturn DEFAULT_MAX_LEVERAGE;\n};\n\nexport const USER_UTILS = {\n\tgetOpenPositionData,\n\tcheckIfUserAccountExists,\n\tgetUserMaxLeverageForMarket,\n};\n"]}
1
+ {"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/common-ui-utils/user.ts"],"names":[],"mappings":";;;AAAA,yCA0ByB;AACzB,oCAAkD;AAClD,uCAA0C;AAC1C,oCAAsC;AAEtC,MAAM,mBAAmB,GAAG,CAC3B,WAAwB,EACxB,aAA6B,EAC7B,IAAU,EACV,gBAAoC,EACpC,iBAA+C,EAC9B,EAAE;IACnB,MAAM,gBAAgB,GAAG,WAAW,CAAC,eAAe,EAAE,CAAC,gBAAgB,CAAC;IAExE,MAAM,SAAS,GAAmB,aAAa;SAC7C,MAAM,CACN,CAAC,QAAQ,EAAE,EAAE,CACZ,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,UAAI,CAAC;QAClC,CAAC,QAAQ,CAAC,gBAAgB,CAAC,EAAE,CAAC,UAAI,CAAC,CACpC;SACA,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;;QACjB,MAAM,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B,CAAC;QAED,OAAO,UAAU,CAChB,CAAC,CAAC,CAAC,GAAG,oBAAoB,CAAC,cAAc,CAAC,GAAG,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAC9D,CAAC;IACH,CAAC;IAED,IAAI,cAAc,EAAE,CAAC;QACpB,sGAAsG;QACtG,iEAAiE;QACjE,MAAM,wBAAwB,GAAG,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC;QACpE,MAAM,mBAAmB,GAAG,wBAAwB;YACnD,CAAC,CAAC,qBAAqB,CAAC,eAAe;gBACtC,CAAC,CAAC,qBAAqB,CAAC,uBAAuB;gBAC/C,CAAC,CAAC,qBAAqB,CAAC,kBAAkB;YAC3C,CAAC,CAAC,qBAAqB,CAAC,kBAAkB,CAAC;QAC5C,OAAO,mBAAmB,CAAC;IAC5B,CAAC;IAED,0EAA0E;IAC1E,OAAO,oBAAoB,CAAC;AAC7B,CAAC,CAAC;AAEW,QAAA,UAAU,GAAG;IACzB,mBAAmB;IACnB,wBAAwB;IACxB,2BAA2B;CAC3B,CAAC","sourcesContent":["import {\n\tBASE_PRECISION_EXP,\n\tBN,\n\tBigNum,\n\tDriftClient,\n\tMarketStatus,\n\tONE,\n\tPRICE_PRECISION,\n\tPRICE_PRECISION_EXP,\n\tPerpMarketConfig,\n\tPerpPosition,\n\tPositionDirection,\n\tPublicKey,\n\tQUOTE_PRECISION_EXP,\n\tQUOTE_SPOT_MARKET_INDEX,\n\tUser,\n\tZERO,\n\tcalculateClaimablePnl,\n\tcalculateCostBasis,\n\tcalculateEntryPrice,\n\tcalculateFeesAndFundingPnl,\n\tcalculatePositionPNL,\n\tgetUserAccountPublicKeySync,\n\tcalculateUnsettledFundingPnl,\n\tisOracleValid,\n\tAMM_RESERVE_PRECISION,\n} from '@drift-labs/sdk';\nimport { OpenPosition, UIMarket } from '../types';\nimport { TRADING_UTILS } from './trading';\nimport { ENUM_UTILS } from '../utils';\n\nconst getOpenPositionData = (\n\tdriftClient: DriftClient,\n\tuserPositions: PerpPosition[],\n\tuser: User,\n\tperpMarketLookup: PerpMarketConfig[],\n\tmarkPriceCallback?: (marketIndex: number) => BN\n): OpenPosition[] => {\n\tconst oracleGuardRails = driftClient.getStateAccount().oracleGuardRails;\n\n\tconst newResult: OpenPosition[] = userPositions\n\t\t.filter(\n\t\t\t(position) =>\n\t\t\t\t!position.baseAssetAmount.eq(ZERO) ||\n\t\t\t\t!position.quoteAssetAmount.eq(ZERO)\n\t\t)\n\t\t.map((position) => {\n\t\t\tconst perpMarketConfig = perpMarketLookup[position.marketIndex];\n\t\t\tconst perpMarket = driftClient.getPerpMarketAccount(position.marketIndex);\n\n\t\t\tconst usdcSpotMarket = driftClient.getSpotMarketAccount(\n\t\t\t\tQUOTE_SPOT_MARKET_INDEX\n\t\t\t);\n\n\t\t\tconst oraclePriceData = driftClient.getOracleDataForPerpMarket(\n\t\t\t\tposition.marketIndex\n\t\t\t);\n\n\t\t\tlet oraclePrice = oraclePriceData.price;\n\n\t\t\t// mark price fetched with a callback so we don't need extra dlob server calls. fallback to oracle\n\t\t\tlet markPrice = markPriceCallback\n\t\t\t\t? markPriceCallback(position.marketIndex) ?? oraclePriceData.price\n\t\t\t\t: oraclePriceData.price;\n\n\t\t\tlet estExitPrice = user.getPositionEstimatedExitPriceAndPnl(\n\t\t\t\tposition,\n\t\t\t\tposition.baseAssetAmount\n\t\t\t)[0];\n\n\t\t\tconst entryPrice = calculateEntryPrice(position);\n\n\t\t\tconst isShort = position.baseAssetAmount.isNeg();\n\n\t\t\tif (UIMarket.checkIsPredictionMarket(perpMarketConfig)) {\n\t\t\t\tconst isResolved =\n\t\t\t\t\tENUM_UTILS.match(perpMarket?.status, MarketStatus.SETTLEMENT) ||\n\t\t\t\t\tENUM_UTILS.match(perpMarket?.status, MarketStatus.DELISTED);\n\n\t\t\t\tif (isResolved) {\n\t\t\t\t\tconst resolvedToNo = perpMarket.expiryPrice.lte(\n\t\t\t\t\t\tZERO.add(perpMarket.amm.orderTickSize)\n\t\t\t\t\t);\n\n\t\t\t\t\tconst price = resolvedToNo\n\t\t\t\t\t\t? ZERO.mul(PRICE_PRECISION)\n\t\t\t\t\t\t: ONE.mul(PRICE_PRECISION);\n\n\t\t\t\t\testExitPrice = price;\n\t\t\t\t\tmarkPrice = price;\n\t\t\t\t\toraclePrice = price;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// if for any reason oracle or mark price blips to 0, fallback to the other one so we don't show a crazy pnl\n\t\t\tif (markPrice.lte(ZERO) && oraclePrice.gt(ZERO)) {\n\t\t\t\tmarkPrice = oraclePrice;\n\t\t\t}\n\n\t\t\tif (oraclePrice.lte(ZERO) && markPrice.gt(ZERO)) {\n\t\t\t\toraclePrice = markPrice;\n\t\t\t}\n\n\t\t\tconst pnlVsMark = TRADING_UTILS.calculatePotentialProfit({\n\t\t\t\tcurrentPositionSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\tcurrentPositionDirection: isShort\n\t\t\t\t\t? PositionDirection.SHORT\n\t\t\t\t\t: PositionDirection.LONG,\n\t\t\t\tcurrentPositionEntryPrice: BigNum.from(entryPrice, PRICE_PRECISION_EXP),\n\t\t\t\ttradeDirection: isShort\n\t\t\t\t\t? PositionDirection.LONG\n\t\t\t\t\t: PositionDirection.SHORT,\n\t\t\t\texitBaseSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\texitPrice: BigNum.from(markPrice, PRICE_PRECISION_EXP),\n\t\t\t\ttakerFeeBps: 0,\n\t\t\t}).estimatedProfit.shiftTo(QUOTE_PRECISION_EXP).val;\n\n\t\t\tconst pnlVsOracle = TRADING_UTILS.calculatePotentialProfit({\n\t\t\t\tcurrentPositionSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\tcurrentPositionDirection: isShort\n\t\t\t\t\t? PositionDirection.SHORT\n\t\t\t\t\t: PositionDirection.LONG,\n\t\t\t\tcurrentPositionEntryPrice: BigNum.from(entryPrice, PRICE_PRECISION_EXP),\n\t\t\t\ttradeDirection: isShort\n\t\t\t\t\t? PositionDirection.LONG\n\t\t\t\t\t: PositionDirection.SHORT,\n\t\t\t\texitBaseSize: BigNum.from(\n\t\t\t\t\tposition.baseAssetAmount.abs(),\n\t\t\t\t\tBASE_PRECISION_EXP\n\t\t\t\t),\n\t\t\t\texitPrice: BigNum.from(oraclePrice, PRICE_PRECISION_EXP),\n\t\t\t\ttakerFeeBps: 0,\n\t\t\t}).estimatedProfit.shiftTo(QUOTE_PRECISION_EXP).val;\n\n\t\t\treturn {\n\t\t\t\tmarketIndex: position.marketIndex,\n\t\t\t\tmarketSymbol: perpMarketConfig.symbol,\n\t\t\t\tdirection: isShort ? 'short' : 'long',\n\t\t\t\tnotional: position.baseAssetAmount\n\t\t\t\t\t.abs()\n\t\t\t\t\t.mul(markPrice)\n\t\t\t\t\t.div(AMM_RESERVE_PRECISION),\n\t\t\t\tbaseSize: position.baseAssetAmount,\n\t\t\t\tmarkPrice,\n\t\t\t\tentryPrice,\n\t\t\t\texitPrice: estExitPrice,\n\t\t\t\tliqPrice: user.isPerpPositionIsolated(position)\n\t\t\t\t\t? user.liquidationPrice(\n\t\t\t\t\t\t\tposition.marketIndex,\n\t\t\t\t\t\t\tZERO,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\t'Isolated'\n\t\t\t\t\t )\n\t\t\t\t\t: user.liquidationPrice(position.marketIndex, ZERO),\n\t\t\t\tquoteAssetNotionalAmount: position.quoteAssetAmount,\n\t\t\t\tquoteEntryAmount: position.quoteEntryAmount,\n\t\t\t\tquoteBreakEvenAmount: position.quoteBreakEvenAmount,\n\t\t\t\tpnlVsMark,\n\t\t\t\tpnlVsOracle,\n\t\t\t\tunsettledPnl: calculateClaimablePnl(\n\t\t\t\t\tperpMarket,\n\t\t\t\t\tusdcSpotMarket,\n\t\t\t\t\tposition,\n\t\t\t\t\toraclePriceData\n\t\t\t\t),\n\t\t\t\tunsettledFundingPnl: calculateUnsettledFundingPnl(perpMarket, position),\n\t\t\t\t// Includes both settled and unsettled funding as well as fees\n\t\t\t\tfeesAndFundingPnl: calculateFeesAndFundingPnl(perpMarket, position),\n\t\t\t\ttotalUnrealizedPnl: calculatePositionPNL(\n\t\t\t\t\tperpMarket,\n\t\t\t\t\tposition,\n\t\t\t\t\ttrue,\n\t\t\t\t\toraclePriceData\n\t\t\t\t),\n\t\t\t\tunrealizedFundingPnl: user.getUnrealizedFundingPNL(\n\t\t\t\t\tposition.marketIndex\n\t\t\t\t),\n\t\t\t\tlastCumulativeFundingRate: position.lastCumulativeFundingRate,\n\t\t\t\topenOrders: position.openOrders,\n\t\t\t\tcostBasis: calculateCostBasis(position),\n\t\t\t\trealizedPnl: position.settledPnl,\n\t\t\t\tpnlIsClaimable: isOracleValid(\n\t\t\t\t\tperpMarket,\n\t\t\t\t\toraclePriceData,\n\t\t\t\t\toracleGuardRails,\n\t\t\t\t\tperpMarket.amm.lastUpdateSlot?.toNumber()\n\t\t\t\t),\n\t\t\t\tmaxMarginRatio: position.maxMarginRatio,\n\t\t\t};\n\t\t});\n\n\treturn newResult;\n};\n\nconst checkIfUserAccountExists = async (\n\tdriftClient: DriftClient,\n\tconfig:\n\t\t| {\n\t\t\t\ttype: 'userPubKey';\n\t\t\t\tuserPubKey: PublicKey;\n\t\t }\n\t\t| {\n\t\t\t\ttype: 'subAccountId';\n\t\t\t\tsubAccountId: number;\n\t\t\t\tauthority: PublicKey;\n\t\t }\n) => {\n\tlet userPubKey: PublicKey;\n\n\tif (config.type === 'userPubKey') {\n\t\tuserPubKey = config.userPubKey;\n\t} else {\n\t\tuserPubKey = getUserAccountPublicKeySync(\n\t\t\tdriftClient.program.programId,\n\t\t\tconfig.authority,\n\t\t\tconfig.subAccountId\n\t\t);\n\t}\n\n\tconst accountInfo = await driftClient.connection.getAccountInfo(userPubKey);\n\n\treturn accountInfo !== null;\n};\n\n/**\n * A user's max leverage for a market is stored on-chain in the `PerpPosition` struct of the `UserAccount`.\n * There are a few scenarios for how a market's max leverage is defined:\n *\n * 1. When the user does not have a position (\"empty\" or not) in the market in their `UserAccount` data,\n * and creates an order for the market, an \"empty\" `PerpPosition` will be upsert to the `UserAccount` data,\n * and will contain the max margin ratio set by the user. Note that the `UserAccount` data can store up\n * to 8 `PerpPosition` structs, and most of the time the majority of the `PerpPosition` structs will be\n * \"empty\" if the user does not have the max 8 perp positions open. The max leverage is then derived from\n * the max margin ratio set in the `PerpPosition` struct.\n *\n * 2. If the user has a position (\"empty\" or not), but no open orders and is provided with a saved max leverage,\n * the saved max leverage is used.\n *\n * 3. When the user does not have a position (\"empty\" or not), it is expected of the UI to store and persist\n * the max leverage in the UI client.\n *\n * 4. In cases where the user has a position before the market max leverage feature was shipped, the\n * position is not expected to have a max margin ratio set, and the UI should display the regular max\n * leverage for the market, unless the user is already in High Leverage Mode, in which case the UI should\n * display the high leverage max leverage for the market (if any).\n */\nconst getUserMaxLeverageForMarket = (\n\tuser: User | undefined,\n\tmarketIndex: number,\n\tmarketLeverageDetails: {\n\t\tregularMaxLeverage: number;\n\t\thighLeverageMaxLeverage: number;\n\t\thasHighLeverage: boolean;\n\t},\n\tuiSavedMaxLeverage?: number\n) => {\n\t// if no saved max leverage is provided, return the regular max leverage for the market\n\tconst DEFAULT_MAX_LEVERAGE =\n\t\tuiSavedMaxLeverage ?? marketLeverageDetails.regularMaxLeverage;\n\n\tif (!user) {\n\t\treturn DEFAULT_MAX_LEVERAGE;\n\t}\n\n\tconst openOrClosedPosition = user.getPerpPosition(marketIndex); // this position does not have to be open, it can be a closed position (a.k.a \"empty\") but has max margin ratio set.\n\n\tif (!openOrClosedPosition) {\n\t\treturn DEFAULT_MAX_LEVERAGE;\n\t}\n\n\tconst positionHasMaxMarginRatioSet = !!openOrClosedPosition.maxMarginRatio;\n\tconst isPositionOpen = !openOrClosedPosition.baseAssetAmount.eq(ZERO);\n\tconst hasNoOpenOrders = openOrClosedPosition.openOrders === 0;\n\n\tif (positionHasMaxMarginRatioSet) {\n\t\t// Special case: open position with no orders - use UI saved value if available\n\t\tif (isPositionOpen && hasNoOpenOrders && uiSavedMaxLeverage) {\n\t\t\treturn uiSavedMaxLeverage;\n\t\t}\n\n\t\treturn parseFloat(\n\t\t\t((1 / openOrClosedPosition.maxMarginRatio) * 10000).toFixed(2)\n\t\t);\n\t}\n\n\tif (isPositionOpen) {\n\t\t// user has an existing position from before PML ship (this means no max margin ratio set onchain yet)\n\t\t// display max leverage for the leverage mode their account is in\n\t\tconst isUserInHighLeverageMode = user.isHighLeverageMode('Initial');\n\t\tconst grandfatheredMaxLev = isUserInHighLeverageMode\n\t\t\t? marketLeverageDetails.hasHighLeverage\n\t\t\t\t? marketLeverageDetails.highLeverageMaxLeverage\n\t\t\t\t: marketLeverageDetails.regularMaxLeverage\n\t\t\t: marketLeverageDetails.regularMaxLeverage;\n\t\treturn grandfatheredMaxLev;\n\t}\n\n\t// user has closed position with no margin ratio set, return default value\n\treturn DEFAULT_MAX_LEVERAGE;\n};\n\nexport const USER_UTILS = {\n\tgetOpenPositionData,\n\tcheckIfUserAccountExists,\n\tgetUserMaxLeverageForMarket,\n};\n"]}
@@ -28,10 +28,7 @@ export type OpenPosition = {
28
28
  totalUnrealizedPnl: BN;
29
29
  costBasis: BN;
30
30
  realizedPnl: BN;
31
- lpShares: BN;
32
31
  pnlIsClaimable: boolean;
33
- remainderBaseAmount?: number;
34
- lpDeriskPrice?: BN;
35
32
  maxMarginRatio: number;
36
33
  };
37
34
  export type BankBalanceUI = SpotPosition & {
@@ -1 +1 @@
1
- {"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/types/user.ts"],"names":[],"mappings":"","sourcesContent":["import { BN, PublicKey, SpotPosition } from '@drift-labs/sdk';\n\nexport type OpenPosition = {\n\tmarketIndex: number;\n\tmarketSymbol: string;\n\tdirection: 'short' | 'long';\n\tnotional: BN;\n\tbaseSize: BN;\n\tentryPrice: BN;\n\texitPrice: BN;\n\tliqPrice: BN;\n\tpnlVsOracle: BN;\n\tpnlVsMark: BN;\n\tquoteAssetNotionalAmount: BN;\n\tquoteEntryAmount: BN;\n\tquoteBreakEvenAmount: BN;\n\tunrealizedFundingPnl: BN;\n\tfeesAndFundingPnl: BN;\n\tlastCumulativeFundingRate: BN;\n\topenOrders: number;\n\t/**\n\t * This is the unsettled pnl that is claimable. Naming is a bit confusing here.\n\t */\n\tunsettledPnl: BN;\n\tunsettledFundingPnl: BN;\n\t/**\n\t * This is the total of unsettled pnl and unsettled funding.\n\t */\n\ttotalUnrealizedPnl: BN;\n\tcostBasis: BN;\n\trealizedPnl: BN;\n\tlpShares: BN;\n\tpnlIsClaimable: boolean;\n\tremainderBaseAmount?: number; // LP only\n\tlpDeriskPrice?: BN; //LP only\n\tmaxMarginRatio: number;\n};\n\nexport type BankBalanceUI = SpotPosition & {\n\taccountId: number;\n\taccountName: string;\n\taccountAuthority: PublicKey;\n};\n"]}
1
+ {"version":3,"file":"user.js","sourceRoot":"","sources":["../../src/types/user.ts"],"names":[],"mappings":"","sourcesContent":["import { BN, PublicKey, SpotPosition } from '@drift-labs/sdk';\n\nexport type OpenPosition = {\n\tmarketIndex: number;\n\tmarketSymbol: string;\n\tdirection: 'short' | 'long';\n\tnotional: BN;\n\tbaseSize: BN;\n\tentryPrice: BN;\n\texitPrice: BN;\n\tliqPrice: BN;\n\tpnlVsOracle: BN;\n\tpnlVsMark: BN;\n\tquoteAssetNotionalAmount: BN;\n\tquoteEntryAmount: BN;\n\tquoteBreakEvenAmount: BN;\n\tunrealizedFundingPnl: BN;\n\tfeesAndFundingPnl: BN;\n\tlastCumulativeFundingRate: BN;\n\topenOrders: number;\n\t/**\n\t * This is the unsettled pnl that is claimable. Naming is a bit confusing here.\n\t */\n\tunsettledPnl: BN;\n\tunsettledFundingPnl: BN;\n\t/**\n\t * This is the total of unsettled pnl and unsettled funding.\n\t */\n\ttotalUnrealizedPnl: BN;\n\tcostBasis: BN;\n\trealizedPnl: BN;\n\tpnlIsClaimable: boolean;\n\tmaxMarginRatio: number;\n};\n\nexport type BankBalanceUI = SpotPosition & {\n\taccountId: number;\n\taccountName: string;\n\taccountAuthority: PublicKey;\n};\n"]}
@@ -54,7 +54,6 @@ const areTwoOpenPositionsEqual = (openPosition1, openPosition2) => {
54
54
  ['costBasis', 'bn'],
55
55
  ['pnlIsClaimable', 'primitive'],
56
56
  ['realizedPnl', 'bn'],
57
- ['lpShares', 'bn'],
58
57
  ];
59
58
  return arePropertiesEqual(openPosition1, openPosition2, propertiesToCompare);
60
59
  };
@@ -1 +1 @@
1
- {"version":3,"file":"equalityChecks.js","sourceRoot":"","sources":["../../src/utils/equalityChecks.ts"],"names":[],"mappings":";;;AAAA,wBAA+B;AAgB/B,MAAM,kBAAkB,GAAG,CAC1B,IAAS,EACT,IAAS,EACT,UAAkC,EACjC,EAAE;IACH,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE;;QACpC,MAAM,YAAY,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QACjC,MAAM,YAAY,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QACjC,MAAM,gBAAgB,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QAErC,QAAQ,YAAY,EAAE,CAAC;YACtB,KAAK,WAAW;gBACf,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC,YAAY,CAAC,CAAC;YAClD,KAAK,gBAAgB;gBACpB,OAAO,CACN,CAAA,MAAA,IAAI,CAAC,YAAY,CAAC,0CAAE,MAAM,OAAK,MAAA,IAAI,CAAC,YAAY,CAAC,0CAA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AW,EAAE,IAAI,CAAC;QACnB,CAAC,0BAA0B,EAAE,IAAI,CAAC;QAClC,CAAC,kBAAkB,EAAE,IAAI,CAAC;QAC1B,CAAC,sBAAsB,EAAE,IAAI,CAAC;QAC9B,CAAC,2BAA2B,EAAE,IAAI,CAAC;QACnC,CAAC,YAAY,EAAE,WAAW,CAAC;QAC3B,CAAC,cAAc,EAAE,IAAI,CAAC;QACtB,CAAC,qBAAqB,EAAE,IAAI,CAAC;QAC7B,CAAC,oBAAoB,EAAE,IAAI,CAAC;QAC5B,CAAC,WAAW,EAAE,IAAI,CAAC;QACnB,CAAC,gBAAgB,EAAE,WAAW,CAAC;QAC/B,CAAC,aAAa,EAAE,IAAI,CAAC;QACrB,CAAC,UAAU,EAAE,IAAI,CAAC;KAClB,CAAC;IAEF,OAAO,kBAAkB,CAAC,aAAa,EAAE,aAAa,EAAE,mBAAmB,CAAC,CAAC;AAC9E,CAAC,CAAC;AAEF,MAAM,yBAAyB,GAAG,CACjC,cAA8B,EAC9B,cAA8B,EAC7B,EAAE;IACH,IAAI,cAAc,CAAC,MAAM,KAAK,cAAc,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAElE,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,KAAK,EAAE,EAAE,CACnD,wBAAwB,CAAC,YAAY,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAC7D,CAAC;AACH,CAAC,CAAC;AAEW,QAAA,eAAe,GAAG;IAC9B,kBAAkB;IAClB,YAAY,EAAE,wBAAwB;IACtC,iBAAiB,EAAE,yBAAyB;CAC5C,CAAC","sourcesContent":["import { ENUM_UTILS } from '.';\nimport { OpenPosition } from '../types';\n\nexport type PropertyType =\n\t| 'primitive'\n\t| 'primitiveArray'\n\t| 'bn'\n\t| 'bignum'\n\t| 'programEnum'\n\t| 'custom';\nexport type PropertyAndType<KeyOfObject> = [\n\tproperty: KeyOfObject,\n\ttype: PropertyType,\n\tcustomEqualityFn?: (a: any, b: any) => boolean\n];\n\nconst arePropertiesEqual = (\n\tobj1: any,\n\tobj2: any,\n\tproperties: PropertyAndType<any>[]\n) => {\n\treturn properties.every((property) => {\n\t\tconst propertyName = property[0];\n\t\tconst propertyType = property[1];\n\t\tconst customEqualityFn = property[2];\n\n\t\tswitch (propertyType) {\n\t\t\tcase 'primitive':\n\t\t\t\treturn obj1[propertyName] === obj2[propertyName];\n\t\t\tcase 'primitiveArray':\n\t\t\t\treturn (\n\t\t\t\t\tobj1[propertyName]?.length === obj2[propertyName]?.length &&\n\t\t\t\t\tobj1[propertyName]?.every((value: any, index: number) => {\n\t\t\t\t\t\treturn value === obj2[propertyName]?.[index];\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\tcase 'bn':\n\t\t\tcase 'bignum':\n\t\t\t\treturn obj1[propertyName]?.eq(obj2[propertyName]);\n\t\t\tcase 'programEnum':\n\t\t\t\treturn ENUM_UTILS.match(obj1[propertyName], obj2[propertyName]);\n\t\t\tcase 'custom':\n\t\t\t\tif (!customEqualityFn)\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t'You need to provide a custom equality function for this property'\n\t\t\t\t\t);\n\n\t\t\t\treturn customEqualityFn(obj1[propertyName], obj2[propertyName]);\n\t\t\tdefault:\n\t\t\t\tthrow new Error('Invalid property type');\n\t\t}\n\t});\n};\n\nconst areTwoOpenPositionsEqual = (\n\topenPosition1: OpenPosition,\n\topenPosition2: OpenPosition\n) => {\n\tconst propertiesToCompare: PropertyAndType<keyof OpenPosition>[] = [\n\t\t['marketIndex', 'primitive'],\n\t\t['marketSymbol', 'primitive'],\n\t\t['direction', 'primitive'],\n\t\t['notional', 'bn'],\n\t\t['baseSize', 'bn'],\n\t\t['entryPrice', 'bn'],\n\t\t['exitPrice', 'bn'],\n\t\t['liqPrice', 'bn'],\n\t\t['pnlVsOracle', 'bn'],\n\t\t['pnlVsMark', 'bn'],\n\t\t['quoteAssetNotionalAmount', 'bn'],\n\t\t['quoteEntryAmount', 'bn'],\n\t\t['unrealizedFundingPnl', 'bn'],\n\t\t['lastCumulativeFundingRate', 'bn'],\n\t\t['openOrders', 'primitive'],\n\t\t['unsettledPnl', 'bn'],\n\t\t['unsettledFundingPnl', 'bn'],\n\t\t['totalUnrealizedPnl', 'bn'],\n\t\t['costBasis', 'bn'],\n\t\t['pnlIsClaimable', 'primitive'],\n\t\t['realizedPnl', 'bn'],\n\t\t['lpShares', 'bn'],\n\t];\n\n\treturn arePropertiesEqual(openPosition1, openPosition2, propertiesToCompare);\n};\n\nconst areOpenPositionListsEqual = (\n\topenPositions1: OpenPosition[],\n\topenPositions2: OpenPosition[]\n) => {\n\tif (openPositions1.length !== openPositions2.length) return false;\n\n\treturn openPositions1.every((openPosition, index) =>\n\t\tareTwoOpenPositionsEqual(openPosition, openPositions2[index])\n\t);\n};\n\nexport const EQUALITY_CHECKS = {\n\tarePropertiesEqual,\n\topenPosition: areTwoOpenPositionsEqual,\n\topenPositionLists: areOpenPositionListsEqual,\n};\n"]}
1
+ {"version":3,"file":"equalityChecks.js","sourceRoot":"","sources":["../../src/utils/equalityChecks.ts"],"names":[],"mappings":";;;AAAA,wBAA+B;AAgB/B,MAAM,kBAAkB,GAAG,CAC1B,IAAS,EACT,IAAS,EACT,UAAkC,EACjC,EAAE;IACH,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE;;QACpC,MAAM,YAAY,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QACjC,MAAM,YAAY,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QACjC,MAAM,gBAAgB,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QAErC,QAAQ,YAAY,EAAE,CAAC;YACtB,KAAK,WAAW;gBACf,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC,YAAY,CAAC,CAAC;YAClD,KAAK,gBAAgB;gBACpB,OAAO,CACN,CAAA,MAAA,IAAI,CAAC,YAAY,CAAC,0CAAE,MAAM,OA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aAAa,EAAE,IAAI,CAAC;QACrB,CAAC,WAAW,EAAE,IAAI,CAAC;QACnB,CAAC,0BAA0B,EAAE,IAAI,CAAC;QAClC,CAAC,kBAAkB,EAAE,IAAI,CAAC;QAC1B,CAAC,sBAAsB,EAAE,IAAI,CAAC;QAC9B,CAAC,2BAA2B,EAAE,IAAI,CAAC;QACnC,CAAC,YAAY,EAAE,WAAW,CAAC;QAC3B,CAAC,cAAc,EAAE,IAAI,CAAC;QACtB,CAAC,qBAAqB,EAAE,IAAI,CAAC;QAC7B,CAAC,oBAAoB,EAAE,IAAI,CAAC;QAC5B,CAAC,WAAW,EAAE,IAAI,CAAC;QACnB,CAAC,gBAAgB,EAAE,WAAW,CAAC;QAC/B,CAAC,aAAa,EAAE,IAAI,CAAC;KACrB,CAAC;IAEF,OAAO,kBAAkB,CAAC,aAAa,EAAE,aAAa,EAAE,mBAAmB,CAAC,CAAC;AAC9E,CAAC,CAAC;AAEF,MAAM,yBAAyB,GAAG,CACjC,cAA8B,EAC9B,cAA8B,EAC7B,EAAE;IACH,IAAI,cAAc,CAAC,MAAM,KAAK,cAAc,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAElE,OAAO,cAAc,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,KAAK,EAAE,EAAE,CACnD,wBAAwB,CAAC,YAAY,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAC7D,CAAC;AACH,CAAC,CAAC;AAEW,QAAA,eAAe,GAAG;IAC9B,kBAAkB;IAClB,YAAY,EAAE,wBAAwB;IACtC,iBAAiB,EAAE,yBAAyB;CAC5C,CAAC","sourcesContent":["import { ENUM_UTILS } from '.';\nimport { OpenPosition } from '../types';\n\nexport type PropertyType =\n\t| 'primitive'\n\t| 'primitiveArray'\n\t| 'bn'\n\t| 'bignum'\n\t| 'programEnum'\n\t| 'custom';\nexport type PropertyAndType<KeyOfObject> = [\n\tproperty: KeyOfObject,\n\ttype: PropertyType,\n\tcustomEqualityFn?: (a: any, b: any) => boolean\n];\n\nconst arePropertiesEqual = (\n\tobj1: any,\n\tobj2: any,\n\tproperties: PropertyAndType<any>[]\n) => {\n\treturn properties.every((property) => {\n\t\tconst propertyName = property[0];\n\t\tconst propertyType = property[1];\n\t\tconst customEqualityFn = property[2];\n\n\t\tswitch (propertyType) {\n\t\t\tcase 'primitive':\n\t\t\t\treturn obj1[propertyName] === obj2[propertyName];\n\t\t\tcase 'primitiveArray':\n\t\t\t\treturn (\n\t\t\t\t\tobj1[propertyName]?.length === obj2[propertyName]?.length &&\n\t\t\t\t\tobj1[propertyName]?.every((value: any, index: number) => {\n\t\t\t\t\t\treturn value === obj2[propertyName]?.[index];\n\t\t\t\t\t})\n\t\t\t\t);\n\t\t\tcase 'bn':\n\t\t\tcase 'bignum':\n\t\t\t\treturn obj1[propertyName]?.eq(obj2[propertyName]);\n\t\t\tcase 'programEnum':\n\t\t\t\treturn ENUM_UTILS.match(obj1[propertyName], obj2[propertyName]);\n\t\t\tcase 'custom':\n\t\t\t\tif (!customEqualityFn)\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t'You need to provide a custom equality function for this property'\n\t\t\t\t\t);\n\n\t\t\t\treturn customEqualityFn(obj1[propertyName], obj2[propertyName]);\n\t\t\tdefault:\n\t\t\t\tthrow new Error('Invalid property type');\n\t\t}\n\t});\n};\n\nconst areTwoOpenPositionsEqual = (\n\topenPosition1: OpenPosition,\n\topenPosition2: OpenPosition\n) => {\n\tconst propertiesToCompare: PropertyAndType<keyof OpenPosition>[] = [\n\t\t['marketIndex', 'primitive'],\n\t\t['marketSymbol', 'primitive'],\n\t\t['direction', 'primitive'],\n\t\t['notional', 'bn'],\n\t\t['baseSize', 'bn'],\n\t\t['entryPrice', 'bn'],\n\t\t['exitPrice', 'bn'],\n\t\t['liqPrice', 'bn'],\n\t\t['pnlVsOracle', 'bn'],\n\t\t['pnlVsMark', 'bn'],\n\t\t['quoteAssetNotionalAmount', 'bn'],\n\t\t['quoteEntryAmount', 'bn'],\n\t\t['unrealizedFundingPnl', 'bn'],\n\t\t['lastCumulativeFundingRate', 'bn'],\n\t\t['openOrders', 'primitive'],\n\t\t['unsettledPnl', 'bn'],\n\t\t['unsettledFundingPnl', 'bn'],\n\t\t['totalUnrealizedPnl', 'bn'],\n\t\t['costBasis', 'bn'],\n\t\t['pnlIsClaimable', 'primitive'],\n\t\t['realizedPnl', 'bn'],\n\t];\n\n\treturn arePropertiesEqual(openPosition1, openPosition2, propertiesToCompare);\n};\n\nconst areOpenPositionListsEqual = (\n\topenPositions1: OpenPosition[],\n\topenPositions2: OpenPosition[]\n) => {\n\tif (openPositions1.length !== openPositions2.length) return false;\n\n\treturn openPositions1.every((openPosition, index) =>\n\t\tareTwoOpenPositionsEqual(openPosition, openPositions2[index])\n\t);\n};\n\nexport const EQUALITY_CHECKS = {\n\tarePropertiesEqual,\n\topenPosition: areTwoOpenPositionsEqual,\n\topenPositionLists: areOpenPositionListsEqual,\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/common",
3
- "version": "1.0.51",
3
+ "version": "1.0.52",
4
4
  "description": "Common functions for Drift",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",